Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[JSC] ForStatement should call CreatePerIterationEnvironment before first iteration #19403

Conversation

shvaikalesh
Copy link
Member

@shvaikalesh shvaikalesh commented Oct 22, 2023

f32cd0f

[JSC] ForStatement should call CreatePerIterationEnvironment before first iteration
https://bugs.webkit.org/show_bug.cgi?id=189799
<rdar://problem/44730906>

Reviewed by Justin Michaud.

This change implements step 2 of ForBodyEvaluation [1], but only if loop initializer contains a function
that captures a variable from outer scope (so-called closure), to preserve performance of the common case.

Emitting prepareLexicalScopeForNextForLoopIteration() unconditionally would otherwise slow down any `for`
loop with body statement containing a closure (which is a quite common) by 10-20%.

Before this patch, a closure inside `for` loop initializer was created with parent scope of the first
iteration, rather than a separate lexical scope, ending up capturing incorrect loop counter value.

Aligns JSC with V8 and SpiderMonkey.

[1]: https://tc39.es/ecma262/#sec-forbodyevaluation

* JSTests/stress/regress-189799.js: Added.
* JSTests/test262/expectations.yaml: Mark 2 tests as passing.
* Source/JavaScriptCore/bytecompiler/NodesCodegen.cpp:
(JSC::ForNode::emitBytecode):
* Source/JavaScriptCore/parser/ASTBuilder.h:
(JSC::ASTBuilder::createForLoop):
* Source/JavaScriptCore/parser/NodeConstructors.h:
(JSC::ForNode::ForNode):
* Source/JavaScriptCore/parser/Nodes.h:
* Source/JavaScriptCore/parser/Parser.cpp:
(JSC::Parser<LexerType>::parseForStatement):
* Source/JavaScriptCore/parser/SyntaxChecker.h:
(JSC::SyntaxChecker::createForLoop):

Canonical link: https://commits.webkit.org/269653@main

7d06af9

Misc iOS, tvOS & watchOS macOS Linux Windows
βœ… πŸ§ͺ style βœ… πŸ›  ios βœ… πŸ›  mac βœ… πŸ›  wpe βœ… πŸ›  wincairo
βœ… πŸ›  ios-sim βœ… πŸ›  mac-AS-debug βœ… πŸ§ͺ wpe-wk2
βœ… πŸ§ͺ webkitperl βœ… πŸ§ͺ ios-wk2 βœ… πŸ§ͺ api-mac βœ… πŸ›  gtk
βœ… πŸ§ͺ ios-wk2-wpt βœ… πŸ§ͺ mac-wk1 βœ… πŸ§ͺ gtk-wk2
βœ… πŸ›  πŸ§ͺ jsc βœ… πŸ§ͺ api-ios βœ… πŸ§ͺ mac-wk2 βœ… πŸ§ͺ api-gtk
βœ… πŸ›  πŸ§ͺ jsc-arm64 βœ… πŸ›  tv βœ… πŸ§ͺ mac-AS-debug-wk2 βœ… πŸ›  jsc-armv7
βœ… πŸ›  tv-sim βœ… πŸ§ͺ jsc-armv7-tests
βœ… πŸ›  watch βœ… πŸ›  jsc-mips
βœ… πŸ›  πŸ§ͺ unsafe-merge βœ… πŸ›  watch-sim βœ… πŸ§ͺ jsc-mips-tests

@shvaikalesh shvaikalesh requested a review from a team as a code owner October 22, 2023 23:03
@shvaikalesh shvaikalesh self-assigned this Oct 22, 2023
@shvaikalesh shvaikalesh added the JavaScriptCore For bugs in JavaScriptCore, the JS engine used by WebKit, other than kxmlcore issues. label Oct 22, 2023
@shvaikalesh shvaikalesh force-pushed the eng/JSC-ForStatement-should-call-CreatePerIterationEnvironment-before-first-iteration branch from 1b96738 to 7d06af9 Compare October 22, 2023 23:08
Copy link
Contributor

@justinmichaud justinmichaud left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

r=me

@shvaikalesh shvaikalesh added the unsafe-merge-queue Applied to send a pull request to merge-queue, but skip building and testing label Oct 23, 2023
…irst iteration

https://bugs.webkit.org/show_bug.cgi?id=189799
<rdar://problem/44730906>

Reviewed by Justin Michaud.

This change implements step 2 of ForBodyEvaluation [1], but only if loop initializer contains a function
that captures a variable from outer scope (so-called closure), to preserve performance of the common case.

Emitting prepareLexicalScopeForNextForLoopIteration() unconditionally would otherwise slow down any `for`
loop with body statement containing a closure (which is a quite common) by 10-20%.

Before this patch, a closure inside `for` loop initializer was created with parent scope of the first
iteration, rather than a separate lexical scope, ending up capturing incorrect loop counter value.

Aligns JSC with V8 and SpiderMonkey.

[1]: https://tc39.es/ecma262/#sec-forbodyevaluation

* JSTests/stress/regress-189799.js: Added.
* JSTests/test262/expectations.yaml: Mark 2 tests as passing.
* Source/JavaScriptCore/bytecompiler/NodesCodegen.cpp:
(JSC::ForNode::emitBytecode):
* Source/JavaScriptCore/parser/ASTBuilder.h:
(JSC::ASTBuilder::createForLoop):
* Source/JavaScriptCore/parser/NodeConstructors.h:
(JSC::ForNode::ForNode):
* Source/JavaScriptCore/parser/Nodes.h:
* Source/JavaScriptCore/parser/Parser.cpp:
(JSC::Parser<LexerType>::parseForStatement):
* Source/JavaScriptCore/parser/SyntaxChecker.h:
(JSC::SyntaxChecker::createForLoop):

Canonical link: https://commits.webkit.org/269653@main
@webkit-commit-queue webkit-commit-queue force-pushed the eng/JSC-ForStatement-should-call-CreatePerIterationEnvironment-before-first-iteration branch from 7d06af9 to f32cd0f Compare October 23, 2023 17:32
@webkit-commit-queue
Copy link
Collaborator

Committed 269653@main (f32cd0f): https://commits.webkit.org/269653@main

Reviewed commits have been landed. Closing PR #19403 and removing active labels.

@webkit-commit-queue webkit-commit-queue merged commit f32cd0f into WebKit:main Oct 23, 2023
@webkit-commit-queue webkit-commit-queue removed the unsafe-merge-queue Applied to send a pull request to merge-queue, but skip building and testing label Oct 23, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
JavaScriptCore For bugs in JavaScriptCore, the JS engine used by WebKit, other than kxmlcore issues.
Projects
None yet
4 participants