Repository navigation
Conversation
|
Status: closed without merge. The rule fires on little evidence and leaves some functions slower for good (684 against 346 ns per call after one long call with one frozen array, 163 against 25 for a fill loop after 200 shifted arrays). Most of its gain is the reoptimization back-off, which a bound on the retry counter gives without the rule. The choice of what to do is on the #785 thread. How this was reproduced (Release // 8 reads of one array. Half of the arrays are frozen.
function read(a) { return a[0] + a[1] + a[2] + a[3] + a[4] + a[5] + a[6] + a[7]; }
noInline(read);
let ordinary = [], frozen = [];
for (let i = 0; i < 64; ++i) {
let a = [], b = [];
for (let j = 0; j < 8; ++j) { a.push(1); b.push(1); }
ordinary.push(a);
frozen.push(Object.freeze(b));
}
for (let i = 0; i < 1000000; ++i)
read((i % 2) ? ordinary[i & 63] : frozen[i & 63]);
print(numberOfDFGCompiles(read));
|
|
Preview build of 55be124: |
… ArrayStorage array A function that uses one array at K sites and sometimes gets an ArrayStorage array (frozen, sealed, sparse, or shifted when it held more than 128 elements) is compiled K + 2 times by the DFG. The array fails the check of the first site, and the exit records the failure in the ArrayProfile of that site only. ArrayMode::fromObserved ignores ArrayStorage at a site until the site's own speculation has failed on it, so each of the other sites needs a failure in a compilation of its own. The reoptimization counter doubles the thresholds each time: 8 sites take 10 compilations and 51,109 exits, and a function with 16 sites is still in the DFG after 6M calls. The sites after the failed check did see the array, in the Baseline code that the exit continued in. So when DFG code is jettisoned for exiting too often, and one of its exits is an array check that failed on ArrayStorage, CodeBlock::tallyFrequentExitSites now records the ArrayStorage array that a site of the same baseline CodeBlock saw last as the speculation failure of that site. The next update of the profile sets SpeculationFailedOnArrayStorage from it. The function is then compiled 2 times, or 3 times when a structure check that has no ArrayProfile fails first. This is for DFG code that could still reach the FTL, and that the exit stub or a Baseline loop jettisons for its exit count. Code that reached the FTL, or that never will, can have run for long before it exited this often. Its ArrayStorage arrays can be rare, and then each site keeps learning from its own exit: a loop that gets 1 frozen array in 10,000 calls runs the same number of instructions as before. A jettison for OSR entries that failed does not count either. An exit counts as such an array check when it is BadCache or BadIndexingType and the ArrayProfile at its bytecode has SpeculationFailedOnArrayStorage or a pending ArrayStorage failure, or when it is BadIndexingType at a bytecode that has no ArrayProfile (a CheckArray that DFGTypeCheckHoistingPhase hoisted). Only the profiles for which ArrayMode::fromObserved ignores ArrayStorage are changed. The array profiles of calls are not changed. The DFG reads them to inline array intrinsics such as push() and indexOf(), and a call that has lost that costs every array much more than a generic access does. With them, a function with three push(), one shift() and one length access costs 43 ns per call for good after 200 calls with an ArrayStorage array, not 18 to 24 ns. Without them it costs 19 to 25 ns. A site that this makes generic has no exit site of its own. FixupPhase left the base of such a length access untyped, because it stopped at the failed attempt to make a GetArrayLength. It now goes on, so the access speculates on a cell like every other GetById. Options::useArrayStorageSamplesAsSpeculationFailures turns this off. * JSTests/stress/array-storage-sample-should-not-be-a-speculation-failure-without-a-failed-array-check.js: Added. * JSTests/stress/array-storage-should-not-recompile-a-function-for-each-site.js: Added. * Source/JavaScriptCore/bytecode/CodeBlock.cpp: (JSC::CodeBlock::jettison): (JSC::CodeBlock::recordArrayStorageSamplesAsSpeculationFailures): (JSC::CodeBlock::tallyFrequentExitSites): * Source/JavaScriptCore/bytecode/CodeBlock.h: * Source/JavaScriptCore/dfg/DFGFixupPhase.cpp: * Source/JavaScriptCore/runtime/OptionsList.h:
dd5b22b to
55be124
Compare
|
Closing this draft without merge. A second review of the final revision found costs that are too high for what the rule gives. All numbers are CPU ns per call on x64 Release
Most of the gain is also not from the rule. On unchanged The branch stays. The finding on #785 is real, and what to do about it is a policy choice that I put to @sosukesuzuki on the thread there. |
Withdrawn, not merged. The rule makes some functions slower for good on little evidence (Notes: "Why this was withdrawn"). The question stays on #785 (
dfg/DFGArrayMode.cpp:174thread).Problem
ArrayMode::fromObserved(dfg/DFGArrayMode.cpp:163) ignores ArrayStorage at a site until that site's own check failed.Fix (as proposed)
CodeBlock::tallyFrequentExitSitesrecords the ArrayStorage array each other indexed access saw last as its failure.Matrix4.multiply61 ns per call (was 124).JSTests/stresstests, one fails onmain.Background
ArrayProfile: the array shapes one bytecode saw, and the last structure from Baseline code.MultiGetByVal(larger).Downsides
Notes
Why this was withdrawn (CPU ns per call, x64 Release
jsc,mainb1e30d0)mainMatrix4.multiply, frozen operand in 1 call of 8mainwithOptions::reoptimizationRetryCounterMaxwritten in the running process. No option sets it today:runtime/Options.cpp:936computes 21. Onmainthe counter reaches 13 or 14 in the first two rows, and each step doubles the exit and tier-up thresholds.put_by_val.ArrayStorage(cherry-pick WebKit/WebKit#75376) #785: keep K + 2 compiles, bound the back-off with an Option, or Keep the elements of a frozen, sealed or non-extensible array in the ArrayStorage vector #752. A narrower rule of this kind (the exit count of 100 only, reads only, from the second jettison) was not built.What needed a decision (as written before the second review)
ArrayStorage(cherry-pick WebKit/WebKit#75376) #785 merged with "K of them in one function need up to K + 2 DFG compiles" under its Downsides. This change treats that as a defect when the arrays are frequent. No user reported it, and [JSC] Array profiles should not turn ordinary arrays intoArrayStorage(cherry-pick WebKit/WebKit#75376) #785 is in no Bun release yet: Bunmainpins a WebKit that has it, bun-v1.4.3 (2026-10-10) does not.main, for good (table below).ArrayStorage(cherry-pick WebKit/WebKit#75376) #785: ArrayStorage inMultiGetByValandMultiPutByVal, so that a mixed site needs no exit and no generic access.bytecode/ArrayProfile.cpp:133(the flag is permanent after one exit) stays open. This change does not answer it. It puts the same flag on more profiles.The rule
CodeBlock::tallyFrequentExitSites, for DFG code only. The jettison reason isJettisonDueToOSRExitorJettisonDueToBaselineLoopReoptimizationTrigger. The baseline block does not havem_didFailFTLCompilation(DFGJITFinalizersets it when the DFG code has no tier-up checks)....OnOSREntryFail, the third reason that counts a reoptimization, is left out. It is for OSR entries that failed. The default thresholds (5 exits for the loop trigger, 100 for this one) do not let it occur.BadCacheorBadIndexingTypewhose ownArrayProfilehasSpeculationFailedOnArrayStorageor a pending ArrayStorage failure structure. OrBadIndexingTypeat a bytecode with noArrayProfile: aCheckArraythatDFGTypeCheckHoistingPhasehoisted to where its variable is set, for an argument the entry of the function.CodeBlockof that exit (the inlined function's block for an inlined exit), but not the profile of a call. It has no pending failure, its last sample is an ArrayStorage structure, and its folded modes have Undecided, Int32, Double or Contiguous and no SlowPutArrayStorage. That last test is a copy of the one inArrayMode::fromObserved(dfg/DFGArrayMode.cpp:164-168).op_call,op_call_ignore_result,op_tail_callandop_iterator_opento inlinepush,pop,shift,indexOfand the other array intrinsics. A flagged call is a real call for every array. A first version had these profiles in the walk. Then a function with threepush, oneshiftand onelengthcost 43 ns per call for good. The cause was 200 calls with an array that was shifted when it held more than 128 elements. The function costs 18 to 24 ns onmainand 19 to 25 ns with this change.m_speculationFailureStructureID, the slot the exit stub writes, andDidPerformFirstRunPruning. Without the second, the next update prunes a profile that has one ordinary mode to ArrayStorage only, and the next compile speculates on ArrayStorage at that site (seen for alengthsite in an inlined function).DFGFixupPhase: alengthaccess that cannot becomeGetArrayLengthnow goes on toCellUse. A site that this change makes generic has no exit site of its own, so the fixup stopped at the failed attempt and left the base untyped. In the loop below with 1 frozen array in 10 calls that was 5,108 instructions per call. With the one-line change it is 4,902, the number ofmain.Options::useArrayStorageSamplesAsSpeculationFailures(default true) turns the rule off.Numbers (x64, Release
jsc, local builds ofmainb1e30d0 and of this branch with the flags of thebun-webkit-linux-amd64lane)DFG compiles and array-check exits,
--useConcurrentJIT=0, half of the arrays frozen unless noted:maino.a[i], K = 4, 8array.every(f): exitsK reads, then a 64-iteration integer loop, default options, 6M calls, 10 interleaved runs, CPU ns per call (median, range) and DFG compiles:
mainmainthese runs leave the reoptimization retry counter of the function at 5, 9, 13, 14 and 14. With this change it is 1. Each step doubles the exit and tier-up thresholds for as long as the baselineCodeBlocklives. That is why K = 12 and up end in the DFG.Matrix4.multiply(its own source, with its frozenMatrix4.IDENTITYas the left operand), default options, 6M calls, 5 runs, CPU ns per call. 1 in 8 calls frozen: 123.9 (121.1 to 127.0), 13 compiles, ends in the DFG onmain. 61.1 (59.8 to 62.3), 2 compiles, ends in the FTL with this change. 1 in 100: 55.2 and 10 compiles, 41.6 and 2.--useConcurrentJIT=0): 38 kinds of sites, 10 sources of ArrayStorage arrays, K = 1, 2, 4, 8, both orders of the two kinds of arrays, 1 ArrayStorage array in 2, 3, 10 or 100 calls. 66 throw (a frozen array takes nopush). 217 have no array-check exit on either build. 919 need fewer compiles or exits. 470 are equal. 10 are worse (thearray.maporder below). Array-check exits in total: 5,979,765 onmain, 1,440,798 with this change.Array::Generic, as onmainonce it settles. That state is slower than the ArrayStorage code before [JSC] Array profiles should not turn ordinary arrays intoArrayStorage(cherry-pick WebKit/WebKit#75376) #785, and this change does not touch it.After an early burst of ArrayStorage arrays (the arrays come N times at the start, then never again. CPU ns per call on ordinary arrays in the last quarter of 4M calls,
--useConcurrentJIT=0, 3 runs, with the DFG compiles)mainfor (i < a.length) s += a[i], 64 elements, no burstMatrix4.multiply, 2,000 frozen, 1 in 8push,shift,length: 200 shifted arrays, 1 in 2main, and the inline caches of that code have seen both kinds of arrays.main, 65 with this change. 2,000, 1 in 2: 82 and 82. 2,000, 1 in 10: 86 and 111.When ArrayStorage arrays are rare (
for (i < a.length) s += a[i], 64 elements, 16M calls,--useConcurrentJIT=0, user instructions)mainmain, 21.8 to 45.8 G with this change, and a median of 37.1 G on both.Not fixed, equal to
mainindexOf,atandincludes, 9 for fourpushand fourpop. The profiles of calls are left out.--useFTLJIT=0, or a function the FTL does not compile): K = 8 stays at 10 compiles.array.length(a.length + a.length + a.length + a[0]): 2 or 3 compiles, 5 or 6 onmain. The frozen array can fail a later site, and the sites before that one do not run in the Baseline code after the exit.ArrayProfilethat fails first (hoisted to the entry, or the one of a property load such asarray[Symbol.iterator]): that jettison teaches nothing, so 3 compiles and 302 exits. This is the usual case for sparse arrays and for arrays that held more than 128 elements whenshift()was called, because they keep the original structure.Worse than
mainarray.map(f)on arrays that keep the original structure (sparse, or shifted), with--useConcurrentJIT=0. In one order of the calls, each compile samples the ArrayStorage array. Thenmapneeds 5 compiles and 1,504 exits, 4 and 703 onmain.get_by_valgeneric one compile sooner. The one check of the array that is left is the one ofnew_array_with_species. It is then hoisted, and a hoisted structure check teaches no profile when it fails.main. With default options, 10 runs each: 3 or 4 compiles and a median of 302 exits, 4 to 6 and 2,276 onmain.Sites made generic with no failed check of their own
CheckArrayfails on Double arrays until the jettison, and the last call that exited before it had a frozen array at two other sites. Those 2 sites become generic.UnlinkedArrayProfilelike any other, so it reaches laterCodeBlocks of the function.What it adds for code that never sees ArrayStorage
objdumpofupdateAllArrayProfilePredictions, bothcomputeUpdatedPrediction,ArrayMode::fromObserved,DFG::OSRExit::compileExit,FTL::compileStub,getArrayProfileandreconcileWeakReferencesAtGCEnd: 0 differing instructions.getArrayProfilefor eachBadCacheorBadIndexingTypeamong them. TypeScript 6.0.2 checking three files, 3 rounds, default options, 3 runs: 439 to 454 jettisons, 202 to 220 calls, 128 to 144 lookups, 7 walks over 12 profiles, 2 or 3 profiles changed. 40 rounds, 6 runs each: 28.4 to 30.1 G instructions onmain, 28.4 to 29.8 G with this change. First 90 files ofJSTests/microbenchmarks: 25 jettisons, 11 calls, 0 lookups, 0 walks.size,nm -S): text 43,911,959 to 43,915,719 bytes (+3,760). The new function is 2,579 bytes,tallyFrequentExitSitesgrows from 461 to 765.bloatyis not installed.DFGFixupPhase: everylengthaccess that cannot becomeGetArrayLengthand whose base is predicted to be a cell now hasCellUse.Tests
array-storage-should-not-recompile-a-function-for-each-site.js: reads, stores,in,length, destructuring, an inlined function, a non-array object and a loop that a Baseline back edge jettisons, with frozen arrays, sparse arrays and arrays aftershift(). Onmain: "read was compiled 8 times, expected 2". Its second run line turns the option off and expects the counts ofmain.array-storage-sample-should-not-be-a-speculation-failure-without-a-failed-array-check.js: seven cases in which a sample must stay a sample. It passes onmaintoo.BadCachewith no profile opens the ruleBadIndexingTypewith no profile does notget_by_val,put_by_val,in_by_val,get_lengthoriterator_nextis out of the walk (5 builds)DidPerformFirstRunPruningis removednew_array_with_speciesis out of the walkop_iterator_openis in the walk...OnOSREntryFailreason is addedlengthsite in the second compile: ArrayStorage without the line, generic with it. The for-in opcodes andput_by_val_directhave no test case.run-javascriptcore-testson this branch. Release build,--filter 'array|get-by-val|put-by-val|getbyval|putbyval|for-of|iterator|destructur|spread|species|freeze|frozen|seal|sparse|indexing|prevent|storage|arrayify|jettison|osr|exit|length': 31,416 runs, 1 failure. Assertion build (ENABLE_ASSERTS=ON),--filter 'array-storage|arraystorage|arrayify|frozen|freeze|seal|sparse|prevent-extensions|preventExtensions|array-profile|arrayprofile|allocation-profile|indexing|jettison|osr-exit|exit-profile|array-should-not|array-without-elements|length': 11,509 runs, 1 failure.stress/bigint-inc-dec-in-place.js.eager-jettison-no-cjitgoes over the 600 MB limit of--memory-limitedon this machine. It does the same on the builds ofmain(3 of 3 runs each).Self-review items not done
ArrayMode::fromObservedand this rule both call: it edits lines of the upstream patch in a commit that is not upstream. The comment inCodeBlock.cppnames the copy.CodeBlock:UnlinkedArrayProfile::updatecopies the flag, and this change does not touch it.Related
SlowPutArrayStorageshape. After it,fromObserveddoes not ignore the shape of a frozen array, and this rule applies to sparse arrays and arrays aftershift()only. Its rebase has to keep the condition inCodeBlock.cppequal to the one inDFGArrayMode.cpp.ArrayStorageWebKit/WebKit#75376 is the upstream patch. This commit has noUSE(BUN_JSC_ADDITIONS)guard, like 4263129, so that it can go upstream with it.