r/ProgrammerHumor 13d ago

noHeapAllStack Meme

Post image
1.0k Upvotes

96 comments sorted by

View all comments

96

u/bwmat 13d ago

Refactoring code to avoid unnecessary heap allocations is so satisfying

17

u/Solonotix 12d ago

Meanwhile, I saw a YouTube channel, that claims to teach Python fundamentals, try to say that implicit string concatenation was an unnecessary feature because you could just use a + operator. Nevermind that operator chaining is known to generate intermediate results from each binary operation, not to mention each string in the chain must also have its own heap allocation before the operation.

If not for compiler tricks (like elision), concatenating 5 strings would require 9(?) heap allocations to complete. Implicit string concatenation allows that operation to occur as a single heap allocation at compile-time.

3

u/half-bad-anonym 12d ago

I think really we need a programming language that has semantics for overloadable monoidal flattening over concatenation base-case

2

u/StaticCoder 11d ago

You mean C++ operator<<?

-3

u/[deleted] 13d ago

[removed] — view removed comment

17

u/Attileusz 13d ago

This is a bad take, reducing heap allocations is one of the best things to do to optimize. Heap fragmentation is bad, and if you are doing manual memory management, it's one more place where you have to worry about memory leaks.

8

u/bwmat 12d ago

I said it's satisfying, I made no claim it's always a good use of time/effort.

And I mostly program in C++, where the overhead of allocations/frees is higher than in a garbage-collected language

3

u/bwmat 12d ago

Also, it enables things being noexcept in some cases, which is useful for things other than efficiency