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

Show HN: Wealthfolio 2.0- Open source investment tracker. Now Mobile and Docker

https://wealthfolio.app/?v=2.0
210•a-fadil•2h ago•79 comments

You can make PS2 games in JavaScript

https://jslegenddev.substack.com/p/you-can-now-make-ps2-games-in-javascript
119•tosh•2h ago•16 comments

Arduino published updated terms and conditions: no longer an open commons

https://www.molecularist.com/2025/11/did-qualcomm-kill-arduino-for-good.html
135•felineflock•3h ago•47 comments

We should all be using dependency cooldowns

https://blog.yossarian.net/2025/11/21/We-should-all-be-using-dependency-cooldowns
126•todsacerdoti•4h ago•100 comments

Helping Valve to Power Up Steam Devices

https://www.igalia.com/2025/11/helpingvalve.html
37•TingPing•2h ago•2 comments

More tales about outages and numeric limits

https://rachelbythebay.com/w/2025/11/18/down/
26•todsacerdoti•5h ago•3 comments

Building a Durable Execution Engine with SQLite

https://www.morling.dev/blog/building-durable-execution-engine-with-sqlite/
23•ingve•1d ago•6 comments

Shop Sans is a typeface for curved text paths

https://www.futurefonts.com/hex/shop-sans
31•tobr•1w ago•11 comments

FAWK: LLMs can write a language interpreter

https://martin.janiczek.cz/2025/11/21/fawk-llms-can-write-a-language-interpreter.html
177•todsacerdoti•9h ago•153 comments

How/why to sweep async tasks under a Postgres table

https://taylor.town/pg-task
9•ostler•1h ago•0 comments

Pivot Robotics (YC W24) Is Hiring for an Industrial Automation Hardware Engineer

https://www.ycombinator.com/companies/pivot-robotics/jobs/7xG9Dc6-mechanical-engineer-controls
1•vigneshrajmohan•2h ago

FizzBuzz with Cosines

https://susam.net/fizz-buzz-with-cosines.html
20•hprotagonist•2h ago•6 comments

Olmo 3: Charting a path through the model flow to lead open-source AI

https://allenai.org/blog/olmo3
319•mseri•12h ago•94 comments

Making a Small RPG

https://jslegenddev.substack.com/p/making-a-small-rpg
108•ibobev•6h ago•25 comments

Command Lines

https://www.wreflection.com/p/command-lines-ai-coding
23•nowflux•3h ago•3 comments

It's hard to build an oscillator

https://lcamtuf.substack.com/p/its-hard-to-build-an-oscillator
184•chmaynard•11h ago•69 comments

Scientists now know that bees can process time, a first in insects

https://www.cnn.com/2025/11/12/science/bees-visual-stimulus-study-scli-intl
160•Brajeshwar•6d ago•84 comments

Make product worse, get money

https://dynomight.net/worse/
34•zdw•4h ago•13 comments

Homeschooling hits record numbers

https://reason.com/2025/11/19/homeschooling-hits-record-numbers/
76•bilsbie•19h ago•147 comments

The New AI Consciousness Paper

https://www.astralcodexten.com/p/the-new-ai-consciousness-paper
61•rbanffy•3h ago•107 comments

I converted a rotary phone into a meeting handset

https://www.stavros.io/posts/i-converted-a-rotary-phone-into-a-meeting-handset/
130•todsacerdoti•1w ago•63 comments

The Anatomy of the Least Squares Method, Part Two

https://thepalindrome.org/p/the-anatomy-of-the-least-squares-ab5
3•tzury•1w ago•0 comments

How Cops Are Using Flock's ALPR Network to Surveil Protesters and Activists

https://www.eff.org/deeplinks/2025/11/how-cops-are-using-flock-safetys-alpr-network-surveil-prote...
157•pseudalopex•2h ago•41 comments

WebAssembly from the Ground Up

https://wasmgroundup.com/
227•gurjeet•6d ago•48 comments

My Favorite Math Problem

https://bytesauna.com/post/my-favorite-math-problem
47•mapehe•4d ago•35 comments

Show HN: Search London StreetView panoramas by text

https://london.publicinsights.uk
16•dfworks•1d ago•8 comments

Open Source and Local Code Mode MCP in Deno Sandboxes

https://portofcontext.com
68•pmkelly4444•1w ago•25 comments

How a French judge was digitally cut off by the USA

https://www.heise.de/en/news/How-a-French-judge-was-digitally-cut-off-by-the-USA-11087561.html
292•i-con•7h ago•334 comments

FEX-emu – Run x86 applications on ARM64 Linux devices

https://fex-emu.com/
260•open-paren•1w ago•113 comments

XBMC 4.0 for the Original Xbox

https://www.xbox-scene.info/articles/announcing-xbmc-40-for-the-original-xbox-r64/
81•zdw•4h ago•43 comments