Re:When zero cost abstractions aren't zero cost
by catlifeonmars on Tuesday August 10, 2021 @03:47AM
(#28126157)
> you would have to stop and think "I can't allocate _my_ byte type! I must allocate u8
No, this is premature optimization. Rather, you would write the code in the most obvious way, and then profile it to figure out which optimizations are worth doing. At that point you can comment your code explaining why it looks all wonky :)
Re:When zero cost abstractions aren't zero cost
by HexDecOctBin on Tuesday August 10, 2021 @06:25AM
(#28127076)
And applying this thinking everything means that your entire code is pervaded with slowness with no obvious hot spots, and you jump with joy since it obviously means that your code is as efficient as it possibly could be (because otherwise the profiler will show some peaks, right?).
Re:When zero cost abstractions aren't zero cost
by catlifeonmars on Tuesday August 10, 2021 @09:35PM
(#28136913)
I know this was meant as (light) sarcasm, but have you ever profiled a nontrivial program that turned out to have zero peaks? My gut tells me this would be really difficult to do by accident. The same way that writing crypto code that is resistant to timing attacks is hard.
Re:When zero cost abstractions aren't zero cost
by HexDecOctBin on Tuesday August 17, 2021 @07:59PM
(#28216193)
I have definitely seen code where the peaks are very shallow, and flattening them does nothing for the performance.
It's just an example to show that zero cost abstractions are actually not zero cost at all.
The language would have you believe the abstraction is zero cost. If you were to really believe it, you would never think about whether you need to allocate a vector of u8 or your custom byte type. They should be one and the same. That's the point of the promise of zero cost abstractions.
By the way, pre-allocating a lot of virtual memory upfront is not unheard of.
In this situation, to avoid paying the cost of the abstraction, you would have to stop and think "I can't allocate _my_ byte type! I must allocate u8, then cast the result to a vector of my custom byte type. Maybe the compiler will not like the cast so I have to create an "unsafe" block and do some pointer casting? (I don't know if that's what you would need to do in rust, or if it would have been something else).