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

Shaders: How to draw high fidelity graphics with just x and y coordinates

https://www.makingsoftware.com/chapters/shaders
81•Garbage•3h ago•14 comments

A monopoly ISP refuses to fix upstream infrastructure

https://sacbear.com/xfinity-wont-fix-internet/
410•vedmed•14h ago•199 comments

Tosijs-schema is a super lightweight schema-first LLM-native JSON schema library

https://www.npmjs.com/package/tosijs-schema
20•podperson•1h ago•9 comments

Almost all Collatz orbits attain almost bounded values

https://mathvideos.org/2023/terence-tao-almost-all-collatz-orbits-attain-almost-bounded-values/
60•measurablefunc•5d ago•11 comments

Unusual circuits in the Intel 386's standard cell logic

https://www.righto.com/2025/11/unusual-386-standard-cell-circuits.html
155•Stratoscope•11h ago•31 comments

After my dad died, we found the love letters

https://www.jenn.site/after-my-dad-died-we-found-the-love-letters/
442•eatitraw•6h ago•215 comments

Typechecking is undecideable when 'type' is a type (1989) [pdf]

https://dspace.mit.edu/bitstream/handle/1721.1/149366/MIT-LCS-TR-458.pdf
15•birdculture•5d ago•2 comments

GCC SC approves inclusion of Algol 68 Front End

https://gcc.gnu.org/pipermail/gcc/2025-November/247020.html
185•edelsohn•13h ago•68 comments

The privacy nightmare of browser fingerprinting

https://kevinboone.me/fingerprinting.html
640•ingve•22h ago•381 comments

We Induced Smells With Ultrasound

https://writetobrain.com/olfactory
560•exr0n•1d ago•151 comments

Racket v9.0

https://blog.racket-lang.org/2025/11/racket-v9-0.html
59•Fice•1h ago•0 comments

Deepnote (YC S19) is hiring engineers to build a better Jupyter notebook

https://deepnote.com/join-us
1•Equiet•3h ago

sit: Create StuffIt archives on Unix systems

https://github.com/thecloudexpanse/sit
25•classichasclass•6d ago•1 comments

Silicon Valley startups: being evil, again and again

https://notesfrombelow.org/article/silicon-valley-startups-doing-evil-again-and-again
74•iSpiderman•3h ago•31 comments

Ubuntu LTS releases to 15 years with Legacy add-on

https://canonical.com/blog/canonical-expands-total-coverage-for-ubuntu-lts-releases-to-15-years-w...
166•taubek•3d ago•96 comments

WorldGen – Text to Immersive 3D Worlds

https://www.meta.com/en-gb/blog/worldgen-3d-world-generation-reality-labs-generative-ai-research/
227•smusamashah•18h ago•75 comments

Claude Code Is Down

https://status.claude.com/incidents/538r2y9cjmhk
31•throwpoaster•2h ago•19 comments

Show HN: Forty.News – Daily news, but on a 40-year delay

https://forty.news
345•foxbarrington•20h ago•137 comments

NTSB report: Decryption of images from the Titan submersible camera [pdf] (2024)

https://data.ntsb.gov/Docket/Document/docBLOB?ID=18741602&FileExtension=pdf&FileName=Underwater%2...
140•bmurray7jhu•14h ago•66 comments

The Boring Part of Bell Labs

https://elizabethvannostrand.substack.com/p/the-boring-part-of-bell-labs
137•AcesoUnderGlass•3d ago•34 comments

Meta buried 'causal' evidence of social media harm, US court filings allege

https://www.reuters.com/sustainability/boards-policy-regulation/meta-buried-causal-evidence-socia...
473•pseudolus•14h ago•186 comments

Maybe that's not liquid water on Mars after all

https://phys.org/news/2025-11-liquid-mars.html
29•howard941•2h ago•16 comments

Antic Magazine Interviews Alan Reeve, the Creator of the Diamond OS (1990)

https://computeradsfromthepast.substack.com/p/antic-magazine-interviews-alan-reeve
11•rbanffy•1w ago•1 comments

$1900 Bug Bounty to Fix the Lenovo Legion Pro 7 16IAX10H's Speakers on Linux

https://github.com/nadimkobeissi/16iax10h-linux-sound-saga
275•rany_•1w ago•120 comments

`satisfies` is my favorite TypeScript keyword (2024)

https://sjer.red/blog/2024-12-21/
191•surprisetalk•4d ago•179 comments

Top MAGA Influencers on X/Twitter Accidentally Unmasked as Foreign Trolls

https://www.thedailybeast.com/top-maga-influencers-accidentally-unmasked-as-foreign-actors/
6•throw0101a•10m ago•3 comments

CERN Council reviews feasibility study for a next-generation collider

https://home.cern/news/press-release/accelerators/cern-council-reviews-feasibility-study-next-gen...
42•elashri•1w ago•9 comments

First kiss dates back 21M years

https://www.bbc.com/news/articles/cr43gq61g2qo
19•1659447091•4d ago•12 comments

The 1957 “Spaghetti-Grows-on-Trees” Hoax

https://www.openculture.com/2025/11/the-1957-spaghetti-grows-on-trees-hoax.html
48•PaulHoule•1w ago•27 comments

Markdown is holding you back

https://newsletter.bphogan.com/archive/issue-45-markdown-is-holding-you-back/
173•zdw•19h ago•129 comments