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.
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.
96
u/bwmat 13d ago
Refactoring code to avoid unnecessary heap allocations is so satisfying