Skip to content

Commit 14211b3

Browse files
committed
[JSC] FTL StringLocaleCompare should skip the equal ASCII prefix a word at a time
https://bugs.webkit.org/show_bug.cgi?id=328034 rdar://190390889 Reviewed by Sosuke Suzuki. FTL's inlined ASCII localeCompare walked both strings one byte at a time, doing a load, compare, and ASCII check per character. Strings sharing a long prefix spend most of the comparison in that prefix. This patch compares 8 bytes and then 4 bytes at a time while both words are all-ASCII. On a mismatch, we compute the first differing byte from the lowest set bit of the XOR and compare the DUCET weights of those bytes extracted from the words themselves. A case-only difference (equal weights) resumes the word pass after that byte. A word containing a non-ASCII byte falls back to the existing byte loop from the word's start, so non-ASCII handling is unchanged. Test: JSTests/stress/string-locale-compare-long-ascii-prefix.js * JSTests/stress/string-locale-compare-long-ascii-prefix.js: Added. (cmp): (ref): (rnd): (randomString): (flat): (flipCase): * Source/JavaScriptCore/ftl/FTLLowerDFGToB3.cpp: (JSC::FTL::DFG::LowerDFGToB3::compileStringLocaleCompare): Canonical link: https://commits.webkit.org/324260@main
1 parent 555e733 commit 14211b3

2 files changed

Lines changed: 164 additions & 12 deletions

File tree

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
function cmp(a, b) { return a.localeCompare(b); }
2+
noInline(cmp);
3+
var collator = new Intl.Collator();
4+
function ref(a, b) { var r = collator.compare(a, b); return r < 0 ? -1 : r > 0 ? 1 : 0; }
5+
var alphabet = "aAbBzZ09 -_.,~\u00e9\u00c9\u00ff\u0080\t";
6+
var seed = 1;
7+
function rnd(n) { seed = (Math.imul(seed, 1103515245) + 12345) & 0x7fffffff; return (seed >>> 16) % n; }
8+
function randomString(len) { var s = ""; for (var i = 0; i < len; ++i) s += alphabet[rnd(alphabet.length)]; return s; }
9+
function flat(s) { return s.split("").join(""); }
10+
function flipCase(s) {
11+
var chars = s.split("");
12+
for (var i = rnd(3) + 1; i--;) {
13+
var j = rnd(chars.length);
14+
var c = chars[j];
15+
chars[j] = c === c.toLowerCase() ? c.toUpperCase() : c.toLowerCase();
16+
}
17+
return chars.join("");
18+
}
19+
var pairs = [];
20+
var pairCount = Math.min(testLoopCount, 1000);
21+
for (var i = 0; i < pairCount; ++i) {
22+
var prefix = randomString(rnd(21));
23+
var a = flat(prefix + randomString(rnd(4)));
24+
var b = flat(prefix + randomString(rnd(4)));
25+
var kind = rnd(4);
26+
if (kind == 0)
27+
b = flat(a);
28+
else if (kind == 1 && a.length)
29+
b = flipCase(a + randomString(rnd(4)));
30+
pairs.push([a, b, ref(a, b)]);
31+
}
32+
for (var i = 0; i < testLoopCount; ++i) {
33+
var p = pairs[i % pairCount];
34+
var r = cmp(p[0], p[1]);
35+
r = r < 0 ? -1 : r > 0 ? 1 : 0;
36+
if (r !== p[2])
37+
throw new Error("Mismatch for " + JSON.stringify(p[0]) + " vs " + JSON.stringify(p[1]) + ": " + r + " expected " + p[2]);
38+
}

‎Source/JavaScriptCore/ftl/FTLLowerDFGToB3.cpp‎

