Debian Codegraph @

js/src/jit-test/tests/heap-analysis/byteSize-of-string.js

raw
1 // |jit-test| skip-if: !getBuildConfiguration("moz-memory")
2 // Run this test only if we're using jemalloc. Other malloc implementations
3 // exhibit surprising behaviors. For example, 32-bit Fedora builds have
4 // non-deterministic allocation sizes.
5
6 // Check JS::ubi::Node::size results for strings.
7
8 // We actually hard-code specific sizes into this test, even though they're
9 // implementation details, because in practice there are only two architecture
10 // variants to consider (32-bit and 64-bit), and if these sizes change, that's
11 // something SpiderMonkey hackers really want to know; they're supposed to be
12 // stable.
13
14 gczeal(0); // Need to control when tenuring happens
15 gcparam('semispaceNurseryEnabled', 0);
16
17 // Hack to skip this test if strings are not allocated in the nursery.
18 {
19 const sample_nursery = "x" + "abc".substr(1);
20 let nursery_enabled = true;
21 const before = byteSize(sample_nursery);
22 gc();
23 const after = byteSize(sample_nursery);
24 if (before == after)
25 nursery_enabled = false;
26 if (!nursery_enabled) {
27 printErr("nursery strings appear to be disabled");
28 quit(0);
29 }
30 }
31
32 // Ion eager runs much of this code in Ion, and Ion nursery-allocates more
33 // aggressively than other modes.
34 if (getJitCompilerOptions()["ion.warmup.trigger"] <= 100)
35 setJitCompilerOption("ion.warmup.trigger", 100);
36
37 if (getBuildConfiguration("pointer-byte-size") == 4)
38 var s = (s32, s64) => s32
39 else
40 var s = (s32, s64) => s64
41
42 // Convert an input string, which is probably an atom because it's a literal in
43 // the source text, to a nursery-allocated string with the same contents. Note
44 // that the string's characters may be allocated in the nursery.
45 function copyString(str) {
46 if (str.length == 0)
47 return str; // Nothing we can do here
48 return ensureLinearString(str.substr(0, 1) + str.substr(1));
49 }
50
51 // Return the nursery byte size of |str|.
52 function nByteSize(str) {
53 // Strings that appear in the source will always be atomized and therefore
54 // will never be in the nursery.
55 return byteSize(copyString(str));
56 }
57
58 // Return the tenured byte size of |str|.
59 function tByteSize(str) {
60 // Strings that appear in the source will always be atomized and therefore
61 // will never be in the nursery. But we'll make them get tenured instead of
62 // using the atom.
63 str = copyString(str);
64 minorgc();
65 return byteSize(str);
66 }
67
68 // There are four representations of linear strings, with the following
69 // capacities:
70 //
71 // 32-bit 64-bit test
72 // representation Latin-1 char16_t Latin-1 char16_t label
73 // ========================================================================
74 // JSExternalString - limited by MaxStringLength - E
75 // JSThinInlineString 8 4 16 8 T
76 // JSFatInlineString 24 12 24 12 F
77 // ThinInlineAtom 12 6 20 10 T
78 // FatInlineAtom 20 10 20 10 F
79 // JSExtensibleString - limited by MaxStringLength - X
80
81 // Notes:
82 // - labels are suffixed with A for atoms and N for non-atoms
83 // - atoms store a 4 byte hash code, and some add to the size to adjust
84 // - Nursery-allocated strings require a header that stores the zone.
85
86 // Expected sizes based on type of string
87 const m32 = (getBuildConfiguration("pointer-byte-size") == 4);
88 const TA = m32 ? 24 : 32; // ThinInlineAtom (includes a hash value)
89 const FA = m32 ? 32 : 32; // FatInlineAtom (includes a hash value)
90 const NA = m32 ? 24 : 32; // NormalAtom
91 const TN = m32 ? 16 : 24; // ThinInlineString
92 const FN = m32 ? 32 : 32; // FatInlineString
93 const XN = m32 ? 16 : 24; // ExtensibleString, has additional storage buffer
94 const RN = m32 ? 16 : 24; // Rope
95 const DN = m32 ? 16 : 24; // DependentString
96 const EN = m32 ? 16 : 24; // ExternalString
97
98 // A function that pads out a tenured size to the nursery size. We store a zone
99 // pointer in the nursery just before the string (4 bytes on 32-bit, 8 bytes on
100 // 64-bit), and the string struct itself must be 8-byte aligned (resulting in
101 // +4 bytes on 32-bit, +0 bytes on 64-bit). The end result is that nursery
102 // strings are 8 bytes larger.
103 const Nursery = m32 ? s => s + 4 + 4 : s => s + 8 + 0;
104
105 // Latin-1
106 assertEq(tByteSize(""), s(TA, TA));
107 assertEq(tByteSize("1"), s(TA, TA));
108 assertEq(tByteSize("1234567"), s(TN, TN));
109 assertEq(tByteSize("12345678"), s(TN, TN));
110 assertEq(tByteSize("123456789"), s(FN, TN));
111 assertEq(tByteSize("123456789.12345"), s(FN, TN));
112 assertEq(tByteSize("123456789.123456"), s(FN, TN));
113 assertEq(tByteSize("123456789.1234567"), s(FN, FN));
114 assertEq(tByteSize("123456789.123456789.123"), s(FN, FN));
115 assertEq(tByteSize("123456789.123456789.1234"), s(FN, FN));
116 assertEq(tByteSize("123456789.123456789.12345"), s(XN+32, XN+32));
117 assertEq(tByteSize("123456789.123456789.123456789.1"), s(XN+32, XN+32));
118 assertEq(tByteSize("123456789.123456789.123456789.12"), s(XN+32, XN+32));
119 assertEq(tByteSize("123456789.123456789.123456789.123"), s(XN+64, XN+64));
120
121 assertEq(nByteSize(""), s(TA, TA));
122 assertEq(nByteSize("1"), s(TA, TA));
123 assertEq(nByteSize("1234567"), s(Nursery(TN), Nursery(TN)));
124 assertEq(nByteSize("12345678"), s(Nursery(TN), Nursery(TN)));
125 assertEq(nByteSize("123456789"), s(Nursery(FN), Nursery(TN)));
126 assertEq(nByteSize("123456789.12345"), s(Nursery(FN), Nursery(TN)));
127 assertEq(nByteSize("123456789.123456"), s(Nursery(FN), Nursery(TN)));
128 assertEq(nByteSize("123456789.1234567"), s(Nursery(FN), Nursery(FN)));
129 assertEq(nByteSize("123456789.123456789.123"), s(Nursery(FN), Nursery(FN)));
130 assertEq(nByteSize("123456789.123456789.1234"), s(Nursery(FN), Nursery(FN)));
131 assertEq(nByteSize("123456789.123456789.12345"), s(Nursery(XN), Nursery(XN)));
132 assertEq(nByteSize("123456789.123456789.123456789.1"), s(Nursery(XN), Nursery(XN)));
133 assertEq(nByteSize("123456789.123456789.123456789.12"), s(Nursery(XN), Nursery(XN)));
134 assertEq(nByteSize("123456789.123456789.123456789.123"), s(Nursery(XN), Nursery(XN)));
135
136 function Atom(s) { return Object.keys({ [s]: true })[0]; }
137 assertEq(byteSize(Atom("1234567")), s(TA, TA));
138 assertEq(byteSize(Atom("12345678")), s(TA, FA));
139 assertEq(byteSize(Atom("123456789.12")), s(TA, FA));
140 assertEq(byteSize(Atom("123456789.123")), s(FA, FA));
141 assertEq(byteSize(Atom("123456789.12345")), s(FA, FA));
142 assertEq(byteSize(Atom("123456789.123456")), s(FA, FA));
143 assertEq(byteSize(Atom("123456789.1234567")), s(FA, FA));
144 assertEq(byteSize(Atom("123456789.123456789.")), s(FA, FA));
145 assertEq(byteSize(Atom("123456789.123456789.1")), s(NA+32, NA+32));
146 assertEq(byteSize(Atom("123456789.123456789.123")), s(NA+32, NA+32));
147 assertEq(byteSize(Atom("123456789.123456789.1234")), s(NA+32, NA+32));
148 assertEq(byteSize(Atom("123456789.123456789.12345")), s(NA+32, NA+32));
149 assertEq(byteSize(Atom("123456789.123456789.123456789.1")), s(NA+32, NA+32));
150 assertEq(byteSize(Atom("123456789.123456789.123456789.12")), s(NA+32, NA+32));
151 assertEq(byteSize(Atom("123456789.123456789.123456789.123")), s(NA+48, NA+48));
152
153 // Inline char16_t atoms.
154 // "Impassionate gods have never seen the red that is the Tatsuta River."
155 // - Ariwara no Narihira
156 assertEq(tByteSize("千"), s(TA, TA));
157 assertEq(tByteSize("千早"), s(TN, TN));
158 assertEq(tByteSize("千早ぶ"), s(TN, TN));
159 assertEq(tByteSize("千早ぶる"), s(TN, TN));
160 assertEq(tByteSize("千早ぶる神"), s(FN, TN));
161 assertEq(tByteSize("千早ぶる神代"), s(FN, TN));
162 assertEq(tByteSize("千早ぶる神代も"), s(FN, TN));
163 assertEq(tByteSize("千早ぶる神代もき"), s(FN, TN));
164 assertEq(tByteSize("千早ぶる神代もきか"), s(FN, FN));
165 assertEq(tByteSize("千早ぶる神代もきかず龍"), s(FN, FN));
166 assertEq(tByteSize("千早ぶる神代もきかず龍田"), s(FN, FN));
167 assertEq(tByteSize("千早ぶる神代もきかず龍田川"), s(XN+32, XN+32));
168 assertEq(tByteSize("千早ぶる神代もきかず龍田川 か"), s(XN+32, XN+32));
169 assertEq(tByteSize("千早ぶる神代もきかず龍田川 から"), s(XN+32, XN+32));
170 assertEq(tByteSize("千早ぶる神代もきかず龍田川 からく"), s(XN+64, XN+64));
171 assertEq(tByteSize("千早ぶる神代もきかず龍田川 からくれなゐに水く"), s(XN+64, XN+64));
172 assertEq(tByteSize("千早ぶる神代もきかず龍田川 からくれなゐに水くく"), s(XN+64, XN+64));
173 assertEq(tByteSize("千早ぶる神代もきかず龍田川 からくれなゐに水くくるとは"), s(XN+64, XN+64));
174
175 assertEq(nByteSize("千"), s(TA, TA));
176 assertEq(nByteSize("千早"), s(Nursery(TN), Nursery(TN)));
177 assertEq(nByteSize("千早ぶ"), s(Nursery(TN), Nursery(TN)));
178 assertEq(nByteSize("千早ぶる"), s(Nursery(TN), Nursery(TN)));
179 assertEq(nByteSize("千早ぶる神"), s(Nursery(FN), Nursery(TN)));
180 assertEq(nByteSize("千早ぶる神代"), s(Nursery(FN), Nursery(TN)));
181 assertEq(nByteSize("千早ぶる神代も"), s(Nursery(FN), Nursery(TN)));
182 assertEq(nByteSize("千早ぶる神代もき"), s(Nursery(FN), Nursery(TN)));
183 assertEq(nByteSize("千早ぶる神代もきか"), s(Nursery(FN), Nursery(FN)));
184 assertEq(nByteSize("千早ぶる神代もきかず龍"), s(Nursery(FN), Nursery(FN)));
185 assertEq(nByteSize("千早ぶる神代もきかず龍田"), s(Nursery(FN), Nursery(FN)));
186 assertEq(nByteSize("千早ぶる神代もきかず龍田川"), s(Nursery(XN), Nursery(XN)));
187 assertEq(nByteSize("千早ぶる神代もきかず龍田川 か"), s(Nursery(XN), Nursery(XN)));
188 assertEq(nByteSize("千早ぶる神代もきかず龍田川 から"), s(Nursery(XN), Nursery(XN)));
189 assertEq(nByteSize("千早ぶる神代もきかず龍田川 からく"), s(Nursery(XN), Nursery(XN)));
190 assertEq(nByteSize("千早ぶる神代もきかず龍田川 からくれなゐに水く"), s(Nursery(XN), Nursery(XN)));
191 assertEq(nByteSize("千早ぶる神代もきかず龍田川 からくれなゐに水くく"), s(Nursery(XN), Nursery(XN)));
192 assertEq(nByteSize("千早ぶる神代もきかず龍田川 からくれなゐに水くくるとは"), s(Nursery(XN), Nursery(XN)));
193
194 // A Latin-1 rope. This changes size when flattened.
195 // "In a village of La Mancha, the name of which I have no desire to call to mind"
196 // - Miguel de Cervantes, Don Quixote
197 var fragment8 = "En un lugar de la Mancha, de cuyo nombre no quiero acordarme"; // 60 characters
198 var rope8 = fragment8;
199 for (var i = 0; i < 10; i++) // 1024 repetitions
200 rope8 = rope8 + rope8;
201
202 assertEq(byteSize(rope8), s(Nursery(RN), Nursery(RN)));
203 minorgc();
204 assertEq(byteSize(rope8), s(RN, RN));
205 var matches8 = rope8.match(/(de cuyo nombre no quiero acordarme)/);
206 assertEq(byteSize(rope8), s(XN + 64 * 1024, XN + 64 * 1024));
207 var ext8 = rope8; // Stop calling it what it's not (though it'll change again soon.)
208
209 // Test extensible strings.
210 //
211 // Appending another copy of the fragment should yield another rope.
212 //
213 // Flattening that should turn the original rope into a dependent string, and
214 // yield a new linear string, of the same size as the original.
215 var rope8a = ext8 + fragment8;
216 assertEq(byteSize(rope8a), s(Nursery(RN), Nursery(RN)));
217 rope8a.match(/x/, function() { assertEq(true, false); });
218 assertEq(byteSize(rope8a), s(Nursery(XN) + 65536, Nursery(XN) + 65536));
219 assertEq(byteSize(ext8), s(DN, DN));
220
221 // Latin-1 dependent strings in the nursery.
222 assertEq(byteSize(ext8.substr(1000, 2000)), s(Nursery(DN), Nursery(DN)));
223 assertEq(byteSize(matches8[0]), s(Nursery(DN), Nursery(DN)));
224 assertEq(byteSize(matches8[1]), s(Nursery(DN), Nursery(DN)));
225
226 // Tenure everything and do it again.
227 ext8 = copyString(ext8);
228 rope8a = ext8 + fragment8;
229 minorgc();
230 assertEq(byteSize(rope8a), s(RN, RN));
231 rope8a.match(/x/, function() { assertEq(true, false); });
232 assertEq(byteSize(rope8a), s(XN + 65536, XN + 65536));
233 assertEq(byteSize(rope8), s(RN, RN));
234
235 // Latin-1 tenured dependent strings.
236 function tenure(s) {
237 minorgc();
238 return s;
239 }
240 assertEq(byteSize(tenure(rope8.substr(1000, 2000))), s(DN, DN));
241 assertEq(byteSize(matches8[0]), s(DN, DN));
242 assertEq(byteSize(matches8[1]), s(DN, DN));
243
244 // A char16_t rope. This changes size when flattened.
245 // "From the Heliconian Muses let us begin to sing"
246 // --- Hesiod, Theogony
247 var fragment16 = "μουσάων Ἑλικωνιάδων ἀρχώμεθ᾽ ἀείδειν";
248 var rope16 = fragment16;
249 for (var i = 0; i < 10; i++) // 1024 repetitions
250 rope16 = rope16 + rope16;
251 assertEq(byteSize(rope16), s(Nursery(RN), Nursery(RN)));
252 let matches16 = rope16.match(/(Ἑλικωνιάδων ἀρχώμεθ᾽)/);
253 assertEq(byteSize(rope16), s(Nursery(XN) + 128 * 1024, Nursery(XN) + 128 * 1024));
254 var ext16 = rope16;
255
256 // char16_t dependent strings in the nursery.
257 assertEq(byteSize(ext16.substr(1000, 2000)), s(Nursery(DN), Nursery(DN)));
258 assertEq(byteSize(matches16[0]), s(Nursery(DN), Nursery(DN)));
259 assertEq(byteSize(matches16[1]), s(Nursery(DN), Nursery(DN)));
260
261 // Test extensible strings.
262 //
263 // Appending another copy of the fragment should yield another rope.
264 //
265 // Flattening that should turn the original rope into a dependent string, and
266 // yield a new linear string, of the some size as the original.
267 rope16a = ext16 + fragment16;
268 assertEq(byteSize(rope16a), s(Nursery(RN), Nursery(RN)));
269 rope16a.match(/x/, function() { assertEq(true, false); });
270 assertEq(byteSize(rope16a), s(Nursery(XN) + 128 * 1024, Nursery(XN) + 128 * 1024));
271 assertEq(byteSize(ext16), s(Nursery(DN), Nursery(DN)));
272
273 // Tenure everything and try again. This time it should steal the extensible
274 // characters and convert the root into an extensible string using them.
275 ext16 = copyString(ext16);
276 rope16a = ext16 + fragment16;
277 minorgc();
278 finishBackgroundFree();
279 assertEq(byteSize(rope16a), s(RN, RN));
280 rope16a.match(/x/, function() { assertEq(true, false); });
281 assertEq(byteSize(rope16a), s(XN + 128 * 1024, XN + 128 * 1024));
282 assertEq(byteSize(ext16), s(RN, RN));
283
284 // Test external strings.
285 //
286 // We only support char16_t external strings and external strings are never
287 // allocated in the nursery. If this ever changes, please add tests for the new
288 // cases. Also note that on Windows mozmalloc's smallest allocation size is
289 // two words compared to one word on other platforms.
290 if (getBuildConfiguration("windows")) {
291 assertEq(byteSize(newString("", {external: true})), s(EN+8, EN+16));
292 assertEq(byteSize(newString("1", {external: true})), s(EN+8, EN+16));
293 assertEq(byteSize(newString("12", {external: true})), s(EN+8, EN+16));
294 assertEq(byteSize(newString("123", {external: true})), s(EN+8, EN+16));
295 assertEq(byteSize(newString("1234", {external: true})), s(EN+8, EN+16));
296 } else {
297 assertEq(byteSize(newString("", {external: true})), s(EN+4, EN+8));
298 assertEq(byteSize(newString("1", {external: true})), s(EN+4, EN+8));
299 assertEq(byteSize(newString("12", {external: true})), s(EN+4, EN+8));
300 assertEq(byteSize(newString("123", {external: true})), s(EN+8, EN+8));
301 assertEq(byteSize(newString("1234", {external: true})), s(EN+8, EN+8));
302 }
303 assertEq(byteSize(newString("12345", {external: true})), s(EN+16, EN+16));
304 assertEq(byteSize(newString("123456789.123456789.1234", {external: true})), s(EN+48, EN+48));
305 assertEq(byteSize(newString("123456789.123456789.12345", {external: true})), s(EN+64, EN+64));
306
307 // Nursery-allocated chars.
308 //
309 // byteSize will not include the space used by the nursery for the chars.
310 assertEq(byteSize(newString("123456789.123456789.12345")), s(Nursery(XN)+0,Nursery(XN)+0));
311 assertEq(byteSize(newString("123456789.123456789.123456789.123")), s(Nursery(XN)+0,Nursery(XN)+0));