forked from facebook/hermes
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSupportTest.cpp
More file actions
709 lines (668 loc) · 18.1 KB
/
SupportTest.cpp
File metadata and controls
709 lines (668 loc) · 18.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
/*
* Copyright (c) Facebook, Inc. and its affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
#include "hermes/BCGen/HBC/ConsecutiveStringStorage.h"
#include "hermes/BCGen/HBC/UniquingStringLiteralTable.h"
#include "hermes/Support/OSCompat.h"
#include "hermes/Support/RegExpSerialization.h"
#include "hermes/VM/Predefined.h"
#include "gtest/gtest.h"
#include <cctype>
#include <string>
#include <vector>
using namespace hermes;
using hermes::oscompat::to_string;
using llvh::StringRef;
namespace {
TEST(StringStorageTest, UniquingRegExpTest) {
// Helper to create nonsense "bytecode" for a string.
auto makeRegExp = [](const char *pat, const char *flags) -> CompiledRegExp {
auto re = CompiledRegExp::tryCompile(pat, flags);
EXPECT_TRUE(re.hasValue());
return std::move(*re);
};
UniquingRegExpTable URET;
auto idx0 = URET.addRegExp(makeRegExp("abc", "m"));
auto idx1 = URET.addRegExp(makeRegExp("def", "m"));
auto idx2 = URET.addRegExp(makeRegExp("abc", ""));
auto idx3 = URET.addRegExp(makeRegExp("abc", "m"));
EXPECT_EQ(0u, idx0);
EXPECT_EQ(1u, idx1);
EXPECT_EQ(2u, idx2);
EXPECT_EQ(0u, idx3);
}
TEST(StringStorageTest, GetStringFromEntryTest) {
std::vector<llvh::StringRef> strings = {"alpha", "beta", "", "\xE2\x84\xAB"};
hbc::ConsecutiveStringStorage storage{strings};
std::string utf8;
for (uint32_t i = 0; i < strings.size(); i++) {
EXPECT_EQ(strings[i], storage.getStringAtIndex(i, utf8));
}
}
TEST(StringStorageTest, ConsecutiveStringStorageTest) {
hbc::UniquingStringLiteralAccumulator USLA;
USLA.addString("hello", /* isIdentifier */ false);
USLA.addString("hello", /* isIdentifier */ false);
USLA.addString("world", /* isIdentifier */ false);
USLA.addString("some string", /* isIdentifier */ false);
USLA.addString("", /* isIdentifier */ false);
USLA.addString("hello", /* isIdentifier */ false);
hbc::StringLiteralTable SLT =
hbc::UniquingStringLiteralAccumulator::toTable(std::move(USLA));
EXPECT_EQ(SLT.count(), 4);
EXPECT_EQ(SLT.getStringID(""), 0);
EXPECT_EQ(SLT.getStringID("hello"), 1);
EXPECT_EQ(SLT.getStringID("some string"), 2);
EXPECT_EQ(SLT.getStringID("world"), 3);
std::string result;
const char *hex = "0123456789ABCDEF";
for (char ch : SLT.acquireStringStorage()) {
if (std::isalpha(ch)) {
result += ch;
} else {
result += "\\x";
result += hex[ch & 0xf];
result += hex[ch >> 4];
}
}
EXPECT_EQ(result, "hellosome\\x02stringworld");
}
TEST(StringStorageTest, PackingStringStorageTest) {
std::vector<llvh::StringRef> strings{"phab", "alphabet", "soup", "ou"};
hbc::ConsecutiveStringStorage storage(strings);
const auto data = storage.acquireStringStorage();
llvh::StringRef dataAsStr((const char *)data.data(), data.size());
EXPECT_EQ(dataAsStr.str(), "phabalphabetsoupou");
}
// Optimized string packing helper.
// We test that our storage does indeed contain strings at the location they
// claim, and that it is shorter than naive packing would imply.
// \param baseStrings is not empty when testing delta optimizing mode; we
// first construct ConsecutiveStringStorage from baseStrings to simulate that
// from the base bytecode, then pass it to the new ConsecutiveStringStorage.
static void test1OptimizingStringStorage(
llvh::ArrayRef<llvh::StringRef> strings,
int line,
bool expectSmaller = true) {
std::string info = " from test on line " + to_string(line);
std::unique_ptr<hbc::ConsecutiveStringStorage> baseConsecutiveStrStorage;
hbc::ConsecutiveStringStorage storage(strings, true /* optimize */);
auto index = storage.acquireStringTable();
auto data = storage.acquireStringStorage();
llvh::StringRef dataAsString((const char *)data.data(), data.size());
size_t idx = 0;
for (const auto &p : index) {
uint32_t offset = p.getOffset();
uint32_t length = p.getLength();
EXPECT_TRUE(offset + length >= offset && offset + length <= data.size())
<< " idx " << idx << info;
EXPECT_EQ(
dataAsString.slice(offset, offset + length).str(), strings[idx].str())
<< " idx " << idx << info;
idx++;
}
// We should have consumed all strings.
EXPECT_EQ(idx, strings.size()) << info;
size_t naiveLength = 0;
for (auto str : strings)
naiveLength += str.size();
if (expectSmaller) {
EXPECT_LT(data.size(), naiveLength) << info;
} else {
EXPECT_LE(data.size(), naiveLength) << info;
}
}
#define TEST_1_OPTIMIZING_STRING_STORAGE(s1, ...) \
test1OptimizingStringStorage({s1, __VA_ARGS__}, __LINE__)
TEST(StringStorageTest, Optimizing) {
TEST_1_OPTIMIZING_STRING_STORAGE("phab", "alphabet", "soup", "ou");
TEST_1_OPTIMIZING_STRING_STORAGE(
"ellitlavehr",
"ranmellit",
"nationsecern",
"octpifine",
"damnation",
"octoutrun",
"recoct",
"utrunsecpar",
"apteran",
"avehr",
"ampe",
"dampedslyish",
"unsticky",
"reconsider",
"octhyloid",
"inetauten",
"nlytelugu",
"derdamped",
"nsi",
"inepanaka",
"dershivoo",
"vooopenly",
"ernatrypa",
"secparflaith",
"ecoc",
"octpeseta",
"nationachime",
"ationremass");
TEST_1_OPTIMIZING_STRING_STORAGE(
"tinfibrin",
"tinach",
"ien",
"urikeelie",
"eeliebandog",
"ssushydroa",
"nonarcing",
"eliekirmew",
"dogorienthostie",
"cerebrology",
"ienzymeoxhead",
"fibrinfueler",
"ebandogorient",
"xheadmegrel",
"inerreason",
"uriviolal",
"gdrawerpeptic",
"ealprancy",
"mammilliform",
"nerimmane",
"centauri",
"tinachill",
"plugdrawer",
"hartin",
"rcingveiner",
"rmew",
"rmewupseal",
"taurienzyme",
"oryssus");
TEST_1_OPTIMIZING_STRING_STORAGE(
"torula",
"hecalpardaobeeish",
"trothecalpardao",
"calbricky",
"nellatecultch",
"ialretold",
"gastrothecal",
"eitfacula",
"opticist",
"vernier",
"cky",
"ulauploop",
"cultchprefab",
"ulabeloam",
"cistwetted",
"crenellate",
"ementrhexis",
"cementrhexis",
"embracement",
"efa",
"lbric",
"ula",
"custodial",
"cistdeceit",
"oldevince");
TEST_1_OPTIMIZING_STRING_STORAGE(
"dhistenog",
"olithprotea",
"concerningly",
"userhallow",
"rangeman",
"ophilebackie",
"roadingtopply",
"paus",
"eback",
"shiner",
"atokapauser",
"emanadance",
"discolith",
"railroading",
"linguneven",
"dingunlock",
"photophile",
"rote",
"keeling",
"pauserlaxism",
"ply",
"hilebatoka",
"manawadhi",
"pau");
TEST_1_OPTIMIZING_STRING_STORAGE(
"tpopatwirl",
"rataurheen",
"ingupline",
"unclimb",
"separata",
"yangoftest",
"uanwigger",
"appboyang",
"dewanship",
"appafraid",
"erabrookrohuna",
"paganistic",
"wiggerslough",
"raidoutpop",
"moeritherian",
"shiploudly",
"rabrookrohun",
"ookfaluns",
"poparnaut",
"loudlydawish",
"ticgenapp",
"unattaining",
"ining",
"unsunspun",
"iggerabrook",
"suluan",
"pun",
"moeri",
"moer",
"herianoodles");
TEST_1_OPTIMIZING_STRING_STORAGE(
"larickfisher",
"laricksulcal",
"sulcalpallor",
"iwi",
"nistretook",
"forthgo",
"etookhowlet",
"iwibaroco",
"lde",
"nistlarick",
"inacrutch",
"kiwiangina",
"tretookisolde",
"rocobronzy",
"redfin",
"ollercantle",
"kiwikiwi",
"annexation",
"normanist",
"thgosialis",
"ronzyogboni",
"gin",
"lor",
"ookhagged",
"sherkoller",
"anginainjure",
"anistfemora",
"nginasicula");
TEST_1_OPTIMIZING_STRING_STORAGE(
"oist",
"unswallowable",
"uplaid",
"ipposeroot",
"ocerialkiaugh",
"keratodermia",
"rmiasmugly",
"cerialmoisty",
"llumcultic",
"culticstarer",
"scabellum",
"nerfacial",
"bus",
"lai",
"icpimplasoekoe",
"recruity",
"rhinocerial",
"maybush",
"xiidbejade",
"plai",
"styahimsa",
"mainerdolium",
"almoistybecram",
"mortmainer",
"ushsheeny",
"dbejadeulidia",
"acialcixiid",
"eenysachet",
"facialhallah",
"ityshippo",
"culticpimpla",
"ulticbrevet");
TEST_1_OPTIMIZING_STRING_STORAGE(
"lilycrakow",
"arklebutoxy",
"echuca",
"tleblotch",
"icrol",
"trotlet",
"letjoiner",
"tletwaggel",
"rklethirty",
"astli",
"onapebb",
"monapebbly",
"ghastlily",
"nerassise",
"ybillerpicrol",
"ucaachage",
"ilyrunted",
"rtytantle",
"sslycardia",
"irtybiller",
"crakowholmic",
"ycrakowcabana",
"anashewel",
"rakowholm",
"omonarefuge",
"helplessly",
"letcorema",
"toromona",
"kowholmicdarkle");
TEST_1_OPTIMIZING_STRING_STORAGE(
"insal",
"becpratey",
"gingrilawa",
"eeri",
"arto",
"sheering",
"chyverdoy",
"untinsaluki",
"botchy",
"chiralmuntin",
"teymothed",
"ingchebec",
"hiralpollam",
"pugging",
"ralmuntinaltaid",
"shelfroom",
"gingrefuel",
"lukiknolly",
"allochiral",
"ollythrust",
"heeri",
"chartometer",
"eri");
TEST_1_OPTIMIZING_STRING_STORAGE(
"celizchak",
"soakeritonia",
"derwi",
"thrailcomply",
"thraileloign",
"indleintort",
"assonant",
"soakerzombie",
"celizc",
"laviclawful",
"ortthrail",
"antflavic",
"windbindle",
"astoncodder",
"baculi",
"antboruca",
"ance",
"ncelblowzy",
"windbodier",
"blowzyastare",
"underwind",
"soakerflurry",
"ndbodi",
"spancel",
"haksoaker",
"oniamythus",
"antbaston",
"odde");
TEST_1_OPTIMIZING_STRING_STORAGE(
"pingbutyne",
"idalrerope",
"ephalusstriae",
"dalre",
"midoidaltrusty",
"tocephalusthemer",
"horseback",
"gustus",
"pyramidoidal",
"bekinkinite",
"alust",
"alusst",
"tipproof",
"tynethakur",
"methylosis",
"leptocephalus",
"siscachou",
"ethylosis",
"initeoculus",
"oidaltrus",
"hooping",
"cachou",
"ebackkegler",
"sebackcavity",
"ylosisunrobe");
TEST_1_OPTIMIZING_STRING_STORAGE(
"hedoleose",
"antproof",
"regenesis",
"dreary",
"swingedalraun",
"esiscreepy",
"enesiswinged",
"talgratis",
"marital",
"hedstolae",
"reedtunica",
"esisbreezy",
"untwitched",
"edo",
"ezy",
"aryhaikal",
"arydebate",
"osejereed");
TEST_1_OPTIMIZING_STRING_STORAGE(
"eatresysteigh",
"risdungol",
"manshivey",
"eryfrasco",
"eamrodent",
"morris",
"ogeejai",
"geemagnum",
"ogeejailer",
"lenvoihaffet",
"gra",
"resydebate",
"rislenvoi",
"haffe",
"hypogee",
"erygraped",
"treamcitric",
"apedreveal",
"dentbowery",
"ogeeatresy",
"anatolian",
"restream",
"odentteaman",
"resyrobing");
TEST_1_OPTIMIZING_STRING_STORAGE(
"eterron",
"eerfulwisher",
"pinacle",
"illmacusi",
"spenc",
"illoraler",
"handbill",
"essdietal",
"stmazucacytost",
"lete",
"rolistmazuca",
"stmazucacuorin",
"oralermirage",
"ucacuori",
"petrolist",
"cleterron",
"inbokarktettix",
"acuorinbokark",
"uncheerful",
"linten",
"fungological",
"raler",
"illspence",
"billstibic",
"rron",
"slimishness");
TEST_1_OPTIMIZING_STRING_STORAGE(
"daceloninae",
"mismkismet",
"semiminor",
"mismkis",
"basicburrah",
"blepharocera",
"minorrinker",
"alanoctroy",
"ker",
"sanguinity",
"smetnargil",
"ismabasic",
"aeatulwar",
"eogaea",
"smkis",
"uinitytwaddy",
"asicgallon",
"agecigala",
"argilniello",
"crustalogical",
"alanviolet",
"noctroyrannel",
"inaehavage",
"atomism",
"phthalan",
"kismetmalati",
"pharoc");
TEST_1_OPTIMIZING_STRING_STORAGE(
"ina",
"rchamomis",
"fulurinal",
"rustful",
"descar",
"unstripped",
"liere",
"oyoreback",
"gerfaucet",
"ruelstamoyo",
"rippedcruels",
"schlieren",
"fulinarch",
"ust",
"yoreb",
"ppedescarp",
"pilger",
"omisminium",
"miniummoiley",
"deepmost",
"amomisminium",
"ucetminyan",
"lierendrafty");
}
// Ensure we don't hang on very long strings.
TEST(StringStorageTest, NoHang) {
std::string s1(16 * 1024 * 1024, 'a');
std::string s2(16 * 1024 * 1024, 'b');
llvh::StringRef sr1 = s1;
llvh::StringRef sr2 = s2;
test1OptimizingStringStorage({sr1, sr2}, __LINE__, false);
}
/// \return The table resulting from adding the strings in \p strings into the
/// accumulator \p accum (defaults to empty), and converting it into a table
/// with optimizations enabled.
hbc::StringLiteralTable tableForStrings(
llvh::ArrayRef<llvh::StringRef> strings,
hbc::UniquingStringLiteralAccumulator accum = {}) {
for (auto str : strings) {
accum.addString(str, /* isIdentifier */ false);
}
return hbc::UniquingStringLiteralAccumulator::toTable(
std::move(accum), /* optimize */ true);
}
TEST(StringStorageTest, DeltaOptimizingModeTest) {
std::vector<llvh::StringRef> baseStrings = {
"ellitlavehr", "ranmellit", "nationsecern", "octpifine",
"damnation", "octoutrun", "recoct", "utrunsecpar",
"apteran", "avehr", "ampe", "dampedslyish",
"unsticky", "reconsider", "octhyloid", "inetauten",
"nlytelugu", "derdamped", "nsi", "inepanaka",
"dershivoo", "vooopenly", "ernatrypa", "secparflaith",
"ecoc", "octpeseta", "nationachime", "ationremass"};
auto baseTable = tableForStrings(baseStrings);
std::vector<unsigned char> baseBuffer = baseTable.acquireStringStorage();
std::vector<StringTableEntry> baseEntries = baseTable.acquireStringTable();
// Create a new table starting with the base storage.
std::vector<llvh::StringRef> newStrings = {
"ina", "rchamomis", "fulurinal", "rustful",
"descar", "unstripped", "liere", "oyoreback",
"gerfaucet", "ruelstamoyo", "rippedcruels", "schlieren",
"fulinarch", "ust", "yoreb", "ppedescarp",
"pilger", "omisminium", "miniummoiley", "deepmost",
"amomisminium", "ucetminyan", "lierendrafty"};
hbc::UniquingStringLiteralAccumulator baseAccumulator{
hbc::ConsecutiveStringStorage{std::vector<StringTableEntry>{baseEntries},
std::vector<unsigned char>{baseBuffer}},
std::vector<bool>(baseEntries.size(), false)};
auto newTable = tableForStrings(newStrings, std::move(baseAccumulator));
// Verify that the new storage buffer starts with the base storage buffer.
auto newBuffer = newTable.acquireStringStorage();
ASSERT_TRUE(newBuffer.size() > baseBuffer.size());
EXPECT_TRUE(
std::equal(baseBuffer.begin(), baseBuffer.end(), newBuffer.begin()));
// Verify that all base IDs are the same.
for (auto str : baseStrings) {
EXPECT_EQ(baseTable.getStringID(str), newTable.getStringID(str));
}
}
TEST(StringAccumulatorTest, Ordering) {
// When outputing a string storage instance, the accumulator sorts its index
// entries. First strings get grouped into "frequency classes". The first
// group contains the top 2^8 strings by number of usages of that string as
// an identifier, the second group is the next (2^16 - 2^8) hottest strings,
// and the last group is all the remaining strings. Then within each class,
// strings are further subdivided into the following categories (emitted in
// the given order):
//
// - Strings that are not identifiers.
// - Strings that are identifiers.
//
// Finally, within each category, strings are sorted first by the offset of
// their character buffer in the string storage, and then by their size.
//
// This test verified the grouping into categories.
hbc::UniquingStringLiteralAccumulator USLA;
USLA.addString("Object", /* isIdentifier */ true);
USLA.addString("String", /* isIdentifier */ false);
USLA.addString("Id0", /* isIdentifier */ true);
USLA.addString("Str0", /* isIdentifier */ false);
USLA.addString("Str1", /* isIdentifier */ false);
USLA.addString("Id1", /* isIdentifier */ true);
USLA.addString("Function", /* isIdentifier */ true);
USLA.addString("Str2", /* isIdentifier */ false);
USLA.addString("Id2", /* isIdentifier */ true);
auto SLT = hbc::UniquingStringLiteralAccumulator::toTable(
std::move(USLA), /* optimize */ false);
std::vector<llvh::StringRef> expectedStrings{
"Str0",
"Str1",
"Str2",
"String",
"Function",
"Id0",
"Id1",
"Id2",
"Object",
};
for (size_t i = 0; i < expectedStrings.size(); ++i) {
EXPECT_EQ(i, SLT.getStringID(expectedStrings[i])) << "at index " << i;
}
std::vector<StringKind::Entry> expectedKinds{
{StringKind::String, 4},
{StringKind::Identifier, 5},
};
auto actualKinds = SLT.getStringKinds();
EXPECT_EQ(actualKinds.size(), expectedKinds.size());
for (size_t i = 0; i < expectedKinds.size(); ++i) {
EXPECT_EQ(expectedKinds[i].kind(), actualKinds[i].kind())
<< "at index " << i;
EXPECT_EQ(expectedKinds[i].count(), actualKinds[i].count())
<< "at index " << i;
}
}
} // end anonymous namespace