Lines changed: 126 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -12410,6 +12410,23 @@ IGNORE_CLANG_WARNINGS_END
1241012410
// const uint8_t* rightData = rightImpl->characters8();
1241112411
// size_t commonLength = std::min(leftLength, rightLength);
1241212412
// size_t index = 0;
12413+
// while (commonLength - index >= 4) {
12414+
// size_t wordSize = commonLength - index >= 8 ? 8 : 4;
12415+
// if (either word at index has a non-ASCII byte)
12416+
// break;
12417+
// if (the words at index are equal) {
12418+
// index += wordSize;
12419+
// continue;
12420+
// }
12421+
// size_t mismatch = index + (offset of the first differing byte in the words);
12422+
// uint8_t leftWeight = ducetLevel1Weights[leftData[mismatch]];
12423+
// uint8_t rightWeight = ducetLevel1Weights[rightData[mismatch]];
12424+
// if ((!leftWeight) | (!rightWeight))
12425+
// goto slow_path;
12426+
// if (leftWeight != rightWeight)
12427+
// return leftWeight > rightWeight ? 1 : -1;
12428+
// index = mismatch + 1;
12429+
// }
1241312430
//
1241412431
// while (index < commonLength) {
1241512432
// uint8_t leftByte = leftData[index];
@@ -12432,8 +12449,7 @@ IGNORE_CLANG_WARNINGS_END
1243212449
// goto slow_path;
1243312450
//
1243412451
// const uint8_t* longerData = leftLength > rightLength ? leftData : rightData;
12435-
// size_t shorterLength = std::min(leftLength, rightLength);
12436-
// if (!ducetLevel1Weights[longerData[shorterLength]])
12452+
// if (!ducetLevel1Weights[longerData[commonLength]])
1243712453
// goto slow_path;
1243812454
// return leftLength > rightLength ? 1 : -1;
1243912455

@@ -12447,6 +12463,19 @@ IGNORE_CLANG_WARNINGS_END
1244712463
LBasicBlock checkDUCET = m_out.newBlock();
1244812464
LBasicBlock is8Bit = m_out.newBlock();
1244912465
LBasicBlock loopSetup = m_out.newBlock();
12466+
LBasicBlock word8Loop = m_out.newBlock();
12467+
LBasicBlock word8CheckDiff = m_out.newBlock();
12468+
LBasicBlock word8Advance = m_out.newBlock();
12469+
LBasicBlock word8NonASCII = m_out.newBlock();
12470+
LBasicBlock word8Mismatch = m_out.newBlock();
12471+
LBasicBlock word8Done = m_out.newBlock();
12472+
LBasicBlock word4Check = m_out.newBlock();
12473+
LBasicBlock word4Body = m_out.newBlock();
12474+
LBasicBlock word4CheckDiff = m_out.newBlock();
12475+
LBasicBlock byteTail = m_out.newBlock();
12476+
LBasicBlock wordMismatch = m_out.newBlock();
12477+
LBasicBlock wordMismatchWeights = m_out.newBlock();
12478+
LBasicBlock wordMismatchResume = m_out.newBlock();
1245012479
LBasicBlock loop = m_out.newBlock();
1245112480
LBasicBlock checkASCII = m_out.newBlock();
1245212481
LBasicBlock checkWeights = m_out.newBlock();
@@ -12476,11 +12505,16 @@ IGNORE_CLANG_WARNINGS_END
1247612505
ValueFromBlock sameImplResult = m_out.anchor(m_out.int32Zero);
1247712506
m_out.branch(m_out.equal(leftImpl, rightImpl), unsure(continuation), unsure(checkDUCET));
1247812507

12479-
// Check JSGlobalObject.m_canDoASCIIUCADUCETLocaleCompare
12508+
// Check JSGlobalObject.m_canDoASCIIUCADUCETLocaleCompare. It is set once, when the default
12509+
// collator is created, and never cleared, so a true value seen here holds forever.
1248012510
m_out.appendTo(checkDUCET, is8Bit);
12481-
m_out.branch(
12482-
m_out.isZero32(m_out.load8ZeroExt32(weakPointer(globalObject), m_heaps.JSGlobalObject_canDoASCIIUCADUCETLocaleCompare)),
12483-
rarely(slowCase), usually(is8Bit));
12511+
if (globalObject->canDoASCIIUCADUCETLocaleCompare())
12512+
m_out.jump(is8Bit);
12513+
else {
12514+
m_out.branch(
12515+
m_out.isZero32(m_out.load8ZeroExt32(weakPointer(globalObject), m_heaps.JSGlobalObject_canDoASCIIUCADUCETLocaleCompare)),
12516+
rarely(slowCase), usually(is8Bit));
12517+
}
1248412518

