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

Leak confirms OpenAI is preparing ads on ChatGPT for public roll out

https://www.bleepingcomputer.com/news/artificial-intelligence/leak-confirms-openai-is-preparing-a...
165•fleahunter•2h ago•88 comments

System 7 natively boots on the Mac mini G4

https://macos9lives.com/smforum/index.php?topic=7711.0
230•ibobev•10h ago•52 comments

WinApps: Run Windows apps as if they were a part of the native Linux OS

https://github.com/winapps-org/winapps
208•klaussilveira•4d ago•93 comments

Airbus A320 – intense solar radiation may corrupt data critical for flight

https://www.airbus.com/en/newsroom/press-releases/2025-11-airbus-update-on-a320-family-precaution...
379•pyrophoenix•16h ago•111 comments

WebR – R in the Browser

https://webr.sh/
22•creata•4d ago•3 comments

Garfield's Proof of the Pythagorean Theorem

https://en.wikipedia.org/wiki/Garfield%27s_proof_of_the_Pythagorean_theorem
77•benbreen•7h ago•40 comments

High air pollution could diminish exercise benefits by half – study

https://scienceclock.com/exercise-may-protect-less-when-air-pollution-is-high-study-finds/
89•ashishgupta2209•2h ago•31 comments

DMT-induced shifts in criticality correlate with self-dissolution

https://www.jneurosci.org/content/early/2025/10/24/JNEUROSCI.0344-25.2025
27•Anon84•1h ago•10 comments

Show HN: Explore what the browser exposes about you

https://neberej.github.io/exposedbydefault/
100•coffeecoders•4d ago•34 comments

Imgur geo-blocked the UK, so I geo-unblocked my network

https://blog.tymscar.com/posts/imgurukproxy/
428•tymscar•19h ago•146 comments

Every mathematician has only a few tricks (2020)

https://mathoverflow.net/questions/363119/every-mathematician-has-only-a-few-tricks
163•nill0•12h ago•37 comments

Confessions of a Software Developer: No More Self-Censorship

https://kerrick.blog/articles/2025/confessions-of-a-software-developer-no-more-self-censorship/
269•Kerrick•15h ago•226 comments

Belgian Police exposed using botnets to manipulate EU data law impact assessment

https://old.reddit.com/r/europe/comments/1p9kxhm/belgian_federal_police_forgot_to_turn_their_vpn/
105•saubeidl•2h ago•16 comments

How stealth addresses work in Monero

https://www.johndcook.com/blog/2025/11/24/monero-stealth-addresses/
37•ibobev•4d ago•22 comments

So you wanna build a local RAG?

https://blog.yakkomajuri.com/blog/local-rag
320•pedriquepacheco•20h ago•75 comments

Molly: An Improved Signal App

https://molly.im/
364•dtj1123•20h ago•215 comments

A triangle whose interior angles sum to zero

https://www.johndcook.com/blog/2025/11/28/tricusp-triangle/
114•tzury•13h ago•53 comments

Anthony Bourdain's Lost Li.st's

https://bourdain.greg.technology/
69•gregsadetsky•3d ago•20 comments

The risk of round numbers and sharp thresholds in clinical practice

https://www.nature.com/articles/s41746-025-02079-y
44•asplake•1w ago•15 comments

Running a Business Means Contact with Reality

https://fredkozlowski.com/2025/11/02/running-a-business-means-contact-with-reality/
5•fkozlowski•3d ago•0 comments

Language is primarily a tool for communication rather than thought (2024) [pdf]

https://gwern.net/doc/psychology/linguistics/2024-fedorenko.pdf
90•netfortius•23h ago•35 comments

Airloom – 3D Flight Tracker

https://objectiveunclear.com/airloom.html
237•azinman2•21h ago•73 comments

The original ABC language, Python's predecessor (1991)

https://github.com/gvanrossum/abc-unix
116•tony•17h ago•43 comments

28M Hacker News comments as vector embedding search dataset

https://clickhouse.com/docs/getting-started/example-datasets/hackernews-vector-search-dataset
420•walterbell•19h ago•155 comments

Show HN: Choose your own adventure style Presentation

https://github.com/Skarlso/adventure-voter
48•skarlso•1w ago•9 comments

True P2P Email on Top of Yggdrasil Network

https://github.com/JB-SelfCompany/Tyr
157•basemi•21h ago•27 comments

A first look at Django's new background tasks

https://roam.be/notes/2025/a-first-look-at-djangos-new-background-tasks/
125•roam•16h ago•33 comments

The undeserved status of the pigeon-hole principle (1991)

https://www.cs.utexas.edu/~EWD/transcriptions/EWD10xx/EWD1094.html
7•tosh•5h ago•0 comments

Electron vs. Tauri

https://www.dolthub.com/blog/2025-11-13-electron-vs-tauri/
102•birdculture•17h ago•53 comments

Show HN: Mu – The Micro Network

https://github.com/asim/mu
45•asim•4d ago•26 comments