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

Basalt Woven Textile

https://materialdistrict.com/material/basalt-woven-textile/
92•rbanffy•4h ago•47 comments

Android/Linux Dual Boot

https://wiki.postmarketos.org/wiki/Dual_Booting/WiP
83•joooscha•3d ago•23 comments

CUDA Ontology

https://jamesakl.com/posts/cuda-ontology/
71•gugagore•3d ago•8 comments

Interactive World History Atlas Since 3000 BC

http://geacron.com/home-en/
11•not_knuth•26m ago•1 comments

Implementation of a Java Processor on a FPGA

https://mavmatrix.uta.edu/electricaleng_theses/337/
36•mghackerlady•3h ago•16 comments

Europe is scaling back GDPR and relaxing AI laws

https://www.theverge.com/news/823750/european-union-ai-act-gdpr-changes
733•ksec•19h ago•824 comments

Loose wire leads to blackout, contact with Francis Scott Key bridge

https://www.ntsb.gov:443/news/press-releases/Pages/NR20251118.aspx
344•DamnInteresting•13h ago•131 comments

Meta Segment Anything Model 3

https://ai.meta.com/sam3/
485•lukeinator42•17h ago•96 comments

AI is a front for consolidation of resources and power

https://www.chrbutler.com/what-ai-is-really-for
341•delaugust•15h ago•256 comments

Researchers discover security vulnerability in WhatsApp

https://www.univie.ac.at/en/news/detail/forscherinnen-entdecken-grosse-sicherheitsluecke-in-whatsapp
230•KingNoLimit•13h ago•85 comments

Building more with GPT-5.1-Codex-Max

https://openai.com/index/gpt-5-1-codex-max/
410•hansonw•16h ago•236 comments

New Proofs Probe Soap-Film Singularities

https://www.quantamagazine.org/new-proofs-probe-soap-film-singularities-20251112/
5•pseudolus•1w ago•0 comments

#!magic, details about the shebang/hash-bang mechanism on various Unix flavours

https://www.in-ulm.de/%7Emascheck/various/shebang/
31•js2•5h ago•6 comments

PHP 8.5 gets released today, here's what's new

https://stitcher.io/blog/new-in-php-85
108•brentroose•4h ago•38 comments

Precise geolocation via Wi-Fi Positioning System

https://www.amoses.dev/blog/wifi-location/
181•nicosalm•12h ago•70 comments

Show HN: An A2A-compatible, open-source framework for multi-agent networks

https://github.com/openagents-org/openagents
41•snasan•4h ago•33 comments

What really happened with the CIA and The Paris Review?

https://www.theparisreview.org/blog/2025/11/11/what-really-happened-with-the-cia-and-the-paris-re...
61•frenzcan•1w ago•3 comments

CLI tool to check the Git status of multiple projects

https://github.com/uralys/check-projects
31•chrisdugne•6d ago•13 comments

Launch HN: Mosaic (YC W25) – Agentic Video Editing

https://mosaic.so
123•adishj•18h ago•116 comments

How Slide Rules Work

https://amenzwa.github.io/stem/ComputingHistory/HowSlideRulesWork/
111•ColinWright•13h ago•28 comments

The Lucas-Lehmer Prime Number Test

https://www.scientificamerican.com/article/how-to-identify-a-prime-number-without-a-computer/
72•beardyw•1w ago•40 comments

Static Web Hosting on the Intel N150: FreeBSD, SmartOS, NetBSD, OpenBSD and Linu

https://it-notes.dragas.net/2025/11/19/static-web-hosting-intel-n150-freebsd-smartos-netbsd-openb...
164•t-3•16h ago•60 comments

The lost cause of the Lisp machines

https://www.tfeb.org/fragments/2025/11/18/the-lost-cause-of-the-lisp-machines/
74•enbywithunix•14h ago•66 comments

Gaming on Linux has never been more approachable

https://www.theverge.com/tech/823337/switching-linux-gaming-desktop-cachyos
407•throwaway270925•12h ago•291 comments

The Complete Work of Charles Darwin Online

https://darwin-online.org.uk/
57•bookofjoe•6d ago•2 comments

The patent office is about to make bad patents untouchable

https://www.eff.org/deeplinks/2025/11/patent-office-about-make-bad-patents-untouchable
441•iamnothere•12h ago•59 comments

Vortex: An extensible, state of the art columnar file format

https://github.com/vortex-data/vortex
76•tanelpoder•5d ago•17 comments

Measuring political bias in Claude

https://www.anthropic.com/news/political-even-handedness
76•gmays•14h ago•117 comments

The sixtyforgan: a Commodore 64 with a spring reverb; chiptunes like a church or

http://www.linusakesson.net/sixtyforgan/
6•fanf2•36m ago•0 comments

Robert Louis Stevenson's Art of Living (and Dying)

https://lithub.com/robert-louis-stevensons-art-of-living-and-dying/
26•Caiero•14h ago•2 comments