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

The Peaceful Transfer of Power in Open Source Projects

https://shkspr.mobi/blog/2025/11/the-peaceful-transfer-of-power-in-open-source-projects/
65•edent•1h ago•30 comments

Your Smartphone, Their Rules: App Stores Enable Corporate-Government Censorship

https://www.aclu.org/news/free-speech/app-store-oligopoly
151•pabs3•1h ago•63 comments

Multimodal Diffusion Language Models for Thinking-Aware Editing and Generation

https://github.com/tyfeld/MMaDA-Parallel
85•lnyan•5h ago•6 comments

The $1k AWS Mistake

https://www.geocod.io/code-and-coordinates/2025-11-18-the-1000-aws-mistake/
110•thecodemonkey•5h ago•118 comments

The Future of Programming (2013) [video]

https://www.youtube.com/watch?v=8pTEmbeENF4
76•jackdoe•6d ago•41 comments

Gemini 3

https://blog.google/products/gemini/gemini-3/
1552•preek•1d ago•973 comments

I made a down detector for down detector

https://downdetectorsdowndetector.com
347•gusowen•15h ago•115 comments

Proxmox Virtual Environment 9.1 available

https://www.proxmox.com/en/about/company-details/press-releases/proxmox-virtual-environment-9-1
28•speckx•44m ago•4 comments

Gymkhana's 1978 Subaru Brat with 9,500-RPM Redline, Active Aero Is One Super Ute

https://www.thedrive.com/news/gymkhanas-1978-subaru-brat-with-9500-rpm-redline-and-active-aero-is...
13•PaulHoule•1w ago•11 comments

Google Antigravity

https://antigravity.google/
971•Fysi•23h ago•952 comments

Pimped Amiga 500

https://www.pimyretro.org/pimped-amiga-500/
60•onename•3h ago•22 comments

I just want working RCS messaging

https://wt.gd/i-just-want-my-rcs-messaging-to-work
172•joecool1029•13h ago•170 comments

Pebble, Rebble, and a path forward

https://ericmigi.com/blog/pebble-rebble-and-a-path-forward/
432•phoronixrly•21h ago•223 comments

Show HN: Browser-based interactive 3D Three-Body problem simulator

https://trisolarchaos.com/?pr=O_8(0.6)&n=3&s=5.0&so=0.00&im=rk4&dt=1.00e-4&rt=1.0e-6&at=1.0e-8&bs...
177•jgchaos•1d ago•62 comments

Ultima VII Revisited

https://github.com/ViridianGames/U7Revisited
160•erickhill•1w ago•43 comments

How to Stay Sane in a World That Rewards Insanity

https://www.joanwestenberg.com/p/how-to-stay-sane-in-a-world-that-rewards-insanity
14•enbywithunix•38m ago•1 comments

Learning to Boot from PXE

https://blog.imraniqbal.org/learning-to-boot-from-pxe/
11•speckx•4h ago•6 comments

How do the pros get someone to leave a cult?

https://www.theguardian.com/science/2025/nov/19/how-to-leave-a-cult-experts-intervention
77•n1b0m•2h ago•41 comments

I wrote a Pong game in a 512-byte boot sector

https://akshatjoshi.com/i-wrote-a-pong-game-in-a-512-byte-boot-sector/
74•akshat666•4d ago•11 comments

Blender 5.0

https://www.blender.org/download/releases/5-0/
894•FrostKiwi•17h ago•286 comments

What Killed Perl?

https://entropicthoughts.com/what-killed-perl
14•speckx•4h ago•16 comments

Cloudflare outage on November 18, 2025 post mortem

https://blog.cloudflare.com/18-november-2025-outage/
1307•eastdakota•15h ago•759 comments

Mojo-V: Secret Computation for RISC-V

https://github.com/toddmaustin/mojo-v
47•fork-bomber•1w ago•14 comments

Gemini 3 Pro Model Card [pdf]

https://storage.googleapis.com/deepmind-media/Model-Cards/Gemini-3-Pro-Model-Card.pdf
248•virgildotcodes•1d ago•323 comments

Itiner-e: A high-resolution dataset of roads of the Roman Empire

https://www.nature.com/articles/s41597-025-06140-z
15•benbreen•1w ago•3 comments

The code and open-source tools I used to produce a science fiction anthology

https://compellingsciencefiction.com/posts/the-code-and-open-source-tools-i-used-to-produce-a-sci...
175•mojoe•23h ago•26 comments

Cloudflare Global Network experiencing issues

https://www.cloudflarestatus.com/incidents/8gmgl950y3h7
2391•imdsm•1d ago•1619 comments

Strace-macOS: A clone of the strace command for macOS

https://github.com/Mic92/strace-macos
78•signa11•14h ago•18 comments

Bluetooth Channel Sounding: The Next Leap in Bluetooth Innovation

https://www.embedded.com/bluetooth-channel-sounding-the-next-leap-in-bluetooth-innovation?_gl=1*8...
60•JoachimS•6d ago•32 comments

A Rigorous Approach to the Algorithmic Composition of Iannis Xenakis(2009) [pdf]

https://monoskop.org/images/3/38/Hoffmann_Peter_Music_Out_of_Nothing_A_Rigorous_Approach_to_Algor...
16•ofalkaed•4d ago•1 comments