r/programming • u/Mashpoe • Feb 09 '20
An Attempt at Making a Better String for C++
https://youtu.be/a1Te_qBEp-g5
u/redweasel Feb 09 '20
Without having yet watched the video...
Too bad you folks have probably never seen the string format(s) used on VMS (later renamed to OpenVMS), "back in the day." The OS itself defined a generalized "descriptor" structure that, for an ordinary character string, supplied the length (in bytes), and starting address in memory, of a sequence of characters constituting the string. Software didn't have to spend time searching byte-by-byte for a terminating null, linefeed, etc. in order to find the end of the string -- just look at the descriptor.
A couple of features made this architecture even more useful and cool.
First, the string descriptor was just a subset of a more general descriptor. In addition to the length and starting-address fields, two others described, respectively, the "class" of the descriptor, with "character string" being just one of many, and the datatype of the individual elements, "character" or otherwise; the result was that a descriptor could also handle multidimensional arrays of all datatypes known to the OS.
Another cool feature of VMS in general was that there were lots and lots of programming languages available: Basic, Fortran, C, Pascal, PL/1, Ada, Assembly, of course, and a vendor-specific language called BLISS, to name just the ones I know of off the top of my head. I'm pretty sure there were others. And the implementation of all languages' datatypes, whether C structs, Fortran arrays, Pascal records, or what, were mapped back to standards in the OS itself. It was therefore trivially easy to write programs in which different pieces were written in different languages, and to not care what language the OS had been written in. There was even a "Language-Sensitive Editor" (LSE) that, with an appropriate template loaded, knew the syntax of each of these languages and let you write guaranteed-compilable code. If you were willing to try, you could even write simple programs in languages you didn't even know. LSE was probably my favorite VMS application ever.
10
u/josefx Feb 09 '20
Software didn't have to spend time searching byte-by-byte for a terminating null, linefeed, etc. in order to find the end of the string -- just look at the descriptor.
std::string also stores the size - the terminating nul is only there for C compatibility.
First, the string descriptor was just a subset of a more general descriptor.
So std::basic_string<char,...> just with run-time overhead?
1
u/redweasel Feb 15 '20
std::string et al didn't exist, even in C++, the ten years I programmed in C++ for a living. Or, if they did, they weren't standardized enough that you could assume they'd be available on any given platform. Since I never used it (at that time or when it became more widely available -- too damn much syntax, all those colons and angle brackets) I can't comment on how your example compares to string descriptors. I expect there's a certain amount of run-time overhead in std::string, too, once you get down to the machine code. Gotta execute some instructions somewhere, to do useful things! ;-)
1
u/josefx Feb 15 '20
I really expected std::string to have been around longer, however it was apparently only included in the c++98 standard to finally stop everyone else from creating their own buggy or incomplete string type.
From your description the descriptor holds at least some type information as data at run time, a
std::basic_string<char,...>on the other hand only has pointer, capacity and size fields, any code interacting with it has to be aware of its data type already at compile time. So its a trade of between storing more data and having more flexibility at run time. Of course if I understand the OpenVMS documentation correctly you could choose between three different string types so that overhead could be avoided at least some of the time.1
u/redweasel Feb 17 '20 edited Feb 17 '20
Heck, I didn't even know it was "included in the C++98 standard." Guess that's the first language update (?) I ignored, along with every one since.
Yes, the descriptor holds type information as data at run time; this allows general-purpose code to figure out what to do.
Ah, yes, I'd forgotten the support for "three different string types," which to me would be "by descriptor" (VMS did a lot of stuff "by descriptor"), counted ASCII (first byte of string is its length; obviously this is limited to 255 characters), and, I presume, null-terminated ASCII for ease of interaction with C. (It's possible the "special-character-terminated" type might have been general-purpose and configurable, so that you could tidily handle e.g. LF-terminated strings and such -- but I don't know that for a fact.)
And -- "understand the OpenVMS documentation..." --Where are you even finding OpenVMS documentation in the first place, these days? (Oh, and note that my VMS-for-a-living days were before* the Alpha chip came out and VAX/VMS was renamed "OpenVMS" so as to support multiple different types of processors. (And I never even heard of Itanium until maybe ten years later.) So anything you read in OpenVMS documentation is liable to be somewhere between "somewhat" and "vastly" newer than what I used on VAX/VMS! You may see features and functions, in the documentation, that I never did, and unknowingly fail to realize how primitive were our tools. ;-) Indeed, we only ever got up to VAX/VMS V5.5-2 on our VAX development machines, and only ever ran a very early (alpha or beta) release of OpenVMS Alpha -- "V1.5-1 HW" -- on our Alpha (a.k.a. AXP) development (or customer!) machines. (For the VAX-to-Alpha port, DEC supplied, and we used, a cool tool called "VEST" -- VAX Executable System Translator -- that treated VAX .EXE files -- executable programs -- as though they were source code, and compiled/assembled/translated them into the AXP instruction set to create native Alpha executables. Just a few months (years?) ago, I found a publicly-accessible AXP system online, but it was much newer than what I had used -- and VEST was gone. :-( Apparently, DEC blithely assumed everybody was finished with all VAX-to-AXP porting work. Which, much as it saddens me, is probably not an unreasonable assumption. On the other hand, my VAXstation at home runs OpenVMS V7.2, which has taken a little getting used to; DEC C is just enough different from VAX C -- even in compatibility mode, such as it is -- that some of my old code requires significant tweaking in order to compile and link successfully. Also, the "X Toolkit" layer of header files (and maybe libraries, for all I know) has been removed, and a lot of my old code from DEC's own DECwindows programming seminars of 1990 will no longer compile. Bummer.)
3
u/Gotebe Feb 10 '20
That's from a time when the OS was imposing la guages and data structure implementation to applications. Since then, the world went with the C interface for the user land and more freedom for apps and languages. But... More freedom then means more trouble integrating between the languages.
BTW... sounds like a lot of overhead for a string. C++ string knows its size, for example. Did VMS implementation do things like small string optimization or copy-on-write, perhaps? Because that's where speed can come from.
1
u/redweasel Feb 15 '20
Personally, I have always preferred language interoperability over a lot of other things. On my first computer you could call "machine code" from BASIC, and on the VAX you could call anything from anything. Did I mention my final project at college? COBOL, Pascal, a little Assembly, and I seem to recall there may have been some Pascal in there.
While I certainly acknowledge -- and have experienced, and taken full advantage of -- the usefulness of having a common interface across a multitude of platforms, I just wish it had been something a little better-planned than C / *NIX. There are a lot of what I consider just plain misfeatures, in both. Unfortunately, most people who use those are so used to them, and have likely never experienced anything else (let alone anything better), and have so shallow an understanding of how things actually work, that they just didn't know. Oh well -- what's done is done. At least I have the satisfaction of sitting back here, shaking my head.
That said -- I wouldn't say that VMS-style string descriptors are "a lot of overhead," especially considering the execution-time savings they offered, especially back when processors were so much slower than they are today. If you needed to append to a string of several thousand bytes, it was much faster to know, right up front, where the end of the first string was, than to have to start by stepping, byte-by-byte, through those several thousand bytes to find it. I'm glad to hear that C++ strings know their lengths in this way -- but when did that happen? I don't particularly remember C++ having a string datatype, per se, when I learned it -- but that was in 1998, so either my memory is shot (age will do that!) or strings are a post-1998 addition.
I'm not sure how to answer your question about "small string optimization or copy-on-write" -- in part because I don't know what those are, and also because one area of VMS I didn't get around to picking apart -- or, rather, haven't yet gotten around to; I still could! -- were the string-handling functions in the OS RTL.
The main place where we achieved speed optimizations was in just plain minimizing the number of instructions being executed. Using Assembly instead of C for manipulating large arrays of single bytes, for instance, saved literally millions -- maybe billions! -- of instruction cycles, as already noted. Arranging data in memory in the most efficient order, "unwrapping" nested loops in algorithms, etc. helped a little with that. We saved a lot by foregoing the "record-oriented" I/O that the OS and language RTLs added, by using the facilities both thoughtfully provided for bypassing the RTL per se and using system-level almost-raw I/O directly to/from the disk blocks comprising a file. The ultimate purpose of all this code was to feed data to a very hungry realtime output peripheral. It used a very wide data bus, and required the host computer to sustain a very high output data rate for up to forty minutes without slowdowns or pauses. So the program that handled that performed all these tricks and used the OS facilities for obtaining a contiguous swath of memory, locking it against virtual-memory-management paging and OS-scheduler swapping, to slice off every conceivable source of unnecessary instruction execution. As a result, our system was able to sustain that heavy-duty data stream to our product, in one process, while in several other processes simultaneously ingesting magnetic tapes to disk files, converting CMYK color data to RGB via millions of matrix multiplications, keeping the operator's console responsive to keyboard input, and allowing Field Service to dial in via modem and do all sorts of stuff in one or more interactive sessions. (ONLY VAX/VMS could do this, at the time. We had several vendors -- SGI, Sun, others -- come through, from time to time, and try to sell us their products in place of VMS -- but when we would describe our requirements -- which the VAX was meeting without even breaking a sweat! -- they'd turn pale, admit that they couldn't even come close, and slink away with their tails between their legs. Eventually our parent company decreed that "all the company's software products must run on Windows NT," and we were forced to make the distasteful transition. NT couldn't come close to handling the data flow, either, of course, so the product itself had to be re-engineered to contain an internal, buffering, hard drive, so that it could store up the first part of the stream as its "high-speed" data source, while NT poked along delivering the rest of the stream as best it could. It was an enormous, expensive, undertaking, not to mention an infuriating one for its complete technical inappropriateness and the brainless high-level-management decision that forced it.)
Anyway, I can't tell you whether VMS did small string optimization, copy-on-write, etc., because I don't know (or at least don't remember) what those are -- and in any case never dug into the innards of that particular part of the system. I might still be able to do so, though -- I still have a little VAX of my own, at home -- if you really want to know and can tell me more about "exactly what I'm looking for."
-17
u/shevy-ruby Feb 09 '20
30 years later, they still try to make C++ better!
So 30 years it was crap. What does this tell us about C++...
4
u/Mashpoe Feb 09 '20
Just because you try to make something better doesn't mean it was necessarily bad
5
u/Mashpoe Feb 09 '20
Disclaimer: This is not meant to be a replacement for std::string, it's just a proof of concept for some cool ideas and a few optimizations. There is still a lot missing from std::string.