frontpage.
newsnewestaskshowjobs

Made with ♥ by @iamnishanth

Open Source @Github

Open in hackernews

Optimizing Heap Allocations in Go: A Case Study

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

Comments

returningfory2•2mo 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•2mo 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•2mo 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•2mo 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•2mo 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•2mo ago
At least you have the tools to understand where things get allocated.
38•2mo ago
instead of this:

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

    t.Buf = nil
rsc•2mo ago
Those are semantically different (one is nil and one is not) but neither allocates.
virexene•2mo 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•2mo 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

Introducing tmux-rs

https://richardscollin.github.io/tmux-rs/
665•Jtsummers•13h ago•215 comments

Flounder Mode – Kevin Kelly on a different way to do great work

https://joincolossus.com/article/flounder-mode/
197•latentnumber•13h ago•41 comments

Launch HN: K-Scale Labs (YC W24) – Open-Source Humanoid Robots

157•codekansas•12h ago•83 comments

AV1@Scale: Film Grain Synthesis, The Awakening

https://netflixtechblog.com/av1-scale-film-grain-synthesis-the-awakening-ee09cfdff40b
182•CharlesW•12h ago•147 comments

Wind Knitting Factory

https://www.merelkarhof.nl/work/wind-knitting-factory
98•bschne•8h ago•25 comments

Manipulating trapped air bubbles in ice for message storage in cold regions

https://www.cell.com/cell-reports-physical-science/fulltext/S2666-3864(25)00221-8
48•__rito__•3d ago•12 comments

Peasant Railgun

https://knightsdigest.com/what-exactly-is-the-peasant-railgun-in-dd-5e/
212•cainxinth•14h ago•157 comments

Poor Man's Back End-as-a-Service (BaaS), Similar to Firebase/Supabase/Pocketbase

https://github.com/zserge/pennybase
157•dcu•13h ago•97 comments

Electronic Arts Leadership Are Out of Their Goddamned Minds

https://aftermath.site/ea-dice-battlefield-battle-royale-free-to-play-f2p
22•dotmanish•1h ago•13 comments

Ubuntu 25.10 Raises RISC-V Profile Requirements

https://www.omgubuntu.co.uk/2025/06/ubuntu-riscv-rva23-support
80•bundie•2d ago•23 comments

Sound Chip, whisper me your secrets [video]

https://media.ccc.de/v/gpn23-302-sound-chip-whisper-me-your-secrets-
10•rasz•2d ago•0 comments

Opening up ‘Zero-Knowledge Proof’ technology

https://blog.google/technology/safety-security/opening-up-zero-knowledge-proof-technology-to-promote-privacy-in-age-assurance/
249•doomroot13•11h ago•151 comments

Where is my von Braun wheel?

https://angadh.com/wherevonbraunwheel
131•speckx•15h ago•98 comments

Caching is an abstraction, not an optimization

https://buttondown.com/jaffray/archive/caching-is-an-abstraction-not-an-optimization/
94•samuel246•2d ago•79 comments

CO2 sequestration through accelerated weathering of limestone on ships

https://www.science.org/doi/10.1126/sciadv.adr7250
35•PaulHoule•5h ago•27 comments

Postcard is now open source

https://www.contraption.co/postcard-open-source/
92•philip1209•12h ago•29 comments

High-Fidelity Simultaneous Speech-to-Speech Translation

https://arxiv.org/abs/2502.03382
73•Bluestein•8h ago•40 comments

You are what you launch: how software became a lifestyle brand

https://omeru.bearblog.dev/lifestyle/
40•freediver•2d ago•33 comments

An Algorithm for a Better Bookshelf

https://cacm.acm.org/news/an-algorithm-for-a-better-bookshelf/
79•pseudolus•2d ago•12 comments

Fei-Fei Li: Spatial intelligence is the next frontier in AI [video]

https://www.youtube.com/watch?v=_PioN-CpOP0
264•sandslash•2d ago•137 comments

Converge (YC S23) well-capitalized New York startup seeks product developers

https://www.runconverge.com/careers
1•thomashlvt•7h ago

Encoding Jake Gyllenhaal into one million checkboxes (2024)

https://ednamode.xyz/blogs/2.html
54•chilipepperhott•12h ago•13 comments

AI for Scientific Search

https://arxiv.org/abs/2507.01903
94•omarsar•13h ago•22 comments

Show HN: I rewrote my notepad calculator as a local-first app with CRDT syncing

https://numpad.io
30•tonyonodi•3d ago•12 comments

Michael Madsen has died

https://www.nytimes.com/2025/07/03/movies/michael-madsen-dead.html
95•anigbrowl•6h ago•28 comments

Astronomers discover 3I/ATLAS – Third interstellar object to visit Solar System

https://www.abc.net.au/news/science/2025-07-03/3i-atlas-a11pl3z-interstellar-object-in-our-solar-system/105489180
290•gammarator•1d ago•161 comments

About AI Evals

https://hamel.dev/blog/posts/evals-faq/
172•TheIronYuppie•3d ago•40 comments

Stalking the Statistically Improbable Restaurant with Data

https://ethanzuckerman.com/2025/07/03/stalking-the-statistically-improbable-restaurant-with-data/
60•nkurz•11h ago•31 comments

Alice's Adventures in a Differentiable Wonderland

https://arxiv.org/abs/2404.17625
141•henning•3d ago•25 comments

(Experiment) Colocating agent instructions with eng docs

https://technicalwriting.dev/ai/agents/colocate.html
5•dannyrosen•3d ago•2 comments