[JSC] Add emitBytecodeInConditionContext for optional chaining#61415
Merged
webkit-commit-queue merged 1 commit intoApr 25, 2026
Conversation
Collaborator
|
EWS run on previous version of this PR (hash f26a233) Details |
Constellation
approved these changes
Apr 9, 2026
f26a233 to
25a17af
Compare
Collaborator
|
EWS run on current version of this PR (hash 25a17af) Details |
https://bugs.webkit.org/show_bug.cgi?id=310815 Reviewed by Yusuke Suzuki. `if (a?.x)` emitted `mov Undefined` + `jfalse` on short-circuit, even though undefined is always falsy. This wastes 2 instructions (8 -> 6) and requires materializing the undefined constant. Implement OptionalChainNode::emitBytecodeInConditionContext to route the short-circuit bail-out directly to falseTarget instead of materializing undefined. The optimization composes with existing condition-context handling in LogicalNotNode, LogicalOpNode, and ConditionalNode, so patterns like `if (!a?.x)`, `if (a?.x && b?.y)`, `while (node?.next)`, and `a?.x ? 1 : 2` are optimized automatically. `delete a?.x` is excluded since its short-circuit result is true, not undefined. Before: [ 1] jundefined_or_null arg1, ->12 [ 4] get_by_id loc5, arg1, "x" [10] jmp ->15 [12] mov loc5, Undefined [15] jfalse loc5, ->20 [18] ret 1 [20] ret 2 After: [ 1] jundefined_or_null arg1, ->15 [ 4] get_by_id loc5, arg1, "x" [10] jfalse loc5, ->15 [13] ret 1 [15] ret 2 Test: JSTests/stress/optional-chaining-in-condition-context.js * JSTests/stress/optional-chaining-in-condition-context.js: Added. (shouldBe): (testNested): (testNot): (testAnd): (testOr): (testTernary): (testWhile): (testDoWhile): (testFor): (testOptCall): (testBracket): (testDelete): (testDeep5): (testMixed): (testPrimitive): (testComplexLogic): (testNestedTernary): (testDoubleNot): (testSideEffect): (testBracketSideEffect): (let.getterObj.get x): (let.getterFalsy.get x): (let.getterThrows.get x): (testGetter): (testGetterInTry): (testProxy): (testSymbol): (testCompareUndef): (testCompareNull): (testCoalesce): (testParenthesized): (testIfElseChain): (testSwitch): (testPoly): (testFinally): (testGenerator): (async testAsync): (testThis): (testInnerChain): (testComma): (get t): (has): * Source/JavaScriptCore/bytecompiler/BytecodeGenerator.cpp: (JSC::BytecodeGenerator::pushOptionalChainTarget): (JSC::BytecodeGenerator::discardOptionalChainTarget): * Source/JavaScriptCore/bytecompiler/BytecodeGenerator.h: * Source/JavaScriptCore/bytecompiler/NodesCodegen.cpp: (JSC::OptionalChainNode::emitBytecodeInConditionContext): * Source/JavaScriptCore/parser/Nodes.h: Canonical link: https://commits.webkit.org/312001@main
25a17af to
bfab895
Compare
Collaborator
|
Committed 312001@main (bfab895): https://commits.webkit.org/312001@main Reviewed commits have been landed. Closing PR #61415 and removing active labels. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
bfab895
25a17af