1248512519
// Check both are 8-bit
1248612520
m_out.appendTo(is8Bit, loopSetup);
@@ -12498,12 +12532,93 @@ IGNORE_CLANG_WARNINGS_END
1249812532
LValue rightData = m_out.loadPtr(rightImpl, m_heaps.StringImpl_data);
1249912533
LValue commonLength = m_out.select(m_out.below(leftLength, rightLength), leftLength, rightLength);
1250012534
LValue tableBase = m_out.constIntPtr(ducetLevel1Weights);
12501-
ValueFromBlock indexAtStart = m_out.anchor(m_out.intPtrZero);
12502-
m_out.branch(m_out.isNull(commonLength), unsure(loopDone), unsure(loop));
12535+
ValueFromBlock word8IndexAtStart = m_out.anchor(m_out.intPtrZero);
12536+
ValueFromBlock word4IndexAtStart = m_out.anchor(m_out.intPtrZero);
12537+
m_out.branch(m_out.below(commonLength, m_out.constIntPtr(8)), unsure(word4Check), unsure(word8Loop));
12538+
12539+
// Skip the equal ASCII prefix 8 bytes at a time. On a mismatch, decide from the first differing
12540+
// byte of the two words; if either word has a non-ASCII byte, let the byte loop decide. The exit
12541+
// edges get their own blocks so the loop body does not set the exit phis on every iteration.
12542+
m_out.appendTo(word8Loop, word8CheckDiff);
12543+
LValue word8Index = m_out.phi(pointerType(), word8IndexAtStart);
12544+
LValue leftWord8 = m_out.load64(m_out.baseIndex(m_heaps.characters8, leftData, word8Index));
12545+
LValue rightWord8 = m_out.load64(m_out.baseIndex(m_heaps.characters8, rightData, word8Index));
12546+
LValue diff8 = m_out.bitXor(leftWord8, rightWord8);
12547+
m_out.branch(m_out.testNonZero64(m_out.bitOr(leftWord8, rightWord8), m_out.constInt64(0x8080808080808080ULL)), rarely(word8NonASCII), usually(word8CheckDiff));
12548+
12549+
m_out.appendTo(word8CheckDiff, word8Advance);
12550+
m_out.branch(m_out.notZero64(diff8), unsure(word8Mismatch), unsure(word8Advance));
12551+
12552+
m_out.appendTo(word8Advance, word8NonASCII);
12553+
LValue nextWord8Index = m_out.add(word8Index, m_out.constIntPtr(8));
12554+
m_out.addIncomingToPhi(word8Index, m_out.anchor(nextWord8Index));
12555+
m_out.branch(m_out.belowOrEqual(m_out.add(nextWord8Index, m_out.constIntPtr(8)), commonLength), unsure(word8Loop), unsure(word8Done));
12556+
12557+
m_out.appendTo(word8NonASCII, word8Mismatch);
12558+
ValueFromBlock byteTailIndexFromWord8 = m_out.anchor(word8Index);
12559+
m_out.jump(byteTail);
12560+
12561+
m_out.appendTo(word8Mismatch, word8Done);
12562+
ValueFromBlock mismatchIndexFromWord8 = m_out.anchor(word8Index);
12563+
ValueFromBlock mismatchLeftWordFromWord8 = m_out.anchor(leftWord8);
12564+
ValueFromBlock mismatchDiffFromWord8 = m_out.anchor(diff8);
12565+
m_out.jump(wordMismatch);
12566+
12567+
m_out.appendTo(word8Done, word4Check);
12568+
ValueFromBlock word4IndexFromWord8 = m_out.anchor(nextWord8Index);
12569+
m_out.jump(word4Check);
12570+
12571+
m_out.appendTo(word4Check, word4Body);
12572+
LValue word4Index = m_out.phi(pointerType(), word4IndexAtStart, word4IndexFromWord8);
12573+
ValueFromBlock byteTailIndexFromWord4Check = m_out.anchor(word4Index);
12574+
m_out.branch(m_out.above(m_out.add(word4Index, m_out.constIntPtr(4)), commonLength), unsure(byteTail), unsure(word4Body));
12575+
12576+
m_out.appendTo(word4Body, word4CheckDiff);
12577+
LValue leftWord4 = m_out.load32(m_out.baseIndex(m_heaps.characters8, leftData, word4Index));
12578+
LValue rightWord4 = m_out.load32(m_out.baseIndex(m_heaps.characters8, rightData, word4Index));
12579+
LValue diff4 = m_out.zeroExt(m_out.bitXor(leftWord4, rightWord4), Int64);
12580+
ValueFromBlock byteTailIndexFromWord4 = m_out.anchor(word4Index);
12581+
m_out.branch(m_out.testNonZero32(m_out.bitOr(leftWord4, rightWord4), m_out.constInt32(0x80808080)), rarely(byteTail), usually(word4CheckDiff));
12582+
12583+
m_out.appendTo(word4CheckDiff, byteTail);
12584+
ValueFromBlock mismatchIndexFromWord4 = m_out.anchor(word4Index);
12585+
ValueFromBlock mismatchLeftWordFromWord4 = m_out.anchor(m_out.zeroExt(leftWord4, Int64));
12586+
ValueFromBlock mismatchDiffFromWord4 = m_out.anchor(diff4);
12587+
ValueFromBlock byteTailIndexAfterWord4 = m_out.anchor(m_out.add(word4Index, m_out.constIntPtr(4)));
12588+
m_out.branch(m_out.notZero64(diff4), unsure(wordMismatch), unsure(byteTail));
12589+
12590+
m_out.appendTo(byteTail, wordMismatch);
12591+
LValue byteTailIndex = m_out.phi(pointerType(), byteTailIndexFromWord8, byteTailIndexFromWord4Check, byteTailIndexFromWord4, byteTailIndexAfterWord4);
12592+
ValueFromBlock indexFromByteTail = m_out.anchor(byteTailIndex);
12593+
m_out.branch(m_out.below(byteTailIndex, commonLength), unsure(loop), unsure(loopDone));
12594+
12595+
m_out.appendTo(wordMismatch, wordMismatchWeights);
12596+
LValue mismatchIndex = m_out.phi(pointerType(), mismatchIndexFromWord8, mismatchIndexFromWord4);
12597+
LValue mismatchLeftWord = m_out.phi(Int64, mismatchLeftWordFromWord8, mismatchLeftWordFromWord4);
12598+
LValue mismatchDiff = m_out.phi(Int64, mismatchDiffFromWord8, mismatchDiffFromWord4);
12599+
LValue lowestDiffBit = m_out.bitAnd(mismatchDiff, m_out.neg(mismatchDiff));
12600+
LValue byteShift = m_out.bitAnd(m_out.bitXor(m_out.ctlz32(lowestDiffBit), m_out.constInt64(63)), m_out.constInt64(~7LL));
12601+
LValue byteShift32 = m_out.castToInt32(byteShift);
12602+
LValue mismatchLeftByte = m_out.bitAnd(m_out.lShr(mismatchLeftWord, byteShift32), m_out.constInt64(0xff));
12603+
LValue mismatchRightByte = m_out.bitAnd(m_out.lShr(m_out.bitXor(mismatchLeftWord, mismatchDiff), byteShift32), m_out.constInt64(0xff));
12604+
LValue mismatchLeftWeight = m_out.load8ZeroExt32(TypedPointer(m_heaps.absolute.atAnyAddress(), m_out.add(tableBase, mismatchLeftByte)));
12605+
LValue mismatchRightWeight = m_out.load8ZeroExt32(TypedPointer(m_heaps.absolute.atAnyAddress(), m_out.add(tableBase, mismatchRightByte)));
12606+
m_out.branch(m_out.bitOr(m_out.isZero32(mismatchLeftWeight), m_out.isZero32(mismatchRightWeight)), rarely(slowCase), usually(wordMismatchWeights));
12607+
12608+
// Equal weights (a case-only difference like A/a) resume the word pass after the differing byte.
12609+
m_out.appendTo(wordMismatchWeights, wordMismatchResume);
12610+
ValueFromBlock wordMismatchResult = m_out.anchor(m_out.select(m_out.above(mismatchLeftWeight, mismatchRightWeight), m_out.int32One, m_out.constInt32(-1)));
12611+
m_out.branch(m_out.notEqual(mismatchLeftWeight, mismatchRightWeight), unsure(continuation), unsure(wordMismatchResume));
12612+
12613+
m_out.appendTo(wordMismatchResume, loop);
12614+
LValue resumeIndex = m_out.add(mismatchIndex, m_out.add(m_out.lShr(byteShift, m_out.constInt32(3)), m_out.intPtrOne));
12615+
m_out.addIncomingToPhi(word8Index, m_out.anchor(resumeIndex));
12616+
m_out.addIncomingToPhi(word4Index, m_out.anchor(resumeIndex));
12617+
m_out.branch(m_out.belowOrEqual(m_out.add(resumeIndex, m_out.constIntPtr(8)), commonLength), unsure(word8Loop), unsure(word4Check));
1250312618

