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

It’s time to free JavaScript

https://javascript.tm/letter
256•pavelai•4h ago•111 comments

I ignore the spotlight as a staff engineer

https://lalitm.com/software-engineering-outside-the-spotlight/
82•todsacerdoti•2h ago•11 comments

Functional Quadtrees

https://lbjgruppen.com/en/posts/functional-quadtree-clojure
9•lbj•37m ago•1 comments

PGlite – Embeddable Postgres

https://pglite.dev/
222•dsego•3h ago•52 comments

Ghostty is now non-profit

https://mitchellh.com/writing/ghostty-non-profit
1189•vrnvu•19h ago•254 comments

RAM is so expensive, Samsung won't even sell it to Samsung

https://www.pcworld.com/article/2998935/ram-is-so-expensive-samsung-wont-even-sell-it-to-samsung....
22•sethops1•36m ago•2 comments

Show HN: Walrus – a Kafka alternative written in Rust

https://github.com/nubskr/walrus
57•janicerk•2d ago•18 comments

Unreal Tournament 2004 is back

https://old.reddit.com/r/unrealtournament/comments/1pdbe69/breaking_unreal_tournament_2004_is_back/
175•keithoffer•3h ago•68 comments

Show HN: MTXT – Music Text Format

https://github.com/Daninet/mtxt
24•daninet•4d ago•7 comments

Building optimistic UI in Rails (and learn custom elements)

https://railsdesigner.com/custom-elements/
20•amalinovic•2h ago•2 comments

Elites could shape mass preferences as AI reduces persuasion costs

https://arxiv.org/abs/2512.04047
272•50kIters•5h ago•277 comments

Micron Announces Exit from Crucial Consumer Business

https://investors.micron.com/news-releases/news-release-details/micron-announces-exit-crucial-con...
638•simlevesque•19h ago•317 comments

Average DRAM price in USD over last 18 months

https://pcpartpicker.com/trends/price/memory/
386•zekrioca•13h ago•276 comments

Interop and MathML Core

https://conflor.es/blog/2025-11-27-interop-and-mathml/
20•todsacerdoti•3d ago•2 comments

Uncloud - Tool for deploying containerised apps across servers without k8s

https://uncloud.run/
194•rgun•7h ago•91 comments

30 years ago today "Netscape and Sun announce JavaScript"

https://web.archive.org/web/20070916144913/http://wp.netscape.com/newsref/pr/newsrelease67.html
52•donohoe•2h ago•14 comments

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

https://conwaylife.com/forums/viewtopic.php?&p=222136#p222136
470•nooks•20h ago•169 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
843•evolve2k•1d ago•728 comments

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

https://finfam.app/blog/credit-union-mortgages
282•mhashemi•17h ago•94 comments

Saturn (YC S24) Is Hiring Senior AI Engineer

https://www.ycombinator.com/companies/saturn/jobs/R9s9o5f-senior-ai-engineer
1•etticat•6h ago

All the Way Down

https://www.futilitycloset.com/2025/11/17/all-the-way-down-2/
35•surprisetalk•5d ago•12 comments

Shrinking Elephants: Managing large projects with Gradle

https://engineering.block.xyz/blog/shrinking-elephants
3•rileymichael•5d ago•0 comments

Why WinQuake exists and how it works

https://fabiensanglard.net/winquake/index.html
113•wicket•11h ago•14 comments

Kea DHCP: Modern, open source DHCPv4 and DHCPv6 server

https://www.isc.org/kea/
103•doener•13h ago•31 comments

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

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

Programming peaked

https://functional.computer/blog/programming-peaked
74•Antibabelic•3h ago•71 comments

8086 Microcode Browser

https://nand2mario.github.io/posts/2025/8086_microcode_browser/
127•zdw•16h ago•1 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...
126•coffee--•14h ago•68 comments

Show HN: A Minimal Monthly Task Planner (printable, offline, no signup)

https://printcalendar.top/
74•defcc•8h ago•26 comments

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

119•rushingcreek•20h ago•87 comments