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

Voyager 1 Is About to Reach One Light-Day from Earth

https://scienceclock.com/voyager-1-is-about-to-reach-one-light-day-from-earth/
480•ashishgupta2209•5h ago•163 comments

A Fast 64-Bit Date Algorithm (30–40% faster by counting dates backwards)

https://www.benjoffe.com/fast-date-64
103•benjoffe•3d ago•26 comments

From blood sugar to brain relief: GLP-1 therapy slashes migraine frequency

https://www.medlink.com/news/from-blood-sugar-to-brain-relief-glp-1-therapy-slashes-migraine-freq...
66•Anon84•3h ago•39 comments

Gemini CLI Tips and Tricks for Agentic Coding

https://github.com/addyosmani/gemini-cli-tips
38•ayoisaiah•1h ago•8 comments

A cell so minimal that it challenges definitions of life

https://www.quantamagazine.org/a-cell-so-minimal-that-it-challenges-definitions-of-life-20251124/
187•ibobev•9h ago•81 comments

A Woman on a Mission to Photograph Every Species of Hummingbird

https://www.audubon.org/magazine/meet-woman-mission-photograph-every-species-of-hummingbird-world
26•zeech•3d ago•2 comments

API that auto-routes to the cheapest AI provider (OpenAI/Anthropic/Gemini)

https://tokensaver.org/
3•h2o_wine•32m ago•1 comments

Show HN: I turned algae into a bio-altimeter and put it on a weather balloon

https://radi8.dev/blog/stratospore/
57•radeeyate•4d ago•5 comments

Statistical Process Control in Python

https://timothyfraser.com/sigma/statistical-process-control-in-python.html
164•lifeisstillgood•11h ago•52 comments

OpenAI needs to raise at least $207B by 2030

https://ft.com/content/23e54a28-6f63-4533-ab96-3756d9c88bad
447•akira_067•4h ago•385 comments

Optery (YC W22) Hiring CISO, Release Manager, Tech Lead (Node), Full Stack Eng

https://www.optery.com/careers/
1•beyondd•2h ago

DRAM prices are spiking, but I don't trust the industry's why

https://www.xda-developers.com/dram-prices-spiking-dont-trust-industry-reasons/
72•binarycrusader•2h ago•31 comments

JOPA: Java compiler in C++, Jikes modernized to Java 6 with Claude

https://github.com/7mind/jopa
35•pshirshov•3d ago•25 comments

China Has Three Reusable Rockets Ready for Their Debut Flights

https://www.china-in-space.com/p/china-has-three-reusable-rockets
21•speckx•59m ago•7 comments

Show HN: KiDoom – Running DOOM on PCB Traces

https://www.mikeayles.com/#kidoom
305•mikeayles•21h ago•41 comments

Copyparty, the FOSS file server [video]

https://www.youtube.com/watch?v=15_-hgsX2V0
190•franczesko•6d ago•48 comments

Surprisingly, Emacs on Android is pretty good

https://kristofferbalintona.me/posts/202505291438/
211•harryday•3d ago•108 comments

Is DWPD Still a Useful SSD Spec?

https://klarasystems.com/articles/is-dwpd-still-useful-ssd-spec/
40•zdw•5d ago•26 comments

Scaleway turns Mac Minis into high‑density, Raspberry Pi–managed servers

https://www.scaleway.com/en/blog/how-we-turn-apples-mac-mini-into-high-performance-dedicated-serv...
64•Lwrless•2h ago•70 comments

Image Diffusion Models Exhibit Emergent Temporal Propagation in Videos

https://arxiv.org/abs/2511.19936
89•50kIters•11h ago•12 comments

Qiskit open-source SDK for working with quantum computers

https://github.com/Qiskit/qiskit
28•thinkingemote•7h ago•2 comments

Slop Detective – Fight the Slop Syndicate

https://slopdetective.kagi.com/
35•speckx•3h ago•13 comments

Jakarta is now the biggest city in the world

https://www.axios.com/2025/11/24/jakarta-tokyo-worlds-biggest-city-population
422•skx001•1d ago•319 comments

CS234: Reinforcement Learning Winter 2025

https://web.stanford.edu/class/cs234/
176•jonbaer•19h ago•43 comments

Show HN: We built an open source, zero webhooks payment processor

https://github.com/flowglad/flowglad
352•agreeahmed•1d ago•201 comments

How to repurpose your old phone into a web server

https://far.computer/how-to/
310•louismerlin•4d ago•106 comments

A new bridge links the math of infinity to computer science

https://www.quantamagazine.org/a-new-bridge-links-the-strange-math-of-infinity-to-computer-scienc...
227•digital55•23h ago•127 comments

Launch HN: Onyx (YC W24) – Open-source chat UI

223•Weves•1d ago•144 comments

I DM'd a Korean presidential candidate and ended up building his core campaign

https://medium.com/@wjsdj2008/i-dmd-a-korean-presidential-candidate-and-ended-up-building-his-cor...
143•wjsdj2009•6h ago•68 comments

1,700-year-old Roman sarcophagus is unearthed in Budapest

https://apnews.com/article/hungary-roman-sarcophagus-discovery-budapest-77a41fe190bbcc167b43d0514...
129•gmays•1d ago•71 comments