|
| 1 | +function shouldBe(actual, expected) { |
| 2 | + if (actual !== expected) |
| 3 | + throw new Error(`expected ${String(expected)} but got ${String(actual)}`); |
| 4 | +} |
| 5 | + |
| 6 | +const symbol = Symbol("tag"); |
| 7 | + |
| 8 | +const makers = { |
| 9 | + empty: { |
| 10 | + tagger: "symbol", |
| 11 | + hot: true, |
| 12 | + make: `return [];`, |
| 13 | + length: 0, |
| 14 | + }, |
| 15 | + int32: { |
| 16 | + tagger: "nine", |
| 17 | + make: `return [i, i + 1, i + 2];`, |
| 18 | + length: 3, |
| 19 | + element: (i, index) => i + index, |
| 20 | + }, |
| 21 | + double: { |
| 22 | + tagger: "one", |
| 23 | + make: `return [i + 0.5, i + 1.5];`, |
| 24 | + length: 2, |
| 25 | + element: (i, index) => i + index + 0.5, |
| 26 | + }, |
| 27 | + contiguous: { |
| 28 | + tagger: "nine", |
| 29 | + hot: true, |
| 30 | + make: `return [i, "b", "c", "d", i + 4];`, |
| 31 | + length: 5, |
| 32 | + element: (i, index) => index % 4 ? "abcd"[index] : i + index, |
| 33 | + }, |
| 34 | + holes: { |
| 35 | + tagger: "one", |
| 36 | + hot: true, |
| 37 | + make: `const array = new Array(9); array[4] = i + 4; array[8] = i + 8; return array;`, |
| 38 | + length: 9, |
| 39 | + element: (i, index) => index % 4 || !index ? undefined : i + index, |
| 40 | + hole: (index) => index % 4 || !index, |
| 41 | + push: false, |
| 42 | + }, |
| 43 | + large: { |
| 44 | + tagger: "symbol", |
| 45 | + make: `const array = new Array(1200); array[0] = i; array[1199] = i + 1; return array;`, |
| 46 | + length: 1200, |
| 47 | + element: (i, index) => !index ? i : index === 1199 ? i + 1 : undefined, |
| 48 | + hole: (index) => index && index !== 1199, |
| 49 | + indices: [0, 1, 600, 1198, 1199], |
| 50 | + }, |
| 51 | + copyOnWrite: { |
| 52 | + tagger: "nine", |
| 53 | + make: `return [1, 2, 3];`, |
| 54 | + length: 3, |
| 55 | + element: (i, index) => index + 1, |
| 56 | + }, |
| 57 | + arrayStorage: { |
| 58 | + tagger: "checked", |
| 59 | + hot: true, |
| 60 | + make: `const array = []; ensureArrayStorage(array); for (let k = 8; k--;) array.unshift(i + k); return array;`, |
| 61 | + length: 8, |
| 62 | + element: (i, index) => i + index, |
| 63 | + }, |
| 64 | + object: { |
| 65 | + tagger: "nine", |
| 66 | + make: `const object = {}; object[0] = i; object[1] = "x"; object[2] = i + 2; return object;`, |
| 67 | + element: (i, index) => index === 1 ? "x" : i + index, |
| 68 | + indices: [0, 1, 2], |
| 69 | + }, |
| 70 | + typedArray: { |
| 71 | + tagger: "symbol", |
| 72 | + make: `const array = new Uint16Array(4); array[1] = i; return array;`, |
| 73 | + length: 4, |
| 74 | + element: (i, index) => index === 1 ? i & 0xffff : 0, |
| 75 | + }, |
| 76 | +}; |
| 77 | + |
| 78 | +const taggers = { |
| 79 | + one: { |
| 80 | + tag: `target.tag = value;`, |
| 81 | + check(target, value) { |
| 82 | + shouldBe(target.tag, value); |
| 83 | + }, |
| 84 | + }, |
| 85 | + checked: { |
| 86 | + tag: `if (target.tag !== undefined) return null; target.tag = value;`, |
| 87 | + check(target, value) { |
| 88 | + shouldBe(target.tag, value); |
| 89 | + }, |
| 90 | + }, |
| 91 | + symbol: { |
| 92 | + tag: `target[symbol] = value;`, |
| 93 | + check(target, value) { |
| 94 | + shouldBe(target[symbol], value); |
| 95 | + }, |
| 96 | + }, |
| 97 | + nine: { |
| 98 | + tag: `target.p0 = value; target.p1 = value + 1; target.p2 = value + 2; target.p3 = value + 3; target.p4 = value + 4; target.p5 = value + 5; target.p6 = value + 6; target.p7 = value + 7; target.p8 = value + 8;`, |
| 99 | + check(target, value) { |
| 100 | + shouldBe(target.p0, value); |
| 101 | + shouldBe(target.p1, value + 1); |
| 102 | + shouldBe(target.p2, value + 2); |
| 103 | + shouldBe(target.p3, value + 3); |
| 104 | + shouldBe(target.p4, value + 4); |
| 105 | + shouldBe(target.p5, value + 5); |
| 106 | + shouldBe(target.p6, value + 6); |
| 107 | + shouldBe(target.p7, value + 7); |
| 108 | + shouldBe(target.p8, value + 8); |
| 109 | + }, |
| 110 | + }, |
| 111 | +}; |
| 112 | + |
| 113 | +let counter = 0; |
| 114 | +function compile(parameters, body) { |
| 115 | + const result = new Function("symbol", `return function (${parameters}) { ${body} /* ${counter++} */ };`)(symbol); |
| 116 | + noInline(result); |
| 117 | + return result; |
| 118 | +} |
| 119 | + |
| 120 | +function checkElements(maker, target, i) { |
| 121 | + shouldBe(target.length, maker.length); |
| 122 | + const indices = maker.indices; |
| 123 | + const count = indices ? indices.length : maker.length; |
| 124 | + for (let k = 0; k < count; ++k) { |
| 125 | + const index = indices ? indices[k] : k; |
| 126 | + shouldBe(target[index], maker.element(i, index)); |
| 127 | + if (maker.hole) |
| 128 | + shouldBe(index in target, !maker.hole(index)); |
| 129 | + } |
| 130 | +} |
| 131 | + |
| 132 | +function run(maker, tagger, make, tag, iterations) { |
| 133 | + const push = maker.push !== false && Array.isArray(make(0)); |
| 134 | + const kept = []; |
| 135 | + for (let i = 0; i < iterations; ++i) { |
| 136 | + const target = tag(make(i), i); |
| 137 | + if (!(i & 63)) |
| 138 | + kept.push(target, i); |
| 139 | + tagger.check(target, i); |
| 140 | + if (i < 64 || !(i & 15)) |
| 141 | + checkElements(maker, target, i); |
| 142 | + if (push) { |
| 143 | + target.push(i); |
| 144 | + shouldBe(target.length, maker.length + 1); |
| 145 | + shouldBe(target[maker.length], i); |
| 146 | + } |
| 147 | + } |
| 148 | + for (let k = 0; k < kept.length; k += 2) { |
| 149 | + const target = kept[k]; |
| 150 | + const i = kept[k + 1]; |
| 151 | + tagger.check(target, i); |
| 152 | + if (push) |
| 153 | + target.pop(); |
| 154 | + checkElements(maker, target, i); |
| 155 | + } |
| 156 | +} |
| 157 | +noInline(run); |
| 158 | + |
| 159 | +const few = Math.max(Math.ceil(testLoopCount / 16), 60); |
| 160 | + |
| 161 | +const shared = {}; |
| 162 | +for (const tagName of ["one", "symbol", "nine"]) |
| 163 | + shared[tagName] = compile("target, value", `${taggers[tagName].tag} return target;`); |
| 164 | + |
| 165 | +for (const makeName in makers) { |
| 166 | + const maker = makers[makeName]; |
| 167 | + const make = compile("i", maker.make); |
| 168 | + const tagger = taggers[maker.tagger]; |
| 169 | + run(maker, tagger, make, compile("target, value", `${tagger.tag} return target;`), maker.hot ? testLoopCount : few); |
| 170 | + if (makeName === "copyOnWrite") |
| 171 | + continue; |
| 172 | + for (const tagName in shared) |
| 173 | + run(maker, taggers[tagName], make, shared[tagName], few); |
| 174 | +} |
0 commit comments