Closed Bug 2066996 Opened 3 months ago Closed 2 months ago

Emit for-of IteratorClose bytecode after the loop

Categories

(Core :: JavaScript Engine, task, P2)

task

Tracking

()

RESOLVED FIXED
157 Branch
Tracking Status
firefox157 --- fixed

People

(Reporter: jandem, Assigned: jandem)

Details

Attachments

(3 external files)

for-of loops need to close the iterator on non-local exits. Today we handle this by emitting the IteratorClose bytecode for each non-local exit path. This has two downsides:

  • The iterator closing is logically outside the try-catch but inside it in the bytecode. Handling this requires ForOfIterClose try notes - see Iain's beautiful ASCII-art here.
  • When the loop has multiple break or return statements, we emit the iterator-close bytecode for each one separately. This becomes a problem with await and using because we emit a lot of iterator-closing bytecode in these cases.

What we do for try-finally and (since bug 2063576) destructuring, is that we emit the IteratorClose code after the main bytecode and then jump to it. After closing the iterator we then continue the non-local exit. Iain and I were hoping this would also work for for-of loops, so I tried it and it does work nicely. This lets us remove the ForOfIterClose try note and we can share bytecode for similar non-local exits.

This also speeds up some for-of loops with break statements because we now avoid some phis that blocked scalar replacement.

The old code didn't handle the CatchA => CatchB => ForOf case correctly, because
it treated both catch blocks as synthetic for-of catch blocks instead of only CatchB.

This caused a debugger jit-test failure when we remove the ForOfIterClose try note
in a later patch.

This changes for-of loops to match what finally blocks and (since bug 2063576)
array destructuring do. The non-local exit bytecode jumps to shared code emitted
after the loop, where we close the iterator and continue the non-local exit.

The patch also simplifies NonLocalExitControl::emitNonLocalJump a bit. We no
longer need the jumpingToCleanup boolean and can instead return immediately.

This lets us remove TryNoteKind::ForOfIterClose in the next patch. It also lets us
share bytecode for similar non-local exits, which matters for await and using loops
because in these cases we can emit a lot of bytecode.

Micro-benchmarks show that some for-of loops with break statements are much faster
because we now avoid some phis that prevented scalar replacement.

Severity: -- → N/A
Priority: -- → P2
Status: ASSIGNED → RESOLVED
Closed: 2 months ago
Resolution: --- → FIXED
Target Milestone: --- → 157 Branch
QA Whiteboard: [qa-triage-done-c158/b157]
You need to log in before you can comment on or make changes to this bug.

Attachment

General

Created:
Updated:
Size: