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

Ghostty is now non-profit

https://mitchellh.com/writing/ghostty-non-profit
949•vrnvu•11h ago•193 comments

New homes in London were delayed by 'energy-hungry' data centres

https://www.bbc.com/news/articles/c0mpr1mvwj3o
59•1659447091•2h ago•16 comments

Valve reveals it’s the architect behind a push to bring Windows games to Arm

https://www.theverge.com/report/820656/valve-interview-arm-gaming-steamos-pierre-loup-griffais
622•evolve2k•1d ago•545 comments

Average DRAM price in USD over last 18 months

https://pcpartpicker.com/trends/price/memory/
170•zekrioca•6h ago•94 comments

Reverse engineering a $1B Legal AI tool exposed 100k+ confidential files

https://alexschapiro.com/security/vulnerability/2025/12/02/filevine-api-100k
588•bearsyankees•12h ago•190 comments

Micron Announces Exit from Crucial Consumer Business

https://investors.micron.com/news-releases/news-release-details/micron-announces-exit-crucial-con...
468•simlevesque•12h ago•232 comments

1D Conway's Life glider found, 3.7B cells long

https://conwaylife.com/forums/viewtopic.php?&p=222136#p222136
396•nooks•12h ago•136 comments

The Differences Between an IndyCar and a F1 Car

https://www.openwheelworld.net/en/indycar101/76/IndyCar_vs_Formula_1_cars
32•1659447091•3d ago•2 comments

Show HN: I built a dashboard to compare mortgage rates across 120 credit unions

https://finfam.app/blog/credit-union-mortgages
213•mhashemi•9h ago•72 comments

Acme, a brief history of one of the protocols which has changed the Internet

https://blog.brocas.org/2025/12/01/ACME-a-brief-history-of-one-of-the-protocols-which-has-changed...
86•coffee--•6h ago•35 comments

Kea DHCP: Modern, open source DHCPv4 and DHCPv6 server

https://www.isc.org/kea/
67•doener•6h ago•21 comments

Russia Bans Roblox

https://www.bbc.com/news/articles/cn41q11gy58o
43•disqard•1h ago•26 comments

RCE Vulnerability in React and Next.js

https://github.com/vercel/next.js/security/advisories/GHSA-9qr9-h5gf-34mp
460•rayhaanj•14h ago•155 comments

Euler Conjecture and CDC 6600

https://fortran-lang.discourse.group/t/euler-conjecture-and-cdc-6600/10501
19•zaikunzhang•2h ago•3 comments

8086 Microcode Browser

https://nand2mario.github.io/posts/2025/8086_microcode_browser/
81•zdw•8h ago•0 comments

Ethiopian Volcano Erupts for First Time in Nearly 12K Years of Records

https://www.smithsonianmag.com/smart-news/ethiopian-volcano-erupts-for-the-first-time-in-nearly-1...
28•pseudolus•3d ago•4 comments

Show HN: Identifiy test coverage gaps in your Go projects

https://github.com/LeanerCloud/testvet
9•alien_•3d ago•1 comments

Launch HN: Phind 3 (YC S22) – Every answer is a mini-app

97•rushingcreek•12h ago•76 comments

Preserving Snow Crystals

https://www.its.caltech.edu/~atomic/snowcrystals/preserve/preserve.htm
31•jameslk•4d ago•8 comments

Lie groups are crucial to some of the most fundamental theories in physics

https://www.quantamagazine.org/what-are-lie-groups-20251203/
113•ibobev•10h ago•43 comments

How to Synthesize a House Loop

https://loopmaster.xyz/tutorials/how-to-synthesize-a-house-loop
205•stagas•6d ago•73 comments

Checked-size array parameters in C

https://lwn.net/SubscriberLink/1046840/3eb9029084cc9e1e/
80•chmaynard•9h ago•28 comments

Anthropic taps IPO lawyers as it races OpenAI to go public

https://www.ft.com/content/3254fa30-5bdb-4c30-8560-7cd7ebbefc5f
322•GeorgeWoff25•20h ago•251 comments

Everyone in Seattle hates AI

https://jonready.com/blog/posts/everyone-in-seattle-hates-ai.html
710•mips_avatar•10h ago•702 comments

What I don’t like about chains of thoughts (2023)

https://samsja.github.io/blogs/cot/blog/
35•jxmorris12•3d ago•14 comments

Why are my headphones buzzing whenever I run my game?

https://alexene.dev/2025/12/03/Why-do-my-headphones-buzz-when-i-run-my-game.html
167•pacificat0r•14h ago•120 comments

You can't fool the optimizer

https://xania.org/202512/03-more-adding-integers
245•HeliumHydride•17h ago•148 comments

Why WinQuake exists and how it works

https://fabiensanglard.net/winquake/index.html
16•wicket•4h ago•0 comments

MinIO is now in maintenance-mode

https://github.com/minio/minio/commit/27742d469462e1561c776f88ca7a1f26816d69e2
443•hajtom•14h ago•262 comments

Rocketable (YC W25) is hiring a founding engineer to automate software companies

https://www.ycombinator.com/companies/rocketable/jobs/CArgzmX-founding-engineer-automation-platform
1•alanwells•13h ago