1250412619
// Main loop: load bytes and branch on equality
1250512620
m_out.appendTo(loop, checkASCII);
12506-
LValue index = m_out.phi(pointerType(), indexAtStart);
12621+
LValue index = m_out.phi(pointerType(), indexFromByteTail);
1250712622
LValue leftByte = m_out.load8ZeroExt32(m_out.baseIndex(m_heaps.characters8, leftData, index));
1250812623
LValue rightByte = m_out.load8ZeroExt32(m_out.baseIndex(m_heaps.characters8, rightData, index));
1250912624
m_out.branch(m_out.notEqual(leftByte, rightByte), unsure(checkWeights), unsure(checkASCII));
@@ -12539,8 +12654,7 @@ IGNORE_CLANG_WARNINGS_END
1253912654
m_out.appendTo(lengthsDiffer, slowCase);
1254012655
LValue isLeftLonger = m_out.above(leftLength, rightLength);
1254112656
LValue longerData = m_out.select(isLeftLonger, leftData, rightData);
12542-
LValue shorterLength = m_out.select(isLeftLonger, rightLength, leftLength);
12543-
LValue nextChar = m_out.load8ZeroExt32(m_out.baseIndex(m_heaps.characters8, longerData, shorterLength));
12657+
LValue nextChar = m_out.load8ZeroExt32(m_out.baseIndex(m_heaps.characters8, longerData, commonLength));
1254412658
LValue nextCharWeight = m_out.load8ZeroExt32(TypedPointer(m_heaps.absolute.atAnyAddress(), m_out.add(tableBase, m_out.zeroExtPtr(nextChar))));
1254512659
LValue lengthResult = m_out.select(isLeftLonger, m_out.int32One, m_out.constInt32(-1));
1254612660
ValueFromBlock lengthResultPhi = m_out.anchor(lengthResult);
@@ -12553,7 +12667,7 @@ IGNORE_CLANG_WARNINGS_END
1255312667

1255412668
// Merge results
1255512669
m_out.appendTo(continuation, lastNext);
12556-
setInt32(m_out.phi(Int32, sameStringResult, sameImplResult, differResultPhi, lengthResultPhi, slowResult));
12670+
setInt32(m_out.phi(Int32, sameStringResult, sameImplResult, wordMismatchResult, differResultPhi, lengthResultPhi, slowResult));
1255712671
}
1255812672

1255912673
void compileStringIndexOf()

0 commit comments

Comments
 (0)