| 1 |
// |jit-test| skip-if: !('stringRepresentation' in this) |
| 2 |
|
| 3 |
// This is to test the correctness of the string deduplication algorithm during |
| 4 |
// the tenuring phase. Same strings below refer to the same character encoding |
| 5 |
// (either latin1 or twobyte) and the same characters. |
| 6 |
|
| 7 |
// Tests: |
| 8 |
// 1. Same strings with same flags and zones should be deduplicated for |
| 9 |
// all linear strings except atoms and external strings. |
| 10 |
// 2. Same strings, but from different zones should not be deduplicated. |
| 11 |
// 3. Same strings, but with different flags should not be deduplicated. |
| 12 |
|
| 13 |
// We require predictable GC timing to make sure the correct |
| 14 |
// strings are tenured together. |
| 15 |
gczeal(0); |
| 16 |
gcparam('semispaceNurseryEnabled', 0); |
| 17 |
|
| 18 |
var helperCode = ` |
| 19 |
function makeInlineStr(isLatin1) { |
| 20 |
var s = isLatin1 ? "123456789*1" : "一二三"; |
| 21 |
return s + s; |
| 22 |
} |
| 23 |
|
| 24 |
// Generic linear strings are non-atom, non-extensible, non-inline |
| 25 |
// linear strings. |
| 26 |
// Generic linear strings can only have latin1 characters. |
| 27 |
function makeGenericLinearStr() { |
| 28 |
return notes(() => 1); |
| 29 |
} |
| 30 |
|
| 31 |
function makeRopeStr(isLatin1) { |
| 32 |
var left = isLatin1 ? "1" : "一"; |
| 33 |
var right = isLatin1 ? "123456789*123456789*123456" : |
| 34 |
"一二三四五六七八九*一二三四五六七八"; |
| 35 |
return left + right; |
| 36 |
} |
| 37 |
|
| 38 |
function makeExtensibleStr(isLatin1) { |
| 39 |
var r = makeRopeStr(isLatin1); |
| 40 |
ensureLinearString(r); |
| 41 |
return r; |
| 42 |
} |
| 43 |
|
| 44 |
function makeExtensibleStrFrom(str) { |
| 45 |
var left = str.substr(0, str.length/2); |
| 46 |
var right = str.substr(str.length/2, str.length); |
| 47 |
var ropeStr = left + right; |
| 48 |
return ensureLinearString(ropeStr); |
| 49 |
} |
| 50 |
|
| 51 |
function makeDependentStr(isLatin1) { |
| 52 |
var e = makeExtensibleStr(isLatin1); |
| 53 |
var r1 = e + "!"; |
| 54 |
var r2 = e + r1; |
| 55 |
ensureLinearString(r2); |
| 56 |
return r1; |
| 57 |
} |
| 58 |
|
| 59 |
function makeDependentStrFrom(str) { |
| 60 |
var e = makeExtensibleStrFrom(str); |
| 61 |
var r1 = e.substr(0, e.length/2) + e.substr(e.length/2, e.length); |
| 62 |
var r2 = e + r1; |
| 63 |
ensureLinearString(r2); |
| 64 |
return r1; |
| 65 |
} |
| 66 |
|
| 67 |
function makeExternalStr(isLatin1) { |
| 68 |
return isLatin1 ? newString("12345678", {external: true}) : |
| 69 |
newString("一二三", {external: true}); |
| 70 |
} |
| 71 |
|
| 72 |
function tenureStringsWithSameChars(str1, str2, isDeduplicatable) { |
| 73 |
minorgc(); |
| 74 |
assertEq(stringRepresentation(str1) == stringRepresentation(str2), |
| 75 |
isDeduplicatable); |
| 76 |
} |
| 77 |
|
| 78 |
function assertDiffStrRepAfterMinorGC(g1, g2) { |
| 79 |
minorgc(); |
| 80 |
g1.eval(\`strRep = stringRepresentation(str);\`); |
| 81 |
g2.eval(\`strRep = stringRepresentation(str);\`); |
| 82 |
assertEq(g1.strRep == g2.strRep, false); |
| 83 |
} |
| 84 |
`; |
| 85 |
|
| 86 |
eval(helperCode); |
| 87 |
|
| 88 |
// test1: |
| 89 |
// Same strings with same flags and zones should be deduplicated for all linear |
| 90 |
// strings except atoms, external strings. |
| 91 |
function test1(isLatin1) { |
| 92 |
const isDeduplicatable = true; |
| 93 |
|
| 94 |
// Deduplicatable: |
| 95 |
// --> Inline Strings |
| 96 |
var str1 = makeInlineStr(isLatin1); |
| 97 |
var str2 = makeInlineStr(isLatin1); |
| 98 |
tenureStringsWithSameChars(str1, str2, isDeduplicatable); |
| 99 |
|
| 100 |
// --> Extensible Strings |
| 101 |
str1 = makeExtensibleStr(isLatin1); |
| 102 |
str2 = makeExtensibleStr(isLatin1); |
| 103 |
tenureStringsWithSameChars(str1, str2, isDeduplicatable); |
| 104 |
|
| 105 |
// --> Dependent Strings |
| 106 |
str1 = makeDependentStr(isLatin1); |
| 107 |
str2 = makeDependentStr(isLatin1); |
| 108 |
tenureStringsWithSameChars(str1, str2, isDeduplicatable); |
| 109 |
|
| 110 |
// --> Generic Linear Strings |
| 111 |
if (isLatin1) { |
| 112 |
var str1 = makeGenericLinearStr(); |
| 113 |
var str2 = makeGenericLinearStr(); |
| 114 |
tenureStringsWithSameChars(str1, str2, isDeduplicatable); |
| 115 |
} |
| 116 |
|
| 117 |
// Non-Deduplicatable: |
| 118 |
// --> Rope Strings |
| 119 |
str1 = makeRopeStr(isLatin1); |
| 120 |
str2 = makeRopeStr(isLatin1); |
| 121 |
tenureStringsWithSameChars(str1, str2, !isDeduplicatable); |
| 122 |
|
| 123 |
// --> Atom strings are deduplicated already but not through string |
| 124 |
// deduplication during tenuring. |
| 125 |
|
| 126 |
// --> External strings are not nursery allocated. |
| 127 |
} |
| 128 |
|
| 129 |
// test2: |
| 130 |
// Same strings, but from different zones should not be deduplicated. |
| 131 |
function test2(isLatin1) { |
| 132 |
var g1 = newGlobal({ newCompartment: true }); |
| 133 |
var g2 = newGlobal({ newCompartment: true }); |
| 134 |
|
| 135 |
g1.eval(helperCode); |
| 136 |
g2.eval(helperCode); |
| 137 |
|
| 138 |
// --> Inline Strings |
| 139 |
g1.eval(`var str = makeInlineStr(${isLatin1}); `); |
| 140 |
g2.eval(`var str = makeInlineStr(${isLatin1}); `); |
| 141 |
assertDiffStrRepAfterMinorGC(g1, g2); |
| 142 |
|
| 143 |
// --> Extensible Strings |
| 144 |
g1.eval(`str = makeExtensibleStr(${isLatin1}); `); |
| 145 |
g2.eval(`str = makeExtensibleStr(${isLatin1}); `); |
| 146 |
assertDiffStrRepAfterMinorGC(g1, g2); |
| 147 |
|
| 148 |
// --> Dependent Strings |
| 149 |
g1.eval(`str = makeDependentStr(${isLatin1}); `); |
| 150 |
g2.eval(`str = makeDependentStr(${isLatin1}); `); |
| 151 |
assertDiffStrRepAfterMinorGC(g1, g2); |
| 152 |
|
| 153 |
// --> Generic Linear Strings |
| 154 |
if (isLatin1) { |
| 155 |
g1.eval(`str = makeGenericLinearStr();`); |
| 156 |
g2.eval(`str = makeGenericLinearStr();`); |
| 157 |
assertDiffStrRepAfterMinorGC(g1, g2); |
| 158 |
} |
| 159 |
} |
| 160 |
|
| 161 |
// test3: |
| 162 |
// Same strings, but with different flags should not be deduplicated. |
| 163 |
function test3(isLatin1) { |
| 164 |
const isDeduplicatable = true; |
| 165 |
|
| 166 |
// --> Dependent String and Extensible String |
| 167 |
var dependentStr = makeDependentStr(isLatin1); |
| 168 |
var extensibleStr = makeExtensibleStrFrom(dependentStr); |
| 169 |
tenureStringsWithSameChars(dependentStr, extensibleStr, !isDeduplicatable); |
| 170 |
|
| 171 |
if (isLatin1) { |
| 172 |
// --> Generic Linear String and Extensible String |
| 173 |
var genericLinearStr = makeGenericLinearStr(); |
| 174 |
var extensibleStr = makeExtensibleStrFrom(genericLinearStr); |
| 175 |
tenureStringsWithSameChars( |
| 176 |
genericLinearStr, |
| 177 |
extensibleStr, |
| 178 |
!isDeduplicatable |
| 179 |
); |
| 180 |
|
| 181 |
// --> Generic Linear String and Dependent String |
| 182 |
var dependentStr = makeDependentStrFrom(genericLinearStr); |
| 183 |
tenureStringsWithSameChars( |
| 184 |
dependentStr, |
| 185 |
genericLinearStr, |
| 186 |
!isDeduplicatable |
| 187 |
); |
| 188 |
} |
| 189 |
|
| 190 |
// --> Inline strings are too short to have the same chars as the extensible |
| 191 |
// strings, generic linear strings and dependent strings |
| 192 |
} |
| 193 |
|
| 194 |
function runTests() { |
| 195 |
var charEncoding = { TWOBYTE: 0, LATIN1: 1 }; |
| 196 |
|
| 197 |
test1(charEncoding.TWOBYTE); |
| 198 |
test2(charEncoding.TWOBYTE); |
| 199 |
test3(charEncoding.TWOBYTE); |
| 200 |
|
| 201 |
test1(charEncoding.LATIN1); |
| 202 |
test2(charEncoding.LATIN1); |
| 203 |
test3(charEncoding.LATIN1); |
| 204 |
} |
| 205 |
|
| 206 |
runTests(); |