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

RuBee

https://computer.rip/2025-11-22-RuBee.html
214•Sniffnoy•6h ago•32 comments

Fran Sans – font inspired by San Francisco light rail displays

https://emilysneddon.com/fran-sans-essay
887•ChrisArchitect•15h ago•119 comments

Disney Lost Roger Rabbit

https://pluralistic.net/2025/11/18/im-not-bad/
149•leephillips•5d ago•45 comments

Ask HN: Hearing aid wearers, what's hot?

177•pugworthy•7h ago•89 comments

Lambda Calculus – Animated Beta Reduction of Lambda Diagrams

https://cruzgodar.com/applets/lambda-calculus
33•perryprog•4h ago•1 comments

The Rust Performance Book (2020)

https://nnethercote.github.io/perf-book/
112•vinhnx•5d ago•8 comments

µcad: New open source programming language that can generate 2D sketches and 3D

https://microcad.xyz/
216•todsacerdoti•13h ago•62 comments

Japan's gamble to turn island of Hokkaido into global chip hub

https://www.bbc.com/news/articles/c8676qpxgnqo
62•1659447091•6h ago•108 comments

Native Secure Enclave backed SSH keys on macOS

https://gist.github.com/arianvp/5f59f1783e3eaf1a2d4cd8e952bb4acf
383•arianvanp•16h ago•161 comments

We stopped roadmap work for a week and fixed bugs

https://lalitm.com/fixits-are-good-for-the-soul/
16•lalitmaganti•17h ago•124 comments

New magnetic component discovered in the Faraday effect after nearly 2 centuries

https://phys.org/news/2025-11-magnetic-component-faraday-effect-centuries.html
142•rbanffy•4d ago•45 comments

Show HN: Stun LLMs with thousands of invisible Unicode characters

https://gibberifier.com
104•wdpatti•7h ago•46 comments

Breakthrough in antimatter production

https://home.cern/news/news/experiments/breakthrough-antimatter-production
11•doener•4d ago•14 comments

Build desktop applications using Go and Web Technologies

https://github.com/wailsapp/wails
43•selvan•5h ago•22 comments

Having Fun with Complex Numbers

https://mathwonder.org/Having-Fun-with-Complex-Numbers/
33•smm16r•5d ago•8 comments

Show HN: Syd – An offline-first, AI-augmented workstation for blue teams

https://www.sydsec.co.uk
12•paul2495•2h ago•3 comments

Set theory with types

https://lawrencecpaulson.github.io//2025/11/21/Typed_Set_Theory.html
54•baruchel•2d ago•11 comments

Moss survived outside of the International Space Station for 9 months

https://www.livescience.com/space/scientists-put-moss-on-the-outside-of-the-international-space-s...
27•geox•3d ago•6 comments

Calculus for Mathematicians, Computer Scientists, and Physicists [pdf]

https://mathcs.holycross.edu/~ahwang/print/calc.pdf
297•o4c•17h ago•67 comments

The Cloudflare outage might be a good thing

https://gist.github.com/jbreckmckye/32587f2907e473dd06d68b0362fb0048
143•radeeyate•6h ago•114 comments

Building the largest known Kubernetes cluster, with 130k nodes

https://cloud.google.com/blog/products/containers-kubernetes/how-we-built-a-130000-node-gke-cluster/
5•TangerineDream•2d ago•0 comments

Hyperoptic: IPv6 and Out-of-Order Packets

https://blog.zakkemble.net/hyperoptic-ipv6-and-out-of-order-packets/
39•speckx•5d ago•2 comments

Passing the Torch – My Last Root DNSSEC KSK Ceremony as Crypto Officer 4

https://technotes.seastrom.com/2025/11/23/passing-the-torch.html
53•greyface-•7h ago•14 comments

Show HN: I wrote a minimal memory allocator in C

https://github.com/t9nzin/memory
94•t9nzin•11h ago•25 comments

Pixar: The Early Days

https://stevejobsarchive.com/stories/pixar-early-days
15•tosh•4d ago•2 comments

A time-travelling door bug in Half Life 2

https://mastodon.gamedev.place/@TomF/115589875974658415
437•AshleysBrain•2d ago•60 comments

Liva AI (YC S25) Is Hiring

https://www.ycombinator.com/companies/liva-ai/jobs/fYP8QP8-growth-intern
1•ashlleymo•11h ago

Ego, empathy, and humility at work

https://matthogg.fyi/a-unified-theory-of-ego-empathy-and-humility-at-work/
55•mrmatthogg•8h ago•18 comments

Band of Holes

https://en.wikipedia.org/wiki/Band_of_Holes
30•user070223•5d ago•7 comments

A desktop app for isolated, parallel agentic development

https://github.com/coder/mux
79•mercat•11h ago•36 comments