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): keep a single map of unresolved references #4107

Merged

Conversation

lucab
Copy link
Member

@lucab lucab commented Jul 8, 2024

This reworks ScopeTree in order to keep a single (root) map of unresolved references. The SemanticBuilder keeps track of all intermediate ones while walking scopes, and it can get rid of all non-root ones once done.

Ref: oxc-project/backlog#32

This reworks `ScopeTree` in order to keep a single (root) map
of unresolved references. The `SemanticBuilder` keeps track of
all intermediate ones while walking scopes, and it can get rid
of all non-root ones once done.
Copy link

graphite-app bot commented Jul 8, 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 8, 2024
@lucab
Copy link
Member Author

lucab commented Jul 8, 2024

/cc @overlookmotel

Copy link

codspeed-hq bot commented Jul 8, 2024

CodSpeed Performance Report

Merging #4107 will not alter performance

Comparing lucab:ups/semantic-scope-root-unresolved-references (c77114a) with main (115ac3b)

Summary

✅ 29 untouched benchmarks

Copy link
Member

@Boshen Boshen left a comment

Choose a reason for hiding this comment

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

Clever! Thank you!

@Boshen Boshen merged commit 9114c8e into oxc-project:main Jul 8, 2024
26 checks passed
@Boshen
Copy link
Member

Boshen commented Jul 8, 2024

@lucab Given your recent contributions, I have sent you invitation to the repo with write permission so you can use graphite. https://github.com/oxc-project/oxc/invitations

@github-actions github-actions bot mentioned this pull request Jul 8, 2024
Boshen added a commit that referenced this pull request Jul 8, 2024
## [0.17.2] - 2024-07-08

### Features

- 115ac3b allocator: Introduce `FromIn` and `IntoIn` traits. (#4088)
(rzvxa)
- 720983a napi/transform: Allow setting `sourceType` to `transform`
(#4113) (Boshen)
- e386b62 semantic: Check for invalid type import assignments (#4097)
(DonIsaac)

### Bug Fixes

- 5472b7c codegen: 256 indentations level is not enough for codegen
(Boshen)
- 5c31236 isolated-declarations: Keep literal value for readonly
property (#4106) (Dunqing)
- e67c7d1 isolated-declarations: Do not infer type for private
parameters (#4105) (Dunqing)
- 3fcad5e isolated_declarations: Remove nested AssignmentPatterns from
inside parameters (#4077) (michaelm)
- f8d77e4 isolated_declarations: Infer type of template literal
expressions as string (#4068) (michaelm)
- 0f02608 semantic: Bind `TSImportEqualsDeclaration`s (#4100) (Don
Isaac)
- 4413e2d transformer: Missing initializer for readonly consructor
properties (#4103) (Don Isaac)

### Performance

- 7ed27b7 isolated-declarations: Use `FxHashSet` instead of `Vec` to
speed up the `contain` (#4074) (Dunqing)
- 9114c8e semantic: Keep a single map of unresolved references (#4107)
(Luca Bruno)

Co-authored-by: Boshen <Boshen@users.noreply.github.com>
@lucab
Copy link
Member Author

lucab commented Jul 8, 2024

For reference, I tried to push unresolved_references directly into Semantic (as suggested in oxc-project/backlog#32) but eventually found out that at least the JSX transformer needs to access it through ScopeTree (it doesn't seem to have access to Semantic in its traversing context):

/// Create `IdentifierReference` for var name in current scope which is read from
fn get_read_identifier_reference<'a>(
span: Span,
name: Atom<'a>,
ctx: &mut TraverseCtx<'a>,
) -> IdentifierReference<'a> {
let reference_id =
ctx.create_reference_in_current_scope(name.to_compact_str(), ReferenceFlag::Read);
IdentifierReference::new_read(span, name, Some(reference_id))
}

@lucab lucab deleted the ups/semantic-scope-root-unresolved-references branch July 8, 2024 11:50
@overlookmotel
Copy link
Collaborator

Nice work @lucab!

I can see potential for one further optimization...

When exiting a scope, UnresolvedReferences for that scope is drained, and then that FxHashMap is never used again. We end up creating a short-lived FxHashMap for every scope.

Instead of unresolved_references being a Vec indexed by scope ID, could it be a stack? When you exit a scope, UnresolvedReferences for that scope would be cleared, but left in unresolved_references. Then when entering next sibling scope, you reuse the same UnresolvedReferences as before. This would reduce allocations, and also you'd mostly be using a FxHashMap which is already warm in cache.

Does that make sense, and do you think it could work/make a difference?

@overlookmotel
Copy link
Collaborator

I tried to push unresolved_references directly into Semantic

Personally, I think ScopeTree is the best place for it. Unresolved references could be considered to be the "global scope".

Boshen pushed a commit that referenced this pull request 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)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-semantic Area - Semantic
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants