- Timestamp:
- Apr 5, 2026, 9:24:11 AM (24 hours ago)
- Branches:
- master
- Children:
- bb9897c
- Parents:
- c1f17aa
- Location:
- doc/theses/mike_brooks_MMath
- Files:
-
- 2 edited
-
plots/string-pbv.gp (modified) (1 diff)
-
string.tex (modified) (35 diffs)
Legend:
- Unmodified
- Added
- Removed
-
doc/theses/mike_brooks_MMath/plots/string-pbv.gp
rc1f17aa re78d969 1 set terminal pdf color enhanced size 6. 0in,3.0in font "Times,17"1 set terminal pdf color enhanced size 6.5in,3.0in font "Times,17" 2 2 #set terminal postscript portrait enhanced size 7.5, 10. color solid 9.5; 3 3 #set terminal wxt size 950,1250 -
doc/theses/mike_brooks_MMath/string.tex
rc1f17aa re78d969 47 47 open( fname.@c_str()@, O_RDONLY ); // null terminated value of string 48 48 \end{cfa} 49 Here, the \CC @c_str@ functiondoes 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{49 Here, the \CC @c_str@ member 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{ 50 50 C functions like \lstinline{strdup} do return allocated storage that must be freed by the caller.} 51 51 % Instead, each \CC string is null terminated just in case it might be needed for this purpose. … … 94 94 s = 'x'; 95 95 s = "abc"; 96 s = 42hh; / * signed char */97 s = 42h; / * short int */96 s = 42hh; // signed char 97 s = 42h; // short int 98 98 s = 0xff; 99 99 \end{cfa} … … 134 134 Conversions from @string@ to @char *@ attempt to be safe. 135 135 The overloaded @strncpy@ function is safe, if the length of the C string is correct. 136 The assignment operator and constructor both allocate the bufferand return its address, meaning the programmer must free it.136 The assignment operator and constructor both allocate storage and return its address, meaning the programmer must free it. 137 137 Note, a C string is always null terminated, implying storage is always necessary for the null. 138 138 \begin{cquote} … … 284 284 285 285 Interestingly, \CC cannot support this generality because it does not use the left-hand side of assignment in expression resolution. 286 While it can special case some combinations:286 While it can special-case some combinations: 287 287 \begin{c++} 288 288 s = 'a' + s; $\C[2in]{// compiles in C++}$ … … 299 299 300 300 The binary operators @*@ and @*=@ repeat a string $N$ times. 301 If $N = 0$, a zero length string, @""@, is returned.301 If $N = 0$, a zero-length string results, @""@. 302 302 \begin{cquote} 303 303 \begin{tabular}{@{}ll@{}} … … 460 460 3 461 461 4 462 10 462 10 // not found, string length 463 463 \end{cfa} 464 464 \end{tabular} … … 470 470 \begin{cfa} 471 471 charclass vowels{ "aeiouy" }; 472 i = include( "aabiuyoo", vowels ); 472 473 i = include( "aaeiuyoo", vowels ); 473 i = include( "aabiuyoo", vowels ); 474 \end{cfa} 475 & 476 \begin{cfa} 477 478 8 // compliant 474 \end{cfa} 475 & 476 \begin{cfa} 477 479 478 2 // b non-compliant 479 8 // compliant, string length 480 480 \end{cfa} 481 481 \end{tabular} 482 482 \end{cquote} 483 483 @vowels@ defines a character class and function @include@ checks if all characters in the string appear in the class (compliance). 484 The position of the last character is returned if the string is compliant or the position of the first non-compliant character.484 The position of the first non-compliant character is returned or the position of the last character if the string is compliant. 485 485 There is no relationship between the order of characters in the two strings. 486 486 Function @exclude@ is the reverse of @include@, checking if all characters in the string are excluded from the class (compliance). … … 493 493 & 494 494 \begin{cfa} 495 8 // compliant 495 8 // compliant, string length 496 496 2 // y non-compliant 497 497 \end{cfa} … … 502 502 \begin{tabular}{@{}ll@{}} 503 503 \begin{cfa} 504 s = include( "aabiuyoo", vowels ); 504 505 s = include( "aaeiuyoo", vowels ); 505 s = include( "aabiuyoo", vowels );506 506 s = exclude( "cdbfghmk", vowels ); 507 507 s = exclude( "cdyfghmk", vowels ); … … 534 534 \end{cquote} 535 535 These 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. 536 The position of the last character is returned if the string is compliant or the position of the first non-compliant character.536 The position of the first non-compliant character is returned or the position of the last character if the string is compliant (and vice versa for @exclude@). 537 537 538 538 The translate operation returns a string with each character transformed by one of the C character transformation functions. … … 1028 1028 When @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, 1029 1029 1030 When changes happen on an aliasing substring that overlap.1030 Changes can happen on an aliasing substring that overlaps. 1031 1031 \input{sharing10.tex} 1032 1032 Strings @s1_crs@ and @s1_mid@ overlap at character 4, @'j'@, because the substrings are 3,2 and 4,2. 1033 1033 When @s1_crs@'s size increases by 1, @s1_mid@'s starting location moves from 4 to 5, but the overlapping character remains, changing to @'+'@. 1034 1034 1035 \PAB{TODO: finish typesetting the demo}1036 1037 %\input{sharing-demo.tex}1038 1039 \VRef[Figure]{f:ParameterPassing} shows similar relationships when passing the results of substring operations by reference and by value to a subprogram.1040 Again, notice the side-effects to other reference parameters as one is modified.1041 1042 1035 \begin{figure} 1043 1036 \begin{cfa} … … 1053 1046 a( 0, 2 ) = "aaa"; 1054 1047 b( 1, 12 ) = "bbb"; 1055 c( 4, 5 ) = "ccc";1048 c( 3, 5 ) = "ccc"; 1056 1049 c = "yyy"; 1057 1050 d( 0, 3 ) = "ddd"; … … 1064 1057 \setlength{\extrarowheight}{-0.5pt} 1065 1058 \begin{tabular}{@{}llllll@{}} 1066 x & 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 & \\ 1059 x & a & b & c & d & e \\ 1060 @"ssttttuuuww"@ & @"sst"@ & @"ttt"@ & @"ttuuu"@ & @"uww"@ & @"uww"@ \\ 1061 @"aaattttuuuww"@ & @"aaat"@ & @"ttt"@ & @"ttuuu"@ & @"uww"@ & @"uww"@ \\ 1062 @"aaatbbbtuuuww"@ & @"aaat"@ & @"tbbb"@ & @"tuuu"@ & @"uww"@ & @"uww"@ \\ 1063 @"aaatbbbtuucccww"@ & @"aaat"@ & @"tbbb"@ & @"tuuccc"@& @"cccww"@ & @"uww"@ \\ 1064 @"aaatbbbyyyww"@ & @"aaat"@ & @"tbbb"@ & @"yyy"@ & @"ww"@ & @"uww"@ \\ 1065 @"aaatbbbyyyddd"@ & @"aaat"@ & @"tbbb"@ & @"yyy"@ & @"ddd"@ & @"uww"@ \\ 1066 @"aaatbbbyyyddd"@ & @"aaat"@ & @"tbbb"@ & @"yyy"@ & @"ddd"@ & @"uww"@ \\ 1067 @"eee"@ & @""@ & @""@ & @""@ & @""@ & @"eee"@ \\ 1068 & 1075 1069 \end{tabular} 1076 1070 \end{tabular} … … 1086 1080 \end{figure} 1087 1081 1082 \begin{figure} 1083 \vspace*{-5pt} 1084 \setlength{\tabcolsep}{1.5pt} 1085 \setlength{\columnsep}{60pt} 1086 \setlength{\columnseprule}{0.2pt} 1087 \setlength{\extrarowheight}{-1pt} 1088 1089 \begin{multicols}{2} 1090 \sf 1091 \begin{tabular}{@{}l@{\hspace{10pt}}l@{}} 1092 \begin{tabular}{@{}*{12}{c}@{}} 1093 \multicolumn{12}{@{}l@{}}{function entry} \\ 1094 x & 0 & 1 & 2 & 3 & 4 & 5 & 6 & 7 & 8 & 9 & 10 \\ 1095 & {\tt s} & {\tt s} & {\tt t} & {\tt t} & {\tt t} & {\tt t} & {\tt u} & {\tt u} & {\tt u} & {\tt w} & {\tt w} \\ 1096 a & 0 & 1 & 2 \\ 1097 & {\tt s} & {\tt s} & {\tt t} \\ 1098 b & & & 0 & 1 & 2 \\ 1099 & & & {\tt t} & {\tt t} & {\tt t} \\ 1100 c & & & & & 0 & 1 & 2 & 3 & 4 \\ 1101 & & & & & {\tt t} & {\tt t} & {\tt u} & {\tt u} & {\tt u} \\ 1102 d & & & & & & & & & 0 & 1 & 2 \\ 1103 & & & & & & & & & {\tt u} & {\tt w} & {\tt w} 1104 \end{tabular} 1105 & 1106 \begin{tabular}{@{}*{5}{c}@{}} 1107 & \\ 1108 & e & 0 & 1 & 2 \\ 1109 & & {\tt u} & {\tt w} & {\tt w} \\ 1110 & & & & \\ 1111 & & & & \\ 1112 & & & & \\ 1113 & & & & \\ 1114 & & & & \\ 1115 & & & & \\ 1116 & & & & \\ 1117 & & & & 1118 \end{tabular} 1119 \end{tabular} 1120 1121 \bigskip 1122 @a( 0, 2 ) = "aaa";@ \\ 1123 \begin{tabular}{@{}l@{\hspace{10pt}}l@{}} 1124 \begin{tabular}{@{}*{13}{c}@{}} 1125 x & 0 & 1 & 2 & 3 & 4 & 5 & 6 & 7 & 8 & 9 & 10 & 11 \\ 1126 & {\tt a} & {\tt a} & {\tt a} & {\tt t} & {\tt t} & {\tt t} & {\tt t} & {\tt u} & {\tt u} & {\tt u} & {\tt w} & {\tt w} \\ 1127 a & 0 & 1 & 2 & 3 \\ 1128 & {\tt a} & {\tt a} & {\tt a} & {\tt t} \\ 1129 b & & & & 0 & 1 & 2 \\ 1130 & & & & {\tt t} & {\tt t} & {\tt t} \\ 1131 c & & & & & & 0 & 1 & 2 & 3 & 4 \\ 1132 & & & & & & {\tt t} & {\tt t} & {\tt u} & {\tt u} & {\tt u} \\ 1133 d & & & & & & & & & & 0 & 1 & 2 \\ 1134 & & & & & & & & & & {\tt u} & {\tt w} & {\tt w} 1135 \end{tabular} 1136 & 1137 \begin{tabular}{@{}*{5}{c}@{}} 1138 & e & 0 & 1 & 2 \\ 1139 & & {\tt u} & {\tt w} & {\tt w} \\ 1140 & & & & \\ 1141 & & & & \\ 1142 & & & & \\ 1143 & & & & \\ 1144 & & & & \\ 1145 & & & & \\ 1146 & & & & \\ 1147 & & & & 1148 \end{tabular} 1149 \end{tabular} 1150 1151 \bigskip 1152 1153 @b( 1, 12 ) = "bbb";@ \\ 1154 \begin{tabular}{@{}l@{\hspace{10pt}}l@{}} 1155 \begin{tabular}{@{}*{14}{c}@{}} 1156 x & 0 & 1 & 2 & 3 & 4 & 5 & 6 & 7 & 8 & 9 & 10 & 11 & 12\\ 1157 & {\tt a} & {\tt a} & {\tt a} & {\tt t} & {\tt b} & {\tt b} & {\tt b} & {\tt t} & {\tt u} & {\tt u} & {\tt u} & {\tt w} & {\tt w} \\ 1158 a & 0 & 1 & 2 & 3 \\ 1159 & {\tt a} & {\tt a} & {\tt a} & {\tt t} \\ 1160 b & & & & 0 & 1 & 2 & 3 \\ 1161 & & & & {\tt t} & {\tt b} & {\tt b} & {\tt b} \\ 1162 c & & & & & & & & 0 & 1 & 2 & 3 \\ 1163 & & & & & & & & {\tt t} & {\tt u} & {\tt u} & {\tt u} \\ 1164 d & & & & & & & & & & & 0 & 1 & 2 \\ 1165 & & & & & & & & & & & {\tt u} & {\tt w} & {\tt w} 1166 \end{tabular} 1167 & 1168 \begin{tabular}{@{}*{5}{c}@{}} 1169 & e & 0 & 1 & 2 \\ 1170 & & {\tt u} & {\tt w} & {\tt w} \\ 1171 & & & & \\ 1172 & & & & \\ 1173 & & & & \\ 1174 & & & & \\ 1175 & & & & \\ 1176 & & & & \\ 1177 & & & & \\ 1178 & & & & 1179 \end{tabular} 1180 \end{tabular} 1181 1182 \bigskip 1183 1184 @c( 3, 5 ) = "ccc";@ \\ 1185 \begin{tabular}{@{}l@{\hspace{10pt}}l@{}} 1186 \begin{tabular}{@{}*{16}{c}@{}} 1187 x & 0 & 1 & 2 & 3 & 4 & 5 & 6 & 7 & 8 & 9 & 10 & 11 & 12 & 13 & 14 \\ 1188 & {\tt a} & {\tt a} & {\tt a} & {\tt t} & {\tt b} & {\tt b} & {\tt b} & {\tt t} & {\tt u} & {\tt u} & {\tt c} & {\tt c} & {\tt c} & {\tt w} & {\tt w} \\ 1189 a & 0 & 1 & 2 & 3 \\ 1190 & {\tt a} & {\tt a} & {\tt a} & {\tt t} \\ 1191 b & & & & 0 & 1 & 2 & 3 \\ 1192 & & & & {\tt t} & {\tt b} & {\tt b} & {\tt b} \\ 1193 c & & & & & & & & 0 & 1 & 2 & 3 & 4 & 5 \\ 1194 & & & & & & & & {\tt t} & {\tt u} & {\tt u} & {\tt c} & {\tt c} & {\tt c} \\ 1195 d & & & & & & & & & & & 0 & 1 & 2 & 3 & 4 \\ 1196 & & & & & & & & & & & {\tt c} & {\tt c} & {\tt c} & {\tt w} & {\tt w} 1197 \end{tabular} 1198 & 1199 \begin{tabular}{@{}*{5}{c}@{}} 1200 & e & 0 & 1 & 2 \\ 1201 & & {\tt u} & {\tt w} & {\tt w} \\ 1202 & & & & \\ 1203 & & & & \\ 1204 & & & & \\ 1205 & & & & \\ 1206 & & & & \\ 1207 & & & & \\ 1208 & & & & \\ 1209 & & & & 1210 \end{tabular} 1211 \end{tabular} 1212 1213 @c = "yyy";@ \\ 1214 \begin{tabular}{@{}l@{\hspace{10pt}}l@{}} 1215 \begin{tabular}{@{}*{13}{c}@{}} 1216 x & 0 & 1 & 2 & 3 & 4 & 5 & 6 & 7 & 8 & 9 & 10 & 11 \\ 1217 & {\tt a} & {\tt a} & {\tt a} & {\tt t} & {\tt b} & {\tt b} & {\tt b} & {\tt y} & {\tt y} & {\tt y} & {\tt w} & {\tt w} \\ 1218 a & 0 & 1 & 2 & 3 \\ 1219 & {\tt a} & {\tt a} & {\tt a} & {\tt t} \\ 1220 b & & & & 0 & 1 & 2 & 3 \\ 1221 & & & & {\tt t} & {\tt b} & {\tt b} & {\tt b} \\ 1222 c & & & & & & & & 0 & 1 & 2 \\ 1223 & & & & & & & & {\tt y} & {\tt y} & {\tt y} \\ 1224 d & & & & & & & & & & & 0 & 1 \\ 1225 & & & & & & & & & & & {\tt w} & {\tt w} 1226 \end{tabular} 1227 & 1228 \begin{tabular}{@{}*{5}{c}@{}} 1229 & e & 0 & 1 & 2 \\ 1230 & & {\tt u} & {\tt w} & {\tt w} \\ 1231 & & & & \\ 1232 & & & & \\ 1233 & & & & \\ 1234 & & & & \\ 1235 & & & & \\ 1236 & & & & \\ 1237 & & & & \\ 1238 & & & & 1239 \end{tabular} 1240 \end{tabular} 1241 1242 \bigskip 1243 @d( 0, 3 ) = "ddd";@ \\ 1244 \begin{tabular}{@{}l@{\hspace{10pt}}l@{}} 1245 \begin{tabular}{@{}*{14}{c}@{}} 1246 x & 0 & 1 & 2 & 3 & 4 & 5 & 6 & 7 & 8 & 9 & 10 & 11 & 12 \\ 1247 & {\tt a} & {\tt a} & {\tt a} & {\tt t} & {\tt b} & {\tt b} & {\tt b} & {\tt y} & {\tt y} & {\tt y} & {\tt d} & {\tt d} & {\tt d} \\ 1248 a & 0 & 1 & 2 & 3 \\ 1249 & {\tt a} & {\tt a} & {\tt a} & {\tt t} \\ 1250 b & & & & 0 & 1 & 2 & 3 \\ 1251 & & & & {\tt t} & {\tt b} & {\tt b} & {\tt b} \\ 1252 c & & & & & & & & 0 & 1 & 2 \\ 1253 & & & & & & & & {\tt y} & {\tt y} & {\tt y} \\ 1254 d & & & & & & & & & & & 0 & 1 & 2 \\ 1255 & & & & & & & & & & & {\tt d} & {\tt d} & {\tt d} 1256 \end{tabular} 1257 & 1258 \begin{tabular}{@{}*{5}{c}@{}} 1259 & e & 0 & 1 & 2 \\ 1260 & & {\tt u} & {\tt w} & {\tt w} \\ 1261 & & & & \\ 1262 & & & & \\ 1263 & & & & \\ 1264 & & & & \\ 1265 & & & & \\ 1266 & & & & \\ 1267 & & & & \\ 1268 & & & & 1269 \end{tabular} 1270 \end{tabular} 1271 1272 \bigskip 1273 1274 @e( 0, 3 ) = "eee";@ \\ 1275 \begin{tabular}{@{}l@{\hspace{10pt}}l@{}} 1276 \begin{tabular}{@{}*{14}{c}@{}} 1277 x & 0 & 1 & 2 & 3 & 4 & 5 & 6 & 7 & 8 & 9 & 10 & 11 & 12 \\ 1278 & {\tt a} & {\tt a} & {\tt a} & {\tt t} & {\tt b} & {\tt b} & {\tt b} & {\tt y} & {\tt y} & {\tt y} & {\tt d} & {\tt d} & {\tt d} \\ 1279 a & 0 & 1 & 2 & 3 \\ 1280 & {\tt a} & {\tt a} & {\tt a} & {\tt t} \\ 1281 b & & & & 0 & 1 & 2 & 3 \\ 1282 & & & & {\tt t} & {\tt b} & {\tt b} & {\tt b} \\ 1283 c & & & & & & & & 0 & 1 & 2 \\ 1284 & & & & & & & & {\tt y} & {\tt y} & {\tt y} \\ 1285 d & & & & & & & & & & & 0 & 1 & 2 \\ 1286 & & & & & & & & & & & {\tt d} & {\tt d} & {\tt d} 1287 \end{tabular} 1288 & 1289 \begin{tabular}{@{}*{5}{c}@{}} 1290 & e & 0 & 1 & 2 \\ 1291 & & {\tt e} & {\tt e} & {\tt e} \\ 1292 & & & & \\ 1293 & & & & \\ 1294 & & & & \\ 1295 & & & & \\ 1296 & & & & \\ 1297 & & & & \\ 1298 & & & & \\ 1299 & & & & 1300 \end{tabular} 1301 \end{tabular} 1302 1303 \bigskip 1304 1305 @x = e;@ \\ 1306 \begin{tabular}{@{}l@{\hspace{10pt}}l@{}} 1307 \begin{tabular}{@{}*{16}{c}@{}} 1308 x & 0 & 1 & 2 \\ 1309 & {\tt e} & {\tt e} & {\tt e} \\ 1310 a & \\ 1311 & "" \\ 1312 b & \\ 1313 & "" \\ 1314 c & \\ 1315 & "" \\ 1316 d & \\ 1317 & "" 1318 \end{tabular} 1319 & 1320 \begin{tabular}{@{}*{5}{c}@{}} 1321 & e & 0 & 1 & 2 \\ 1322 & & {\tt e} & {\tt e} & {\tt e} \\ 1323 & & & & \\ 1324 & & & & \\ 1325 & & & & \\ 1326 & & & & \\ 1327 & & & & \\ 1328 & & & & \\ 1329 & & & & \\ 1330 & & & & 1331 \end{tabular} 1332 \end{tabular} 1333 \end{multicols} 1334 1335 \caption{Execution of Function \lstinline{test}} 1336 \label{f:ParametersPassingResults} 1337 \end{figure} 1338 1339 \VRef[Figure]{f:ParameterPassing} shows similar relationships when passing the results of substring operations by reference and by value to a subprogram. 1340 Because this example is complex, \VRef[Figure]{f:ParametersPassingResults} shows line-by-line changes to the reference parameters @x@, @a@, @b@, @c@, @d@, and value parameter @e@ as function @test@ executes (read top to bottom, left to right). 1341 The assignment to variable @a@ changes its substring @"ss"@ to @"aaa"@ increasing @a@ to @"aaat"@ (3 to 4 characters), increasing @x@ from 11 to 12 characters, and pushing @b@, @c@ and @d@ right one character in @x@ because there is no overlap with the changed characters. 1342 Similarly, the assignment to variable @b@ changes its substring @"tt"@ to @"bbb"@ increasing @b@ to @"tbbb"@ (3 to 4 characters), increasing @x@ from 12 to 13 characters, and pushing @c@ and @d@ right one character in @x@ because there is no overlap with the changed characters. 1343 The assignment to variable @c@ changes its substring @"u"@ (at the end) to @"ccc"@ increasing @c@ to @"tuuccc"@ (4 to 6 characters), increasing @x@ from 13 to 15 characters, and @d@ remains in the same position because it overlaps with the changed characters and its @"u"@ grows to @"ccc"@ giving @"cccww"@. 1344 The second assignment to variable @c@ changes its substring @"tuuccc"@ to @"yyy"@ decreasing @c@ to @"yyy"@ (6 to 3 characters), decreasing @x@ from 15 to 12 characters, and the 3 character overlapping with @d@ are removed leaving @"ww"@. 1345 The assignment to variable @d@ changes its substring @"ww"@ to @"ddd"@ increasing @d@ from 2 to 3 characters, increasing @x@ from 11 to 12 characters. 1346 The assignment to variable @e@ changes its substring @"uww"@ to @"eee"@ with no change in length. 1347 Finally, the assignment to @x@ sets it pointing at @e@, and all prior overlapping strings with @x@ become empty (null) strings. 1348 1088 1349 1089 1350 \section{Storage Management} 1090 1351 1091 This section discusses issues related to storage management of strings. 1092 Specifically, it is common for strings to logically overlap partially or completely. 1093 \begin{cfa} 1094 string s1 = "abcdef"; 1095 string s2 = s1; $\C{// complete overlap, s2 == "abcdef"}$ 1096 string s3 = s1.substr( 0, 3 ); $\C{// partial overlap, s3 == "abc"}$ 1097 \end{cfa} 1098 This raises the question of how strings behave when an overlapping component is changed, 1099 \begin{cfa} 1100 s3[1] = 'w'; $\C{// what happens to s1 and s2?}$ 1101 \end{cfa} 1102 which is restricted by a string's mutable or immutable property. 1103 For example, Java's immutable strings require copy-on-write when any overlapping string changes. 1104 Note, the notion of underlying string mutability is not specified by @const@; \eg in \CC: 1105 \begin{cfa} 1106 const string s1 = "abc"; 1107 \end{cfa} 1108 @const@ applies to the @s1@ pointer to @"abc"@, and @"abc"@ is an immutable constant that is \emph{copied} into the string's storage. 1109 Hence, @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. 1352 This section discusses the implementation of the prior string semantics, specifically, when strings overlap partially or completely. 1353 % First, the issue of a string's mutable or immutable property is clarified. 1354 % Java's immutable strings require copy-on-write when any overlapping string changes. 1355 % However, \CFA/\CC strings are always mutability, even if specified with @const@. 1356 % \begin{cfa} 1357 % const string s1 = "abc"; 1358 % \end{cfa} 1359 % Here, @const@ applies to the implicit @s1@ pointer to @"abc"@, and @"abc"@ is an immutable constant that is \emph{copied} into the string's storage. 1360 % Hence, @s1@ is never pointing at an immutable constant and its underlying string is always mutable, without some addition mechanism as for explicit pointers/references. 1361 % \begin{cfa} 1362 % const int * const cip; // pointer and its referent are const 1363 % const string s1 @const@ = "abc"; // second const applies to implicit referent 1364 % \end{cfa} 1110 1365 1111 1366 … … 1113 1368 \label{string-general-impl} 1114 1369 1115 Acentrepiece of the string module is its memory manager.1370 The centrepiece of the string module is its memory manager. 1116 1371 The management scheme defines a shared buffer for string text. 1117 1372 Allocation in this buffer is always via a bump-pointer; … … 1136 1391 Normally, one global context is appropriate for an entire program; 1137 1392 concurrency is discussed in \VRef{s:ControllingImplicitSharing}. 1138 A string is a handle to a node in a linked list containing information about astring text in the buffer.1393 A string is a handle to a node in a linked list containing information about string text in the buffer. 1139 1394 The list is doubly linked for $O(1)$ insertion and removal at any location. 1140 1395 Strings are ordered in the list by text start address. … … 1168 1423 The resulting handle is then placed in the correct sorted position in the list, possible requiring a short linear search to locate the position. 1169 1424 For string operations resulting in a new string, that string is allocated at the end of the buffer. 1170 For shared-edit strings, handles that originally reference dcontaining locations need to see the new value at the new buffer location.1425 For shared-edit strings, handles that originally reference containing locations need to see the new value at the new buffer location. 1171 1426 These strings are moved to appropriate locations at the end of the list \see{details in \VRef{sharing-impl}}. 1172 1427 For nonshared-edit strings, a containing string can be moved and the other strings can remain in the same position. … … 1205 1460 \end{cfa} 1206 1461 Such basic examples use the @this@ address only to gain access to the values being managed. 1207 But lifecycle logic can use the address, too, \eg add the @this@ object to a collection at creation and remove it at destruction. 1208 \begin{cfa} 1209 // header (.hfa) 1210 struct N { $\C[3in]{// list node}$ 1462 But lifecycle logic can use the \emph{address}, too, \eg add the @this@ object to a collection at creation and remove it at destruction. 1463 \begin{cfa} 1464 struct Thread { $\C[3in]{// list node}$ 1211 1465 // private 1212 inline dlink( N);1466 inline dlink( Thread ); 1213 1467 }; 1214 void ?{}( N & ); $\C{// default constructor}$ 1215 void ^?{}( N & ); $\C{// destructor}\CRT$ 1216 // implementation (.cfa) 1217 static dlist( N ) @list_N@; 1218 void ?{}( N & this ) { insert_last( list_N, @this@ ) } 1219 void ^?{}( N & this ) { remove( this ); } 1220 \end{cfa} 1221 A module providing the @N@ (node) type can traverse @list_N@ to manipulate the objects. 1222 Hence, 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. 1468 static dlist( Thread ) @ready_queue@; 1469 void ?{}( Thread & this ) { /* initialize thread */ insert_last( ready_queue, @this@ ); } 1470 void ^?{}( Thread & this ) { remove( @this@ ); /* de-initialize thread */ } $\C{// implicitly use ready\_queue}\CRT$ 1471 \end{cfa} 1472 A module providing the @Thread@ (node) type can traverse @ready_queue@ to manipulate the objects. 1473 Hence, declaring a @Thread@ not only ensures that it begins with an initially ``good'' value, but it also provides an implicit subscription to a service (scheduler) that keeps the value ``good'' during its lifetime. 1223 1474 Again, both \CFA and \CC support this usage style. 1224 1475 1225 A third capability concerns \emph{implicit ly} requested copies.1226 When 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.1227 In 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.1228 In the return direction, the roles are reversed and the copy-constructor call happens near the return of control.1476 A third capability concerns \emph{implicit} copying. 1477 For passing and returning by value in function call, the parameters and return results do not require construction because after allocation the argument or return value is copied into the corresponding object. 1478 In the parameter direction, the language's function-call arranges copy-constructor calls at the control transfer into the callee. 1479 In the return direction, the roles are reversed and the copy-constructor calls happen at the return to the caller. 1229 1480 \CC supports this capability. % without qualification. 1230 1481 \CFA offers limited support; … … 1232 1483 1233 1484 \CC also offers move constructors and return-value optimization~\cite{RVO20}. 1234 These 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. 1485 These features help reduce unhelpful copy-constructor calls, which, for types like the initial @S@ example, lead to extra memory allocations. 1486 \CFA does not currently have these features; 1487 adding similarly-intended features to \CFA is desirable. 1236 1488 However, this section is about a problem in the realization of features that \CFA already supports. 1237 Hence, the comparison continues with the classic version of \CC that treat edsuch copy-constructor calls as necessary.1489 Hence, the comparison continues with the classic version of \CC that treats such copy-constructor calls as necessary. 1238 1490 1239 1491 To summarize the unsupported combinations, the relevant features are: … … 1257 1509 \begin{cfa} 1258 1510 struct U { ... }; 1259 // RAII to go here1260 void f( U u ) { F_BODY( u )}1511 // add RAII ctor/dtor function 1512 void f( U u ) { ... /* access fields of u */ ... } 1261 1513 U x; 1262 1514 f( x ); 1263 1515 \end{cfa} 1264 However, adding custom RAII (at ``...go here'')changes things.1516 However, adding RAII constructors/destructor changes things. 1265 1517 1266 1518 \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). 1267 1519 The current \CFA scheme is still using a by-value C call. 1268 1520 C does a @memcpy@ on structures passed by value. 1269 And so, @F_BODY@ sees the bits of @__u_for_f@ occurring at an address that has never been presented to the @U@ lifecycle functions.1270 If @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@.1521 And so, the body of @f@ sees the bits of @__u_for_f@ occurring at an address that has never been presented to the @U@ lifecycle functions. 1522 If @U@ is trying to have a style-\ref{p:feature2} invariant, it shows up broken in @f@: references supposedly to @u@ are actually to @__u_for_f@. 1271 1523 The \CC scheme does not have this problem because it constructs the @u@ copy in the correct location within @f@. 1272 1524 Yet, 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. … … 1282 1534 void f( U u ) { 1283 1535 1284 F_BODY( u );1536 ... /* access fields of u */ ... 1285 1537 1286 1538 } … … 1299 1551 void f( U * __u_orig ) { 1300 1552 @U u = * __u_orig;@ // call copy ctor 1301 F_BODY( u );1553 ... /* access fields of u */ ... 1302 1554 // call dtor, u 1303 1555 } … … 1447 1699 1448 1700 A further abstraction helps distinguish the two senses of sharing. 1449 A 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, w ith the ``share'' optiongiven.1701 A 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, when the ``share'' option is given. 1450 1702 The SES is represented by a second linked list among the handles. 1451 A string that shares edits with no otheris in a SES by itself.1452 Inside a SES, a logical modification of one substring portion may change the logical value in another substring portion, depending on whether the two actuallyoverlap.1453 Conversely, no logicalvalue change can flow outside of a SES.1454 Even if a modification on one string handle does not reveal itself \emph{logically} to an ther 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.1703 A string with no sharing is in a SES by itself. 1704 Inside a SES, a modification of one substring portion may change the value in another substring portion, depending on whether the two overlap. 1705 Conversely, no value change can flow outside of a SES. 1706 Even if a modification on one string handle does not reveal itself \emph{logically} to another 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. 1455 1707 1456 1708 … … 1473 1725 The functions @c@, @d@ and @f@ never share anything, because they are in a sharing-disabled context. 1474 1726 Executing 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} 1727 \begin{description}[itemsep=0pt,parsep=0pt] 1476 1728 \item[share:] the copy being deferred, as described through the rest of this section (fast), or 1477 1729 \item[copy:] the copy performed eagerly (slow). … … 1518 1770 However, there is now a conditional check required on the fast-path to switch between short and long string operations. 1519 1771 1520 It might be possible to pack 16- or 32-bit Unicode characters within the same string buffer as 8-bit characters.1772 It might be possible to pack 16- or 32-bit Unicode characters within the same string buffer as for 8-bit characters. 1521 1773 Again, locations for identification flags must be found and checked along the fast path to select the correct actions. 1522 1774 Handling utf8 (variable length) is more problematic because simple pointer arithmetic cannot be used to stride through the variable-length characters. … … 1527 1779 \label{s:PerformanceAssessment} 1528 1780 1529 I assessed the \CFA string library's speed and memory usage against strings in \CC STL.1530 Overall, this analysis shows that features common to both APIs comes at no substantial cost in theperformance.1781 I assessed the \CFA string library's speed and memory usage against strings in the \CC STL. 1782 Overall, this analysis shows common features in both APIs comes at no substantial cost in performance. 1531 1783 Moreover, the comparison shows that \CFA's high-level string features simplify text processing because the STL requires users to think more about memory management. 1532 1784 When the user does, and is successful, STL's performance can be very good. … … 1541 1793 1542 1794 These tests use a \emph{corpus} of strings. 1543 The ir lengths are important; the specific characters occurring in them are immaterial.1795 The lengths are important not the contents. 1544 1796 In a result graph, a corpus's mean string-length is often the independent variable on the x-axis. 1545 1797 When a corpus contains strings of different lengths, the lengths are drawn from a lognormal distribution. … … 1553 1805 The special treatment of length 16 deals with the SSO in STL @string@, currently not implemented in \CFA. 1554 1806 A fixed-size or from-16 distribution ensures that \CC's extra-optimized cases are isolated within, or removed from, the comparison. 1555 In all experiments that use a corpus, its text is generated and loaded into the system under test before the timedphase begins.1807 In all experiments that use a corpus, text is generated and loaded before the timing phase begins. 1556 1808 To ensure comparable results, a common memory allocator is used for \CFA and \CC. 1557 1809 \CFA runs the llheap allocator~\cite{Zulfiqar22}, which is also plugged into \CC. 1558 1810 The llheap allocator is significantly better than the standard @glibc@ allocator. 1559 1811 1560 The operations being measured take dozens of nanoseconds, so a succession of many invocations is run and timed as a group.1812 The operations being measured take only dozens of nanoseconds, so a succession of many invocations is run and timed as a group. 1561 1813 The 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. 1562 1814 Timing outcomes report mean nanoseconds per invocation, which includes harness overhead and the targeted string API execution. … … 1566 1818 In this mode, the \CFA string operates similarly to \CC's, by using a heap allocation for string text. 1567 1819 Some experiments include measurements in this mode for baselining purposes, called ``\CC emulation mode'' or ``nosharing''. 1568 1569 1820 See~\VRef{s:ExperimentalEnvironment} for a description of the hardware environment. 1570 1821 … … 1578 1829 // set alarm duration 1579 1830 for ( ... ) { $\C[1.5in]{// loop for duration}$ 1831 // reset accum to empty 1580 1832 for ( i; N ) { $\C{// perform multiple appends (concatenations)}$ 1581 1833 accum += corpus[ f( i ) ]; … … 1585 1837 \end{cfa} 1586 1838 The harness's outer loop executes for the experiment duration. 1587 The string is reset to empty before appending ( not shown).1839 The string is reset to empty before appending (see below). 1588 1840 The inner loop builds up a growing-length string with successive appends. 1589 1841 Each run targets a specific (mean) corpus string length and produces one data point on the result graph. 1590 1842 Three specific comparisons are made with this harness. 1591 1843 Each picks its own independent-variable basis of comparison. 1592 All three comparisons use the varying-from-1 corpus construction, \ie they allow the STL to show its advantage for SSO.1844 All three comparisons use the varying-from-1 corpus construction, \ie they allow the STL to show its SSO advantage. 1593 1845 1594 1846 1595 1847 \subsubsection{Fresh vs Reuse in \CC, Emulation Baseline} 1596 1848 1597 The 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@.1849 The first experiment compares \CFA with \CC in nosharing mode using @malloc@/@free@. 1598 1850 % This experiment establishes a baseline for other experiments. 1599 1851 This experiment also introduces the first \CC coding pitfall, which the next experiment shows is helped by turning on \CFA sharing. 1600 1852 % This pitfall shows, a \CC programmer must pay attention to string variable reuse. 1601 In 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).1853 In the following, both programs start with @accum@ empty and build it up by appending @N@ strings (type @string@ in \CC and the faster @string_res@ in \CFA). 1602 1854 \begin{cquote} 1603 1855 \setlength{\tabcolsep}{40pt} … … 1649 1901 1650 1902 \VRef[Figure]{fig:string-graph-peq-cppemu} shows the resulting performance. 1651 The two fresh (solid spline lines) and the two reuse (dash splinelines) are identical, except for lengths $\le$10, where the \CC SSO has a 40\% average and minimally 24\% advantage.1903 The two fresh (solid lines) and the two reuse (dash lines) are identical, except for lengths $\le$10, where the \CC SSO has a 40\% average and minimally 24\% advantage. 1652 1904 The 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. 1653 1905 While allocation reduction is huge, data copying dominates the cost, so the lines are still reasonably close together. … … 1919 2171 The point chosen as \CFA's default liveness threshold (20\%) is marked with a circle. 1920 2172 For most string lengths, this point occurs as the doubling benefit becomes subjectively negligible. 1921 At len-500 , the amount of space needed to achieve 20\% liveness is so muchthat last-level cache misses begin occurring generating a further slowdown.2173 At len-500 (top line in plot (a)), the amount of space needed to achieve 20\% liveness is so much ($>$~4M) that last-level cache misses begin occurring generating a further slowdown. 1922 2174 This effect is an anomaly of the experimental setup trying to compare such varied sizes in one view; 1923 2175 the len-500 default point is included only to provide a holistic picture of the STL comparisons (discussed next). … … 1942 2194 Beside \CFA's principal bars, a bar for separate-compilation overhead (\emph{sep.\ comp.}) shows the benefit that STL enjoys by using monolithic compilation. 1943 2195 \CFA currently forgoes this benefit. 1944 This overhead figure was obtained by hacking a version of the \CFA string library to have a header-only implementation and measuring the resulting speed.2196 This overhead figure is obtained by hacking a version of the \CFA string library to have a header-only implementation and measuring the resulting speed. 1945 2197 The difference between separately compiled (normal) and header-only (hacked) versions is the reported overhead. 1946 2198 It represents how much \CFA could speed up if it switched to a header-only implementation to match STL. … … 1982 2234 } 1983 2235 \end{lstlisting} 2236 2237 2238 \section{Future Work} 2239 2240 \CFA strings provide an interesting set of operations for manipulating sets of characters. 2241 However, experience is needed to determine which operations are superfluous and which are missing. 2242 It is hard to find a strong string API in other general-purpose programming-languages to compare with, as most have simple string APIs, especially I/O. 2243 Supporting regular-expressions, as in text-editors and specialize string languages, \eg AWK~\cite{AWK}, Ruby~\cite{Ruby}, SNOBOL~\cite{SNOBOL}, is an important extension. 2244 String sharing also needs experience to determine if the rules for overlapping behaviour provide the right features for complex editing applications. 2245 2246 Extending \CFA strings to non-ASCII characters is also a significant upgrade required for non-Latin languages. 2247 Working with fixed-size 16 and 32-bit characters should be a straightforward extension from 8-bit characters. 2248 Variable-sized UTF-8/16/32 characters require a significant rethink for implementation. 2249 For example, high-performance manipulation may require a companion array of pointers to the variable-sized characters. 2250 2251 Finally, adding the \CC SSO trick and merging the two-part string-implementation, once the RAII problem in \CFA is fixed, will provide even better performance.
Note:
See TracChangeset
for help on using the changeset viewer.