←back to thread

42 points coneonthefloor | 2 comments | | HN request time: 0s | source
Show context
ranger_danger ◴[] No.44609646[source]
You might be interested in https://github.com/antirez/sds
replies(1): >>44610559 #
fsckboy ◴[] No.44610559[source]
neat, i like it, has some of the same ideas i've used in my string packages

but i did see a place to shave a byte in the sds data struct. The null terminator is a wasted field, that byte (or int) should be used to store the amount of free space left in the buffer (as a proxy for strlen). When there is no space left in the buffer, the free space value will be.... a very convenient 0 heheh

hey, OP said he wants to be a better C programmer!

replies(1): >>44610703 #
ranger_danger ◴[] No.44610703[source]
> The null terminator is a wasted field

I think that would break its "Compatible with normal C string functions" feature.

replies(1): >>44610765 #
fsckboy ◴[] No.44610765[source]
nooooo you don't understand. when the buffer is not full, the string will be zero terminated "in buffer" (which is how it works as is anyway). when the buffer is full, the "free count" at the end will do double duty, both as a zero count and a zero terminater
replies(1): >>44610876 #
ranger_danger ◴[] No.44610876[source]
But calling "normal C string functions" don't know about the "free count" byte, right? So it wouldn't be updated... unless I'm misunderstanding something.
replies(2): >>44611146 #>>44611838 #
1. dernett ◴[] No.44611838{3}[source]
I'm assuming he's talking about this specific small string optimization: https://www.youtube.com/watch?v=kPR8h4-qZdk&t=409s
replies(1): >>44612173 #
2. fsckboy ◴[] No.44612173[source]
just watched, yes, that is the same optimization