4 ms·
Thanks for the feedback. You are right. I added some text about that and mentioned you at the bottom. C++ implementations seem to store the capacity in the str
by qznc 8y ago
Thanks for the feedback. You are right. I added some text about that and mentioned you at the bottom.
C++ implementations seem to store the capacity in the string object instead of the next to the referenced memory as I described in the article. Do you know why?
- Someone 8y agoI would guess compiler writers have data that indicates that the advantage “longer strings can be stored using SSO” beats the disadvantage “every short string is longer”. It also means accessing the capacity field doesn’t involve accessing another cache line, but I can’t think of situations where that matters, as every time code needs to access capacity, the CPU will soon do something with the pointed to object (if only to free it. Or are there real-world allocators that don’t look at memory ‘near’ to-be-freed objects?)
- cheerlessbog 8y agoIf you are comparing two strings for equality, most times the length will differ and you can avoid the dereference. Also an empty string need not have a pointer.
- hyperpape 8y agoEquality comparisons might short-circuit if two strings differ in length (they do in Java). I’d never thought about it before, but I’m guessing that only makes sense if you’re assume normalization already happened.
- Someone 8y agoGood example. AFAIK, C++ strings are little more than sequences of char, and string equals compares those sequences. It won’t notice when the two strings happen to be equivalent under some Unicode normalization.