News for hackers. Stuff that matters.
Re:Appending to an std:string character-by-character: how do... by Permit on Thursday October 26, 2023 @07:05PM (#38032785)
> Under macOS (LLVM 15), I get that clang doubles the capacity and add one

That’s interesting, why add one? Is there something to suggest this is better than simply doubling in real world scenarios?

Re:Appending to an std:string character-by-character: how do... by kevinventullo on Thursday October 26, 2023 @07:27PM (#38032956)
It’s a stretch, but perhaps this is related to the desire to avoid hash tables which are of size exactly a power of two?

https://cs.stackexchange.com/questions/19020/why-should-one-...

Re:Appending to an std:string character-by-character: how do... by pjscott on Thursday October 26, 2023 @07:50PM (#38033120)
LLVM's libc++ doesn't use a generic growable vector thing for std::string; they rolled their own, specifically for strings. (I checked the source. Alas, no explanatory comments and nothing in the "initial libc++ import" git commit message.)
Re:Appending to an std:string character-by-character: how do... by akomtu on Thursday October 26, 2023 @07:11PM (#38032829)
The trailing 0 char?
Re:Appending to an std:string character-by-character: how do... by tedunangst on Thursday October 26, 2023 @07:32PM (#38032997)
In theory, it should be (len - 1) x 2 + 1 in that case.
Re:Appending to an std:string character-by-character: how do... by jltsiren on Thursday October 26, 2023 @08:24PM (#38033314)
With an allocation of size n, you have capacity for n-1 characters and the trailing 0, and the trailing 0 does not count as a character. If you double the size of the allocation to 2n, the capacity increases to 2(n-1) + 1.
Re:Appending to an std:string character-by-character: how do... by tedunangst on Thursday October 26, 2023 @09:23PM (#38033655)
So why does it increase the size of the allocation to 2n + 1?
Re:Appending to an std:string character-by-character: how do... by jltsiren on Thursday October 26, 2023 @10:05PM (#38033868)
Allocation size is capacity + 1. If you double the allocation size to 2 * (capacity + 1), the new capacity is 2 * capacity + 1.
Re:Appending to an std:string character-by-character: how do... by anonymoushn on Friday October 27, 2023 @12:41AM (#38034766)
Surely 2 * (capacity + 1) = 2 * capacity + 2
Re:Appending to an std:string character-by-character: how do... by jltsiren on Friday October 27, 2023 @01:38AM (#38035064)
That is the new allocation size. The new capacity is one character less. The allocation must include space for the trailing 0, which is not a part of the string and is hence not included in the length/capacity numbers.
Re:Appending to an std:string character-by-character: how do... by anonymoushn on Friday October 27, 2023 @02:27AM (#38035309)
I see, thanks for your patience :)
Re:Appending to an std:string character-by-character: how do... by ace2358 on Friday October 27, 2023 @05:31AM (#38036375)
Great thread, I was scratching me head a bit too.
Re:Appending to an std:string character-by-character: how do... by bluGill on Thursday October 26, 2023 @07:49PM (#38033115)
They may also know something about the memory allocator.
Re:Appending to an std:string character-by-character: how do... by rerdavies on Friday October 27, 2023 @01:02AM (#38034883)
Better L1/L2 cache performance? I'd put good money on that explanation.