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

Fix overly aggressive function hoisting (#3125) #4000

Closed
wants to merge 13 commits into from
Closed

Fix overly aggressive function hoisting (#3125) #4000

wants to merge 13 commits into from

Conversation

dasZGFz
Copy link
Contributor

@dasZGFz dasZGFz commented Nov 26, 2019

Attempt to fix #3125

Before submitting the PR, please make sure you do the following

  • It's really useful if your PR relates to an outstanding issue, so please reference it in your PR, or create an explanatory one for discussion. In many cases features are absent for a reason.
  • This message body should clearly illustrate what problems it solves. If there are related issues, remember to reference them.
  • Ideally, include a test that fails without this PR but passes with it. PRs will only be merged once they pass CI. (Remember to npm run lint!)

Tests

  • Run the tests tests with npm test or yarn test)

@dasZGFz dasZGFz changed the title try 1 [WIP]: Fix #3125 Nov 26, 2019
@dasZGFz dasZGFz changed the title [WIP]: Fix #3125 Fix #3125 Nov 26, 2019
Copy link
Member

@mrkishi mrkishi left a comment

Choose a reason for hiding this comment

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

I think this is going the right direction! @Conduitry or @Rich-Harris should probably weigh in.

Do you think looking into the other aliasing forms is worth it? I believe so since we have so many truly hoistable functions even accounting for aliasing.

src/compiler/compile/Component.ts Outdated Show resolved Hide resolved
src/compiler/compile/Component.ts Outdated Show resolved Hide resolved
src/compiler/interfaces.ts Outdated Show resolved Hide resolved
src/compiler/compile/Component.ts Outdated Show resolved Hide resolved
@Conduitry
Copy link
Member

Is it possible to write a runtime test for this instead of a js sample test, which is a lot more brittle?

@@ -160,4 +160,5 @@ export interface Var {
hoistable?: boolean;
subscribable?: boolean;
is_reactive_dependency?: boolean;
aliased?: boolean;
Copy link
Member

Choose a reason for hiding this comment

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

What exactly does the aliased field mean? I see that it's another thing that can stop variables from being hoisted, but I don't understand what it's checking for.

Copy link
Member

Choose a reason for hiding this comment

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

The idea was to have aliased mean whether the variable is passed around functions or given other names as to avoid hoisting functions/objects because they could've been modified through these other names.

We'd still have to track all forms of aliasing though.

Copy link
Member

Choose a reason for hiding this comment

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

Tracking all forms of aliasing doesn't sound like it would be possible. Is this another situation where we just need to document how the compiler works - like how only certain types of assignments trigger reactivity, rather than embarking on a never-ending journey to catch more cases?

Copy link
Member

Choose a reason for hiding this comment

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

Hm, why not? We wouldn't need perfect tracking, just knowing whether the variable escapes a single name -- the possibility it's used elsewhere. The other option would be to disable function hoisting (like currently for objects), since the way it stands functions that shouldn't be hoisted, are.

@@ -1079,7 +1093,7 @@ export default class Component {
} else if (owner === instance_scope) {
const variable = var_lookup.get(name);

if (variable.reassigned || variable.mutated) hoistable = false;
if (variable.reassigned || variable.mutated || variable.aliased) hoistable = false;
Copy link
Member

Choose a reason for hiding this comment

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

I don't think this is the right place for this. This is marking the function not hoistable if it references aliased variables, instead of marking the function not hoistable if itself is aliased.

@stale
Copy link

stale bot commented Jun 26, 2021

This pull request has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

@stale stale bot added the stale-bot label Jun 26, 2021
@stale stale bot removed the stale-bot label Jun 26, 2021
@stale stale bot removed the stale-bot label Jun 27, 2021
@benmccann benmccann changed the title Fix #3125 Fix overly aggressive function hoisting (#3125) Jul 23, 2021
@dummdidumm
Copy link
Member

Closing as the PR is severely outdated, there doesn't seem concensus on how to move forward and the code might change anyway with v5

@dummdidumm dummdidumm closed this Mar 2, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Function hoisting is too aggressive
6 participants