news.volyx.in

Making a Go program faster with a one-character change (hmarr.com)

511 points by hcm · 1393 days ago · 236 comments on HN

Article summary

The article describes how a one-character change in a Go program resulted in a 1.7x speedup. The change involved modifying a function to return a reference to a struct in a slice instead of a copy of the struct. The author used profiling tools, including pprof and flamegraphs, to identify the performance bottleneck. The change reduced heap allocations and improved performance.

Main themes

  • Go programming
  • performance optimization
  • profiling tools
  • code semantics
  • benchmarking
  • programming languages comparison

What commenters say

  • Some commenters argued that the optimization is only valid if the returned pointer is not used for mutation.
  • Others suggested that the compiler should be able to optimize the code without altering its semantics.
  • There was disagreement over the meaning of '70% faster' and how to accurately describe performance improvements.
  • A few commenters discussed the potential for a lint to detect oversized copies and the challenges of optimizing code without altering its behavior.
  • Some argued that the concept of 'speed per unit of work' is undefined and that performance improvements should be described in terms of time reduction rather than speed increase.
  • Others pointed out that the optimization changes the semantics of the code and could have unintended consequences if not carefully considered.
  • A few commenters shared their experiences with profiling and optimizing code in other languages, including Rust and Java.