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

Bazzite: The next generation of Linux gaming

https://bazzite.gg/
182•doener•4h ago•96 comments

All it takes is for one to work out

https://alearningaday.blog/2025/11/28/all-it-takes-is-for-one-to-work-out-2/
325•herbertl•6h ago•169 comments

Landlock-Ing Linux

https://blog.prizrak.me/post/landlock/
121•razighter777•4h ago•36 comments

The HTTP Query Method

https://www.ietf.org/archive/id/draft-ietf-httpbis-safe-method-w-body-14.html
26•Ivoah•3d ago•4 comments

Be Like Clippy

https://be-clippy.com/
221•Aloha•6h ago•143 comments

Learning Feynman's Trick for Integrals

https://zackyzz.github.io/feynman.html
114•Zen1th•6h ago•17 comments

Meshtastic

https://meshtastic.org/
10•debo_•1h ago•1 comments

Scala

https://www.huygens-fokker.org/scala/
22•onestay42•2h ago•3 comments

Stopping bad guys from using my open source project (feedback wanted)

https://evanhahn.com/stopping-bad-guys-from-using-my-open-source-project/
12•emschwartz•1h ago•14 comments

Show HN: Nano PDF – A CLI Tool to Edit PDFs with Gemini's Nano Banana

https://github.com/gavrielc/Nano-PDF
91•GavCo•5h ago•19 comments

Blender facial animation tool. What else should it do?

https://github.com/shun126/livelinkface_arkit_receiver/wiki
33•happy-game-dev•2d ago•11 comments

Let go of StackOverflow; communities must take ownership

https://ahelwer.ca/post/2025-11-25-stackoverflow/
41•tensegrist•4d ago•41 comments

The Origins of Scala (2009)

https://www.artima.com/articles/the-origins-of-scala
53•todsacerdoti•6h ago•30 comments

An update on the Farphone's battery

https://far.computer/battery-update/
57•louismerlin•1d ago•44 comments

Testing shows automotive glassbreakers can't break modern automotive glass

https://www.core77.com/posts/138925/Testing-Shows-Automotive-Glassbreakers-Cant-Break-Modern-Auto...
86•surprisetalk•10h ago•80 comments

Datacenters in space aren't going to work

https://taranis.ie/datacenters-in-space-are-a-terrible-horrible-no-good-idea/
134•mindracer•12h ago•117 comments

Rare X-ray images of a 4.5-ton satellite that returned intact from space

https://www.empa.ch/web/s604/eureca-satellit-mit-roentgenmethoden-untersucht
56•giuliomagnifico•3d ago•8 comments

Show HN: Network Monitor – a GUI to spot anomalous connections on your Linux

98•grigio•5d ago•35 comments

Zero knowlege proof of compositeness

https://www.johndcook.com/blog/2025/11/29/zkp-composite/
87•ColinWright•8h ago•27 comments

Leak confirms OpenAI is preparing ads on ChatGPT for public roll out

https://www.bleepingcomputer.com/news/artificial-intelligence/leak-confirms-openai-is-preparing-a...
517•fleahunter•14h ago•495 comments

A new Little Prince museum has opened its doors in Switzerland

https://www.lepetitprince.com/en/events-around-the-world/a-new-little-prince-museum-has-opened-it...
4•gnabgib•1h ago•0 comments

Hardening the C++ Standard Library at scale

https://queue.acm.org/detail.cfm?id=3773097
118•ndesaulniers•6d ago•53 comments

The CRDT Dictionary: A Field Guide to Conflict-Free Replicated Data Types

https://www.iankduncan.com/engineering/2025-11-27-crdt-dictionary/
161•birdculture•14h ago•19 comments

1964 Recompiling Engine Documentation (2001) [pdf]

https://emudev.org/docs/1964-recompiling-engine-documentation.pdf
5•davikr•4d ago•0 comments

AccessOwl (YC S22) Is Hiring a Technical Account Manager (IAM)

https://www.ycombinator.com/companies/accessowl/jobs/dGC3pcO-technical-account-manager-identity-a...
1•philipeller•9h ago

Student perceptions of AI coding assistants in learning

https://arxiv.org/abs/2507.22900
58•victorbuilds•8h ago•80 comments

Post-mortem of Shai-Hulud attack on November 24th, 2025

https://posthog.com/blog/nov-24-shai-hulud-attack-post-mortem
75•makepanic•3d ago•60 comments

Bronze Age mega-settlement in Kazakhstan has advanced urban planning, metallurgy

https://archaeologymag.com/2025/11/bronze-age-mega-settlement-in-kazakhstan/
119•CGMthrowaway•1w ago•35 comments

Anthony Bourdain's Lost Li.st's

https://bourdain.greg.technology/
226•gregsadetsky•3d ago•75 comments

We're learning more about what Vitamin D does

https://www.technologyreview.com/2025/11/21/1128206/vitamin-d-bodies-bone-health-immune/
130•Brajeshwar•9h ago•118 comments