PGO is the thing that blew my mind the most about Go when i found out about it. It's classic Go a 'no way this exists' solution. Go is compiled ahead of time. This means at build time, the Go source code is translated directly into machine code for a specific operating system and architecture, creating a self-contained executable file. Unlike a JIT compiler, which performs compilation at runtime, allowing it to potentially make optimizations based on actual execution patterns. JIT is the reason why JavaScript is decently faster than other dynamic languages like Python. Because the JS engine runs a background process that inspect's the running machine code to identify hot-paths (repetitive sections that can be recompiled to produce more optimized machine code), same thing for the JVM. While JIT produces faster code, it comes with a memory cost. Anyone who ran a production server for any of the JVM based languages or JavaScript knows the pain of scaling this at some point. So Go produces fast machine code at compile time with small memory footprint while JIT-compiled languages are faster during runtime. But that's not actually it, Go has PGO. PGO allows the Go compiler to leverage real-world runtime information to make better optimization decisions during the ahead-of-time compilation process. Here's how it works: 1- Profiling: You run your compiled Go application in a realistic environment (testing or even production) and collect performance profiles. These profiles capture data about how the code executes like which functions are called most often (hot paths), how branches are typically taken, etc. 2- Recompilation: You then feed this profile data back into the Go compiler during the next build (go build -pgo=profile.pprof ...). 3- Optimized Build: The compiler uses the insights from the profile to make more informed decisions. It might inline functions more aggressively on hot paths, optimize code layout, or make other adjustments based on the observed runtime behavior. The result is that Go can achieve the optimization benefits typically associated with JIT compilation but without the runtime overhead. To enable PGO, first, you need to import the pprof package in your Go app like _ "net/http/pprof" which will automatically registers HTTP handlers like /debug/pprof/profile on Go's default HTTP server mux than runs with http.ListenAndServe(). Now fetch live profiles from the /debug/pprof/* endpoints using curl, than do the PGO build like the example below

Apr 30, 2025 路 7:00 PM UTC

7
35
1
295
Replying to @Chikor_Zi
This is something from the C world. Its awesome, dont get me wrong 馃槈. But its not a Go thing per se.
1
4
I had no idea. It's nice seeing low level capabilities in easy to use backend language.
1
2
Replying to @Chikor_Zi
If you want to do profiling analysis, also, you can attach go tool pprof directly to the server, too: go tool pprof "http://localhost:6060/debug/pprof/profile?seconds=30" Personally, though, I prefer writing the file directly from the program itself. I just check for a PPROF environment variable at the top of main() and then write to it if it's set. Can get a better sample of the program's entire runtime that way. Also doesn't have the overhead of running an HTTP server if the program itself isn't using "net/http".
7
Replying to @Chikor_Zi
go ia the best, that's amazing
3
Replying to @Chikor_Zi
We had PGO for a long time for C/C++. I am always a bit scare of it as now you could well get new code gen bug or very difficult bugs (think race conditions) going in and out as the profile data get refreshed. But it can do nice thing, like de-virtualization
2
Replying to @Chikor_Zi
that's wild fr
2
Replying to @Chikor_Zi
Maybe there's something subtly different about what your describing but this seems like ancient technology which every language and compiler used to do. There's major compatibility and complexity tradeoffs for minor performance gains. In some scenarios it's worth it but in general probably not.
Replying to @Chikor_Zi
Sorry for my stupid question ( I'm a Dev-student in Prog-langs ): Q: - what is a PGO ?