frontpage.
newsnewestaskshowjobs

Made with ♥ by @iamnishanth

Open Source @Github

fp.

Open in hackernews

Optimizing Heap Allocations in Go: A Case Study

https://www.dolthub.com/blog/2025-04-18-optimizing-heap-allocations/
54•ingve•7mo ago

Comments

returningfory2•7mo ago
> It's possible that this is a compiler bug. It's also possible that there's some fringe case where the reference actually can escape via that method call, and the compiler doesn't have enough context to rule it out.

Here's an example, I think: suppose the method spawns a new goroutine that contains a reference to `chunkStore`. This goroutine can outlive the `ReadBytes` function call, and thus Go has to heap allocate the thing being referenced.

In general, this kind of example makes me suspect that Go's escape analysis algorithm treats any method call as a black box and heap allocates anything being passed to it by reference.

athorax•7mo ago

  The notion of stack vs heap allocation isn't something that even exists in the language. Users are expected to not worry about it... until, of course, until you're optimizing performance and you need to worry about it.
This is one of the best and worst aspects with Go. Anyone can write pretty performant code without having to understand the underlying memory model. If you get to the point where you are trying to optimize at this level, the benefits of using a more approachable language start to fall apart and you spend more time chasing implementation details.
nu11ptr•7mo ago
In general, it is a win, since it lets you code faster and 80-90% the performance doesn't matter. Over time, you learn generally what leads to heap allocs and what doesn't. In rare hot spot, using -m will show you the allocations and you can optimize.
athorax•7mo ago
I would generally agree. It's good enough performance for most applications. For those that it isn't fast enough for (even with optimizations like these), it still allows for rapid prototyping to arrive at that conclusion.
Ygg2•7mo ago
I think same applies to any GC language. Ride is fun until GC starts either taking too much time, too much memory or taking too much of CPU.
Thaxll•7mo ago
At least you have the tools to understand where things get allocated.
38•7mo ago
instead of this:

    t.Buf = []byte{}
you can just do:

    t.Buf = nil
rsc•7mo ago
Those are semantically different (one is nil and one is not) but neither allocates.
virexene•7mo ago
I wonder if the reason the escape analysis fails could be that, for small enough types, the concrete value is directly inlined inside the interface value, instead of the latter being "a smart pointer" as the author said. So when the compiler needs to take a reference to the concrete value in `vs.chunkStore`, that ends up as an internal pointer inside the `vs` allocation, requiring it to be on the heap.

Either that or the escape analysis just isn't smart enough; taking a pointer to an internal component of an interface value seems like a bit of a stretch.

Snawoot•6mo ago
I had an attempt to improve performance of memory allocation with the use of arenas in Go and I chose freelist datastructure[1]

It almost doesn't use unsafe except one line to cast pointer types. I measured practical performance boost with "container/list" implementation hooked to my allocator. All in all it performs 2-5 times faster or up to 10 times faster if we can get rid[2] of any and allocations implied by the use of it.

All in all, heap allocations can be not that bad at all if you approach them from another angle.

[1]: https://github.com/Snawoot/freelist

[2]: https://github.com/Snawoot/list

Cloudflare Global Network experiencing issues

https://www.cloudflarestatus.com/?t=1
1618•imdsm•3h ago•1128 comments

Nearly all UK drivers say headlights are too bright

https://www.bbc.com/news/articles/c1j8ewy1p86o
113•YeGoblynQueenne•36m ago•80 comments

Do Not Put Your Site Behind Cloudflare If You Don't Need To

https://huijzer.xyz/posts/123/do-not-put-your-site-behind-cloudflare-if-you-dont
81•huijzer•1h ago•44 comments

How Quake.exe got its TCP/IP stack

https://fabiensanglard.net/quake_chunnel/index.html
276•billiob•6h ago•36 comments

Experiment: Making TypeScript Immutable-by-Default

https://evanhahn.com/typescript-immutability-experiment/
16•ingve•51m ago•1 comments

Ruby 4.0.0 Preview2 Released

https://www.ruby-lang.org/en/news/2025/11/17/ruby-4-0-0-preview2-released/
83•pansa2•1h ago•22 comments

