Closed Bug 1971446 Opened 10 months ago Closed 9 months ago

Remove hole check from LoadElementV

Categories

(Core :: JavaScript Engine: JIT, enhancement, P2)

enhancement

Tracking

()

RESOLVED FIXED
142 Branch
Tracking Status
firefox142 --- fixed

People

(Reporter: iain, Assigned: iain)

References

(Blocks 1 open bug)

Details

(Whiteboard: [js-perf-next])

Attachments

(3 files)

We use LLoadElementV to load a value from an array in Ion. When we do, we unconditionally generate a branchTestMagic to ensure that the value loaded is not the magic hole value. When we are looping over a large array, the cost of this check can add up. For example, on this microbenchmark, I get a 25% performance improvement by commenting out the hole check:

function foo(arr, i) {
  return arr[i];
}

function bar() {
  let result = 0;
  for (var i = 0; i < arr.length; i++) {
    result += foo(arr, i);
  }
  return result;
}

with ({}) {}
let arr = [];
for (var i = 0; i < 10000; i++) {
  arr.push(i);
}

let start = performance.now()
for (var i = 0; i < 100000; i++) {
  bar();
}
print(performance.now() - start);

There are more efficient ways to do this. For example, checking the IsNonPacked flag on the elements is probably a bit cheaper and, more importantly, can be hoisted outside the loop because it doesn't depend on the index.

So we could add a MIR which generates a instruction like https://searchfox.org/mozilla-central/source/js/src/jit/CodeGenerator.cpp#15126-15127 and add it before every place we generate the MLoadElement instruction?

Flags: needinfo?(iireland)

That instruction is setting the NonPacked flag when we store a hole value. We want to guard that the flag is not set, similar to GuardArrayIsPacked, but without the array-specific length == initializedLength check.

I wrote a patch for this yesterday. You can see the try run here.

Flags: needinfo?(iireland)

Thank you so much for explaining i shall study the patch! :-)

Assignee: nobody → iireland
Status: NEW → ASSIGNED
Whiteboard: [js-perf-next]
Blocks: 1973117
Status: ASSIGNED → RESOLVED
Closed: 9 months ago
Resolution: --- → FIXED
Target Milestone: --- → 142 Branch
QA Whiteboard: [qa-triage-done-c143/b142]
You need to log in before you can comment on or make changes to this bug.

Attachment

General

Created:
Updated:
Size: