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

perf(semantic): store unresolved refs in a stack #4162

Merged
merged 1 commit into from
Jul 10, 2024

Conversation

lucab
Copy link
Collaborator

@lucab lucab commented Jul 10, 2024

This tweaks SemanticBuilder logic in order to accumulate unresolved
references in a stack, getting rid of the previous index-vector which
is not required under the current access pattern.

Ref: #4107 (comment)

Copy link

graphite-app bot commented Jul 10, 2024

Your org has enabled the Graphite merge queue for merging into main

Add the label “merge” to the PR and Graphite will automatically add it to the merge queue when it’s ready to merge. Or use the label “hotfix” to add to the merge queue as a hot fix.

You must have a Graphite account and log in to Graphite in order to use the merge queue. Sign up using this link.

@github-actions github-actions bot added the A-semantic Area - Semantic label Jul 10, 2024
Copy link
Collaborator Author

lucab commented Jul 10, 2024

This stack of pull requests is managed by Graphite. Learn more about stacking.

Join @lucab and the rest of your teammates on Graphite Graphite

Copy link

codspeed-hq bot commented Jul 10, 2024

CodSpeed Performance Report

Merging #4162 will improve performances by 4.44%

Comparing ups/semantics-refs-stack (2203143) with main (f2b3273)

Summary

⚡ 5 improvements
✅ 24 untouched benchmarks

Benchmarks breakdown

Benchmark main ups/semantics-refs-stack Change
semantic[antd.js] 212.1 ms 205.9 ms +3.03%
semantic[checker.ts] 136.3 ms 130.5 ms +4.44%
semantic[pdf.mjs] 33.3 ms 32 ms +3.99%
transformer[checker.ts] 203 ms 195.4 ms +3.87%
transformer[pdf.mjs] 53 ms 51.2 ms +3.63%

@overlookmotel
Copy link
Collaborator

Nice work! However, I think we can potentially do even better.

What I had in mind is that you never call unresolved_references.pop(). Instead, hold on to the hash map and re-use it. That way we avoid generating (and allocating) new hash map when entering a scope most of the time.

unresolved_references is Vec<UnresolvedReferences>, indexed by scope depth.

e.g. with this input:

function foo() {}
function bar() {}
  • Enter top level scope:
    • Depth is 0.
    • Push a UnresolvedReferences hash map to unresolved_references.
    • Current hashmap is unresolved_references[0].
  • Enter scope for foo:
    • Increment depth to 1.
    • Push another UnresolvedReferences hash map to unresolved_references.
    • Current hashmap is unresolved_references[1].
  • Exit scope for foo:
    • Clear current hashmap (unresolved_references[1]).
    • Decrement depth to 0.
    • Do NOT pop from unresolved_references.
    • Current hashmap is now unresolved_references[0].
  • Enter scope for bar:
    • Increment depth to 1.
    • Do NOT push to unresolved_references.
    • Current hashmap is unresolved_references[1] (i.e. reuse same hash map that was used for foo)
  • Exit scope for bar: same as for foo
  • Exit top level scope: same

Does this make any sense? I think it'll be a further perf boost due to reducing allocations, and reusing allocations which are already warm in the cache.

@lucab lucab marked this pull request as ready for review July 10, 2024 12:34
@lucab
Copy link
Collaborator Author

lucab commented Jul 10, 2024

@overlookmotel I see, you want to reuse previously allocated hashmaps (sorry I missed that point from your original request). Do you mind if I try to do that in a separate PR after landing this one, so that it's easier to see the performance delta?

@overlookmotel
Copy link
Collaborator

Do you mind if I try to do that in a separate PR after landing this one

Yes, sure. Whichever way you prefer. In any case, this PR is already a really nice perf boost. Also... feel free not to do it at all! It's completely up to you what you want to work on. I'm just making suggestions, not commands!

@overlookmotel
Copy link
Collaborator

overlookmotel commented Jul 10, 2024

If you're happy with this PR, let me know and I'll merge.

@overlookmotel overlookmotel changed the title refactor(semantic): store unresolved refs in a stack perf(semantic): store unresolved refs in a stack Jul 10, 2024
@lucab
Copy link
Collaborator Author

lucab commented Jul 10, 2024

Yes, this one is ready to merge.

@Boshen Boshen added the 0-merge Merge with Graphite Merge Queue label Jul 10, 2024
Copy link

graphite-app bot commented Jul 10, 2024

Merge activity

  • Jul 10, 10:44 AM EDT: The merge label 'merge' was detected. This PR will be added to the Graphite merge queue once it meets the requirements.
  • Jul 10, 10:46 AM EDT: Boshen added this pull request to the Graphite merge queue.
  • Jul 10, 11:11 AM EDT: Boshen merged this pull request with the Graphite merge queue.

This tweaks `SemanticBuilder` logic in order to accumulate unresolved
references in a stack, getting rid of the previous index-vector which
is not required under the current access pattern.

Ref: #4107 (comment)
@graphite-app graphite-app bot merged commit 2203143 into main Jul 10, 2024
26 checks passed
@graphite-app graphite-app bot deleted the ups/semantics-refs-stack branch July 10, 2024 15:11
@github-actions github-actions bot mentioned this pull request Jul 11, 2024
Boshen added a commit that referenced this pull request Jul 11, 2024
## [0.20.0] - 2024-07-11

- 5731e39 ast: [**BREAKING**] Store span details inside comment struct
(#4132) (Luca Bruno)

### Features

- 67fe75e ast, ast_codegen: Pass the `scope_id` to the `enter_scope`
event. (#4168) (rzvxa)
- 54cd04a minifier: Implement dce with var hoisting (#4160) (Boshen)
- 44a894a minifier: Implement return statement dce (#4155) (Boshen)
- 725571a napi/transformer: Add `jsx` option to force parsing with jsx
(#4133) (Boshen)

### Bug Fixes

- 48947a2 ast: Put `decorators` before everything else. (#4143) (rzvxa)
- 7a059ab cfg: Double resolution of labeled statements. (#4177) (rzvxa)
- 4a656c3 lexer: Incorrect lexing of large hex/octal/binary literals
(#4072) (DonIsaac)
- 28eeee0 parser: Fix asi error diagnostic pointing at invalid text
causing crash (#4163) (Boshen)

### Performance

- ddfa343 diagnostic: Use `Cow<'static, str>` over `String` (#4175)
(DonIsaac)
- 2203143 semantic: Store unresolved refs in a stack (#4162) (lucab)
- fca9706 semantic: Faster search for leading comments (#4140) (Boshen)

### Documentation

- bdcc298 ast: Update the note regarding the `ast_codegen` markers.
(#4149) (rzvxa)

### Refactor

- 03ad1e3 semantic: Tweak comment argument type (#4157) (lucab)

Co-authored-by: Boshen <Boshen@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
0-merge Merge with Graphite Merge Queue A-semantic Area - Semantic
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants