summaryrefslogtreecommitdiffstats
path: root/js/src/jit-test/tests/gc/deduplicateTenuringStrings.js
blob: 1b8259cc15299547fe2bb0f4207d8acd1ca47f45 (plain)
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
// |jit-test| skip-if: !('stringRepresentation' in this)

// This is to test the correctness of the string deduplication algorithm during
// the tenuring phase. Same strings below refer to the same character encoding
// (either latin1 or twobyte) and the same characters.

// Tests:
// 1. Same strings with same flags and zones should be deduplicated for
// all linear strings except atoms and external strings.
// 2. Same strings, but from different zones should not be deduplicated.
// 3. Same strings, but with different flags should not be deduplicated.

// We require predictable GC timing to make sure the correct
// strings are tenured together.
gczeal(0);

var helperCode = `
function makeInlineStr(isLatin1) {
  var s = isLatin1 ? "123456789*1" : "一二三";
  return s + s;
}

// Generic linear strings are non-atom, non-extensible, non-inline
// linear strings.
// Generic linear strings can only have latin1 characters.
function makeGenericLinearStr() {
  return notes(() => 1);
}

function makeRopeStr(isLatin1) {
  var left = isLatin1 ? "1" : "一";
  var right = isLatin1 ? "123456789*123456789*123456" :
    					   "一二三四五六七八九*一二三四五六七八";
  return left + right;
}

function makeExtensibleStr(isLatin1) {
  var r = makeRopeStr(isLatin1);
  ensureLinearString(r);
  return r;
}

function makeExtensibleStrFrom(str) {
  var left = str.substr(0, str.length/2);
  var right = str.substr(str.length/2, str.length);
  var ropeStr = left + right;
  return ensureLinearString(ropeStr);
}

function makeDependentStr(isLatin1) {
  var e = makeExtensibleStr(isLatin1);
  var r1 = e + "!";
  var r2 = e + r1;
  ensureLinearString(r2);
  return r1;
}

function makeDependentStrFrom(str) {
  var e = makeExtensibleStrFrom(str);
  var r1 = e.substr(0, e.length/2) + e.substr(e.length/2, e.length);
  var r2 = e + r1;
  ensureLinearString(r2);
  return r1;
}

function makeExternalStr(isLatin1) {
  return isLatin1 ? newString("12345678", {external: true}) :
                    newString("一二三", {external: true});
}

function tenureStringsWithSameChars(str1, str2, isDeduplicatable) {
  minorgc();
  assertEq(stringRepresentation(str1) == stringRepresentation(str2),
	   isDeduplicatable);
}

function assertDiffStrRepAfterMinorGC(g1, g2) {
  minorgc();
  g1.eval(\`strRep = stringRepresentation(str);\`);
  g2.eval(\`strRep = stringRepresentation(str);\`);
  assertEq(g1.strRep == g2.strRep, false);
}
`;

eval(helperCode);

// test1:
// Same strings with same flags and zones should be deduplicated for all linear
// strings except atoms, external strings.
function test1(isLatin1) {
  const isDeduplicatable = true;

  // Deduplicatable:
  // --> Inline Strings
  var str1 = makeInlineStr(isLatin1);
  var str2 = makeInlineStr(isLatin1);
  tenureStringsWithSameChars(str1, str2, isDeduplicatable);

  // --> Extensible Strings
  str1 = makeExtensibleStr(isLatin1);
  str2 = makeExtensibleStr(isLatin1);
  tenureStringsWithSameChars(str1, str2, isDeduplicatable);

  // --> Dependent Strings
  str1 = makeDependentStr(isLatin1);
  str2 = makeDependentStr(isLatin1);
  tenureStringsWithSameChars(str1, str2, isDeduplicatable);

  // --> Generic Linear Strings
  if (isLatin1) {
    var str1 = makeGenericLinearStr();
    var str2 = makeGenericLinearStr();
    tenureStringsWithSameChars(str1, str2, isDeduplicatable);
  }

  // Non-Deduplicatable:
  // --> Rope Strings
  str1 = makeRopeStr(isLatin1);
  str2 = makeRopeStr(isLatin1);
  tenureStringsWithSameChars(str1, str2, !isDeduplicatable);

  // --> Atom strings are deduplicated already but not through string
  // deduplication during tenuring.

  // --> External strings are not nursery allocated.
}

// test2:
// Same strings, but from different zones should not be deduplicated.
function test2(isLatin1) {
  var g1 = newGlobal({ newCompartment: true });
  var g2 = newGlobal({ newCompartment: true });

  g1.eval(helperCode);
  g2.eval(helperCode);

  // --> Inline Strings
  g1.eval(`var str = makeInlineStr(${isLatin1}); `);
  g2.eval(`var str = makeInlineStr(${isLatin1}); `);
  assertDiffStrRepAfterMinorGC(g1, g2);

  // --> Extensible Strings
  g1.eval(`str = makeExtensibleStr(${isLatin1}); `);
  g2.eval(`str = makeExtensibleStr(${isLatin1}); `);
  assertDiffStrRepAfterMinorGC(g1, g2);

  // --> Dependent Strings
  g1.eval(`str = makeDependentStr(${isLatin1}); `);
  g2.eval(`str = makeDependentStr(${isLatin1}); `);
  assertDiffStrRepAfterMinorGC(g1, g2);

  // --> Generic Linear Strings
  if (isLatin1) {
    g1.eval(`str = makeGenericLinearStr();`);
    g2.eval(`str = makeGenericLinearStr();`);
    assertDiffStrRepAfterMinorGC(g1, g2);
  }
}

// test3:
// Same strings, but with different flags should not be deduplicated.
function test3(isLatin1) {
  const isDeduplicatable = true;

  // --> Dependent String and Extensible String
  var dependentStr = makeDependentStr(isLatin1);
  var extensibleStr = makeExtensibleStrFrom(dependentStr);
  tenureStringsWithSameChars(dependentStr, extensibleStr, !isDeduplicatable);

  if (isLatin1) {
    // --> Generic Linear String and Extensible String
    var genericLinearStr = makeGenericLinearStr();
    var extensibleStr = makeExtensibleStrFrom(genericLinearStr);
    tenureStringsWithSameChars(
      genericLinearStr,
      extensibleStr,
      !isDeduplicatable
    );

    // --> Generic Linear String and Dependent String
    var dependentStr = makeDependentStrFrom(genericLinearStr);
    tenureStringsWithSameChars(
      dependentStr,
      genericLinearStr,
      !isDeduplicatable
    );
  }

  // --> Inline strings are too short to have the same chars as the extensible
  // strings, generic linear strings and dependent strings
}

function runTests() {
  var charEncoding = { TWOBYTE: 0, LATIN1: 1 };

  test1(charEncoding.TWOBYTE);
  test2(charEncoding.TWOBYTE);
  test3(charEncoding.TWOBYTE);

  test1(charEncoding.LATIN1);
  test2(charEncoding.LATIN1);
  test3(charEncoding.LATIN1);
}

runTests();