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(wasm): generate scope text with visitor #5264

Merged
merged 1 commit into from
Aug 27, 2024

Conversation

overlookmotel
Copy link
Contributor

@overlookmotel overlookmotel commented Aug 27, 2024

Follow-on after #5232. oxc_wasm build scopes text with a single AST traversal. Previous implementation was O($n^2$).

If we can assume scopes are listed in traversal order, then we could do it a bit more efficiently just from ScopeTree, but this approach of using Visit will handle out-of-order scope IDs (which you'd get if printing a post-transform ScopeTree).

Also reduce creating and discarding Strings for indentation - reuse a single string instead.

Copy link

graphite-app bot commented Aug 27, 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.

Copy link
Contributor Author

overlookmotel commented Aug 27, 2024

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

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

@overlookmotel overlookmotel marked this pull request as ready for review August 27, 2024 13:18
Copy link

codspeed-hq bot commented Aug 27, 2024

CodSpeed Performance Report

Merging #5264 will not alter performance

Comparing 08-27-perf_wasm_generate_scope_text_with_visitor (6a49dc3) with main (05d25e2)

Summary

✅ 29 untouched benchmarks

@Dunqing
Copy link
Member

Dunqing commented Aug 27, 2024

I am considering merging scope and symbol into one, based on

fn get_scope_snapshot(semantic: &Semantic, scopes: impl Iterator<Item = ScopeId>) -> String {
let scope_tree = semantic.scopes();
let mut result = String::default();
result.push('[');
scopes.enumerate().for_each(|(index, scope_id)| {
if index != 0 {
result.push(',');
}
let flags = scope_tree.get_flags(scope_id);
result.push('{');
let child_ids = semantic
.scopes()
.descendants_from_root()
.filter(|id| {
scope_tree.get_parent_id(*id).is_some_and(|parent_id| parent_id == scope_id)
})
.collect::<Vec<_>>();
result.push_str("\"children\":");
result.push_str(&get_scope_snapshot(semantic, child_ids.iter().copied()));
result.push(',');
result.push_str(format!("\"flags\": \"{flags:?}\",").as_str());
result.push_str(format!("\"id\": {},", scope_id.index()).as_str());
result.push_str(
format!(
"\"node\": {:?},",
semantic.nodes().kind(scope_tree.get_node_id(scope_id)).debug_name()
)
.as_str(),
);
result.push_str("\"symbols\": ");
let bindings = scope_tree.get_bindings(scope_id);
result.push('[');
bindings.iter().enumerate().for_each(|(index, (name, symbol_id))| {
if index != 0 {
result.push(',');
}
result.push('{');
result.push_str(
format!("\"flags\": \"{:?}\",", semantic.symbols().get_flags(*symbol_id)).as_str(),
);
result.push_str(format!("\"id\": {},", symbol_id.index()).as_str());
result.push_str(format!("\"name\": {name:?},").as_str());
result.push_str(
format!(
"\"node\": {:?},",
semantic
.nodes()
.kind(semantic.symbols().get_declaration(*symbol_id))
.debug_name()
)
.as_str(),
);
{
result.push_str("\"references\": ");
result.push('[');
semantic
.symbols()
.get_resolved_reference_ids(*symbol_id)
.iter()
.enumerate()
.for_each(|(index, reference_id)| {
if index != 0 {
result.push(',');
}
let reference = &semantic.symbols().references[*reference_id];
result.push('{');
result
.push_str(format!("\"flags\": \"{:?}\",", reference.flags()).as_str());
result.push_str(format!("\"id\": {},", reference_id.index()).as_str());
result.push_str(
format!("\"name\": {:?},", semantic.reference_name(reference)).as_str(),
);
result.push_str(
format!("\"node_id\": {}", reference.node_id().index()).as_str(),
);
result.push('}');
});
result.push(']');
}
result.push('}');
});
result.push(']');
result.push('}');
});
result.push(']');
result
}

Because I think keeping the consistent with our semantic snapshot makes it easy to debug the bugs

@overlookmotel
Copy link
Contributor Author

I see your point of it being easier for debugging if the format is consistent. But I think visually it might be a bit too much information all in one place to combine the 2. It'd work if you had expand/contract buttons so you didn't have to see everything at once. Maybe that's achievable with our new fancy Playground?

@Boshen Boshen added the 0-merge Merge with Graphite Merge Queue label Aug 27, 2024 — with Graphite App
Copy link

graphite-app bot commented Aug 27, 2024

Merge activity

Follow-on after #5232. `oxc_wasm` build scopes text with a single AST traversal. Previous implementation was O($n^2$).

If we can assume scopes are listed in traversal order, then we could do it a bit more efficiently just from `ScopeTree`, but this approach of using `Visit` will handle out-of-order scope IDs (which you'd get if printing a post-transform `ScopeTree`).

Also reduce creating and discarding `String`s for indentation - reuse a single string instead.
@Boshen Boshen force-pushed the 08-27-perf_wasm_generate_scope_text_with_visitor branch from bc32c2e to 6a49dc3 Compare August 27, 2024 13:49
@graphite-app graphite-app bot merged commit 6a49dc3 into main Aug 27, 2024
24 checks passed
@graphite-app graphite-app bot deleted the 08-27-perf_wasm_generate_scope_text_with_visitor branch August 27, 2024 13:53
eryue0220 added a commit to eryue0220/oxc that referenced this pull request Aug 27, 2024
…o fix/jest-vitest-compat

* 'fix/jest-vitest-compat' of github.com:eryue0220/oxc:
  fix(wasm): reference ast from prettier (oxc-project#5268)
  feat(wasm): output symbol IDs in scope tree (oxc-project#5266)
  perf(wasm): generate scope text with visitor (oxc-project#5264)
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
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants