Skip to content

Commit 7b2dc73

Browse files
committed
[JSC] Skip identical printable ASCII prefix in Intl.Collator ASCII fast path
https://bugs.webkit.org/show_bug.cgi?id=327979 rdar://190333228 Reviewed by Sosuke Suzuki. compareASCIIWithUCADUCET first scanned equal-length strings with equal() and then walked the same characters again comparing DUCET weights. For 8-bit strings, this patch merges both into a single pass. The identical prefix is skipped 16 bytes at a time with SIMD while every byte is printable ASCII (0x20-0x7E), since all of those have non-zero DUCET level-1 weight and an identical run of them cannot decide the comparison. The per-character comparison resumes from the first byte that differs or needs checking. The final overlapping 16-byte load avoids a per-character tail. 8-15 byte strings use two overlapping 64-bit loads instead. The level-3 tie-break pass also starts from the first difference instead of position 0, since the identical prefix has identical level-3 weights. Benchmark results with --useJIT=0 are following. ToT Patched intl-collator-compare-long-prefix-different-length 125.0767+-1.0766 ^ 83.3447+-0.4762 ^ definitely 1.5007x faster intl-collator-compare-equal-long 85.4302+-1.0364 ^ 83.0366+-0.7213 ^ definitely 1.0288x faster intl-collator-compare-16bit-long-prefix-primary-diff 151.4125+-0.8296 ^ 121.6623+-1.5823 ^ definitely 1.2445x faster intl-collator-compare-long-prefix-case-diff 174.4286+-0.9481 ^ 85.0297+-0.4806 ^ definitely 2.0514x faster intl-collator-compare-long-prefix-primary-diff 129.0799+-0.9748 ^ 83.8850+-0.5827 ^ definitely 1.5388x faster intl-collator-compare-first-char-diff 83.3505+-0.9395 ^ 81.5479+-0.5391 ^ definitely 1.0221x faster intl-collator-compare-sort-short-keys 164.9051+-2.5775 ^ 144.1785+-0.4210 ^ definitely 1.1438x faster Tests: JSTests/microbenchmarks/intl-collator-compare-16bit-long-prefix-primary-diff.js JSTests/microbenchmarks/intl-collator-compare-equal-long.js JSTests/microbenchmarks/intl-collator-compare-first-char-diff.js JSTests/microbenchmarks/intl-collator-compare-long-prefix-case-diff.js JSTests/microbenchmarks/intl-collator-compare-long-prefix-different-length.js JSTests/microbenchmarks/intl-collator-compare-long-prefix-primary-diff.js JSTests/microbenchmarks/intl-collator-compare-sort-short-keys.js * JSTests/microbenchmarks/intl-collator-compare-16bit-long-prefix-primary-diff.js: Added. (test): * JSTests/microbenchmarks/intl-collator-compare-equal-long.js: Added. (test): * JSTests/microbenchmarks/intl-collator-compare-first-char-diff.js: Added. (test): * JSTests/microbenchmarks/intl-collator-compare-long-prefix-case-diff.js: Added. (test): * JSTests/microbenchmarks/intl-collator-compare-long-prefix-different-length.js: Added. (test): * JSTests/microbenchmarks/intl-collator-compare-long-prefix-primary-diff.js: Added. (test): * JSTests/microbenchmarks/intl-collator-compare-sort-short-keys.js: Added. (makeKeys): (test): * Source/JavaScriptCore/runtime/IntlObjectInlines.h: (JSC::identicalPrintableASCIIPrefixLength): (JSC::compareASCIIWithUCADUCET): Canonical link: https://commits.webkit.org/324132@main
1 parent a7187ed commit 7b2dc73

8 files changed

Lines changed: 203 additions & 8 deletions
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
function test(collator, a, b, n) {
2+
let result = 0;
3+
for (let i = 0; i < n; ++i)
4+
result += collator.compare(a, b);
5+
return result;
6+
}
7+
8+
let collator = new Intl.Collator("en");
9+
let prefix = "The quick brown fox jumps over the lazy dog 0123456789 ABCDEFGHIJ";
10+
let a = $vm.make16BitStringIfPossible(prefix + "a");
11+
let b = $vm.make16BitStringIfPossible(prefix + "b");
12+
13+
let result = test(collator, a, b, 2e6);
14+
if (result !== -2e6)
15+
throw new Error("Bad result: " + result);
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
function test(collator, a, b, n) {
2+
let result = 0;
3+
for (let i = 0; i < n; ++i)
4+
result += collator.compare(a, b);
5+
return result;
6+
}
7+
8+
let collator = new Intl.Collator("en");
9+
let text = "The quick brown fox jumps over the lazy dog 0123456789 ABCDEFGHIJ";
10+
let a = text.split("").join("");
11+
let b = text.split("").join("");
12+
13+
let result = test(collator, a, b, 2e6);
14+
if (result !== 0)
15+
throw new Error("Bad result: " + result);
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
function test(collator, a, b, n) {
2+
let result = 0;
3+
for (let i = 0; i < n; ++i)
4+
result += collator.compare(a, b);
5+
return result;
6+
}
7+
8+
let collator = new Intl.Collator("en");
9+
let suffix = "he quick brown fox jumps over the lazy dog 0123456789 ABCDEFGHIJ";
10+
let a = ("a" + suffix).split("").join("");
11+
let b = ("b" + suffix).split("").join("");
12+
13+
let result = test(collator, a, b, 2e6);
14+
if (result !== -2e6)
15+
throw new Error("Bad result: " + result);
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
function test(collator, a, b, n) {
2+
let result = 0;
3+
for (let i = 0; i < n; ++i)
4+
result += collator.compare(a, b);
5+
return result;
6+
}
7+
8+
let collator = new Intl.Collator("en");
9+
let prefix = "The quick brown fox jumps over the lazy dog 0123456789 ABCDEFGHIJ";
10+
let a = (prefix + "a").split("").join("");
11+
let b = (prefix + "A").split("").join("");
12+
13+
let result = test(collator, a, b, 2e6);
14+
if (result !== -2e6)
15+
throw new Error("Bad result: " + result);
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
function test(collator, a, b, n) {
2+
let result = 0;
3+
for (let i = 0; i < n; ++i)
4+
result += collator.compare(a, b);
5+
return result;
6+
}
7+
8+
let collator = new Intl.Collator("en");
9+
let prefix = "The quick brown fox jumps over the lazy dog 0123456789 ABCDEFGHIJ";
10+
let a = prefix.split("").join("");
11+
let b = (prefix + "z").split("").join("");
12+
13+
let result = test(collator, a, b, 2e6);
14+
if (result !== -2e6)
15+
throw new Error("Bad result: " + result);
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
function test(collator, a, b, n) {
2+
let result = 0;
3+
for (let i = 0; i < n; ++i)
4+
result += collator.compare(a, b);
5+
return result;
6+
}
7+
8+
let collator = new Intl.Collator("en");
9+
let prefix = "The quick brown fox jumps over the lazy dog 0123456789 ABCDEFGHIJ";
10+
let a = (prefix + "a").split("").join("");
11+
let b = (prefix + "b").split("").join("");
12+
13+
let result = test(collator, a, b, 2e6);
14+
if (result !== -2e6)
15+
throw new Error("Bad result: " + result);
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
function makeKeys(count)
2+
{
3+
let syllables = ["ka", "ri", "to", "Mo", "su", "Na", "be", "lo", "Ze", "pi", "du", "fa", "ge", "ho", "Ji", "wu"];
4+
let keys = [];
5+
let seed = 1;
6+
for (let i = 0; i < count; ++i) {
7+
let key = "";
8+
let length = 4 + (i % 5);
9+
for (let j = 0; j < length; ++j) {
10+
seed = (seed * 1103515245 + 12345) & 0x7fffffff;
11+
key += syllables[seed % syllables.length];
12+
}
13+
keys.push(key);
14+
}
15+
return keys;
16+
}
17+
18+
function test(collator, keys, n) {
19+
let checksum = 0;
20+
for (let i = 0; i < n; ++i) {
21+
let sorted = keys.slice().sort(collator.compare);
22+
checksum += sorted[0].length + sorted[sorted.length - 1].length;
23+
}
24+
return checksum;
25+
}
26+
27+
let collator = new Intl.Collator("en");
28+
let keys = makeKeys(500);
29+
let sorted = keys.slice().sort(collator.compare);
30+
for (let i = 1; i < sorted.length; ++i) {
31+
if (collator.compare(sorted[i - 1], sorted[i]) > 0)
32+
throw new Error("Bad order at " + i);
33+
}
34+
35+
let expected = (sorted[0].length + sorted[sorted.length - 1].length) * 3000;
36+
let result = test(collator, keys, 3000);
37+
if (result !== expected)
38+
throw new Error("Bad result: " + result);

‎Source/JavaScriptCore/runtime/IntlObjectInlines.h‎

Lines changed: 75 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,8 @@
3434
#include "JSObject.h"
3535
#include "ObjectConstructor.h"
3636
#include <unicode/ucol.h>
37+
#include <wtf/SIMDHelpers.h>
38+
#include <wtf/UnalignedAccess.h>
3739

3840
WTF_ALLOW_UNSAFE_BUFFER_USAGE_BEGIN
3941

@@ -241,21 +243,83 @@ UCollationResult compareASCIIWithUCADUCETLevel3(std::span<const CharacterType1>
241243
return UCOL_EQUAL;
242244
}
243245

