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

SHA1-Hulud the Second Comming – Postman, Zapier, PostHog All Compromised via NPM

https://www.aikido.dev/blog/shai-hulud-strikes-again-hitting-zapier-ensdomains
57•birdculture•34m ago•10 comments

NSA and IETF, part 3: Dodging the issues at hand

https://blog.cr.yp.to/20251123-dodging.html
203•upofadown•4h ago•69 comments

X Just Accidentally Exposed a Covert Influence Network Targeting Americans

https://weaponizedspaces.substack.com/p/x-just-accidentally-exposed-a-vast
146•adriand•30m ago•58 comments

Show HN: Cynthia – Reliably play MIDI music files – MIT / Portable / Windows

https://www.blaizenterprises.com/cynthia.html
51•blaiz2025•2h ago•14 comments

Inside Rust's std and parking_lot mutexes – who wins?

https://blog.cuongle.dev/p/inside-rusts-std-and-parking-lot-mutexes-who-win
25•signa11•4d ago•1 comments

Chrome Jpegxl Issue Reopened

https://issues.chromium.org/issues/40168998
90•markdog12•4h ago•19 comments

Fast Lua runtime written in Rust

https://astra.arkforge.net/
62•akagusu•2h ago•32 comments

Serflings is a remake of The Settlers 1

https://www.simpleguide.net/serflings.xhtml
66•doener•2d ago•17 comments

Shai-Hulud Returns: Over 300 NPM Packages Infected

https://helixguard.ai/blog/malicious-sha1hulud-2025-11-24
527•mrdosija•5h ago•426 comments

We stopped roadmap work for a week and fixed bugs

https://lalitm.com/fixits-are-good-for-the-soul/
151•lalitmaganti•1d ago•244 comments

Slicing Is All You Need: Towards a Universal One-Sided Distributed MatMul

https://arxiv.org/abs/2510.08874
62•matt_d•5d ago•4 comments

RuBee

https://computer.rip/2025-11-22-RuBee.html
299•Sniffnoy•13h ago•52 comments

Disney Lost Roger Rabbit

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

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

https://www.bbc.com/news/articles/c8676qpxgnqo
209•1659447091•13h ago•355 comments

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

https://microcad.xyz/
332•todsacerdoti•19h ago•106 comments

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

285•pugworthy•14h ago•151 comments

Show HN: Virtual SLURM HPC cluster in a Docker Compose

https://github.com/exactlab/vhpc
24•ciclotrone•4d ago•5 comments

Lambda Calculus – Animated Beta Reduction of Lambda Diagrams

https://cruzgodar.com/applets/lambda-calculus
115•perryprog•11h ago•8 comments

Native Secure Enclave backed SSH keys on macOS

https://gist.github.com/arianvp/5f59f1783e3eaf1a2d4cd8e952bb4acf
435•arianvanp•22h ago•181 comments

I built an faster Notion in Rust

https://imedadel.com/outcrop/
99•PaulHoule•4d ago•54 comments

The Rust Performance Book (2020)

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

New magnetic component discovered in the Faraday effect

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

Show HN: Stun LLMs with thousands of invisible Unicode characters

https://gibberifier.com
162•wdpatti•13h ago•72 comments

Fran Sans – font inspired by San Francisco light rail displays

https://emilysneddon.com/fran-sans-essay
1046•ChrisArchitect•22h ago•129 comments

Ego, empathy, and humility at work

https://matthogg.fyi/a-unified-theory-of-ego-empathy-and-humility-at-work/
116•mrmatthogg•14h ago•39 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/
65•TangerineDream•2d ago•51 comments

Set theory with types

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

I put a real search engine into a Lambda, so you only pay when you search

https://nixiesearch.substack.com/p/i-put-a-real-search-engine-into-a
24•shutty•5h ago•4 comments

The Cloudflare outage might be a good thing

https://gist.github.com/jbreckmckye/32587f2907e473dd06d68b0362fb0048
211•radeeyate•13h ago•148 comments

Bureau of Meteorology's new boss asked to examine $96M bill for website redesign

https://www.abc.net.au/news/2025-11-23/bureau-of-meteorology-new-website-cost-blowout-to-96-milli...
85•OuterVale•4h ago•58 comments