27 ms·
If you don't understand how lifetimes and associated aspects of the language work, you might very realistically run into situations where your code will be a mi
by mostlylurks 3y ago
If you don't understand how lifetimes and associated aspects of the language work, you might very realistically run into situations where your code will be a million times slower than the python, java, etc. equivalent, not faster. For instance, copying a massive dynamically allocated container in every iteration of a hot loop instead of passing a reference (much less likely to do on accident than in C++, but still possible), if you don't understand value vs reference semantics, copying, moving, etc, since garbage collected languages don't force you to learn these things, and passing around references to large collections is cheap in those languages.
- astrange 3y agoThat sounds like the problem is the container itself contains value types rather than references, but then doesn't implement copy-on-write, which is a simple way to solve this.
- gpderetta 3y ago'Some people when confronted with container copy problems think "I know, I'll use COW". Now they have two problems.' With apologies to jwz.
- xxs 3y ago>For instance, copying a massive dynamically allocated container in every iteration Java has references only by design, so you can't do that.
- RGBCube 3y agoExactly, that's the point.
- raggi 3y agoso then you do that, making stuff on the heap, doing extra zero'ing and so on, and your debug build gets faster, and you call it good. then someone profiles a release build, removes the allocation and watches the optimizer wipe out the entire copy because all you were really doing is touching a single field. there's no general case here.
- creata 3y ago> much less likely to do on accident than in C++, but still possible Tangent: I can't think of a single other language that's like this: the higher-level languages tend to pass by reference, and the lower-level languages tend to have you explicitly clone things.