244-
template<typename CharacterType1, typename CharacterType2>
245-
inline std::optional<UCollationResult> compareASCIIWithUCADUCET(std::span<const CharacterType1> characters1, std::span<const CharacterType2> characters2)
246+
// Every byte in [0x20, 0x7E] has a non-zero DUCET level-1 weight, so a run of identical such bytes cannot
247+
// decide the comparison at any level. The overlapping final loads only revisit bytes already known to be
248+
// part of that run.
249+
ALWAYS_INLINE size_t identicalPrintableASCIIPrefixLength(const Latin1Character* characters1, const Latin1Character* characters2, size_t length)
246250
{
247-
if (characters1.size() == characters2.size()) {
248-
if (equal(characters1.data(), characters2))
249-
return UCOL_EQUAL;
251+
constexpr size_t stride = SIMD::stride<uint8_t>;
252+
if (length >= stride) {
253+
auto findStop = [&](size_t offset) {
254+
auto input1 = SIMD::load(std::bit_cast<const uint8_t*>(characters1 + offset));
255+
auto input2 = SIMD::load(std::bit_cast<const uint8_t*>(characters2 + offset));
256+
auto different = SIMD::bitNot(SIMD::equal(input1, input2));
257+
auto control = SIMD::lessThan(input1, SIMD::splat<uint8_t>(0x20));
258+
auto deleteOrNonASCII = SIMD::greaterThan(input1, SIMD::splat<uint8_t>(0x7E));
259+
return SIMD::findFirstNonZeroIndex(SIMD::bitOr(different, control, deleteOrNonASCII));
260+
};
261+
size_t position = 0;
262+
for (; position + stride <= length; position += stride) {
263+
if (auto index = findStop(position))
264+
return position + *index;
265+
}
266+
if (position == length)
267+
return length;
268+
if (auto index = findStop(length - stride))
269+
return length - stride + *index;
270+
return length;
250271
}
251272

273+
if (length >= sizeof(uint64_t)) {
274+
// Only the lowest set bit is exact: borrows and carries out of a flagged byte can flag the bytes above it.
275+
auto findStop = [&](size_t offset) -> std::optional<size_t> {
276+
uint64_t word1 = WTF::unalignedLoad<uint64_t>(characters1 + offset);
277+
uint64_t word2 = WTF::unalignedLoad<uint64_t>(characters2 + offset);
278+
uint64_t nonPrintable = (word1 | (word1 - 0x2020202020202020ULL) | (word1 + 0x0101010101010101ULL)) & 0x8080808080808080ULL;
279+
if (uint64_t stop = (word1 ^ word2) | nonPrintable)
280+
return std::countr_zero(stop) / 8;
281+
return std::nullopt;
282+
};
283+
if (auto index = findStop(0))
284+
return *index;
285+
if (auto index = findStop(length - sizeof(uint64_t)))
286+
return length - sizeof(uint64_t) + *index;
287+
return length;
288+
}
289+
290+
return 0;
291+
}
292+
293+
template<typename CharacterType1, typename CharacterType2>
294+
inline std::optional<UCollationResult> compareASCIIWithUCADUCET(std::span<const CharacterType1> characters1, std::span<const CharacterType2> characters2)
295+
{
252296
auto* data1 = characters1.data();
253297
auto* data2 = characters2.data();
254298
size_t commonLength = std::min(characters1.size(), characters2.size());
255-
for (unsigned position = 0; position < commonLength; ++position) {
299+
size_t position = 0;
300+
301+
if constexpr (std::is_same_v<CharacterType1, Latin1Character> && std::is_same_v<CharacterType2, Latin1Character>)
302+
position = identicalPrintableASCIIPrefixLength(data1, data2, commonLength);
303+
else if (characters1.size() == characters2.size() && equal(data1, characters2))
304+
return UCOL_EQUAL;
305+
306+
size_t firstDifference = commonLength;
307+
for (; position < commonLength; ++position) {
256308
auto lhs = data1[position];
257309
auto rhs = data2[position];
258310

311+
if (lhs == rhs) {
312+
if (canUseASCIIUCADUCETComparison(lhs)) [[likely]]
313+
continue;
314+
if constexpr (std::is_same_v<CharacterType1, Latin1Character> && std::is_same_v<CharacterType2, Latin1Character>) {
315+
if (firstDifference == commonLength && characters1.size() == characters2.size() && equal(data1 + position, characters2.subspan(position)))
316+
return UCOL_EQUAL;
317+
}
318+
return std::nullopt;
319+
}
320+
if (firstDifference == commonLength)
321+
firstDifference = position;
322+
259323
if (!canUseASCIIUCADUCETComparison(lhs) || !canUseASCIIUCADUCETComparison(rhs))
260324
return std::nullopt;
261325

@@ -271,8 +335,11 @@ inline std::optional<UCollationResult> compareASCIIWithUCADUCET(std::span<const
271335
return leftWeight > rightWeight ? UCOL_GREATER : UCOL_LESS;
272336
}
273337

274-
if (characters1.size() == characters2.size())
275-
return compareASCIIWithUCADUCETLevel3(characters1, characters2);
338+
if (characters1.size() == characters2.size()) {
339+
if (firstDifference == commonLength)
340+
return UCOL_EQUAL;
341+
return compareASCIIWithUCADUCETLevel3(characters1.subspan(firstDifference), characters2.subspan(firstDifference));
342+
}
276343

277344
// If the next character is valid, then we do not need to look into the rest of characters.
278345
if (characters1.size() > characters2.size()) {

0 commit comments

Comments
 (0)