The Miracle of Wörgl

https://scf.green/story-of-worgl-and-others/
67•simonebrunozzi•3h ago•33 comments

Short Little Difficult Books

https://countercraft.substack.com/p/short-little-difficult-books
10•crescit_eundo•24m ago•0 comments

GoSign Desktop RCE flaws affecting users in Italy

https://www.ush.it/2025/11/14/multiple-vulnerabilities-gosign-desktop-remote-code-execution/
32•ascii•2h ago•14 comments

Gemini 3 Pro Model Card

https://pixeldrain.com/u/hwgaNKeH
273•Topfi•3h ago•183 comments

The Uselessness of "Fast" and "Slow" in Programming

https://jerf.org/iri/post/2025/the_uselessness_of_fast/
67•zdw•6d ago•30 comments

Multiple Digital Ocean services down

https://status.digitalocean.com/incidents/lgt5xs2843rx
69•inanothertime•1h ago•22 comments

How many video games include a marriage proposal? At least one

https://32bits.substack.com/p/under-the-microscope-ncaa-basketball
276•bbayles•4d ago•70 comments

Show HN: I built a synth for my daughter

https://bitsnpieces.dev/posts/a-synth-for-my-daughter/
1208•random_moonwalk•6d ago•203 comments

Roma Lister, Aradia, and the Speculative Origins of a Witchcraft Revival

https://publicdomainreview.org/essay/roma-lister-aradia/
7•Vigier•5d ago•0 comments

Ditch your (mut)ex, you deserve better

https://chrispenner.ca/posts/mutexes
101•commandersaki•6d ago•117 comments

Ruby Symbols

https://tech.stonecharioteer.com/posts/2025/ruby-symbols/
50•stonecharioteer•6d ago•30 comments

Mathematics and Computation (2019) [pdf]

https://www.math.ias.edu/files/Book-online-Aug0619.pdf
8•nill0•2h ago•0 comments

Azure hit by 15 Tbps DDoS attack using 500k IP addresses

https://www.bleepingcomputer.com/news/microsoft/microsoft-aisuru-botnet-used-500-000-ips-in-15-tb...
413•speckx•21h ago•271 comments

The surprising benefits of giving up

https://nautil.us/the-surprising-benefits-of-giving-up-1248362/
130•jnord•10h ago•106 comments

When Reverse Proxies Surprise You: Hard Lessons from Operating at Scale

https://www.infoq.com/articles/scaling-reverse-proxies/
74•miggy•5d ago•6 comments

Langfuse (YC W23) Hiring OSS Support Engineers in Berlin and SF

https://jobs.ashbyhq.com/langfuse/5ff18d4d-9066-4c67-8ecc-ffc0e295fee6
1•clemo_ra•7h ago

Unofficial "Tier 4" Rust Target for older Windows versions

https://github.com/rust9x/rust
114•kristianp•12h ago•66 comments

A/B Tests over Evals

https://www.raindrop.ai/blog/thoughts-on-evals/
8•Nischalj10•4d ago•3 comments

My stages of learning to be a socially normal person

https://sashachapin.substack.com/p/my-six-stages-of-learning-to-be-a
536•eatitraw•3d ago•361 comments

Rebecca Heineman has died

https://www.pcgamer.com/gaming-industry/legendary-game-designer-programmer-space-invaders-champio...
711•shdon•13h ago•124 comments

Compiling Ruby to machine language

https://patshaughnessy.net/2025/11/17/compiling-ruby-to-machine-language
268•todsacerdoti•18h ago•49 comments

'Fear really drives him': is Alex Karp of Palantir the world's scariest CEO?

https://www.theguardian.com/technology/2025/nov/18/fear-really-drives-him-is-alex-karp-of-palanti...
14•mellosouls•1h ago•3 comments

Astrophotographer snaps skydiver falling in front of the sun

https://www.iflscience.com/the-fall-of-icarus-you-have-never-seen-an-astrophotography-picture-lik...
426•doener•2d ago•83 comments

Project Gemini

https://geminiprotocol.net/
308•andsoitis•22h ago•173 comments