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•7mo 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

Can Dutch universities do without Microsoft?

https://dub.uu.nl/en/news/can-dutch-universities-do-without-microsoft
106•robtherobber•1h ago•71 comments

So you wanna build a local RAG?

https://blog.yakkomajuri.com/blog/local-rag
22•pedriquepacheco•52m ago•1 comments

Don't tug on that, you never know what it might be attached to

https://blog.plover.com/2016/07/01/#tmpdir
41•todsacerdoti•1h ago•8 comments

True P2P Email on Top of Yggdrasil Network

https://github.com/JB-SelfCompany/Tyr
29•basemi•1h ago•4 comments

C++ Web Server on my custom hobby OS

https://oshub.org/projects/retros-32/posts/getting-a-webserver-running
14•joexbayer•31m ago•3 comments

Atuin’s New Runbook Execution Engine

https://blog.atuin.sh/introducing-the-new-runbook-execution-engine/
61•emschwartz•3d ago•8 comments

Show HN: An LLM-Powered Tool to Catch PCB Schematic Mistakes

https://netlist.io/
7•wafflesfreak•16m ago•1 comments

Meta hiding $27B in debt using advanced geometry

https://stohl.substack.com/p/exclusive-credit-report-shows-meta
147•FreeQueso•1h ago•61 comments

Bringing Sexy Back. Internet surveillance has killed eroticism

https://lux-magazine.com/article/privacy-eroticism/
37•eustoria•41m ago•5 comments

Show HN: Glasses to detect smart-glasses that have cameras

https://github.com/NullPxl/banrays
415•nullpxl•11h ago•150 comments

AI Adoption Rates Starting to Flatten Out

https://www.apolloacademy.com/ai-adoption-rates-starting-to-flatten-out/
73•toomuchtodo•1h ago•31 comments

Petition to formally recognize open source work as civic service in Germany

https://www.openpetition.de/petition/online/anerkennung-von-open-source-arbeit-als-ehrenamt-in-de...
344•PhilippGille•3h ago•91 comments

Tech Titans Amass Multimillion-Dollar War Chests to Fight AI Regulation

https://www.wsj.com/tech/ai/tech-titans-amass-multimillion-dollar-war-chests-to-fight-ai-regulati...
141•thm•8h ago•141 comments

Moss: a Rust Linux-compatible kernel in 26,000 lines of code

https://github.com/hexagonal-sun/moss
302•hexagonal-sun•6d ago•76 comments

Pocketbase – open-source realtime back end in 1 file

https://pocketbase.io/
543•modinfo•14h ago•146 comments

Stellantis Is Spamming Owners' Screens with Pop-Up Ads for New Car Discounts

https://www.thedrive.com/news/stellantis-is-spamming-owners-screens-with-pop-up-ads-for-new-car-d...
43•cf100clunk•1h ago•13 comments

The Signal Is the Noise

https://www.magazine.dirt.fyi/p/the-signal-is-the-noise
11•surprisetalk•1h ago•4 comments

A Tale of Four Fuzzers

https://tigerbeetle.com/blog/2025-11-28-tale-of-four-fuzzers/
45•jorangreef•5h ago•13 comments

Apple and Intel Rumored to Partner on Mac Chips

https://www.macrumors.com/2025/11/28/intel-rumored-to-supply-new-mac-chip/
38•bigyabai•46m ago•4 comments

A Remarkable Assertion from A16Z

https://nealstephenson.substack.com/p/a-remarkable-assertion-from-a16z
238•boplicity•5h ago•97 comments

Tell HN: Want a better HN? Visit /newest

170•alecco•1h ago•52 comments

Swedish publishers file police report against Meta's Zuckerberg for fraud

https://www.sverigesradio.se/artikel/swedish-publishers-file-police-report-against-metas-zuckerbe...
70•Frieren•2h ago•17 comments

Generating 3D Meshes from Text

https://cprimozic.net/notes/posts/generating-3d-meshes-from-text/
8•todsacerdoti•2h ago•1 comments

A Repository with 44 Years of Unix Evolution

https://www.spinellis.gr/pubs/conf/2015-MSR-Unix-History/html/Spi15c.html
74•lioeters•8h ago•18 comments

Playtiles: The Pocket-Sized Gaming Platform

https://get.playtil.es
12•surprisetalk•1h ago•3 comments

Writing Builds Resilience in Everyday Challenges by Changing Your Brain

https://scienceclock.com/writing-builds-resilience-in-everyday-challenges-by-changing-your-brain/
17•PikelEmi•4h ago•2 comments

The Math of Why You Can't Focus at Work

https://justoffbyone.com/posts/math-of-why-you-cant-focus-at-work/
55•0x79de•8h ago•16 comments

SQLite as an Application File Format

https://sqlite.org/appfileformat.html
90•gjvc•9h ago•56 comments

Show HN: Spikelog – A simple metrics service for scripts, cron jobs, and MVPs

https://spikelog.com
25•dsmurrell•1d ago•12 comments

Pink Lexical Slime: The Dark Side of Autocorrect

https://www.cyberdemon.org/2017/12/12/pink-lexical-slime.html
7•dmazin•1h ago•1 comments