source: doc/theses/mike_brooks_MMath/string.tex@ c824afd

stuck-waitfor-destruct
Last change on this file since c824afd was 5d300ba, checked in by Peter A. Buhr <pabuhr@…>, 5 months ago

proofread string chapter

  • Property mode set to 100644
File size: 95.5 KB
RevLine 
[bbf6a180]1\chapter{String}
2
[195f43d]3\vspace*{-20pt}
[cef5bfc]4This chapter presents my work on designing and building a modern string type in \CFA.
[d9aee90]5The discussion starts with an overview of the string API, then a number of interesting string problems, followed by how these issues are resolved in this work.
[16915b1]6
7
[c01a2fd]8\section{String Operations}
[174a11a]9
[931f1b4]10% https://en.wikipedia.org/wiki/Comparison_of_programming_languages_(string_functions)
11
[56ec508]12\VRef[Figure]{f:StrApiCompare} shows a general comparison of string APIs for C, \CC, Java and \CFA.
13It provides a classic ``cheat sheet'', summarizing the names of the most-common closely-equivalent operations.
[195f43d]14The over-arching commonality is that operations work on groups of characters for assigning, copying, scanning, and updating.
[5e8d75bb]15
[195f43d]16\begin{figure}[h]
[174a11a]17\begin{cquote}
[5e8d75bb]18\begin{tabular}{@{}l|l|l|l@{}}
[5d300ba]19C @char [ ]@ & \CC @string@ & Java @String@ & \CFA @string@ \\
[174a11a]20\hline
[411aa65]21@strcpy@, @strncpy@ & @=@ & @=@ & @=@ \\
22@strcat@, @strncat@ & @+@, @+=@ & @+@, @+=@ & @+@, @+=@ \\
[5e8d75bb]23@strcmp@, @strncmp@ & @==@, @!=@, @<@, @<=@, @>@, @>=@
[411aa65]24 & @equals@, @compareTo@
25 & @==@, @!=@, @<@, @<=@, @>@, @>=@ \\
26@strlen@ & @length@, @size@ & @length@ & @size@ \\
27@[ ]@ & @[ ]@ & @charAt@ & @[ ]@ \\
28@strncpy@ & @substr@ & @substring@ & @( )@, on RHS of @=@ \\
29@strncpy@ & @replace@ & @replace@ & @( )@, on LHS of @=@ \\
30@strstr@ & @find@ & @indexOf@ & @find@ \\
[31be464]31@strcspn@ & @find_first_of@ & @matches@ & @exclude@ \\
32@strspn@ & @find_first_not_of@ & @matches@ & @include@ \\
[411aa65]33N/A & @c_str@, @data@ & N/A & @strcpy@, @strncpy@ \\
[174a11a]34\end{tabular}
35\end{cquote}
[56ec508]36\caption{Language comparison of string API}
[5e8d75bb]37\label{f:StrApiCompare}
38\end{figure}
39
[b0296dba]40As mentioned in \VRef{s:String}, a C string uses null termination rather than a length, which leads to explicit storage management;
41hence, most of its group operations are error prone and expensive due to copying.
[195f43d]42Most high-level string libraries use a separate length field and specialized storage management to implement group operations.
[06ffa95]43Interestingly, \CC strings retain null termination in case it is needed to interface with C library functions.
[c01a2fd]44\begin{cfa}
[195f43d]45int open( @const char * pathname@, int flags );
[b766c9b7]46string fname{ "test.cc" );
[56ec508]47open( fname.@c_str()@, O_RDONLY ); // null terminated value of string
[c01a2fd]48\end{cfa}
[195f43d]49Here, the \CC @c_str@ function does not create a new null-terminated C string from the \CC string, as that requires passing ownership of the C string to the caller for eventual deletion.\footnote{
[b766c9b7]50C functions like \lstinline{strdup} do return allocated storage that must be freed by the caller.}
[195f43d]51% Instead, each \CC string is null terminated just in case it might be needed for this purpose.
[b766c9b7]52Providing this backwards compatibility with C has a ubiquitous performance and storage cost.
[c01a2fd]53
[fc8ec54]54
[411aa65]55\section{\CFA \lstinline{string} Type}
[fc8ec54]56\label{s:stringType}
57
58The \CFA string type is for manipulation of dynamically-sized character-strings versus C @char *@ type for manipulation of statically-sized null-terminated character-strings.
[780727f]59Therefore, the amount of storage for a \CFA string changes dynamically at runtime to fit the string size, whereas the amount of storage for a C string is fixed at compile time.
60As a result, a @string@ declaration does not specify a maximum length, where a C string array does.
61For \CFA, as a @string@ dynamically grows and shrinks in size, so does its underlying storage.
[5d300ba]62For C, as a string dynamically grows and shrinks in size, its underlying storage does not.
[fc8ec54]63The maximum storage for a \CFA @string@ value is @size_t@ characters, which is $2^{32}$ or $2^{64}$ respectively.
64A \CFA string manages its length separately from the string, so there is no null (@'\0'@) terminating value at the end of a string value.
[602ac05]65Hence, a \CFA string cannot be passed to a C string manipulation function, such as @strcat@.
[b0296dba]66Like C strings, characters in a @string@ are numbered from the left starting at 0 (because subscripting is zero-origin), and in \CFA numbered from the right starting at -1.
[f8913b7c]67\begin{cquote}
[b0296dba]68\rm
[931f1b4]69\begin{tabular}{@{}rrrrll@{}}
70\small\tt "a & \small\tt b & \small\tt c & \small\tt d & \small\tt e" \\
[f8913b7c]710 & 1 & 2 & 3 & 4 & left to right index \\
72-5 & -4 & -3 & -2 & -1 & right to left index
73\end{tabular}
74\end{cquote}
[b0296dba]75The following operations manipulate an instance of type @string@, where the discussion assumes the following declarations.
[fc8ec54]76\begin{cfa}
77#include @<string.hfa>@
[931f1b4]78@string@ s = "abcde", name = "MIKE", digit = "0123456789";
[b0296dba]79const char cs[$\,$] = "abc";
[fc8ec54]80int i;
81\end{cfa}
[931f1b4]82Note, the include file @<string.hfa>@ to access type @string@.
[fc8ec54]83
84
85\subsection{Implicit String Conversions}
86
87The ability to convert from internal (machine) to external (human) format is useful in situations other than I/O.
[d9aee90]88Hence, the basic types @char@, @char *@, @int@, @double@, @_Complex@, including any signness and size variations, implicitly convert to type @string@ (as in Java).
[fc8ec54]89\begin{cquote}
[5d300ba]90\setlength{\tabcolsep}{10pt}
91\begin{tabular}{@{}llll@{}}
[fc8ec54]92\begin{cfa}
[780727f]93string s = 5;
[b0296dba]94s = 'x';
95s = "abc";
[780727f]96s = 42hh; /* signed char */
97s = 42h; /* short int */
98s = 0xff;
[fc8ec54]99\end{cfa}
100&
101\begin{cfa}
[780727f]102"5"
[fc8ec54]103"x"
104"abc"
[780727f]105"42"
106"42"
107"255"
[56ec508]108\end{cfa}
109&
110\begin{cfa}
[780727f]111s = (ssize_t)MIN;
112s = (size_t)MAX;
113s = 5.5;
114s = 5.5L;
115s = 5.5+3.4i;
116s = 5.5L+3.4Li;
[56ec508]117\end{cfa}
118&
119\begin{cfa}
[fc8ec54]120"-9223372036854775808"
121"18446744073709551615"
122"5.5"
123"5.5"
124"5.5+3.4i"
125"5.5+3.4i"
126\end{cfa}
127\end{tabular}
[56ec508]128\end{cquote}
129Conversions can be explicitly specified using a compound literal.
130\begin{cfa}
[5d300ba]131s = (string){ 5 }; s = (string){ "abc" }; s = (string){ 5.5 };
[56ec508]132\end{cfa}
[602ac05]133
[780727f]134Conversions from @string@ to @char *@ attempt to be safe.
[5d300ba]135The overloaded @strncpy@ function is safe, if the length of the C string is correct.
[780727f]136The assignment operator and constructor both allocate the buffer and return its address, meaning the programmer must free it.
137Note, a C string is always null terminated, implying storage is always necessary for the null.
[56ec508]138\begin{cquote}
[5d300ba]139\begin{tabular}{@{}ll@{}}
[56ec508]140\begin{cfa}
[780727f]141string s = "abcde";
142char cs[4];
[56ec508]143strncpy( cs, s, sizeof(cs) );
[5d300ba]144char * cp = s; // ownership
[56ec508]145delete( cp );
[780727f]146cp = s + ' ' + s; // ownership
[56ec508]147delete( cp );
148\end{cfa}
149&
150\begin{cfa}
[780727f]151
152
[56ec508]153"abc\0", in place
154"abcde\0", malloc
[780727f]155
[56ec508]156"abcde abcde\0", malloc
[780727f]157
[56ec508]158\end{cfa}
159\end{tabular}
160\end{cquote}
[fc8ec54]161
162
163\subsection{Length}
164
[5d300ba]165The @len@ operation (short for @strlen@) returns the length of a C (not including the terminating null) or \CFA string.
166For compatibility, an overloaded @strlen@ works with \CFA strings.
[fc8ec54]167\begin{cquote}
[5d300ba]168\begin{tabular}{@{}ll@{}}
[fc8ec54]169\begin{cfa}
[56ec508]170i = len( "" );
171i = len( "abc" );
172i = len( cs );
173i = strlen( cs );
174i = len( name );
175i = strlen( name );
[fc8ec54]176\end{cfa}
177&
178\begin{cfa}
1790
1803
1813
[56ec508]1823
1834
[fc8ec54]1844
185\end{cfa}
186\end{tabular}
187\end{cquote}
188
189
190\subsection{Comparison Operators}
191
[780727f]192The binary relational, @<@, @<=@, @>@, @>=@, and equality, @==@, @!=@, operators compare \CFA strings using lexicographical ordering, where longer strings are greater than shorter strings.
[602ac05]193In C, these operators compare the C string pointer not its value, which does not match programmer expectation.
[b0296dba]194C strings use function @strcmp@ to lexicographically compare the string value.
[5d300ba]195Java has the same issue with @==@ (reference) and @.equals@ (value) comparison.
[fc8ec54]196
197
198\subsection{Concatenation}
199
[b0296dba]200The binary operators @+@ and @+=@ concatenate C @char@, @char *@ and \CFA strings, creating the sum of the characters.
[780727f]201\begin{cquote}
[5d300ba]202\setlength{\tabcolsep}{5pt}
203\begin{tabular}{@{}ll|ll|ll@{}}
[fc8ec54]204\begin{cfa}
[56ec508]205s = "";
206s = 'a' + 'b';
207s = 'a' + "b";
208s = "a" + 'b';
209s = "a" + "b";
210\end{cfa}
211&
212\begin{cfa}
213
214"ab"
215"ab"
216"ab"
217"ab"
218\end{cfa}
219&
220\begin{cfa}
221s = "";
[fc8ec54]222s = 'a' + 'b' + s;
[56ec508]223s = 'a' + 'b' + s;
224s = 'a' + "b" + s;
225s = "a" + 'b' + s;
[fc8ec54]226\end{cfa}
227&
228\begin{cfa}
229
[56ec508]230"ab"
231"abab"
232"ababab"
233"abababab"
234\end{cfa}
235&
236\begin{cfa}
237s = "";
238s = s + 'a' + 'b';
239s = s + 'a' + "b";
240s = s + "a" + 'b';
241s = s + "a" + "b";
242\end{cfa}
243&
244\begin{cfa}
[fc8ec54]245
[56ec508]246"ab"
247"abab"
248"ababab"
249"abababab"
[fc8ec54]250\end{cfa}
251\end{tabular}
[780727f]252\end{cquote}
[b0296dba]253However, including @<string.hfa>@ can result in ambiguous uses of the overloaded @+@ operator.\footnote{Combining multiple packages in any programming language can result in name clashes or ambiguities.}
[780727f]254For example, subtracting characters or pointers has valid use-cases:
[56ec508]255\begin{cfa}
[780727f]256ch - '0' $\C[2in]{// find character offset}$
257cs - cs2; $\C{// find pointer offset}\CRT$
[56ec508]258\end{cfa}
[b0296dba]259addition is less obvious
[56ec508]260\begin{cfa}
[780727f]261ch + 'b' $\C[2in]{// add character values}$
262cs + 'a'; $\C{// move pointer cs['a']}\CRT$
[b0296dba]263\end{cfa}
[d9aee90]264There are legitimate use cases for arithmetic with @signed@/@unsigned@ characters (bytes), and these types are treated differently from @char@ in \CC and \CFA.
265However, backwards compatibility makes it impossible to restrict or remove addition on type @char@.
[b0296dba]266Similarly, it is impossible to restrict or remove addition on type @char *@ because (unfortunately) it is subscripting: @cs + 'a'@ implies @cs['a']@ or @'a'[cs]@.
267
[5d300ba]268The prior \CFA concatenation examples show complex mixed-mode interactions among @char@, @char *@, and @string@ constants work correctly (@string@ variables are the same).
[b0296dba]269The reason is that the \CFA type-system handles this kind of overloading well using the left-hand assignment-type and complex conversion costs.
270Hence, the type system correctly handles all uses of addition (explicit or implicit) for @char *@.
271\begin{cfa}
272printf( "%s %s %s %c %c\n", "abc", cs, cs + 3, cs['a'], 'a'[cs] );
[56ec508]273\end{cfa}
[b0296dba]274Only @char@ addition can result in ambiguities, and only when there is no left-hand information.
[56ec508]275\begin{cfa}
[780727f]276ch = ch + 'b'; $\C[2in]{// LHS disambiguate, add character values}$
277s = 'a' + 'b'; $\C{// LHS disambiguate, concatenate characters}$
[411aa65]278printf( "%c\n", @'a' + 'b'@ ); $\C{// no LHS information, ambiguous}$
279printf( "%c\n", @(return char)@('a' + 'b') ); $\C{// disambiguate with ascription cast}\CRT$
[56ec508]280\end{cfa}
[b0296dba]281The ascription cast, @(return T)@, disambiguates by stating a (LHS) type to use during expression resolution (not a conversion).
282Fortunately, character addition without LHS information is rare in C/\CFA programs, so repurposing the operator @+@ for @string@ types is not a problem.
[780727f]283Note, other programming languages that repurpose @+@ for concatenation, can have similar ambiguity issues.
[56ec508]284
[b0296dba]285Interestingly, \CC cannot support this generality because it does not use the left-hand side of assignment in expression resolution.
[56ec508]286While it can special case some combinations:
287\begin{c++}
288s = 'a' + s; $\C[2in]{// compiles in C++}$
289s = "a" + s;
290\end{c++}
291it cannot generalize to any number of steps:
292\begin{c++}
293s = 'a' + 'b' + s; $\C{// does not compile in C++}\CRT$
294s = "a" + "b" + s;
295\end{c++}
[fc8ec54]296
297
298\subsection{Repetition}
299
300The binary operators @*@ and @*=@ repeat a string $N$ times.
[56ec508]301If $N = 0$, a zero length string, @""@, is returned.
[fc8ec54]302\begin{cquote}
[5d300ba]303\begin{tabular}{@{}ll@{}}
[fc8ec54]304\begin{cfa}
[b0296dba]305s = 'x' * 0;
[fc8ec54]306s = 'x' * 3;
307s = "abc" * 3;
[780727f]308s = ("MIKE" + ' ') * 3;
[fc8ec54]309\end{cfa}
310&
311\begin{cfa}
[780727f]312""
[56ec508]313"xxx"
314"abcabcabc"
315"MIKE MIKE MIKE "
[fc8ec54]316\end{cfa}
317\end{tabular}
318\end{cquote}
[b0296dba]319Like concatenation, there is a potential ambiguity with multiplication of characters;
[780727f]320multiplication of pointers does not exist in C.
[b0296dba]321\begin{cfa}
[5d300ba]322ch = ch * 3; $\C[2in]{// LHS disambiguate, multiply character value}$
[780727f]323s = 'a' * 3; $\C{// LHS disambiguate, concatenate characters}$
[411aa65]324printf( "%c\n", @'a' * 3@ ); $\C{// no LHS information, ambiguous}$
325printf( "%c\n", @(return char)@('a' * 3) ); $\C{// disambiguate with ascription cast}\CRT$
[b0296dba]326\end{cfa}
327Fortunately, character multiplication without LHS information is even rarer than addition, so repurposing the operator @*@ for @string@ types is not a problem.
[fc8ec54]328
329
330\subsection{Substring}
[780727f]331
332The substring operation returns a subset of a string starting at a position in the string and traversing a length, or matching a pattern string.
[fc8ec54]333\begin{cquote}
[5d300ba]334\setlength{\tabcolsep}{8pt}
335\begin{tabular}{@{}ll|ll@{}}
[780727f]336\multicolumn{2}{@{}c}{\textbf{length}} & \multicolumn{2}{c@{}}{\textbf{pattern}} \\
337\multicolumn{4}{@{}l}{\lstinline{string name = "PETER"}} \\
338\begin{cfa}
339s = name( 0, 4 );
340s = name( 1, 4 );
341s = name( 2, 4 );
342s = name( 4, -2 );
343s = name( 8, 2 );
344s = name( 0, -2 );
345s = name( -1, -2 );
[fc8ec54]346s = name( -3 );
347\end{cfa}
348&
349\begin{cfa}
[780727f]350"PETE"
351"ETER"
352"TER" // clip length to 3
353"ER"
[5d300ba]354"" // clip, beyond right
355"" // clip, beyond left
[780727f]356"ER"
357"TER" // to end of string
[56ec508]358\end{cfa}
359&
360\begin{cfa}
[780727f]361s = name( "ET" );
[56ec508]362s = name( "WW" );
363
364
365
366
[780727f]367
368
[56ec508]369\end{cfa}
370&
371\begin{cfa}
[780727f]372"ET"
373"" // does not occur
374
375
[56ec508]376
377
378
379
[fc8ec54]380\end{cfa}
381\end{tabular}
382\end{cquote}
[780727f]383For the length form, a negative starting position is a specification from the right end of the string.
[fc8ec54]384A negative length means that characters are selected in the opposite (right to left) direction from the starting position.
385If the substring request extends beyond the beginning or end of the string, it is clipped (shortened) to the bounds of the string.
[f8913b7c]386If the substring request is completely outside of the original string, a null string is returned.
[780727f]387For the pattern-form, it returns the pattern string if the pattern matches or a null string if the pattern does not match.
[602ac05]388The usefulness of this mechanism is discussed next.
[fc8ec54]389
[b0296dba]390The substring operation can appear on the left side of assignment, where it defines a replacement substring.
391The length of the right string may be shorter, the same, or longer than the length of left string.
[f8913b7c]392Hence, the left string may decrease, stay the same, or increase in length.
393\begin{cquote}
[5d300ba]394\begin{tabular}{@{}ll@{}}
[56ec508]395\begin{cfa}[escapechar={}]
[f8913b7c]396digit( 3, 3 ) = "";
397digit( 4, 3 ) = "xyz";
398digit( 7, 0 ) = "***";
[56ec508]399digit(-4, 3 ) = "$$$";
400digit( 5 ) = "LLL";
[fc8ec54]401\end{cfa}
[f8913b7c]402&
[56ec508]403\begin{cfa}[escapechar={}]
404"0126789"
405"0126xyz"
406"0126xyz"
407"012$$$z"
408"012$$LLL"
[f8913b7c]409\end{cfa}
410\end{tabular}
411\end{cquote}
[5d300ba]412Here, substring pattern matching is useful on the left-hand side of assignment.
[56ec508]413\begin{cquote}
[5d300ba]414\begin{tabular}{@{}ll@{}}
[56ec508]415\begin{cfa}[escapechar={}]
416digit( "$$" ) = "345";
417digit( "LLL") = "6789";
[fc8ec54]418\end{cfa}
[56ec508]419&
[fc8ec54]420\begin{cfa}
[56ec508]421"012345LLL"
422"0123456789"
[fc8ec54]423\end{cfa}
[56ec508]424\end{tabular}
425\end{cquote}
[5d300ba]426Supporting a regular-expression pattern is a possible extension.
[fc8ec54]427
[780727f]428The replace operation extends substring to substitute all occurrences.
[f8913b7c]429\begin{cquote}
[5d300ba]430\begin{tabular}{@{}ll@{}}
[fc8ec54]431\begin{cfa}
[931f1b4]432s = replace( "PETER", "E", "XX" );
433s = replace( "PETER", "ET", "XX" );
434s = replace( "PETER", "W", "XX" );
[fc8ec54]435\end{cfa}
[f8913b7c]436&
437\begin{cfa}
[931f1b4]438"PXXTXXR"
439"PXXER"
440"PETER"
[f8913b7c]441\end{cfa}
442\end{tabular}
443\end{cquote}
[931f1b4]444The replacement is done left-to-right and substituted text is not examined for replacement.
445
446
447\subsection{Searching}
[fc8ec54]448
[780727f]449The @find@ operation returns the position of the first occurrence of a key in a string.
[b0296dba]450If the key does not appear in the string, the length of the string is returned.
[931f1b4]451\begin{cquote}
[5d300ba]452\begin{tabular}{@{}ll@{}}
[fc8ec54]453\begin{cfa}
[931f1b4]454i = find( digit, '3' );
[602ac05]455i = find( digit, "45" );
[b0296dba]456i = find( digit, "abc" );
[fc8ec54]457\end{cfa}
[931f1b4]458&
[fc8ec54]459\begin{cfa}
[931f1b4]4603
4614
[b0296dba]46210
[fc8ec54]463\end{cfa}
[931f1b4]464\end{tabular}
465\end{cquote}
[b0296dba]466
[d9aee90]467A character-class operation indicates if a string is composed completely of a particular class of characters, \eg, alphabetic, numeric, vowels, \etc.
[931f1b4]468\begin{cquote}
[5d300ba]469\begin{tabular}{@{}ll@{}}
[fc8ec54]470\begin{cfa}
[931f1b4]471charclass vowels{ "aeiouy" };
472i = include( "aaeiuyoo", vowels );
473i = include( "aabiuyoo", vowels );
[fc8ec54]474\end{cfa}
[931f1b4]475&
476\begin{cfa}
[fc8ec54]477
[931f1b4]4788 // compliant
4792 // b non-compliant
480\end{cfa}
481\end{tabular}
482\end{cquote}
[b0296dba]483@vowels@ defines a character class and function @include@ checks if all characters in the string appear in the class (compliance).
484The position of the last character is returned if the string is compliant or the position of the first non-compliant character.
[931f1b4]485There is no relationship between the order of characters in the two strings.
486Function @exclude@ is the reverse of @include@, checking if all characters in the string are excluded from the class (compliance).
487\begin{cquote}
[5d300ba]488\begin{tabular}{@{}ll@{}}
[fc8ec54]489\begin{cfa}
[931f1b4]490i = exclude( "cdbfghmk", vowels );
491i = exclude( "cdyfghmk", vowels );
[fc8ec54]492\end{cfa}
[931f1b4]493&
[fc8ec54]494\begin{cfa}
[931f1b4]4958 // compliant
4962 // y non-compliant
[fc8ec54]497\end{cfa}
[931f1b4]498\end{tabular}
499\end{cquote}
500Both forms can return the longest substring of compliant characters.
501\begin{cquote}
[5d300ba]502\begin{tabular}{@{}ll@{}}
[fc8ec54]503\begin{cfa}
[931f1b4]504s = include( "aaeiuyoo", vowels );
505s = include( "aabiuyoo", vowels );
506s = exclude( "cdbfghmk", vowels );
507s = exclude( "cdyfghmk", vowels );
[fc8ec54]508\end{cfa}
[931f1b4]509&
[fc8ec54]510\begin{cfa}
[931f1b4]511"aaeiuyoo"
512"aa"
513"cdbfghmk"
514"cd"
[fc8ec54]515\end{cfa}
[931f1b4]516\end{tabular}
517\end{cquote}
[fc8ec54]518
[31be464]519There are also versions of @include@ and @exclude@, returning a position or string, taking a validation function, like one of the C character-class functions.\footnote{It is part of the hereditary of C that these function take and return an \lstinline{int} rather than a \lstinline{bool}, which affects the function type.}
[931f1b4]520\begin{cquote}
[5d300ba]521\begin{tabular}{@{}ll@{}}
[fc8ec54]522\begin{cfa}
[b0296dba]523i = include( "1FeC34aB", @isxdigit@ );
524i = include( ".,;'!\"", @ispunct@ );
525i = include( "XXXx", @isupper@ );
[fc8ec54]526\end{cfa}
[931f1b4]527&
[fc8ec54]528\begin{cfa}
[931f1b4]5298 // compliant
5306 // compliant
5313 // non-compliant
[fc8ec54]532\end{cfa}
[931f1b4]533\end{tabular}
534\end{cquote}
[d9aee90]535These operations perform an \emph{apply} of the validation function to each character, where the function returns a boolean indicating a stopping condition for the search.
[b0296dba]536The position of the last character is returned if the string is compliant or the position of the first non-compliant character.
[fc8ec54]537
[931f1b4]538The translate operation returns a string with each character transformed by one of the C character transformation functions.
539\begin{cquote}
[5d300ba]540\begin{tabular}{@{}ll@{}}
[fc8ec54]541\begin{cfa}
[931f1b4]542s = translate( "abc", @toupper@ );
543s = translate( "ABC", @tolower@ );
544int tospace( int c ) { return isspace( c ) ? ' ' : c; }
545s = translate( "X X\tX\nX", @tospace@ );
[fc8ec54]546\end{cfa}
[931f1b4]547&
[fc8ec54]548\begin{cfa}
[931f1b4]549"ABC"
550"abc"
551
552"X X X X"
[fc8ec54]553\end{cfa}
[931f1b4]554\end{tabular}
555\end{cquote}
[fc8ec54]556
[56ec508]557
[b0296dba]558\subsection{Returning N on Search Failure}
[fc8ec54]559
[b0296dba]560Some of the prior string operations are composite, \eg string operations returning the longest substring of compliant characters (@include@) are built using a search and then substring the appropriate text.
561However, string search can fail, which is reported as an alternate search outcome, possibly an exception.
562Many string libraries use a return code to indicate search failure, with a failure value of @0@ or @-1@ (PL/I~\cite{PLI} returns @0@).
[5d300ba]563This semantics leads to an awkward pattern, which can appear many times in a string library or user code.
[602ac05]564\begin{cfa}
[b0296dba]565i = exclude( s, alpha );
566if ( i != -1 ) return s( 0, i );
567else return "";
[602ac05]568\end{cfa}
[5d300ba]569The problem is that substring does the wrong thing or fails with the failure return-code in most string libraries, so it has to be special cased.
[b0296dba]570
[d9aee90]571\CFA adopts a return code but the failure value is taken from the index-of function in APL~\cite{apl}, which returns the length of the target string $N$ (or $N+1$ for 1 origin).
[b0296dba]572This semantics allows many search and substring functions to be written without conditions, \eg:
[602ac05]573\begin{cfa}
[b0296dba]574string include( const string & s, int (*f)( int ) ) { return @s( 0, include( s, f ) )@; }
575string exclude( const string & s, int (*f)( int ) ) { return @s( 0, exclude( s, f ) )@; }
[602ac05]576\end{cfa}
577In string systems with an $O(1)$ length operator, checking for failure is low cost.
[fc8ec54]578\begin{cfa}
[602ac05]579if ( include( line, alpha ) == len( line ) ) ... // not found, 0 origin
[fc8ec54]580\end{cfa}
[b0296dba]581\VRef[Figure]{f:ExtractingWordsText} compares \CC and \CFA string code for extracting words from a line of text, repeatedly removing non-word text and then a word until the line is empty.
582The \CFA code is simpler solely because of the choice for indicating search failure.
[d9aee90]583(A simplification of the \CC version is to concatenate a sentinel character at the end of the line so the call to @find_first_not_of@ does not fail.)
[b0296dba]584
585\begin{figure}
586\begin{cquote}
[5d300ba]587\begin{tabular}{@{}ll@{}}
[b0296dba]588\multicolumn{1}{c}{\textbf{\CC}} & \multicolumn{1}{c}{\textbf{\CFA}} \\
589\begin{cfa}
590for ( ;; ) {
591 string::size_type posn = line.find_first_of( alpha );
592 if ( posn == string::npos ) break;
593 line = line.substr( posn );
594 posn = line.find_first_not_of( alpha );
595 if ( posn != string::npos ) {
596 cout << line.substr( 0, posn ) << endl;
597 line = line.substr( posn );
598 } else {
599 cout << line << endl;
600 line = "";
601 }
602}
603\end{cfa}
604&
605\begin{cfa}
[411aa65]606for () {
[b0296dba]607 size_t posn = exclude( line, alpha );
608 if ( posn == len( line ) ) break;
609 line = line( posn );
610 posn = include( line, alpha );
611
612 sout | line( 0, posn );
613 line = line( posn );
614
615
616
617
618}
619\end{cfa}
620\end{tabular}
621\end{cquote}
622\caption{Extracting Words from Line of Text}
623\label{f:ExtractingWordsText}
624\end{figure}
[fc8ec54]625
626
627\subsection{C Compatibility}
628
[602ac05]629To ease conversion from C to \CFA, \CFA provides companion C @string@ functions.
630Hence, it is possible to convert a block of C string operations to \CFA strings just by changing the type @char *@ to @string@.
[d9aee90]631\begin{cquote}
632\begin{tabular}{@{}ll@{}}
[5d300ba]633\multicolumn{2}{@{}l@{}}{\lstinline{char s[32]; // changing s's type to string works}} \\
[602ac05]634\begin{cfa}
[b0296dba]635strlen( s );
636strnlen( s, 3 );
637strcmp( s, "abc" );
638strncmp( s, "abc", 3 );
[d9aee90]639\end{cfa}
640&
641\begin{cfa}
[602ac05]642strcpy( s, "abc" );
643strncpy( s, "abcdef", 3 );
644strcat( s, "xyz" );
645strncat( s, "uvwxyz", 3 );
[fc8ec54]646\end{cfa}
[d9aee90]647\end{tabular}
648\end{cquote}
[fc8ec54]649However, the conversion fails with I/O because @printf@ cannot print a @string@ using format code @%s@ because \CFA strings are not null terminated.
[602ac05]650Nevertheless, this capability does provide a useful starting point for conversion to safer \CFA strings.
[fc8ec54]651
652
[602ac05]653\subsection{I/O Operators}
654
[5d300ba]655The ability to input and output a string is as essential as for any other type.
656The goal for character I/O is to work with groups rather than individual characters.
[602ac05]657A comparison with \CC string I/O is presented as a counterpoint to \CFA string I/O.
658
659The \CC output @<<@ and input @>>@ operators are defined on type @string@.
660\CC output for @char@, @char *@, and @string@ are similar.
661The \CC manipulators are @setw@, and its associated width controls @left@, @right@ and @setfill@.
662\begin{cquote}
[5d300ba]663\begin{tabular}{@{}ll@{}}
[602ac05]664\begin{c++}
665string s = "abc";
666cout << setw(10) << left << setfill( 'x' ) << s << endl;
667\end{c++}
668&
669\begin{c++}
670
671"abcxxxxxxx"
672\end{c++}
673\end{tabular}
674\end{cquote}
675
676The \CFA input/output operator @|@ is defined on type @string@.
[b0296dba]677\CFA output for @char@, @char *@, and @string@ are similar.
[602ac05]678The \CFA manipulators are @bin@, @oct@, @hex@, @wd@, and its associated width control and @left@.
679\begin{cquote}
[5d300ba]680\begin{tabular}{@{}ll@{}}
[602ac05]681\begin{cfa}
682string s = "abc";
683sout | bin( s ) | nl
684 | oct( s ) | nl
685 | hex( s ) | nl
686 | wd( 10, s ) | nl
687 | wd( 10, 2, s ) | nl
688 | left( wd( 10, s ) );
[56ec508]689\end{cfa}
[602ac05]690&
691\begin{cfa}
692
693"0b1100001 0b1100010 0b1100011"
694"0141 0142 0143"
695"0x61 0x62 0x63"
696" abc"
697" ab"
698"abc "
699\end{cfa}
700\end{tabular}
701\end{cquote}
[8136df1]702\CC @setfill@ is not considered an important string manipulator.
[602ac05]703
[b0296dba]704\CC input matching for @char@, @char *@, and @string@ are similar, where \emph{all} input characters are read from the current point in the input stream to the end of the type size, format width, whitespace, end of line (@'\n'@), or end of file.
[602ac05]705The \CC manipulator is @setw@ to restrict the size.
706Reading into a @char@ is safe as the size is 1, @char *@ is unsafe without using @setw@ to constraint the length (which includes @'\0'@), @string@ is safe as its grows dynamically as characters are read.
707\begin{cquote}
[5d300ba]708\begin{tabular}{@{}ll@{}}
[602ac05]709\begin{c++}
710char ch, c[10];
711string s;
712cin >> ch >> setw( 5 ) >> c >> s;
[b0296dba]713@abcde fg@
[602ac05]714\end{c++}
715&
716\begin{c++}
717
[56ec508]718
[602ac05]719'a' "bcde" "fg"
[56ec508]720
[602ac05]721\end{c++}
722\end{tabular}
723\end{cquote}
[411aa65]724Input text can be \emph{gulped}, including whitespace, from the current point to an arbitrary delimiter character using @getline@.
[5d300ba]725\begin{cquote}
726\setlength{\tabcolsep}{10pt}
727\begin{tabular}{@{}ll@{}}
728\multicolumn{1}{c}{\textbf{\CC}} & \multicolumn{1}{c}{\textbf{\CFA}} \\
729\begin{cfa}
730string s1, s2, s3;
731getline( cin, s1, 'a' );
732getline( cin, s2, 'w' );
733getline( cin, s3 );
734cout << s1 << ' ' << s2 << ' ' << s3 << endl;
735@bbbad ddwxyz@
736\end{cfa}
737&
738\begin{cfa}
739
740sin | getline( s1, 'a' )
741 | getline( s2, 'w' )
742 | getline( s3 );
743sout | s1 | s2 | s3; "bbb" "d dd" "xyz"
744
745\end{cfa}
746\end{tabular}
747
748\end{cquote}
[602ac05]749
[b0296dba]750The \CFA philosophy for input is that, for every constant type in C, these constants should be usable as input.
[602ac05]751For example, the complex constant @3.5+4.1i@ can appear as input to a complex variable.
752\CFA input matching for @char@, @char *@, and @string@ are similar.
753C-strings may only be read with a width field, which should match the string size.
754Certain input manipulators support a scanset, which is a simple regular expression from @printf@.
[b0296dba]755The \CFA manipulators for these types are @wdi@,\footnote{Due to an overloading issue in the type-resolver, the input width name must be temporarily different from the output, \lstinline{wdi} versus \lstinline{wd}.} and its associated width control and @left@, @quote@, @incl@, @excl@, and @getline@.
[602ac05]756\begin{cquote}
757\setlength{\tabcolsep}{10pt}
[5d300ba]758\begin{tabular}{@{}ll@{}}
[602ac05]759\begin{c++}
760char ch, c[10];
761string s;
762sin | ch | wdi( 5, c ) | s;
[b0296dba]763@abcde fg@
[5d300ba]764sin | @quote@( ch ) | @quote@( wdi( sizeof(c), c ) ) | @quote@( s, '[', ']' ) | nl;
765@'a' "bcde" [fg]@
[602ac05]766sin | incl( "a-zA-Z0-9 ?!&\n", s ) | nl;
[b0296dba]767@x?&000xyz TOM !.@
[602ac05]768sin | excl( "a-zA-Z0-9 ?!&\n", s );
[b0296dba]769@<>{}{}STOP@
[602ac05]770\end{c++}
771&
772\begin{c++}
773
774
[b0296dba]775'a' "bcde" "fg"
[602ac05]776
[b0296dba]777'a' "bcde" "fg"
[602ac05]778
779"x?&000xyz TOM !"
780
781"<>{}{}"
782
783\end{c++}
784\end{tabular}
785\end{cquote}
[411aa65]786Note, the ability to read in quoted strings with whitespace to match with program string constants.
[b0296dba]787The @nl@ at the end of an input ignores the rest of the line.
[fc8ec54]788
789
[602ac05]790\subsection{Assignment}
[fc8ec54]791
[195f43d]792While \VRef[Figure]{f:StrApiCompare} emphasizes cross-language similarities, it elides many specific operational differences.
[b0296dba]793For example, the \CC @replace@ function selects a substring in the target and substitutes it with the source string, which can be smaller or larger than the substring.
794\CC modifies the mutable receiver object, replacing by position (zero origin) and length.
795\begin{c++}
[195f43d]796string s1 = "abcde";
[5d300ba]797s1.replace( 2, 3, "xy" ); "abxy"
[b0296dba]798\end{c++}
799Java cannot modify the receiver (immutable strings) so it returns a new string, replacing by text.
[06ffa95]800\label{p:JavaReplace}
[195f43d]801\begin{java}
802String s = "abcde";
[5d300ba]803String r = s.replace( "cde", "xy" ); "abxy"
[b0296dba]804\end{java}
805Java also provides a mutable @StringBuffer@, replacing by position (zero origin) and length.
[06ffa95]806\begin{java}
[195f43d]807StringBuffer sb = new StringBuffer( "abcde" );
[5d300ba]808sb.replace( 2, 5, "xy" ); "abxy"
[b0296dba]809\end{java}
810However, there are anomalies.
811@StringBuffer@'s @substring@ returns a @String@ copy that is immutable rather than modifying the receiver.
812As well, the operations are asymmetric, \eg @String@ has @replace@ by text but not replace by position and vice versa for @StringBuffer@.
813
[d9aee90]814More significant operational differences relate to storage management, often appearing through assignment (@target = source@), and are summarized in \VRef[Figure]{f:StrSemanticCompare}, which defines properties type abstraction, state, symmetry, and referent.
[195f43d]815The following discussion justifies the figure's yes/no entries per language.
[5e8d75bb]816
817\begin{figure}
[195f43d]818\setlength{\extrarowheight}{2pt}
[5d300ba]819\setlength{\tabcolsep}{5pt}
820\begin{tabularx}{\textwidth}{@{}p{0.8in}XXcccc@{}}
[195f43d]821 & & & \multicolumn{4}{@{}c@{}}{\underline{Supports Helpful?}} \\
[5e8d75bb]822 & Required & Helpful & C & \CC & Java & \CFA \\
823\hline
[5d300ba]824Type Abstraction
[195f43d]825 & Low-level: The string type is a varying amount of text communicated via a parameter or return.
826 & High-level: The string-typed relieves the user of managing memory for the text.
827 & no & yes & yes & yes \\
[5e8d75bb]828\hline
[195f43d]829State
[5e8d75bb]830 & \multirow{2}{2in}
[195f43d]831 {Fast Initialize: The target receives the characters of the source without copying the characters, resulting in an Alias or Snapshot.}
[f85de47]832 & Alias: The target variable names the source text; changes made in either variable are visible in both.
[195f43d]833 & yes & yes & no & yes \\
[5e8d75bb]834\cline{3-7}
835 &
[f85de47]836 & Snapshot: Subsequent operations on either the source or target variable do not affect the other.
[195f43d]837 & no & no & yes & yes \\
[5e8d75bb]838\hline
839Symmetry
[195f43d]840 & Laxed: The target's type is anything string-like; it may have a different status concerning ownership.
841 & Strict: The target's type is the same as the source; both strings are equivalent peers concerning ownership.
[411aa65]842 & N/A & no & yes & yes \\
[5e8d75bb]843\hline
844Referent
[195f43d]845 & Variable-Constrained: The target can accept the entire text of the source.
846 & Fragment: The target can accept an arbitrary substring of the source.
847 & no & no & yes & yes
848\end{tabularx}
[5e8d75bb]849
850\noindent
851Notes
[195f43d]852\begin{itemize}[parsep=0pt]
[5e8d75bb]853\item
854 All languages support Required in all criteria.
855\item
856 A language gets ``Supports Helpful'' in one criterion if it can do so without sacrificing the Required achievement on all other criteria.
857\item
[f85de47]858 The C ``string'' is @char *@, under the conventions of @<string.h>@. Because this type does not manage a text allocation, symmetry does not apply.
[5e8d75bb]859\item
[411aa65]860 The Java @String@ class is analyzed; its @StringBuffer@ class behaves similarly to \CC.
[5e8d75bb]861\end{itemize}
[195f43d]862\caption{Comparison of languages' strings, storage management perspective.}
[5e8d75bb]863\label{f:StrSemanticCompare}
864\end{figure}
865
[411aa65]866In C, these declarations are very different.
[5e8d75bb]867\begin{cfa}
[f85de47]868char x[$\,$] = "abcde";
869char * y = "abcde";
[5e8d75bb]870\end{cfa}
[411aa65]871Both associate the declared name with the fixed, six contiguous bytes: @{'a', 'b', 'c', 'd', 'e', 0}@.
872But @x@ is allocated on the stack (with values filled at the declaration), while @y@ refers to the executable's read-only data-section.
[f85de47]873With @x@ representing an allocation, it offers information in @sizeof(x)@ that @y@ does not.
[411aa65]874But this extra information is second-class, as it can only be used in the immediate lexical context, \ie it cannot be passed to string operations or user functions.
[f85de47]875Only pointers to text buffers are first-class, and discussed further.
[5e8d75bb]876\begin{cfa}
[f85de47]877char * s = "abcde";
[411aa65]878char * s1 = s; $\C[2.25in]{// alias state, N/A symmetry, variable-constrained referent}$
879char * s2 = &s[1]; $\C{// alias state, N/A symmetry, variable-constrained referent}$
880char * s3 = &s2[1]; $\C{// alias state, N/A symmetry, variable-constrained referent}\CRT$
[f85de47]881printf( "%s %s %s %s\n", s, s1, s2, s3 );
882$\texttt{\small abcde abcde bcde cde}$
[5e8d75bb]883\end{cfa}
[06ffa95]884Note, all of these aliased strings rely on the single null termination character at the end of @s@.
[f85de47]885The issue of symmetry does not apply to a low-level API because no management of a text buffer is provided.
886With the @char *@ type not managing the text storage, there is no ownership question, \ie operations on @s1@ or @s2@ never lead to their memory becoming reusable.
[195f43d]887While @s3@ is a valid C-string that contains a proper substring of @s1@, the @s3@ technique does not constitute having a fragment referent because null termination implies the substring cannot be chosen arbitrarily; the technique works only for suffixes.
[5e8d75bb]888
[195f43d]889In \CC, @string@ offers a high-level abstraction.
890\begin{cfa}
891string s = "abcde";
892string & s1 = s; $\C[2.25in]{// alias state, lax symmetry, variable-constrained referent}$
[f85de47]893string s2 = s; $\C{// no fast-initialize (strict symmetry, variable-constrained referent)}$
894string s3 = s.substr( 1, 2 ); $\C{// no fast-initialize (strict symmetry, fragment referent)}$
895string s4 = s3.substr( 1, 1 ); $\C{// no fast-initialize (strict symmetry, fragment referent)}$
[195f43d]896cout << s << ' ' << s1 << ' ' << s2 << ' ' << s3 << ' ' << s4 << endl;
897$\texttt{\small abcde abcde abcde bc c}$
898string & s5 = s.substr(2,4); $\C{// error: cannot point to temporary}\CRT$
899\end{cfa}
[411aa65]900The @s1@ lax symmetry reflects how its validity depends on the lifetime of @s@.
[f85de47]901It is common practice in \CC to use the @s1@-style for a by-reference function parameter.
902Doing so assumes that the callee only uses the referenced string for the duration of the call, \ie no storing the parameter (as a reference) for later.
903So, when the called function is a constructor, its definition typically uses an @s2@-style copy-initialization.
[195f43d]904Exceptions to this pattern are possible, but require the programmer to assure safety where the type system does not.
[411aa65]905The @s3@ initialization must copy the substring to support a subsequent @c_str@ call, which provides null-termination, generally at a different position than the source string's.
[f85de47]906@s2@ assignment could be made fast, by reference-counting the text area and using copy-on-write, but would require an implementation upgrade.
[195f43d]907
908In Java, @String@ also offers a high-level abstraction:
[06ffa95]909\begin{java}
[195f43d]910String s = "abcde";
911String s1 = s; $\C[2.25in]{// snapshot state, strict symmetry, variable-constrained referent}$
912String s2 = s.substring( 1, 3 ); $\C{// snapshot state (possible), strict symmetry, fragment referent}$
913String s3 = s2.substring( 1, 2 ); $\C{// snapshot state (possible), strict symmetry, fragment referent}\CRT$
914System.out.println( s + ' ' + s1 + ' ' + s2 + ' ' + s3 );
915$\texttt{\small abcde abcde bc c}$
[5d300ba]916System.out.println( (s == s1) + " " + (s == s2) + " " + (s2 == s3) );
[195f43d]917$\texttt{\small true false false}$
[06ffa95]918\end{java}
[f85de47]919Note, Java's @substring@ takes a start and end position, rather than a start position and length.
[195f43d]920Here, facts about Java's implicit pointers and pointer equality can over complicate the picture, and so are ignored.
921Furthermore, Java's string immutability means string variables behave as simple values.
922The result in @s1@ is the pointer in @s@, and their pointer equality confirm no time is spent copying characters.
923With @s2@, the case for fast-copy is more subtle.
924Certainly, its value is not pointer-equal to @s@, implying at least a further allocation.
[411aa65]925But because Java is \emph{not} constrained to use a null-terminated representation, a standard-library implementation is free to refer to the source characters in-place.
[f85de47]926Java does not meet the aliasing requirement because immutability makes it impossible to modify.
[06ffa95]927Java's @StringBuffer@ provides aliasing (see @replace@ example on \VPageref{p:JavaReplace}), though without supporting symmetric treatment of a fragment referent, \eg @substring@ of a @StringBuffer@ is a @String@;
928as a result, @StringBuffer@ scores as \CC.
[f85de47]929The easy symmetry that the Java string enjoys is aided by Java's garbage collection; Java's @s1@ is doing effectively the operation of \CC's @s1@, though without the consequence of complicating memory management.
930
931% former \PAB{What complex storage management is going on here?}
932% answer: the variable names in the sentence were out of date; fixed
[06ffa95]933
934Finally, in \CFA, @string@ also offers a high-level abstraction:
[5e8d75bb]935\begin{cfa}
[195f43d]936string s = "abcde";
[f85de47]937string & s1 = s; $\C[2.25in]{// alias state, lax symmetry, variable-constrained referent}$
[195f43d]938string s2 = s; $\C{// snapshot state, strict symmetry, variable-constrained referent}$
[f85de47]939string s3 = s`share; $\C{// alias state, strict symmetry, variable-constrained referent}$
940string s4 = s( 1, 2 ); $\C{// snapshot state, strict symmetry, fragment referent}$
[5d300ba]941string s5 = s4( 1, 1 )`share; $\C{// alias state, strict symmetry, fragment referent}\CRT$
[195f43d]942sout | s | s1 | s2 | s3 | s4 | s5;
943$\texttt{\small abcde abcde abcde abcde bc c}$
[5e8d75bb]944\end{cfa}
[195f43d]945% all helpful criteria of \VRef[Figure]{f:StrSemanticCompare} are satisfied.
946The \CFA string manages storage, handles all assignments, including those of fragment referents with fast initialization, provides the choice between snapshot and alias semantics, and does so symmetrically with one type (which assures text validity according to the lifecycles of the string variables).
[f85de47]947The @s1@ case is the same in \CFA as in \CC.
948But the \CFA @s2@ delivers a fast snapshot, while the \CC @s2@ does not.
949\CFA offers the snapshot-vs-alias choice in @s2@-vs-@s3@ and @s4@-vs-@s5@.
950Regardless of snapshot-vs-alias and fragment-vs-whole, the target is always of the same @string@ type.
[5e8d75bb]951
[f85de47]952% former \PAB{Need to discuss the example, as for the other languages.}
953% answer: done
[5e8d75bb]954
955
[411aa65]956\subsection{Logical Overlap}
[c01a2fd]957
[f85de47]958It may be unfamiliar to combine \VRef[Figure]{f:StrSemanticCompare}'s alias state and fragment referent in one API, or at the same time.
959This section shows the capability in action.
[c01a2fd]960
[411aa65]961\begin{comment}
962The metaphor of a GUI text-editor is used to illustrate combining these features.
963Most editors allow selecting a consecutive block of text (highlighted) to define an aliased substring within a document.
964Typing in this area overwrites the prior text, replacing the selected text with less, same, or more text.
965Importantly, the document also changes size, not just the selection.
966%This alias model is assigning to an aliased substring for two strings overlapping by total containment: one is the selected string, the other is the document.
967Extend the metaphor to two selected areas, where one area can be drag-and-dropped into another, changing the text in the drop area and correspondingly changing the document.
[a17f496]968When the selected areas are independent, the semantics of the drag-and-drop are straightforward.
[411aa65]969However, for overlapping selections, either partial or full, there are multiple useful semantics.
970For example, two areas overlap at the top, or bottom, or a block at a corner, where one areas is dropped into the other.
971For selecting a smaller area within a larger, and dropping the smaller area into the larger to replace it.
972In both cases, meaningful semantics must be constructed or the operation precluded.
973However, without this advanced capability, certain operations become multi-step, possible requiring explicit temporaries.
974\end{comment}
975
976A GUI text-editor provides a metaphor.
977Selecting a block of text using the mouse defines an aliased substring within a document.
978Typing in this area overwrites what was there, replacing the originally selected text with more or less text.
979But the \emph{containing document} also grows or shrinks, not just the selection.
980This action models assigning to an aliased substring when one string is completely contained in the other.
981
982Extend the metaphor to a multi-user editor.
983If Alice selects a range of text at the bottom, while Bob is rewriting a paragraph at the top, Alice's selection holds onto the characters initially selected, unaffected by Bob making the document grow/shrink even though Alice's start index in the document is changing.
984This action models assigning to an aliased substring when the two strings do not overlap.
985
986Logically, Alice's and Bob's actions on the whole document are like two single-user-edit cases, giving the semantics of the individual edits flowing into a whole.
987But, there is no need to have two separate document strings.
988Even if a third selection removes all the text, both Alice's and Bob's strings remain.
989The independence of their selections assumes that the editor API does not allow the selection to be enlarged, \ie adding text from the containing environment, which may have disappeared.
990
991This leaves the ``Venn-diagram overlap'' cases, where Alice's and Bob's selections overlap at the top, bottom, or corner.
992In this case, the selection areas are dependent, and so, changes in content and size in one may have an affect in the other.
993There are multiple possible semantics for this case.
994The remainder of this section shows the chosen semantics for all of the cases.
995
996String sharing is expressed using the @`share@ marker to indicate aliasing (mutations shared) \vs snapshot (not quite an immutable result, but one with subsequent mutations isolated).
[5d300ba]997This aliasing relationship is a \newterm{sticky property} established at initialization.
[06ffa95]998For example, here strings @s1@ and @s1a@ are in an aliasing relationship, while @s2@ is in a copy relationship.
[489d3ba]999\input{sharing1.tex}
[06ffa95]1000Here, the aliasing (@`share@) causes partial changes (subscripting) to flow in both directions.
[411aa65]1001(In the following examples, note how @s1@ and @s1a@ change together, and @s2@ is independent.)
[489d3ba]1002\input{sharing2.tex}
[06ffa95]1003Similarly for complete changes.
[489d3ba]1004\input{sharing3.tex}
[06ffa95]1005Because string assignment copies the value, RHS aliasing is irrelevant.
1006Hence, aliasing of the LHS is unaffected.
[489d3ba]1007\input{sharing4.tex}
1008
[411aa65]1009Now, consider string @s1_mid@ being an alias in the middle of @s1@, along with @s2@, made by a copy from the middle of @s1@.
[489d3ba]1010\input{sharing5.tex}
[06ffa95]1011Again, @`share@ passes changes in both directions; copy does not.
1012As a result, the index values for the position of @b@ are 1 in the longer string @"abcd"@ and 0 in the shorter aliased string @"bc"@.
1013This alternate positioning also applies to subscripting.
[489d3ba]1014\input{sharing6.tex}
1015
[06ffa95]1016Finally, assignment flows through the aliasing relationship without affecting its structure.
[489d3ba]1017\input{sharing7.tex}
[06ffa95]1018In the @"ff"@ assignment, the result is straightforward to accept because the flow direction is from contained (small) to containing (large).
1019The following rules explain aliasing substrings that flow in the opposite direction, large to small.
[bbf6a180]1020
[f85de47]1021Growth and shrinkage are natural extensions, as for the text-editor analogy, where an empty substring is as real as an empty string.
[489d3ba]1022\input{sharing8.tex}
[bbf6a180]1023
[06ffa95]1024Multiple portions of a string can be aliased.
1025% When there are several aliasing substrings at once, the text editor analogy becomes an online multi-user editor.
1026%I should be able to edit a paragraph in one place (changing the document's length), without my edits affecting which letters are within a mouse-selection that you had made previously, somewhere else.
[489d3ba]1027\input{sharing9.tex}
[06ffa95]1028When @s1_bgn@'s size increases by 3, @s1_mid@'s starting location moves from 1 to 4 and @s1_end@'s from 3 to 6,
[489d3ba]1029
[411aa65]1030When changes happen on an aliasing substring that overlap.
[489d3ba]1031\input{sharing10.tex}
[5d300ba]1032Strings @s1_crs@ and @s1_mid@ overlap at character 4, @'j'@, because the substrings are 3,2 and 4,2.
[8f250e0]1033When @s1_crs@'s size increases by 1, @s1_mid@'s starting location moves from 4 to 5, but the overlapping character remains, changing to @'+'@.
[489d3ba]1034
[06ffa95]1035\PAB{TODO: finish typesetting the demo}
[489d3ba]1036
1037%\input{sharing-demo.tex}
1038
[b0296dba]1039\VRef[Figure]{f:ParameterPassing} shows similar relationships when passing the results of substring operations by reference and by value to a subprogram.
1040Again, notice the side-effects to other reference parameters as one is modified.
1041
1042\begin{figure}
1043\begin{cfa}
1044// x, a, b, c, & d are substring results passed by reference
1045// e is a substring result passed by value
1046void test( string & x, string & a, string & b, string & c, string & d, string e ) {
1047\end{cfa}
1048\begin{cquote}
1049\setlength{\tabcolsep}{2pt}
1050\begin{tabular}{@{}ll@{}}
1051\begin{cfa}
1052
1053 a( 0, 2 ) = "aaa";
1054 b( 1, 12 ) = "bbb";
1055 c( 4, 5 ) = "ccc";
1056 c = "yyy";
1057 d( 0, 3 ) = "ddd";
1058 e( 0, 3 ) = "eee";
1059 x = e;
1060}
1061\end{cfa}
1062&
1063\sf
1064\setlength{\extrarowheight}{-0.5pt}
1065\begin{tabular}{@{}llllll@{}}
1066x & a & b & c & d & e \\
1067@"aaaxxxxxxxxx"@ & @"aaax"@ & @"xxx"@ & @"xxxxx"@ & @"xxx"@ & @"xxx"@ \\
1068@"aaaxbbbxxxxxx"@ & @"aaax"@ & @"xbbb"@ & @"xxxx"@ & @"xxx"@ & @"xxx"@ \\
1069@"aaaxbbbxxxcccxx"@ & @"aaax"@ & @"xbbb"@ & @"xxxccc"@& @"cccxx"@ & @"xxx"@ \\
1070@"aaaxbbbyyyxx"@ & @"aaax"@ & @"aaab"@ & @"yyy"@ & @"xx"@ & @"xxx"@ \\
1071@"aaaxbbbyyyddd"@ & @"aaax"@ & @"xbbb"@ & @"yyy"@ & @"ddd"@ & @"xxx"@ \\
1072@"aaaxbbbyyyddd"@ & @"aaax"@ & @"xbbb"@ & @"yyy"@ & @"ddd"@ & @"eee"@ \\
1073@"eee"@ & @""@ & @""@ & @""@ & @"eee"@ \\
1074 & \\
1075\end{tabular}
1076\end{tabular}
1077\end{cquote}
1078\begin{cfa}
1079int main() {
1080 string x = "xxxxxxxxxxx";
1081 test( x, x(0, 3), x(2, 3), x(4, 5), x(8, 5), x(8, 5) );
1082}
1083\end{cfa}
1084\caption{Parameter Passing}
1085\label{f:ParameterPassing}
1086\end{figure}
1087
[489d3ba]1088
[411aa65]1089\section{Storage Management}
[489d3ba]1090
[f85de47]1091This section discusses issues related to storage management of strings.
1092Specifically, it is common for strings to logically overlap partially or completely.
[8f250e0]1093\begin{cfa}
[f85de47]1094string s1 = "abcdef";
1095string s2 = s1; $\C{// complete overlap, s2 == "abcdef"}$
1096string s3 = s1.substr( 0, 3 ); $\C{// partial overlap, s3 == "abc"}$
[8f250e0]1097\end{cfa}
[f85de47]1098This raises the question of how strings behave when an overlapping component is changed,
[1b39705]1099\begin{cfa}
[f85de47]1100s3[1] = 'w'; $\C{// what happens to s1 and s2?}$
[1b39705]1101\end{cfa}
[f85de47]1102which is restricted by a string's mutable or immutable property.
1103For example, Java's immutable strings require copy-on-write when any overlapping string changes.
1104Note, the notion of underlying string mutability is not specified by @const@; \eg in \CC:
[1b39705]1105\begin{cfa}
[f85de47]1106const string s1 = "abc";
[1b39705]1107\end{cfa}
[411aa65]1108@const@ applies to the @s1@ pointer to @"abc"@, and @"abc"@ is an immutable constant that is \emph{copied} into the string's storage.
1109Hence, @s1@ is not pointing at an immutable constant and its underlying string is mutable, unless some other designation is specified, such as Java's global immutable rule.
[bbf6a180]1110
1111
[411aa65]1112\subsection{General Implementation}
[f85de47]1113\label{string-general-impl}
[bbf6a180]1114
[1b39705]1115A centrepiece of the string module is its memory manager.
1116The management scheme defines a shared buffer for string text.
[f85de47]1117Allocation in this buffer is always via a bump-pointer;
1118the buffer is compacted and/or relocated (to grow) when it fills.
[1b39705]1119A string is a smart pointer into this buffer.
[bbf6a180]1120
[411aa65]1121This cycle of frequent cheap allocations, interspersed with infrequent expensive compactions, has obvious similarities to a general-purpose memory-manager based on garbage collection (GC).
[1b39705]1122A few differences are noteworthy.
[8f250e0]1123First, in a general purpose manager, the allocated objects may contain pointers to other objects, making the transitive reachability of these objects a crucial property.
[1b39705]1124Here, the allocations are text, so one allocation never keeps another alive.
[f85de47]1125Second, in a general purpose manager, the handle that keeps an allocation alive is a bare pointer.
[411aa65]1126For strings, a fatter representation is acceptable because this pseudo-pointer is only used for entry into the string-heap, not for general data-substructure linking around the general heap.
[bbf6a180]1127
[0dffe91]1128\begin{figure}
[a7d93cb]1129\includegraphics{memmgr-basic.pdf}
[0dffe91]1130\caption{String memory-management data structures}
1131\label{f:memmgr-basic}
1132\end{figure}
[bbf6a180]1133
[1b39705]1134\VRef[Figure]{f:memmgr-basic} shows the representation.
[8f250e0]1135The heap header and text buffer define a sharing context.
[411aa65]1136Normally, one global context is appropriate for an entire program;
1137concurrency is discussed in \VRef{s:ControllingImplicitSharing}.
[5d300ba]1138A string is a handle to a node in a linked list containing information about a string text in the buffer.
[1b39705]1139The list is doubly linked for $O(1)$ insertion and removal at any location.
[f85de47]1140Strings are ordered in the list by text start address.
[411aa65]1141The heap header maintains a next-allocation pointer, @alloc@, pointing to the last live allocation in the buffer.
[1b39705]1142No external references point into the buffer and the management procedure relocates the text allocations as needed.
[8f250e0]1143A string handle references a containing string, while its string is contiguous and not null terminated.
[1b39705]1144The length sets an upper limit on the string size, but is large (4 or 8 bytes).
[8f250e0]1145String handles can be allocated in the stack or heap, and represent the string variables in a program.
1146Normal C life-time rules apply to guarantee correctness of the string linked-list.
[411aa65]1147The text buffer is large enough with good management so that often only one dynamic allocation is necessary during program execution, but not so large as to cause program bloat.
[8f250e0]1148% During this period, strings can vary in size dynamically.
[1b39705]1149
1150When the text buffer fills, \ie the next new string allocation causes @alloc@ to point beyond the end of the buffer, the strings are compacted.
1151The linked handles define all live strings in the buffer, which indirectly defines the allocated and free space in the buffer.
[411aa65]1152The string handles are maintained in sorted order, so the handle list can be traversed, copying the first live text to the start of the buffer, and subsequent strings after each other.
[5d300ba]1153After compaction, if free storage is still less than the new string allocation, a larger text buffer is heap-allocated, the current buffer is copied into the new buffer, and the original buffer is freed.
[f85de47]1154Note, the list of string handles is structurally unaffected during a compaction;
1155only the text pointers in the handles are modified to new buffer locations.
[1b39705]1156
[8f250e0]1157Object lifecycle events are the \emph{subscription-management} triggers in such a service.
[602ac05]1158There are two fundamental string-creation functions: importing external text like a C-string or reading a string, and initialization from an existing \CFA string.
[1b39705]1159When importing, storage comes from the end of the buffer, into which the text is copied.
[5d300ba]1160To maintain sorted order, the new string handle is inserted at the end of the handle list because the new text is at the end of the buffer.
[f85de47]1161When initializing from text already in the buffer, the new handle is a second reference into the original run of characters.
[5d300ba]1162To maintain sorted order, the new handle's linked-list position is after the original handle.
[1b39705]1163Both string initialization styles preserve the string module's internal invariant that the linked-list order matches the buffer order.
1164For string destruction, handles are removed from the list.
[411aa65]1165Once the last handle using a run of buffer characters is destroyed, that buffer space is excluded from use until the next compaction.
[1b39705]1166
[411aa65]1167Certain string operations result in a substring of another string.
1168The resulting handle is then placed in the correct sorted position in the list, possible requiring a short linear search to locate the position.
[1b39705]1169For string operations resulting in a new string, that string is allocated at the end of the buffer.
1170For shared-edit strings, handles that originally referenced containing locations need to see the new value at the new buffer location.
[f85de47]1171These strings are moved to appropriate locations at the end of the list \see{details in \VRef{sharing-impl}}.
1172For nonshared-edit strings, a containing string can be moved and the other strings can remain in the same position.
1173String assignment works similarly to string initialization, maintaining the invariant of linked-list order matching buffer order.
[bbf6a180]1174
[f85de47]1175At the level of the memory manager, these modifications can always be explained as assignment and appendment.
1176For example, an append is an assignment into the empty substring at the end of the buffer.
[8f250e0]1177Favourable conditions allow for in-place editing: where there is room for the resulting value in the original buffer location, and where all handles referring to the original buffer location see the new value.
[f85de47]1178One notable example of favourable conditions occurs because the most recently written string is often the last in the buffer, after which a large amount of free space occurs.
[5d300ba]1179Now, repeated appends (reading) can occur without copying previously accumulated characters.
[8f250e0]1180However, the general case requires a new buffer allocation: where the new value does not fit in the old place, or if other handles are still using the old value.
[bbf6a180]1181
1182
[411aa65]1183\subsection{RAII Limitations}
[f85de47]1184\label{string-raii-limit}
1185
1186Earlier work on \CFA~\cite[ch.~2]{Schluntz17} implemented object constructors and destructors for all types (basic and user defined).
[411aa65]1187A constructor is a user-defined function run implicitly \emph{after} an object's storage is allocated, and a destructor is a user-defined function run \emph{before} an object's storage is deallocated.
[f85de47]1188This feature, called Resource Acquisition Is Initialization (RAII)~\cite[p.~389]{Stroustrup94}, helps guarantee invariants for users before accessing an object and for the programming environment after an object terminates.
1189
1190The purposes of these invariants goes beyond ensuring authentic values inside an object.
1191Invariants can also track data about managed objects in other data structures.
1192Reference counting is an example application of an invariant outside of the immediate data value.
1193With a reference-counting smart-pointer, the constructor and destructor \emph{of a pointer type} track the lifecycle of the object it points to.
1194Both \CC and \CFA RAII systems are powerful enough to achieve reference counting.
1195
[5d300ba]1196In general, a lifecycle function has access to an object by location, \ie constructors and destructors receive a parameter providing an object's memory address.\footnote{
1197Historically in \CC, the type of \lstinline[language=C++]{this} is a pointer versus a reference;
1198in \CFA, the first parameter of a constructor or destructor must be a reference.}
[f85de47]1199\begin{cfa}
1200struct S { int * ip; };
1201void ?{}( S & @this@ ) { this.ip = new(); } $\C[3in]{// default constructor}$
[5d300ba]1202void ?{}( S & @this@, int i ) { this{}; *this.ip = i; } $\C{// initializing constructor}$
1203void ?{}( S & @this@, S s ) { this{ *s.ip }; } $\C{// copy constructor}$
[f85de47]1204void ^?{}( S & @this@ ) { delete( this.ip ); } $\C{// destructor}\CRT$
1205\end{cfa}
1206Such basic examples use the @this@ address only to gain access to the values being managed.
[5d300ba]1207But lifecycle logic can use the address, too, \eg add the @this@ object to a collection at creation and remove it at destruction.
[f85de47]1208\begin{cfa}
[5d300ba]1209// header (.hfa)
1210struct N { $\C[3in]{// list node}$
[f85de47]1211 // private
[5d300ba]1212 inline dlink( N );
[f85de47]1213};
[5d300ba]1214void ?{}( N & ); $\C{// default constructor}$
1215void ^?{}( N & ); $\C{// destructor}\CRT$
1216// implementation (.cfa)
1217static dlist( N ) @list_N@;
1218void ?{}( N & this ) { insert_last( list_N, @this@ ) }
1219void ^?{}( N & this ) { remove( this ); }
1220\end{cfa}
1221A module providing the @N@ (node) type can traverse @list_N@ to manipulate the objects.
1222Hence, declaring a @N@ not only ensures that it begins with an initially ``good'' value, but it also provides an implicit subscription to a service that keeps the value ``good'' during its lifetime.
[f85de47]1223Again, both \CFA and \CC support this usage style.
1224
1225A third capability concerns \emph{implicitly} requested copies.
1226When stack-allocated objects are used as parameter and return values, a sender's version exists in one stack frame and a receiver's version exists in another.
[411aa65]1227In the parameter direction, the language's function-call handling must arrange for a copy-constructor call to happen, at a time near the control transfer into the callee. %, with the source as the caller's (sender's) version and the target as the callee's (receiver's) version.
1228In the return direction, the roles are reversed and the copy-constructor call happens near the return of control.
[5d300ba]1229\CC supports this capability. % without qualification.
[411aa65]1230\CFA offers limited support;
[5d300ba]1231simple examples work, but implicit copying does not combine successfully with other RAII capabilities.
[411aa65]1232
1233\CC also offers move constructors and return-value optimization~\cite{RVO20}.
1234These features help reduce unhelpful copy-constructor calls, which, for types like the @S@ example, would lead to extra memory allocations.
1235\CFA does not currently have these features; adding similarly-intended features to \CFA is desirable.
1236However, this section is about a problem in the realization of features that \CFA already supports.
1237Hence, the comparison continues with the classic version of \CC that treated such copy-constructor calls as necessary.
[f85de47]1238
1239To summarize the unsupported combinations, the relevant features are:
1240\begin{enumerate}
1241\item
[5d300ba]1242\label{p:feature1}
[f85de47]1243 Object provider implements lifecycle functions to manage a resource outside of the object.
1244\item
[5d300ba]1245\label{p:feature2}
1246 Object provider implements lifecycle functions to store references to the object, often originating from outside of it.
[f85de47]1247\item
[5d300ba]1248\label{p:feature3}
[f85de47]1249 Object user expects to pass (in either direction) an object by value for function calls.
1250\end{enumerate}
[5d300ba]1251\CC supports all three simultaneously.
1252\CFA does not currently support \ref{p:feature2} and \ref{p:feature3} on the same object, though \ref{p:feature1} works along with either one of \ref{p:feature2} or \ref{p:feature3}.
1253\CFA needs to be fixed to support all three simultaneously.
[f85de47]1254
[5d300ba]1255The reason \CFA does not support \ref{p:feature2} with \ref{p:feature3} is a holdover from how \CFA lowered function calls to C, before \CFA got references and RAII.
1256At that time, adhering to a principal of minimal intervention, this code was treated as a passthrough:
[f85de47]1257\begin{cfa}
[411aa65]1258struct U { ... };
[f85de47]1259// RAII to go here
[5d300ba]1260void f( U u ) { F_BODY( u ) }
[f85de47]1261U x;
1262f( x );
1263\end{cfa}
[5d300ba]1264However, adding custom RAII (at ``...go here'') changes things.
1265
1266\VRef[Figure]{f:CodeLoweringRAII} shows the common \CC lowering~\cite[Sec. 3.1.2.3]{cxx:raii-abi} (right) proceeds differently than the present \CFA lowering (left).
1267The current \CFA scheme is still using a by-value C call.
1268C does a @memcpy@ on structures passed by value.
1269And so, @F_BODY@ sees the bits of @__u_for_f@ occurring at an address that has never been presented to the @U@ lifecycle functions.
1270If @U@ is trying to have a style- \ref{p:feature2} invariant, it shows up broken in @F_BODY@: references supposedly to @u@ are actually to @__u_for_f@.
1271The \CC scheme does not have this problem because it constructs the @u@ copy in the correct location within @f@.
1272Yet, the current \CFA scheme is sufficient to deliver style-\ref{p:feature1} invariants (in this style-\ref{p:feature3} use case) because this scheme still does the correct number of lifecycle calls, using correct values, at correct times.
1273So, reference-counting or simple ownership applications get their invariants respected under call/return-by-value.
1274
1275\begin{figure}
1276\centering
[411aa65]1277\begin{tabular}{@{}l|l@{}}
[5d300ba]1278\multicolumn{1}{@{}c|}{\CFA today} & \multicolumn{1}{c@{}}{\CC, \CFA future} \\
[f85de47]1279\begin{cfa}
1280struct U {...};
1281// RAII elided
[5d300ba]1282void f( U u ) {
1283
[411aa65]1284 F_BODY( u );
[5d300ba]1285
[f85de47]1286}
1287U x; // call default ctor
[5d300ba]1288{
1289 @U __u_for_f = x;@ // call copy ctor
1290 f( __u_for_f );
1291 // call dtor, __u_for_f
1292}
[f85de47]1293// call dtor, x
1294\end{cfa}
1295&
1296\begin{cfa}
1297struct U {...};
1298// RAII elided
[5d300ba]1299void f( U * __u_orig ) {
1300 @U u = * __u_orig;@ // call copy ctor
[411aa65]1301 F_BODY( u );
[5d300ba]1302 // call dtor, u
[f85de47]1303}
1304U x; // call default ctor
[5d300ba]1305
1306
1307f( &x ) ;
1308
1309
[f85de47]1310// call dtor, x
1311\end{cfa}
1312\end{tabular}
1313
[5d300ba]1314\caption{Code Lowering for RAII}
1315\label{f:CodeLoweringRAII}
1316\end{figure}
[f85de47]1317
1318% [Mike is not currently seeing how distinguishing initialization from assignment is relevant]
1319% as opposed to assignment where sender and receiver are in the same stack frame.
1320% What is crucial for lifecycle management is knowing if the receiver is initialized or uninitialized, \ie an object is or is not currently associated with management.
1321% To provide this knowledge, languages differentiate between initialization and assignment to a left-hand side.
1322% \begin{cfa}
1323% Obj obj2 = obj1; $\C[1.5in]{// initialization, obj2 is initialized}$
1324% obj2 = obj1; $\C{// assignment, obj2 must be initialized for management to work}\CRT$
1325% \end{cfa}
1326% Initialization occurs at declaration by value, parameter by argument, return temporary by function call.
1327% Hence, it is necessary to have two kinds of constructors: by value or object.
1328% \begin{cfa}
1329% Obj obj1{ 1, 2, 3 }; $\C[1.5in]{// by value, management is initialized}$
1330% Obj obj2 = obj1; $\C{// by obj, management is updated}\CRT$
1331% \end{cfa}
1332% When no object management is required, initialization copies the right-hand value.
1333% Hence, the calling convention remains uniform, where the unmanaged case uses @memcpy@ as the initialization constructor and managed uses the specified initialization constructor.
1334
1335% [Mike is currently seeing RVO as orthogonal, addressed with the footnote]
1336% to a temporary.
1337% For example, in \CC:
1338% \begin{c++}
1339% struct S {...};
1340% S identity( S s ) { return s; }
1341% S s;
1342% s = identity( s ); // S temp = identity( s ); s = temp;
1343% \end{c++}
1344% the generated code for the function call created a temporary with initialization from the function call, and then assigns the temporary to the object.
1345% This two step approach means extra storage for the temporary and two copies to get the result into the object variable.
1346% \CC{17} introduced return value-optimization (RVO)~\cite{RVO20} to ``avoid copying an object that a function returns as its value, including avoiding creation of a temporary object''.
1347% \CFA uses C semantics for function return giving direct value-assignment, which eliminates unnecessary code, but skips an essential feature needed by lifetime management.
1348% The following discusses the consequences of this semantics with respect to lifetime management of \CFA strings.
1349
[5d300ba]1350The string API offers style \ref{p:feature3}'s pass-by-value, \eg in the return of @"a" + "b"@.
1351Its implementation uses the style-\ref{p:feature2} invariant of the string handles being linked to each other, helping to achieve high performance.
[411aa65]1352Since these two RAII styles cannot coexist, a workaround splits the API into two layers: one that provides pass-by-value, built upon the other with inter-linked handles.
[f85de47]1353The layer with pass-by-value incurs a performance penalty, while the layer without delivers the desired runtime performance.
[411aa65]1354The slower, friendlier High Level API (HL, type @string@) wraps the faster, more primitive Low Level API (LL, type @string_res@, abbreviating ``resource'').
[f85de47]1355Both APIs present the same features, up to return-by-value operations being unavailable in LL and implemented via the workaround in HL.
1356The intention is for most future code to target HL.
[5d300ba]1357When the RAII issue is fixed, the full HL feature set is achievable using the LL-style lifetime management.
1358Then, HL can be removed, LL's type renamed to @string@, and programs generated with the current HL will run faster.
[f85de47]1359In the meantime, performance-critical sections of applications must use LL.
1360Subsequent performance experiments \see{\VRef{s:PerformanceAssessment}} use the LL API when comparing \CFA to other languages.
1361This measurement gives a fair estimate of the goal state for \CFA.
1362A separate measure of the HL overhead is also included.
[5d300ba]1363Hence, \VRef[Section]{string-general-impl} is describing the goal state for \CFA.
1364In present state, the internal type @string_res@ replaces @string@ for an inter-linked handle.
[f85de47]1365
[411aa65]1366To use LL, a programmer rewrites invocations using pass-by-value APIs into invocations where resourcing is more explicit.
1367Many invocations are unaffected, notably assignment and comparison.
[5d300ba]1368\VRef[Figure]{f:HL_LL_Lowering} shows, of the capabilities listed in \VRef[Figure]{f:StrApiCompare}, only three cases need revisions.
1369The actual HL workaround wraps @string@ as a pointer to a uniquely owned, heap-allocated @string_res@.
1370This arrangement has @string@ using style-\ref{p:feature1} RAII, which is compatible with pass-by-value.
1371
1372\begin{figure}
1373\centering
1374\begin{tabular}{@{}ll@{}}
[f85de47]1375HL & LL \\
1376\hline
1377\begin{cfa}
[411aa65]1378
[f85de47]1379string s = "a" + "b";
1380\end{cfa}
1381&
1382\begin{cfa}
1383string_res sr = "a";
1384sr += b;
1385\end{cfa}
1386\\
1387\hline
1388\begin{cfa}
1389string s = "abcde";
[5d300ba]1390string s2 = s(2, 3); // s2 == "cde"
[411aa65]1391
[5d300ba]1392s(2,3) = "x"; // s == "abx" && s2 == "cde"
[f85de47]1393\end{cfa}
1394&
1395\begin{cfa}
1396string_res sr = "abcde";
[5d300ba]1397string_res sr2 = {sr, 2, 3}; // sr2 == "cde"
[f85de47]1398string_res sr_mid = { sr, 2, 3, SHARE };
[5d300ba]1399sr_mid = "x"; // sr == "abx" && sr2 == "cde"
[f85de47]1400\end{cfa}
1401\\
1402\hline
1403\begin{cfa}
1404string s = "abcde";
[411aa65]1405
[5d300ba]1406s[2] = "xxx"; // s == "abxxxde"
[f85de47]1407\end{cfa}
1408&
1409\begin{cfa}
1410string_res sr = "abcde";
1411string_res sr_mid = { sr, 2, 1, SHARE };
[5d300ba]1412mid = "xxx"; // sr == "abxxxde"
[f85de47]1413\end{cfa}
1414\end{tabular}
[5d300ba]1415
1416\caption{HL to LL Lowering}
1417\label{f:HL_LL_Lowering}
1418\end{figure}
[f85de47]1419
1420
[411aa65]1421\subsection{Sharing Implementation}
[f85de47]1422\label{sharing-impl}
[bbf6a180]1423
[411aa65]1424The \CFA string module has two mechanisms to deal with string handles sharing text.
[f85de47]1425In the first type of sharing, the user requests that both string handles be views of the same logical, modifiable string.
[8f250e0]1426This state is typically produced by the substring operation.
1427\begin{cfa}
1428string s = "abcde";
1429string s1 = s( 1, 2 )@`share@; $\C[2.25in]{// explicit sharing}$
1430s[1] = 'x'; $\C{// change s and s1}\CRT$
1431sout | s | s1;
1432$\texttt{\small axcde xc}$
1433\end{cfa}
[411aa65]1434Here, the source string-handle is referencing an entire string, and the resulting, newly made, string handle is referencing a contained portion of the original.
1435In this state, a modification made in the overlapping area is visible in both strings.
[bbf6a180]1436
[8f250e0]1437The second type of sharing happens when the system implicitly delays the physical execution of a logical \emph{copy} operation, as part of its copy-on-write optimization.
1438This state is typically produced by constructing a new string, using an original string as its initialization source.
1439\begin{cfa}
1440string s = "abcde";
1441string s1 = s( 1, 2 )@@; $\C[2.25in]{// no sharing}$
1442s[1] = 'x'; $\C{// copy-on-write s1}\CRT$
1443sout | s | s1;
1444$\texttt{\small axcde bc}$
1445\end{cfa}
1446In this state, a subsequent modification done on one handle triggers the deferred copy action, leaving the handles referencing different text within the buffer, holding distinct values.
[bbf6a180]1447
[411aa65]1448A further abstraction helps distinguish the two senses of sharing.
[f85de47]1449A share-edit set (SES) is an equivalence class over string handles, being the reflexive, symmetric and transitive closure of the relationship of one string being constructed from another, with the ``share'' option given.
[8f250e0]1450The SES is represented by a second linked list among the handles.
1451A string that shares edits with no other is in a SES by itself.
[f85de47]1452Inside a SES, a logical modification of one substring portion may change the logical value in another substring portion, depending on whether the two actually overlap.
[8f250e0]1453Conversely, no logical value change can flow outside of a SES.
1454Even if a modification on one string handle does not reveal itself \emph{logically} to anther handle in the same SES (because they do not overlap), if the modification is length-changing, completing the modification requires visiting the second handle to adjust its location in the sliding text.
[bbf6a180]1455
1456
[411aa65]1457\subsection{Controlling Implicit Sharing}
[b0296dba]1458\label{s:ControllingImplicitSharing}
[62b5940]1459
[b0296dba]1460There are tradeoffs associated with sharing and its implicit copy-on-write mechanism.
1461Several qualitative matters are detailed in \VRef{s:PerformanceAssessment}.
1462In detail, string sharing has inter-linked string handles, so managing one string is also managing the neighbouring strings, and from there, a data structure of the ``set of all strings.''
1463Therefore, it is useful to toggle this capability on or off when it is not providing any application benefit.
[62b5940]1464
[b0296dba]1465The \CFA string library provides the type @string_sharectx@ to control an ambient sharing context.
1466It allows two adjustments: to opt out of sharing entirely or to begin sharing within a private context.
1467Running with sharing disabled can be thought of as a \CC STL-emulation mode, where each string is dynamically allocated.
1468The chosen mode applies for the duration of the lifetime of the created @string_sharectx@ object, up to being suspended by child lifetimes of different contexts.
1469\VRef[Figure]{fig:string-sharectx} illustrates this behaviour by showing the stack frames of a program in execution.
1470In this example, the single-letter functions are called in alphabetic order.
1471The functions @a@, @b@ and @g@ share string character ranges with each other, because they occupy a common sharing-enabled context.
1472The function @e@ shares within itself (because its is in a sharing-enabled context), but not with the rest of the program (because its context is not occupied by any of the rest of the program).
1473The functions @c@, @d@ and @f@ never share anything, because they are in a sharing-disabled context.
1474Executing the example does not produce an interesting outcome, but the comments in the picture indicate when the logical copy operation runs with
1475\begin{description}
[411aa65]1476 \item[share:] the copy being deferred, as described through the rest of this section (fast), or
1477 \item[copy:] the copy performed eagerly (slow).
[b0296dba]1478\end{description}
1479Only eager copies can cross @string_sharectx@ boundaries.
1480The intended use is with stack-managed lifetimes, in which the established context lasts until the current function returns, and affects all functions called that do not create their own contexts.
[bbf6a180]1481
[411aa65]1482\begin{figure}
1483 \begin{tabular}{ll}
1484 \lstinputlisting[language=CFA, firstline=10, lastline=55]{sharectx.run.cfa}
1485 &
1486 \raisebox{-0.17\totalheight}{\includegraphics{string-sharectx.pdf}} % lower
1487 \end{tabular}
1488 \caption{Controlling copying vs sharing of strings using \lstinline{string_sharectx}.}
1489 \label{fig:string-sharectx}
1490\end{figure}
[bbf6a180]1491
1492
[411aa65]1493\subsection{Sharing and Threading}
[b0296dba]1494
1495The \CFA string library provides no thread safety, the same as \CC string, providing similar performance goals.
1496Threads can create their own string buffers and avoid passing these strings to other threads, or require that shared strings be immutable, as concurrent reading is safe.
1497A positive consequence of this approach is that independent threads can use the sharing buffer without locking overhead.
1498When string sharing amongst threads is required, program-wide string-management can toggled to non-sharing using @malloc@/@free@, where the storage allocator is assumed to be thread-safe.
1499Finally, concurrent users of string objects can provide their own mutual exclusion.
1500
1501
[411aa65]1502\subsection{Short-String Optimization}
[bbf6a180]1503
[411aa65]1504\CC implements a short-string ($\le$16) optimization (SSO).
1505As a string header contains a pointer to the string text, this pointer can be tagged and used to contain a short string, removing a dynamic memory allocation/deallocation.
1506\begin{c++}
1507class string {
1508 union {
1509 struct { $\C{// long string, text in heap}$
1510 size_t size;
1511 char * strptr;
1512 } lstr;
1513 char sstr[sizeof(lstr)]; $\C{// short string <16 characters, text in situ}$
1514 };
1515 // some tagging for short or long strings
1516};
1517\end{c++}
1518However, there is now a conditional check required on the fast-path to switch between short and long string operations.
[bbf6a180]1519
[b0296dba]1520It might be possible to pack 16- or 32-bit Unicode characters within the same string buffer as 8-bit characters.
1521Again, locations for identification flags must be found and checked along the fast path to select the correct actions.
[5d300ba]1522Handling utf8 (variable length) is more problematic because simple pointer arithmetic cannot be used to stride through the variable-length characters.
[b0296dba]1523Trying to use a secondary array of fixed-sized pointers/offsets to the characters is possible, but raises the question of storage management for the utf8 characters themselves.
[bbf6a180]1524
1525
[411aa65]1526\section{Performance Assessment}
[1b39705]1527\label{s:PerformanceAssessment}
[bbf6a180]1528
[8f250e0]1529I assessed the \CFA string library's speed and memory usage against strings in \CC STL.
[5d300ba]1530Overall, this analysis shows that features common to both APIs comes at no substantial cost in the performance.
1531Moreover, the comparison shows that \CFA's high-level string features simplify text processing because the STL requires users to think more about memory management.
[f85de47]1532When the user does, and is successful, STL's performance can be very good.
[5d300ba]1533But if a user does understand the consequences of the STL representation, performance becomes poor.
[411aa65]1534The \CFA string lets the user work at the level of just putting the right text into the right variables, with corresponding performance degradations reduced or eliminated.
[bbf6a180]1535
[f85de47]1536% The final test shows the overall win of the \CFA text-sharing mechanism.
1537% It exercises several operations together, showing \CFA enabling clean user code to achieve performance that STL requires less-clean user code to achieve.
[bbf6a180]1538
[8f250e0]1539
1540\subsection{Methodology}
1541
[f85de47]1542These tests use a \emph{corpus} of strings.
1543Their lengths are important; the specific characters occurring in them are immaterial.
[5d300ba]1544In a result graph, a corpus's mean string-length is often the independent variable on the x-axis.
[e0350e0]1545When a corpus contains strings of different lengths, the lengths are drawn from a lognormal distribution.
[411aa65]1546Therefore, strings much longer than the mean occur less often and strings slightly shorter than the mean occur most often.
[f85de47]1547A corpus's string sizes are one of:
[bbf6a180]1548\begin{description}
[fc8ec54]1549 \item [Fixed-size] all string lengths are of the stated size.
[e0350e0]1550 \item [Varying 1 and up] the string lengths are drawn from the lognormal distribution with a stated mean and all lengths occur.
1551 \item [Varying 16 and up] string lengths are drawn from the lognormal distribution with the stated mean, but only lengths 16 and above occur; thus, the stated mean is above 16.
[bbf6a180]1552\end{description}
[411aa65]1553The special treatment of length 16 deals with the SSO in STL @string@, currently not implemented in \CFA.
[f85de47]1554A fixed-size or from-16 distribution ensures that \CC's extra-optimized cases are isolated within, or removed from, the comparison.
[fc8ec54]1555In all experiments that use a corpus, its text is generated and loaded into the system under test before the timed phase begins.
[f85de47]1556To ensure comparable results, a common memory allocator is used for \CFA and \CC.
[411aa65]1557\CFA runs the llheap allocator~\cite{Zulfiqar22}, which is also plugged into \CC.
[5d300ba]1558The llheap allocator is significantly better than the standard @glibc@ allocator.
[f85de47]1559
1560The operations being measured take dozens of nanoseconds, so a succession of many invocations is run and timed as a group.
[411aa65]1561The experiments run for a fixed duration (5 seconds), as determined by re-checking @clock()@ every 10,000 invocations, which is never more often than once per 80 ms.
1562Timing outcomes report mean nanoseconds per invocation, which includes harness overhead and the targeted string API execution.
[bbf6a180]1563
[411aa65]1564As discussed in \VRef[Section]{string-raii-limit}, general performance comparisons are made using \CFA's faster, low-level string API, named @string_res@.
1565\VRef{s:ControllingImplicitSharing} presents an operational mode where \CFA string sharing is turned off.
1566In this mode, the \CFA string operates similarly to \CC's, by using a heap allocation for string text.
1567Some experiments include measurements in this mode for baselining purposes, called ``\CC emulation mode'' or ``nosharing''.
[bbf6a180]1568
[8136df1]1569See~\VRef{s:ExperimentalEnvironment} for a description of the hardware environment.
1570
[bbf6a180]1571
[8f250e0]1572\subsection{Test: Append}
[bbf6a180]1573
[411aa65]1574These tests measure the speed of appending strings from the corpus onto a larger, growing string.
1575They show \CFA performing comparably to \CC overall, though with penalties for simple API misuses.
[f85de47]1576The basic harness is:
1577\begin{cfa}
[411aa65]1578// set alarm duration
1579for ( ... ) { $\C[1.5in]{// loop for duration}$
1580 for ( i; N ) { $\C{// perform multiple appends (concatenations)}$
1581 accum += corpus[ f( i ) ];
[f85de47]1582 }
[411aa65]1583 count += N; $\C{// count number of appends}\CRT$
[f85de47]1584}
1585\end{cfa}
[411aa65]1586The harness's outer loop executes for the experiment duration.
1587The string is reset to empty before appending (not shown).
1588The inner loop builds up a growing-length string with successive appends.
1589Each run targets a specific (mean) corpus string length and produces one data point on the result graph.
[f85de47]1590Three specific comparisons are made with this harness.
1591Each picks its own independent-variable basis of comparison.
[411aa65]1592All three comparisons use the varying-from-1 corpus construction, \ie they allow the STL to show its advantage for SSO.
[f85de47]1593
1594
1595\subsubsection{Fresh vs Reuse in \CC, Emulation Baseline}
1596
[411aa65]1597The first experiment compares \CFA with \CC, with \CFA operating in nosharing mode and \CC having no other mode, hence both string package are using @malloc@/@free@.
1598% This experiment establishes a baseline for other experiments.
1599This experiment also introduces the first \CC coding pitfall, which the next experiment shows is helped by turning on \CFA sharing.
1600% This pitfall shows, a \CC programmer must pay attention to string variable reuse.
1601In the following, both programs are doing the same thing: start with @accum@ empty and build it up by appending @N@ strings (type @string@ in \CC and the faster @string_res@ in \CFA).
[8f250e0]1602\begin{cquote}
[411aa65]1603\setlength{\tabcolsep}{40pt}
[8f250e0]1604\begin{tabular}{@{}ll@{}}
[fc8ec54]1605% \multicolumn{1}{c}{\textbf{fresh}} & \multicolumn{1}{c}{\textbf{reuse}} \\
[8f250e0]1606\begin{cfa}
[bbf6a180]1607
[8f250e0]1608for ( ... ) {
[411aa65]1609 @string_res accum;@ $\C[1.5in]{// fresh}$
1610 for ( N )
1611 accum @+=@ ... $\C{// append}\CRT$
[8f250e0]1612}
1613\end{cfa}
1614&
1615\begin{cfa}
[f85de47]1616string_res accum;
[8f250e0]1617for ( ... ) {
[411aa65]1618 @accum = "";@ $\C[1.5in]{// reuse}$
1619 for ( N )
1620 accum @+=@ ... $\C{// append}\CRT$
[8f250e0]1621}
1622\end{cfa}
1623\end{tabular}
1624\end{cquote}
[411aa65]1625The difference is creating a new or reusing an existing string variable.
1626The pitfall is that most programmers do not consider this difference.
1627However, creating a new variable implies deallocating the previous string storage and allocating new empty storage.
1628As the string grows, further deallocations/allocations are required to release the previous and extend the current string storage.
1629So, the fresh version is constantly restarting with zero string storage, while the reuse version benefits from having its prior large storage from the last append sequence.
[8f250e0]1630
[bbf6a180]1631\begin{figure}
[a7d93cb]1632\centering
[f85de47]1633 \includegraphics{plot-string-peq-cppemu.pdf}
[a7d93cb]1634% \includegraphics[width=\textwidth]{string-graph-peq-cppemu.png}
[411aa65]1635 \caption{Fresh vs Reuse in \CC, Emulation Baseline.
1636 Average time per iteration with one \lstinline{x += y} invocation (lower is better).
1637 Comparing \CFA's STL emulation mode with STL implementations, and comparing the fresh with reused reset styles.}
[8f250e0]1638 \label{fig:string-graph-peq-cppemu}
[411aa65]1639 \bigskip
1640 \bigskip
[2410424]1641 \includegraphics{plot-string-peq-sharing.pdf}
[411aa65]1642% \includegraphics[width=\textwidth]{string-graph-peq-sharing.png}
1643 \caption{\CFA Compromise for Fresh \vs Reuse.
1644 Average time per iteration with one \lstinline{x += y} invocation (lower is better).
1645 Comparing \CFA's sharing mode with STL, and comparing the fresh with reused reset styles.
1646 The \CC results are repeated from \VRef[Figure]{fig:string-graph-peq-cppemu}.}
1647 \label{fig:string-graph-peq-sharing}
[bbf6a180]1648\end{figure}
1649
[f85de47]1650\VRef[Figure]{fig:string-graph-peq-cppemu} shows the resulting performance.
[5d300ba]1651The two fresh (solid spline lines) and the two reuse (dash spline lines) are identical, except for lengths $\le$10, where the \CC SSO has a 40\% average and minimally 24\% advantage.
[411aa65]1652The gap between the fresh and reuse lines is the removal of the dynamic memory allocates and reuse of prior storage, \eg 100M allocations for fresh \vs 100 allocations for reuse across all experiments.
1653While allocation reduction is huge, data copying dominates the cost, so the lines are still reasonably close together.
[f85de47]1654
1655
[411aa65]1656\subsubsection{\CFA's Sharing Mode}
[f85de47]1657
[411aa65]1658This comparison is the same as the last one, except the \CFA implementation is using sharing mode.
1659Hence, both \CFA's fresh and reuse versions have no memory allocations, and as before, only for reuse does \CC have no memory allocations.
1660\VRef[Figure]{fig:string-graph-peq-sharing} shows the resulting performance.
1661For fresh at append lengths 5 and above, \CFA is now closer to the \CC reuse performance, because of removing the dynamic allocations.
1662However, for reuse, \CFA has slowed down slightly, to performance matching the new fresh version, as the two versions are now implemented virtually the same.
1663The reason for the \CFA reuse slow-down is the overhead of managing the sharing scheme (primarily maintaining the list of handles), without gaining any benefit.
[f85de47]1664
[411aa65]1665\begin{comment}
1666FIND A HOME!!!
1667The potential benefits of the sharing scheme do not give \CFA an edge over \CC when appending onto a reused string, though the first one helps \CFA win at going onto a fresh string. These abilities are:
1668\begin{itemize}
1669\item
1670To grow a text allocation repeatedly without copying it elsewhere.
1671This ability is enabled by \CFA's most-recently modified string being located immediately before the text buffer's \emph{shared} bump-pointer area, \ie often a very large greenfield, relative to the \emph{individual} string being grown.
1672With \CC-reuse, this benefit is already reaped by the user's reuse of a pre-stretched allocation.
1673Yet \CC-fresh pays the higher cost because its room to grow for free is at most a constant times the original string's length.
1674\item
1675To share an individual text allocation across multiple related strings.
1676This ability is not applicable to appending with @+=@.
1677It in play in [xref sub-experiment pta] and [xref experiment pbv].
1678\item
1679To share a text arena across unrelated strings, sourcing disparate allocations from a common place.
1680That is, always allocating from a bump pointer, and never maintaining free lists.
1681This ability is not relevant to running any append scenario on \CFA with sharing, because appending modifies an existing allocation and is not driving several allocations.
1682This ability is assessed in [xref experiment allocn].
1683\end{itemize}
1684This cost, of slowing down append-with-reuse, is \CFA paying the piper for other scenarios done well.
1685\CFA prioritizes the fresh use case because it is more natural.
1686The \emph{user-invoked} reuse scheme is an unnatural programming act because it deliberately misuses lexical scope: a variable (@accum@) gets its lifetime extended beyond the scope in which it is used.
[bbf6a180]1687
[411aa65]1688A \CFA user needing the best performance on an append scenario can still access the \CC-like speed by invoking noshare.
1689This (indirect) resource management is memory-safe, as compared to that required in \CC to use @string&@, where knowledge of another string's lifetime comes into play.
1690This abstraction opt-out is also different from invoking the LL API-level option.
1691In fact, these considerations are orthogonal.
1692But the key difference is that invoking the LL API would be a temporary measure, to use a workaround of a known \CFA language issue; choosing to exempt a string from sharing is a permanent act of program tuning.
1693Beyond these comparisons, opting for noshare actually provides program ``eye candy,'' indicating that under-the-hood thinking is becoming relevant here.
1694\end{comment}
[f85de47]1695
1696
[411aa65]1697\subsubsection{Misusing Concatenation}
[f85de47]1698
[411aa65]1699A further pitfall occurs writing the apparently equivalent @x = x + y@ \vs @x += y@.
[f85de47]1700For numeric types, the generated code is equivalent, giving identical performance.
1701However, for string types there can be a significant difference.
[411aa65]1702This pitfall is a particularly likely for beginners.
[f85de47]1703
1704In earlier experiments, the choice of \CFA API among HL and LL had no impact on the functionality being tested.
1705Here, however, the @+@ operation, which returns its result by value, is only available in HL.
[5d300ba]1706The \CFA @+=@ is obtained by inlining the HL implementation of @+@, which is done using LL's @+=@, into the test harness, while omitting the HL-inherent extra dynamic allocation. The HL-upon-LL @+@ implementation, is:
1707\begin{cquote}
1708\setlength{\tabcolsep}{20pt}
1709\begin{tabular}{@{}ll@{}}
[f85de47]1710\begin{cfa}
[5d300ba]1711struct string { // simple ownership
1712 string_res * inner; // RAII manages malloc/free
[f85de47]1713};
1714void ?+=?( string & s, string s2 ) {
1715 (*s.inner) += (*s2.inner);
1716}
[5d300ba]1717\end{cfa}
1718&
1719\begin{cfa}
[f85de47]1720string @?+?@( string lhs, string rhs ) {
1721 string ret = lhs;
1722 ret @+=@ rhs;
1723 return ret;
1724}
[5d300ba]1725
[f85de47]1726\end{cfa}
[5d300ba]1727\end{tabular}
1728\end{cquote}
[f85de47]1729This @+@ implementation is also the goal implementation of @+@ once the HL/LL workaround is no longer needed. Inlining the induced LL steps into the test harness gives:
1730\begin{cquote}
1731\setlength{\tabcolsep}{20pt}
1732\begin{tabular}{@{}ll@{}}
1733\multicolumn{1}{c}{\textbf{Goal}} & \multicolumn{1}{c}{\textbf{Measured}} \\
1734\begin{cfa}
1735
1736for ( ... ) {
1737 string accum;
1738 for ( ... ) {
1739 accum = @accum + ...@
1740
1741
1742 }
1743}
1744\end{cfa}
1745&
1746\begin{cfa}
1747string_res pta_ll_temp;
1748for ( ... ) {
1749 string_res accum;
1750 for ( ... ) {
1751 pta_ll_temp = @accum@;
1752 pta_ll_temp @+= ...@;
1753 accum = pta_ll_temp;
1754 }
1755}
1756\end{cfa}
1757\end{tabular}
1758\end{cquote}
[411aa65]1759Note, the goal code functions today in HL but with slower performance.
[bbf6a180]1760
1761\begin{figure}
[a7d93cb]1762\centering
[2410424]1763 \includegraphics{plot-string-pta-sharing.pdf}
[a7d93cb]1764% \includegraphics[width=\textwidth]{string-graph-pta-sharing.png}
[411aa65]1765 \caption{CFA's low overhead for misusing concatenation. Average time per iteration with one \lstinline{x += y} invocation (lower is better). Comparing \CFA (having implicit sharing activated) with STL, and comparing the \lstinline{+}-then-\lstinline{=} with the \lstinline{+=} append styles. The \lstinline{+=} results are repeated from \VRef[Figure]{fig:string-graph-peq-sharing}.}
[8f250e0]1766 \label{fig:string-graph-pta-sharing}
[bbf6a180]1767\end{figure}
1768
[411aa65]1769\VRef[Figure]{fig:string-graph-pta-sharing} gives the outcome, where the Y-axis is log scale because of the large differences.
1770The STL's penalty is $8 \times$ while \CFA's is only $2 \times$, averaged across the cases shown here.
[8f250e0]1771Moreover, the STL's gap increases with string size, while \CFA's converges.
[f85de47]1772So again, \CFA helps users who just want to treat strings as values, and not think about the resource management under the covers.
[bbf6a180]1773
[5d300ba]1774\PAB{Something is wrong with these sentences: While not a design goal, and not graphed, \CFA in STL-emulation mode outperformed STL in this case.
1775User-managed allocation reuse did not affect either implementation in this case; only ``fresh'' results are shown.}
[a7d93cb]1776
[bbf6a180]1777
[411aa65]1778\subsection{Test: Pass Argument}
[f85de47]1779
1780STL has a penalty for passing a string by value, which forces users to think about memory management when communicating values with a function.
[411aa65]1781The key \CFA value-add is that a user can think of a string simply as a value; this test shows that \CC charges a stiff penalty for thinking this way, while \CFA does not.
[f85de47]1782This test illustrates a main advantage of the \CFA sharing algorithm (in one case).
[411aa65]1783It shows STL's SSO providing a successful mitigation (in the other case).
[f85de47]1784
1785The basic operation considered is:
[b1c220a]1786\begin{cfa}
1787void foo( string s );
1788string s = "abc";
1789foo( s );
1790\end{cfa}
[8f250e0]1791With implicit sharing active, \CFA treats this operation as normal and supported.
[5d300ba]1792Again, an HL-LL difference requires a mockup as LL does not directly support pass-by-value.
[f85de47]1793\begin{cquote}
1794\setlength{\tabcolsep}{20pt}
1795\begin{tabular}{@{}ll@{}}
1796\multicolumn{1}{c}{\textbf{Goal}} & \multicolumn{1}{c}{\textbf{Measured}} \\
1797\begin{cfa}
1798void helper( string q ) {
1799
1800}
[411aa65]1801for ( ... ) { // loop for duration
1802 helper( corpus[ f( i ) ] );
1803 count += 1;
[f85de47]1804}
1805\end{cfa}
1806&
1807\begin{cfa}
1808void helper( string_res & qref ) {
1809 string_res q = { qref, COPY_VALUE };
1810}
1811
1812
1813
1814
1815\end{cfa}
1816\end{tabular}
1817\end{cquote}
[411aa65]1818The goal (HL) version gives the modified test harness, with a single loop.
1819Each iteration uses a corpus item as the argument to the function call.
[5d300ba]1820These corpus items are imported to the string heap before beginning the timed run.
[f85de47]1821
[bbf6a180]1822\begin{figure}
[a7d93cb]1823\centering
[2410424]1824 \includegraphics{plot-string-pbv.pdf}
[a7d93cb]1825% \includegraphics[width=\textwidth]{string-graph-pbv.png}
[2410424]1826 \begin{tabularx}{\linewidth}{>{\centering\arraybackslash}X >{\centering\arraybackslash}X} (a) & (b) \end{tabularx}
[a7d93cb]1827 \caption{Average time per iteration (lower is better) with one call to a function that takes a by-value string argument, comparing \CFA (having implicit sharing activated) with STL.
[e0350e0]1828(a) With \emph{Varying 1 and up} corpus construction, in which the STL-only benefit of SSO optimization occurs, in varying degrees, at all string sizes.
[2410424]1829(b) With \emph{Fixed-size} corpus construction, in which this benefit applies exactly to strings with length below 16.}
[8f250e0]1830 \label{fig:string-graph-pbv}
[bbf6a180]1831\end{figure}
1832
[a7d93cb]1833\VRef[Figure]{fig:string-graph-pbv} shows the costs for calling a function that receives a string argument by value.
[5d300ba]1834STL's performance worsens uniformly as string length increases, except for short strings due to SSO, while \CFA has the same performance at all sizes.
[f85de47]1835While improved, the \CFA cost to pass a string is still nontrivial.
[8f250e0]1836The contributor is adding and removing the callee's string handle from the global list.
[411aa65]1837This cost is $1.5 \times$ to $2 \times$ over STL's when SSO applies, but is avoidable once \CFA realizes this optimization.
1838At the larger sizes, the STL runs more than $3 \times$ slower, because it has to allocation/deallocate storage for the parameter and copy the argument string to the parameter.
1839If the \CC string is passed by reference, the results are better and flat across string lengths like \CFA.
[bbf6a180]1840
1841
[f85de47]1842\subsection{Test: Allocate}
[bbf6a180]1843
[f85de47]1844This test directly compares the allocation schemes of the \CFA string (sharing active), with the STL string.
1845It treats the \CFA scheme as a form of garbage collection (GC), and the STL scheme as an application of malloc-free.
1846The test shows that \CFA enables faster speed in exchange for greater memory usage.
[bbf6a180]1847
[a17f496]1848A precise garbage-collector, which knows all pointers and may modify them, often runs faster than malloc-free in an amortized analysis, even though it must occasionally stop to collect (latency issues).
1849The speedup happens indirectly because GC is able to move objects.
[411aa65]1850(In the case of the mini-allocator powering the \CFA string library, objects are runs of text.)
[a17f496]1851Moving objects creates a large contiguous store of available memory;
1852fresh allocations consume from this area using light-weight \emph{bump-allocation}.
1853A malloc-free implementation cannot move objects because the location of allocated objects is unknown;
1854hence, free-space management is more complex managing linked structures of freed allocations and possibly coalescing freed blocks.
1855
1856GC occurs lazily, so the lifetime of unreachable allocations is unknown, \eg GC may never be triggered.
1857A garbage collector can minimize the memory footprint for unreachable allocations by collecting eagerly.
1858However, this approach can negate GC's speed advantage.
1859Alternatively, collecting infrequently can consume large amounts of resident and virtual memory.
1860In contrast, a program using malloc-free tries to release an allocation as soon as it is unreachable.
1861This approach reduces the memory footprint (depending on the allocator), but has a per allocation cost for each free.
1862% [TODO: find citations for the above knowledge]
1863Therefore, a speed \vs memory tradeoff is a good comparator for \CFA--STL string allocations.
1864%The test verifies that it is so and quantifies the returns available.
[bbf6a180]1865
[8f250e0]1866These tests manipulate a tuning knob that controls how much extra space to use.
[a17f496]1867Specific values of this knob are not user-visible and are not presented in the results.
[8f250e0]1868Instead, its two effects (amount of space used and time per operation) are shown.
[a17f496]1869The independent variable is the liveness target, which is the fraction of the text buffer in use at the end of a collection.
1870The allocator expands its text buffer during a collection if the actual live fraction exceeds this target.
[bbf6a180]1871
[f85de47]1872\begin{figure}
1873\centering
1874 \includegraphics{string-perf-alloc.pdf}
[e0350e0]1875 \caption{Memory-allocation test's harness and its resulting pattern of memory usage under a bump-pointer-only scheme.}
[f85de47]1876 \label{fig:string-perf-alloc}
1877\end{figure}
1878
1879\VRef[Figure]{fig:string-perf-alloc} shows the memory-allocation pattern that the test harness drives.
1880Note how this pattern creates few long-lived strings and many short-lived strings.
1881To extend this pattern to the scale of a performance test, the harness is actually recursive, to achieve the nesting (three levels in the figure), and iterative, to achieve the fan-out (factor two / binary tree in the figure).
1882\begin{cfa}
1883void f( int depth ) {
1884 if (depth == 0) return;
1885 string_res x = corpus[...]; // importing from char * here
1886 COUNT_ONE_OP_DONE
1887 for ( fanOut ) {
1888 // elided: terminate fixed-duration experiment
1889 f( depth - 1 );
1890 }
1891}
1892START_TIMER
1893f( launchDepth );
1894STOP_TIMER
1895\end{cfa}
1896The runs presented use a call depth of 1000 and a fan-out of 1.006, which means that approximately one call in 167 makes two recursive calls, while the rest make one.
[a17f496]1897This sizing keeps an average of 836 strings live and keeps the amounts of consumed memory within the machine's last-level cache.
1898The time measurement is nanoseconds per @f@-call, for internal or leaf strings.
[f85de47]1899
1900% String lifetime (measured in number of subsequent string allocations) is ?? distributed, because each node in the call tree survives as long as its descendent calls.
1901
1902
[bbf6a180]1903\begin{figure}
[a7d93cb]1904\centering
[2410424]1905 \includegraphics{plot-string-allocn.pdf}
[a17f496]1906 \begin{tabularx}{\linewidth}{>{\centering\arraybackslash}X >{\centering\arraybackslash}X} (a) Vertical, Lower is better. \\ Horizontal, leftward is better. & (b) \hspace*{5pt} STL CFA \hspace*{20pt} STL \hspace*{10pt} CFA \hspace*{10pt} \end{tabularx}
1907 \caption{Space and time performance, under varying fraction-live targets, for the five string lengths shown, at \emph{Varying 16 and up} corpus construction.}
[8f250e0]1908 \label{fig:string-graph-allocn}
[bbf6a180]1909\end{figure}
1910
[a17f496]1911\VRef[Figure]{fig:string-graph-allocn} shows the experimental results.
1912In plot (a), there is one labeled curve for each of the five chosen string sizes, 20, 50, 100, 200, 500.
[e0350e0]1913A labeled curve shows the \CFA speed and space combinations achievable by varying the fraction-live tuning parameter.
[a17f496]1914Stepping along the curve from top-left toward bottom-right means the string library has a smaller fraction of its heap remain live after collection, \ie it doubles its size more often.
1915Hence, memory approximately doubles between points.
1916The additional memory means less time is spent collecting, so the curve shows lower (better) times per operation (y-axis).
1917This doubling benefit diminishes, so the curves begin to flatten.
1918
1919The point chosen as \CFA's default liveness threshold (20\%) is marked with a circle.
1920For most string lengths, this point occurs as the doubling benefit becomes subjectively negligible.
1921At len-500, the amount of space needed to achieve 20\% liveness is so much that last-level cache misses begin occurring generating a further slowdown.
1922This effect is an anomaly of the experimental setup trying to compare such varied sizes in one view;
1923the len-500 default point is included only to provide a holistic picture of the STL comparisons (discussed next).
1924
1925Staying within plot (a), each \emph{tradeoff} line (green) connects the default-tuned \CFA performance point with its length-equivalent STL performance point.
1926STL always uses less memory (occurs to the left of) than its \CFA equivalent.
1927Ideally, \CFA results would be faster (STL occurs above it);
1928however, this inversion only occurs for smaller string lengths.
1929That is, the STL-to-\CFA tradeoff line goes down and to the right for small strings, but for longer strings, it goes up and to the right.
[e0350e0]1930
1931Plot (b) offers insight into why larger lengths are not currently showing the anticipated tradeoff, and suggests possible remedies.
1932This plot breaks down the time spent, comparing STL--\CFA tradeoffs, at successful len-50 with unsuccessful len-200.
[a17f496]1933Data are sourced from running the experiment under \emph{perf}, recording samples of the call stack, and imposing a mutually-exclusive classification on these call stacks.
1934Reading a stacked bar from the top down, \emph{text import} captures samples where a routine like @memcpy@ is running, so that the string library is copying characters from the corpus source into its allocated working space.
1935\emph{Malloc-free} means literally one of those routines (taking nontrivial time only for STL), while \emph{gc} is the direct \CFA(-only) equivalent.
1936All of the attributions so far occur while a string is live; further time spent in a string's lifecycle management functions is attributed \emph{ctor-dtor}.
[e0350e0]1937Skipping to the bottom-most attribution, \emph{harness-leaf} represents samples where the youngest live stack frame is simply the recursive @helper@ routine of \VRef[Figure]{fig:string-perf-alloc}.
1938The skipped attribution \emph{Other} has samples that meet none of the criteria above.
1939
1940\PAB{From Mike: need to add the sep-comp side bar to the graph.}
1941
1942Beside \CFA's principal bars, a bar for separate-compilation overhead (\emph{sep.\ comp.}) shows the benefit that STL enjoys by using monolithic compilation.
1943\CFA currently forgoes this benefit.
1944This overhead figure was obtained by hacking a version of the \CFA string library to have a header-only implementation and measuring the resulting speed.
1945The difference between separately compiled (normal) and header-only (hacked) versions is the reported overhead.
[a17f496]1946It represents how much \CFA could speed up if it switched to a header-only implementation to match STL.
[e0350e0]1947The difference jumps noticeably between the different string sizes, but has little correlation with string size ($r=0.3$).
1948Associating this potential improvement with the \emph{other} attribution is a stylistic choice.
1949
1950Analyzing the stacked bars from the bottom up, the first two categories, \emph{harness-leaf} and \emph{other} both give baseline times that should not be matters of STL-\CFA difference.
1951The \emph{harness-leaf} category is called out as a quality check; this attribution is equal across all four setups, ruling out mysterious ``everything is X\% slower'' effects.
1952But with the \emph{other} attribution, STL beats \CFA by an amount that matches the STL's benefit from avoiding separate compilation.
1953In the \emph{ctor-dtor} category, \CFA is spending more time than STL to inter-link its string handles.
1954Comparing STL's \emph{malloc-free} with \CFA's \emph{gc}, \CFA's is considerably shorter, in spite of its copying, because \emph{gc} only spends time on live strings, while \emph{malloc-free} works on all strings.
1955Most of the every-string bookkeeping occurs under STL's \emph{malloc-free} and \CFA's \emph{ctor-dtor}.
1956So the critical comparison is STL's $(\textit{ctor-dtor} + \textit{malloc-free})$ \vs \CFA's $(\textit{ctor-dtor} + \textit{gc})$.
1957Here, \CFA is a clear winner.
[a17f496]1958Moreover, the discussion so far holds for both the successful and unsuccessful string lengths, albeit with the length-correlated duration of \emph{gc} (due to copying) encroaching on \CFA's advantage, though not eliminating it.
[e0350e0]1959The total attributions so far see \CFA ahead, possibly by more than is shown, depending on how one views the separate-compilation difference.
[a17f496]1960Under a \CFA-generous separate-compilation stance, \CFA is equal or ahead in every important category so far.
[e0350e0]1961
1962The remaining attribution, \emph{text-import}, is thus a major reason that \CFA is currently unsuccessful at delivering improved speed with long strings. \PAB{From Mike: need to finish this point.}
[bbf6a180]1963
[f85de47]1964% \subsection{Test: Normalize}
[bbf6a180]1965
[f85de47]1966% This test is more applied than the earlier ones.
1967% It combines the effects of several operations.
1968% It also demonstrates a case of the \CFA API allowing user code to perform well, while being written without overt memory management, while achieving similar performance in STL requires adding memory-management complexity.
[bbf6a180]1969
[f85de47]1970% To motivate: edits being rare
[bbf6a180]1971
[f85de47]1972% The program is doing a specialized find-replace operation on a large body of text.
1973% In the program under test, the replacement is just to erase a magic character.
1974% But in the larger software problem represented, the rewrite logic belongs to a module that was originally intended to operate on simple, modest-length strings.
1975% The challenge is to apply this packaged function across chunks taken from the large body.
1976% Using the \CFA string library, the most natural way to write the helper module's function also works well in the adapted context.
1977% Using the STL string, the most natural ways to write the helper module's function, given its requirements in isolation, slow down when it is driven in the adapted context.
[bbf6a180]1978
1979\begin{lstlisting}
1980void processItem( string & item ) {
[8f250e0]1981 // find issues in item and fix them
[bbf6a180]1982}
1983\end{lstlisting}
Note: See TracBrowser for help on using the repository browser.