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

Nano Banana Pro

https://blog.google/technology/ai/nano-banana-pro/
967•meetpateltech•15h ago•569 comments

Android and iPhone users can now share files, starting with the Pixel 10

https://blog.google/products/android/quick-share-airdrop/
584•abraham•13h ago•338 comments

FEX-emu – Run x86 applications on ARM64 Linux devices

https://fex-emu.com/
138•open-paren•1w ago•42 comments

WebAssembly from the Ground Up

https://wasmgroundup.com/
26•gurjeet•5d ago•2 comments

New Glenn Update

https://www.blueorigin.com/news/new-glenn-upgraded-engines-subcooled-components-drive-enhanced-pe...
146•rbanffy•9h ago•71 comments

New OS aims to provide (some) compatibility with macOS

https://github.com/ravynsoft/ravynos
186•kasajian•9h ago•79 comments

Over-regulation is doubling the cost

https://rein.pk/over-regulation-is-doubling-the-cost
126•bilsbie•7h ago•197 comments

NTSB Preliminary Report – UPS Boeing MD-11F Crash [pdf]

https://www.ntsb.gov/Documents/Prelimiary%20Report%20DCA26MA024.pdf
160•gregsadetsky•12h ago•167 comments

Why top firms fire good workers

https://www.rochester.edu/newscenter/employee-turnover-why-top-firms-churn-good-workers-681832/
96•hhs•5h ago•85 comments

Data-at-Rest Encryption in DuckDB

https://duckdb.org/2025/11/19/encryption-in-duckdb
156•chmaynard•10h ago•16 comments

Hilbert space: Treating functions as vectors

https://eli.thegreenplace.net/2025/hilbert-space-treating-functions-as-vectors/
28•signa11•1w ago•12 comments

Exploring the Fragmentation of Wayland, an xdotool adventure

https://www.semicomplete.com/blog/xdotool-and-exploring-wayland-fragmentation/
69•viraptor•5d ago•39 comments

The Lions Operating System

https://lionsos.org
138•plunderer•12h ago•30 comments

GitHut – Programming Languages and GitHub (2014)

https://githut.info/
60•tonyhb•8h ago•22 comments

Okta's NextJS-0auth troubles

https://joshua.hu/ai-slop-okta-nextjs-0auth-security-vulnerability
258•ramimac•2d ago•97 comments

CBP is monitoring US drivers and detaining those with suspicious travel patterns

https://apnews.com/article/immigration-border-patrol-surveillance-drivers-ice-trump-9f5d05469ce8c...
638•jjwiseman•10h ago•714 comments

He built underground maze of light-filled earth homes in CA Sierras [video]

https://www.youtube.com/watch?v=U0bHhmpyKGg
35•surprisetalk•1w ago•9 comments

Free interactive tool that shows you how PCIe lanes work on motherboards

https://mobomaps.com
178•tagyro•1d ago•31 comments

Show HN: F32 – An Extremely Small ESP32 Board

https://github.com/PegorK/f32
214•pegor•1d ago•36 comments

Adversarial poetry as a universal single-turn jailbreak mechanism in LLMs

https://arxiv.org/abs/2511.15304
261•capgre•18h ago•140 comments

Microsoft makes Zork open-source

https://opensource.microsoft.com/blog/2025/11/20/preserving-code-that-shaped-generations-zork-i-i...
514•tabletcorry•12h ago•210 comments

Show HN: Search London StreetView panoramas by text

https://london.publicinsights.uk
5•dfworks•11h ago•2 comments

Tube: A subway route planner in Dyalog APL (2011)

https://dfns.dyalog.com/tube_n_index.htm
4•shawa_a_a•4d ago•0 comments

Two recently found works of J.S. Bach presented in Leipzig [video]

https://www.youtube.com/watch?v=4hXzUGYIL9M#t=15m19s
129•Archelaos•3d ago•83 comments

Show HN: My hobby OS that runs Minecraft

https://astral-os.org/posts/2025/10/31/astral-minecraft.html
151•avaliosdev•3d ago•16 comments

Launch HN: Poly (YC S22) – Cursor for Files

48•aabhay•12h ago•51 comments

Interactive World History Atlas Since 3000 BC

http://geacron.com/home-en/
306•not_knuth•20h ago•131 comments

Measuring Latency (2015)

https://bravenewgeek.com/everything-you-know-about-latency-is-wrong/
19•dempedempe•4h ago•7 comments

Go Cryptography State of the Union

https://words.filippo.io/2025-state/
149•ingve•13h ago•52 comments

Ask HN: How are Markov chains so different from tiny LLMs?

143•JPLeRouzic•3d ago•100 comments