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

Verifying your Matrix devices is becoming mandatory

https://element.io/blog/verifying-your-devices-is-becoming-mandatory-2/
84•LorenDB•3h ago•64 comments

Loose wire leads to blackout, contact with Francis Scott Key bridge

https://www.ntsb.gov:443/news/press-releases/Pages/NR20251118.aspx
265•DamnInteresting•7h ago•109 comments

Researchers discover security vulnerability in WhatsApp

https://www.univie.ac.at/en/news/detail/forscherinnen-entdecken-grosse-sicherheitsluecke-in-whatsapp
156•KingNoLimit•7h ago•48 comments

Europe is scaling back GDPR and relaxing AI laws

https://www.theverge.com/news/823750/european-union-ai-act-gdpr-changes
565•ksec•13h ago•587 comments

Meta Segment Anything Model 3

https://ai.meta.com/sam3/
315•lukeinator42•10h ago•60 comments

Building more with GPT-5.1-Codex-Max

https://openai.com/index/gpt-5-1-codex-max/
353•hansonw•10h ago•203 comments

Precise geolocation via Wi-Fi Positioning System

https://www.amoses.dev/blog/wifi-location/
119•nicosalm•6h ago•63 comments

What really happened with the CIA and The Paris Review?

https://www.theparisreview.org/blog/2025/11/11/what-really-happened-with-the-cia-and-the-paris-re...
24•frenzcan•1w ago•1 comments

What Influence Has the BBC Had on History?

https://www.historytoday.com/archive/head-head/what-influence-has-bbc-had-history
5•pepys•2d ago•0 comments

Robert Louis Stevenson's Art of Living (and Dying)

https://lithub.com/robert-louis-stevensons-art-of-living-and-dying/
7•Caiero•8h ago•0 comments

Launch HN: Mosaic (YC W25) – Agentic Video Editing

https://mosaic.so
112•adishj•12h ago•106 comments

AI is a front for consolidation of resources and power

https://www.chrbutler.com/what-ai-is-really-for
150•delaugust•8h ago•109 comments

Measuring the impact of AI scams on the elderly

https://simonlermen.substack.com/p/can-ai-models-be-jailbroken-to-phish
64•DalasNoin•3h ago•21 comments

How Slide Rules Work

https://amenzwa.github.io/stem/ComputingHistory/HowSlideRulesWork/
67•ColinWright•6h ago•19 comments

Three Hapsburgs and a Reporter Walk into a Canadian Vault

https://www.nytimes.com/2025/11/16/insider/florentine-diamond-hapsburgs.html
5•samclemens•2d ago•2 comments

Gaming on Linux has never been more approachable

https://www.theverge.com/tech/823337/switching-linux-gaming-desktop-cachyos
275•throwaway270925•6h ago•203 comments

Larry Summers resigns from OpenAI board

https://www.cnbc.com/2025/11/19/larry-summers-epstein-openai.html
287•koolba•14h ago•298 comments

Thunderbird adds native Microsoft Exchange email support

https://blog.thunderbird.net/2025/11/thunderbird-adds-native-microsoft-exchange-email-support/
347•babolivier•16h ago•101 comments

Static Web Hosting on the Intel N150: FreeBSD, SmartOS, NetBSD, OpenBSD and Linu

https://it-notes.dragas.net/2025/11/19/static-web-hosting-intel-n150-freebsd-smartos-netbsd-openb...
131•t-3•10h ago•45 comments

The patent office is about to make bad patents untouchable

https://www.eff.org/deeplinks/2025/11/patent-office-about-make-bad-patents-untouchable
305•iamnothere•6h ago•28 comments

Blame as a Service

https://www.humaninvariant.com/blog/blame
87•humaninvariant•1w ago•9 comments

Vortex: An extensible, state of the art columnar file format

https://github.com/vortex-data/vortex
51•tanelpoder•5d ago•8 comments

Racing karts on a Rust GPU kernel driver

https://www.collabora.com/news-and-blog/news-and-events/racing-karts-on-a-rust-gpu-kernel-driver....
52•mfilion•7h ago•3 comments

CornHub

https://cornhub.website/
24•andy99•4h ago•4 comments

Microsoft AI CEO pushes back against critics after recent Windows AI backlash

https://www.windowscentral.com/microsoft/windows-11/microsoft-ai-ceo-pushes-back-against-critics-...
122•thewebguyd•7h ago•129 comments

Measuring political bias in Claude

https://www.anthropic.com/news/political-even-handedness
45•gmays•8h ago•65 comments

Branching with or Without PII: The Future of Environments

https://neon.com/blog/branching-environments-anonymized-pii
13•emschwartz•1w ago•3 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
157•enbywithunix•13h ago•133 comments

Cognitive and mental health correlates of short-form video use

https://psycnet.apa.org/fulltext/2026-89350-001.html
232•smartmic•8h ago•164 comments

A $1k AWS mistake

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