3 ms·
Dumb(?) question from a non-kernel programmer: can strings in the kernel be limited to those with lengths <2^16 (65536)? Then the length can be stored in the un
by avidphantasm 4y ago
Dumb(?) question from a non-kernel programmer: can strings in the kernel be limited to those with lengths <2^16 (65536)? Then the length can be stored in the unused bits of the pointer. This probably creates more problems than it solves, especially if we ever have CPUs that want to address more than 256 GiB of memory. This would also only work on 64-bit architectures.
- avidphantasm 4y agoOr if say, most strings tend to be small (<256 chars), use 8 unused bits of the pointer for the length. If the string is longer than that, mask the bits to 0. Then the string handling functions can have a fast path that uses the length from pointer if available, if not, walks the entire string.
- jabl 4y agoI think most(?) C++ implementations of std::string use something called a short-string optimization, where the string type is something like struct string { size_t len; union { char* ptr_to_malloced_string; char short_string[sizeof(char*)]; } } So if the length of the string is shorter than the size of a pointer, the string is stored inline in the struct, and if the length is longer it's a separate allocated object.
- notamy 4y ago> especially if we ever have CPUs that want to address more than 256 GiB of memory. Given how common of a scenario this is for Linux, I imagine this sort of a change would be more trouble than it's worth...
- mort96 4y agoFYI, we have consumer systems with over a terabyte of RAM.