6 ms·
An even better solution: https://www.digitalmars.com/articles/C-biggest-mistake.html https://www.digitalmars.com/articles/C-biggest-mistake.html With the leng
by WalterBright 5y ago
An even better solution:
https://www.digitalmars.com/articles/C-biggest-mistake.html https://www.digitalmars.com/articles/C-biggest-mistake.html
With the lengths of strings known, copying becomes fast, safe, and trivial.
- Hackbraten 5y agoNot everyone is going to be happy about 8 bytes overhead for every buffer. Also, not every C-style API will migrate. If you consume one of those, you may still have to count the length, which costs cycles.
- WalterBright 5y agoThe proposal does not take away any C features.
- scottlamb 5y ago> Not everyone is going to be happy about 8 bytes overhead for every buffer. Then prefix it with four bytes (which, if you omit the trailing NUL, adds three bytes in total). If your C strings are longer than 2^32 bytes, you're probably doing something wrong. I also should point out that memory is typically cheap. Cache is more precious, and I'd expect on balance adding a length would pollute the cache less than scanning through the whole string unnecessarily. But I agree with your second point: > Also, not every C-style API will migrate. If you consume one of those, you may still have to count the length, which costs cycles. Similarly, the article started with this text: > Like them or not, null-terminated strings are essential to C, and working with them is necessary in all but the most trivial programs. You can do things nicely in your own code, but you still need NUL-terminated strings when dealing with existing code. And dealing with existing code is often the reason to pick C...
- iib 5y agoI would assume the people bothered by an increase in byte-sized overhead are not the ones that are using 2^32 length strings. About the cache, I think for small strings, scanning the whole string may still be faster. Though adding a four-byte length prefix would make certain functions like strlen almost go away.
- attractivechaos 5y agoFour bytes are still overhead. Also, you often need to keep the capacity of a string. Another four bytes. The C string APIs are like the lowest denominator of all use cases. When you know string lengths, you can avoid quadratic strcat() or other performance pitfalls with the existing APIs. On the other hand, if the APIs forced everyone to keep string lengths, performance/memory/convenience would be affected for certain use cases. Memory is not cheap when you deal with many short strings or when you do embedded programming.
- scottlamb 5y ago> Four bytes are still overhead. I hedged with "typically" but I'm a little skeptical about the environments in which this is a problem. Embedded stuff that's severely memory constrained probably doesn't deal with that many strings. But you could use a variable-sized length prefix as another commenter suggested to have no overhead on short strings. Its size would be decided at allocation time so you're not shifting the string. You could indicate it via high bits of the length or low bits of 4-byte-aligned string pointers. It's an extra branch or table lookup or the like on access though. > Also, you often need to keep the capacity of a string. Another four bytes Nonsense. There are exactly no situations where you need to keep a capacity with this scheme and not with traditional C strings.
- deleted 5y ago[deleted]
- attractivechaos 5y agoFor two standard formats in our field, you sometimes need to keep billions of short strings in memory. Many of these strings are 2 bytes or several bytes in length. The parser in C packs them as NULL terminated strings. The C++ and Java parsers use the std::string or the string type. They use a lot more memory. Even a 4-byte overhead would double the memory. Yes, you can have a smart way to keep short strings to waste less or even no memory (one of the two formats is using this trick), but it makes code harder to read and hurts performance due to the extra check/conversion. As to the capacity field, we sometimes have to keep it around. The question is: do you keep it inside the string struct/memory block (like std::string) or let users handle it? We have varying preferences in different cases. It is hard to design APIs optimal in all cases.
- mkcg 5y agoYou could also prefix it with a varint : in this case it would not require any extra byte compared to the NUL terminated string when the length of the string is less than 128 chars.
- jjav 5y ago> I also should point out that memory is typically cheap. Not in the early 70s, no. Even in the late 80s I was writing code very conscious of every single byte that could be saved anywhere.
- loup-vaillant 5y agoActually, it's 7 bytes (you can remove `\0` at the end), and that's if you insist on being able to exceed 4GB with your strings. Personally, depending on the use case, I'd be willing to have strings limited to 256 bytes (no overhead), 64K bytes (1 byte of overhead), and 4G bytes (3 bytes of overhead). Though combining them all might be quite the nightmare…
- codesnik 5y agoyou can also have something akin to MIDI encoding with higher bit reserved to mark that next byte is a part of length field, too. up to 128 bytes with no overhead, up to 16kb with one byte overhead, and so on. Such a detail would be easy to hide in the library. But I wouldn't be surprised that alignment and other troubles would eat any possible gain.
- WalterBright 5y agoWon't work with UTF-8.
- thrwaeasddsaf 5y agoWorks with any encoding and arbitrary binary data. The last byte of the length field doesn't have the high bit set, and whatever follows after that is completely irrelevant. But still needless complexity for this.
- moonchild 5y agoSure it is, the length is out of band. In fact, you could even use utf-8 to encode the length!
- habibur 5y ago> But I wouldn't be surprised that alignment ... troubles would eat any possible gain. It's string. Alignment shouldn't be a problem. Rather alignment issues are for the pointers to that string.
- dnr 5y ago
- rectang 5y agoSaving 8 bytes, and saving a few cycles by scanning rather than counting... is an argument for using NUL-terminated strings as an optimization in specialized circumstances. In the common case, the extra 8 bytes and the cycles saved (if any) don't matter — and are not worth the sacrifice of making it harder to program correctly.
- kevin_thibedeau 5y ago> the extra 8 bytes and the cycles saved (if any) don't matter You wouldn't last in embedded development with that sort of attitude. When you're nearly out of RAM you can't afford to waste overhead of fat strings.
- pkaye 5y agoNot every embedded system is memory constrained these days.
- rectang 5y agoIndeed. And besides, didn't I account for precisely that scenario? Memory-constrained embedded development is a specialized situation that could call for using NUL-terminated strings.
- pjmlp 5y agoI was using Turbo Basic, Turbo Pascal, Turbo C++ (with C++ collections), and Clipper on MS-DOS, with 640 KB of memory. It was just fine, then there are those that nowadays even toy with C++17 on C64. “Rich Code for Tiny Computers: A Simple Commodore 64 Game in C++17” https://www.youtube.com/watch?v=zBkNBP00wJE https://www.youtube.com/watch?v=zBkNBP00wJE Really, unless we are talking about PIC and AVRs with like 4KB, we are optimizing for the wrong target.
- mh7 5y agoYou can't afford to waste cycles on O(n) strlen either.
- mhh__ 5y agoIf someone implemented that, you might even call it betterC...
- dheera 5y agoIt always boggles me why people went with null-terminated strings in C instead of just putting the length of the string in the first 4 bytes and then the string after that. And just generally doing that for all arrays. That way if you're passed ONLY a pointer to the array, you can always tell how long it is without requiring the caller to tell you how long it is, which seems to be a common theme among C functions. And that also allows your string to contain null characters, which is useful in many circumstances.
- saagarjha 5y agoBecause this is how strings in C have always been done and this is how the language and pretty much all libraries treat strings.
- WalterBright 5y agoEmbedding the length means you cannot slice strings. To get a substring one has to reallocate. Of course, null termination also means that slicing is restricted to being a tail.