Re:Libc++'s Implementation of std::string
by fyp on Thursday January 30, 2020 @09:45PM
(#22198520)
It's a tradeoff. In libc++'s version you still have string size stored in the top 7 bits so you just need a bitshift to get size. It sounds like fb's implementation would require looping until null terminator to get the size.
[ Parent
| on HN ]
Re:Libc++'s Implementation of std::string
by edflsafoiewq on Thursday January 30, 2020 @09:54PM
(#22198565)
IIRC you store (23 - size) in the last byte in "small string" mode, so when the size gets to 23, the last byte is 0, doubling as the null terminator.
Re:Libc++'s Implementation of std::string
by lilyball on Thursday January 30, 2020 @10:55PM
(#22198881)
Surely it must be (23 - size) << 1, otherwise you won't guarantee a 0 in the LSB.
Re:Libc++'s Implementation of std::string
by SamReidHughes on Thursday January 30, 2020 @11:24PM
(#22199037)
On little endian systems it uses two MSb's, on big endian systems it uses two LSb's. There are actually two flag bits.
Re:Libc++'s Implementation of std::string
by edflsafoiewq on Thursday January 30, 2020 @11:10PM
(#22198960)
If you use the LSB of the last byte for a flag, you're using the 56th bit of one of the {data,size,cap} words. I'd use the MSB in this case, since you can shift that off easier.
Re:Libc++'s Implementation of std::string
by roel_v on Thursday January 30, 2020 @10:12PM
(#22198671)
To save those who, like me, were going to comment 'that would violate the standard because std::string::size is required to be O(1) complexity' a Google - the standard recommends but doesn't require that.
Re:Libc++'s Implementation of std::string
by epistasis on Thursday January 30, 2020 @10:31PM
(#22198771)
Facebook's implementation would still be constant time for short strings, because there's a constant which bounds the runtime.
Though I hear that the definition of big-O notation has shifted a bit in Silicon Valley these days so maybe that answer would get me in trouble in an interview.
It's true that big-O notation only concerns behavior with large N, but it's a bit disingenuous to say that the loop executes a constant number of times -- by that argument, you could say that if you implemented size() by strlen() it's O(1) because the string must be less than 2^64 bytes long on a 64-bit machine.
So I can see why someone would claim that implementing size() via strlen() "only" for small strings shouldn't be considered O(1), because strlen() is O(n) and within that class of strings the runtime is increasing as the length increases.
Re:Libc++'s Implementation of std::string
by coolplants on Tuesday February 04, 2020 @07:17AM
(#22234701)
I think it’s a bit more disingenuous to compare the magnitudes of 2^64 and 23 for the sake of argument, as if 2^64 isn’t practically asymptotic.
Re:Libc++'s Implementation of std::string
by coolplants on Thursday January 30, 2020 @10:29PM
(#22198763)
Even if the standard required O(1) it would conform because the loop is bounded (<= 23), it’s not O(n)