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

feat(semantic): track cfg index per ast node #2210

Conversation

TzviPM
Copy link
Contributor

@TzviPM TzviPM commented Jan 29, 2024

This allows looking up a cfg index from an ast node in a semantics return. This allows later passes to better make use of the cfg.

@github-actions github-actions bot added A-linter Area - Linter A-semantic Area - Semantic labels Jan 29, 2024
@Boshen Boshen requested a review from u9g January 30, 2024 04:27
Copy link

codspeed-hq bot commented Jan 30, 2024

CodSpeed Performance Report

Merging #2210 will degrade performances by 3.02%

Comparing TzviPM:01-29-feat_semantic_track_cfg_index_per_ast_node (25385be) with main (622a2c3)

Summary

❌ 1 (👁 1) regressions
✅ 16 untouched benchmarks

Benchmarks breakdown

Benchmark main TzviPM:01-29-feat_semantic_track_cfg_index_per_ast_node Change
👁 semantic[antd.js] 750.7 ms 774 ms -3.02%

@u9g
Copy link
Contributor

u9g commented Jan 30, 2024

You’re doing a ton more work to store these in a hashmap still, maybe its worth making this an indexmap using the same AstNodeId as keytype? Still you are doing a lot more work and there will be a perf regression here. I’d be curious to know what specific new usecase this enables.

@TzviPM TzviPM force-pushed the 01-29-feat_semantic_track_cfg_index_per_ast_node branch 3 times, most recently from 95ac891 to 0797478 Compare January 30, 2024 15:40
@TzviPM
Copy link
Contributor Author

TzviPM commented Jan 30, 2024

Now that I have write access, I will try to use graphite inside of oxc directly to manage PR chains. I was doing this part in my own fork, but you can see my WIP of using this code in TzviPM#5. That PR is currently failing because of CFG issues (I think) and I will tune the performance of this PR once I have the use case working as well.

@TzviPM
Copy link
Contributor Author

TzviPM commented Jan 30, 2024

You’re doing a ton more work to store these in a hashmap still, maybe its worth making this an indexmap using the same AstNodeId as keytype? Still you are doing a lot more work and there will be a perf regression here. I’d be curious to know what specific new usecase this enables.

I think the problem is 2-fold:

  1. As you mentioned, an index map makes sense here.

  2. I introduced an additional branch inside of a hotpath. I wonder if it would be better to always store the current index, and then just correct it after for functions. In this way, the cpu won't suffer from branch prediction failures. Initially, I thought the cpu would be smart enough to just predict the next branch better based on this one (in enter_kind), but thtlat may have been too much faith in an ol' bucket of bolts 😊

@TzviPM
Copy link
Contributor Author

TzviPM commented Jan 30, 2024

At the end of the day, I'm not calculating anything extra, just storing an association that's already calculated. Im sure there's a way to do this without a 40% performance degredation.

@TzviPM
Copy link
Contributor Author

TzviPM commented Jan 30, 2024

Ooh, what if the ast node was actually not a u32, but actually a u24 with a u8. Basically, we always create an ast node before a node index, and then we associate the ast node with the created index. If we create the index first, then every time we make a node, we store it's ID as the node index (currently 32 bits, but just 24 bits might be enough) followed by an ast node inner index which denotes the position of the ast node within the CFG node.

In this way, we don't need to store any additional data on the heap, just make some in-register calculations to go from an ast node ID to the CFG node index.

@Boshen Boshen marked this pull request as draft January 31, 2024 03:38
@Boshen
Copy link
Member

Boshen commented Jan 31, 2024

Changed to draft due to performance regression.

Please excuse me for not understanding any of this right now. The only blocker from myside is the performance regression and nothing else.

@TzviPM
Copy link
Contributor Author

TzviPM commented Jan 31, 2024

TzviPM#6 is the downstream PR that implements no_this_before_super. Now that I have everything wired up trivially, I will start tweaking the solution to fix the performance regression.

@TzviPM TzviPM force-pushed the 01-29-feat_semantic_track_cfg_index_per_ast_node branch 2 times, most recently from 2617dd0 to 6ef24c1 Compare January 31, 2024 20:15
@TzviPM
Copy link
Contributor Author

TzviPM commented Jan 31, 2024

Okay, so indexmap definitely helped. I tried removing the conditional, and it actually did regress further:

image

@TzviPM TzviPM force-pushed the 01-29-feat_semantic_track_cfg_index_per_ast_node branch from 6ef24c1 to df491c8 Compare January 31, 2024 22:36
@TzviPM
Copy link
Contributor Author

TzviPM commented Jan 31, 2024

My initial removing of the conditional ended up always double-setting the function nodes. I fixed that by simply re-ordering. Then, instead of having a separate node map, I tried storing the cfg index right on the astnode of the semantic struct. This also makes more sense to me in terms of separation of concerns and boosted performance dramatically.

@TzviPM TzviPM force-pushed the 01-29-feat_semantic_track_cfg_index_per_ast_node branch from df491c8 to 80a3140 Compare January 31, 2024 22:50
@TzviPM TzviPM force-pushed the 01-29-feat_semantic_track_cfg_index_per_ast_node branch from 80a3140 to 25385be Compare January 31, 2024 22:53
@TzviPM TzviPM requested a review from Boshen January 31, 2024 23:11
@TzviPM TzviPM marked this pull request as ready for review January 31, 2024 23:11
@Boshen Boshen merged commit e561457 into oxc-project:main Feb 1, 2024
20 checks passed
IWANABETHATGUY pushed a commit to IWANABETHATGUY/oxc that referenced this pull request May 29, 2024
This allows looking up a cfg index from an ast node in a semantics
return. This allows later passes to better make use of the cfg.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-linter Area - Linter A-semantic Area - Semantic
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants