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•6mo ago

Comments

returningfory2•6mo 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•6mo 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•6mo 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•6mo 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•6mo 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•6mo ago
At least you have the tools to understand where things get allocated.
38•6mo ago
instead of this:

    t.Buf = []byte{}
you can just do:

    t.Buf = nil
rsc•6mo ago
Those are semantically different (one is nil and one is not) but neither allocates.
virexene•6mo 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•6mo 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

Why is Zig so cool?

https://nilostolte.github.io/tech/articles/ZigCool.html
275•vitalnodo•8h ago•145 comments

Snapchat open-sources Valdi a cross-platform UI framework

https://github.com/Snapchat/Valdi
198•yehiaabdelm•7h ago•48 comments

Becoming a Compiler Engineer

https://rona.substack.com/p/becoming-a-compiler-engineer
186•lalitkale•9h ago•82 comments

Myna: Monospace typeface designed for symbol-heavy programming languages

https://github.com/sayyadirfanali/Myna
246•birdculture•12h ago•108 comments

How did I get here?

https://how-did-i-get-here.net/
195•zachlatta•11h ago•34 comments

Immutable Software Deploys Using ZFS Jails on FreeBSD

https://conradresearch.com/articles/immutable-software-deploy-zfs-jails
54•vermaden•6h ago•20 comments

Ruby Solved My Problem

https://newsletter.masilotti.com/p/ruby-already-solved-my-problem
212•joemasilotti•12h ago•79 comments

Why I love OCaml (2023)

https://mccd.space/posts/ocaml-the-worlds-best/
314•art-w•17h ago•218 comments

How to find your ideal customer, right away

https://www.reifyworks.com/writing/2023-01-30-iicp
17•mrbbk•4d ago•2 comments

YouTube Removes Windows 11 Bypass Tutorials, Claims 'Risk of Physical Harm'

https://news.itsfoss.com/youtube-removes-windows-11-bypass-tutorials/
551•WaitWaitWha•10h ago•193 comments

Cerebras Code now supports GLM 4.6 at 1000 tokens/sec

https://www.cerebras.ai/code
68•nathabonfim59•7h ago•43 comments

FSF40 Hackathon

https://www.fsf.org/events/fsf40-hackathon
73•salutis•5d ago•2 comments

How a devboard works (and how to make your own)

https://kaipereira.com/journal/build-a-devboard
63•kaipereira•8h ago•8 comments

Show HN: Find matching acrylic paints for any HEX color

https://acrylicmatch.com/
13•dotspencer•4d ago•6 comments

Can you save on LLM tokens using images instead of text?

https://pagewatch.ai/blog/post/llm-text-as-image-tokens/
13•lpellis•6d ago•4 comments

Venn Diagram for 7 Sets

https://moebio.com/research/sevensets/
117•bramadityaw•3d ago•24 comments

Running a 68060 CPU in Quadra 650

https://github.com/ZigZagJoe/Macintosh-Q650-68060
27•zdw•6h ago•2 comments

Local First Htmx

https://elijahm.com/posts/local_first_htmx/
16•srid•4h ago•9 comments

Transducer: Composition, abstraction, performance (2018)

https://funktionale-programmierung.de/en/2018/03/22/transducer.html
91•defmarco•3d ago•3 comments

Angel Investors, a Field Guide

https://www.jeanyang.com/posts/angel-investors-a-field-guide/
128•azhenley•14h ago•27 comments

Mullvad: Shutting down our search proxy Leta

https://mullvad.net/en/blog/shutting-down-our-search-proxy-leta
106•holysoles•6h ago•60 comments

Why I love my Boox Palma e-reader

https://minimal.bearblog.dev/why-i-love-my-boox-palma-e-reader/
55•pastel5•5d ago•29 comments

Using the Web Monetization API for fun and profit

https://blog.tomayac.com/2025/11/07/using-the-web-monetization-api-for-fun-and-profit/
49•tomayac•9h ago•13 comments

Analysis of Hedy Lamarr's Contribution to Spread-Spectrum Communication

https://researchers.one/articles/24.01.00001v4
53•drmpeg•7h ago•37 comments

Ribir: Non-intrusive GUI framework for Rust/WASM

https://github.com/RibirX/Ribir
55•adamnemecek•10h ago•7 comments

Blood, Brick and Legend: The Chemistry of Dracula's Castle

https://news.research.gatech.edu/2025/10/31/blood-brick-and-legend-chemistry-draculas-castle
4•dhfbshfbu4u3•4d ago•0 comments

Shell Grotto: England's mysterious underground seashell chamber

https://boingboing.net/2025/09/05/shell-grotto-englands-mysterious-underground-seashell-chamber.html
19•the-mitr•3d ago•6 comments

VLC's Jean-Baptiste Kempf Receives the European SFS Award 2025

https://fsfe.org/news/2025/news-20251107-01.en.html
297•kirschner•10h ago•52 comments

James Watson has died

https://www.nytimes.com/2025/11/07/science/james-watson-dead.html
287•granzymes•11h ago•158 comments

FAA to restrict commercial rocket launches to overnight hours

https://www.space.com/space-exploration/launches-spacecraft/faa-restricts-commercial-rocket-launc...
128•bookmtn•8h ago•65 comments