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

Fixes Numeric type propagation #6461

Merged
merged 21 commits into from
Aug 28, 2024
Merged

Fixes Numeric type propagation #6461

merged 21 commits into from
Aug 28, 2024

Conversation

esdrubal
Copy link
Contributor

@esdrubal esdrubal commented Aug 23, 2024

Description

To fix the Numeric type propagation we now do the type checking of code blocks twice, first to collect all the unification and second to unify the types of a previous namespace with the variable declarations types of the current namespace.

This change incurs a performance drop in the compilation time. The benchmark compile takes 25% more time to run.

This can be improved significantly by using LexicalScopes instead of cloning the whole namespace on each scope.

Fixes #6371

Checklist

  • I have linked to any relevant issues.
  • I have commented my code, particularly in hard-to-understand areas.
  • I have updated the documentation where relevant (API docs, the reference, and the Sway book).
  • I have added tests that prove my fix is effective or that my feature works.
  • I have added (or requested a maintainer to add) the necessary Breaking* or New Feature labels where relevant.
  • I have done my best to ensure that my PR adheres to the Fuel Labs Code Review Standards.
  • I have requested a review from the relevant team or maintainers.

@esdrubal esdrubal self-assigned this Aug 23, 2024
@esdrubal esdrubal added bug Something isn't working P: high Should be looked at if there are no critical issues left audit-report Related to the audit report labels Aug 23, 2024
Copy link

Benchmark for 9178377

Click to view benchmark
Test Base PR %
code_action 5.3±0.39ms 5.0±0.07ms -5.66%
code_lens 287.6±15.43ns 290.0±6.15ns +0.83%
compile 2.8±0.09s 4.5±0.05s +60.71%
completion 4.7±0.04ms 4.6±0.07ms -2.13%
did_change_with_caching 2.6±0.04s 4.3±0.05s +65.38%
document_symbol 910.2±28.26µs 873.1±23.85µs -4.08%
format 70.6±0.83ms 70.0±0.70ms -0.85%
goto_definition 339.2±3.38µs 341.9±4.23µs +0.80%
highlight 9.1±0.11ms 8.8±0.74ms -3.30%
hover 492.5±8.13µs 494.7±5.61µs +0.45%
idents_at_position 118.8±3.43µs 116.8±1.32µs -1.68%
inlay_hints 642.6±20.05µs 667.9±24.67µs +3.94%
on_enter 2.0±0.06µs 2.1±0.05µs +5.00%
parent_decl_at_position 3.7±0.03ms 3.6±0.04ms -2.70%
prepare_rename 336.6±10.20µs 339.6±7.34µs +0.89%
rename 9.4±0.01ms 9.0±0.11ms -4.26%
semantic_tokens 1171.6±44.67µs 1220.2±14.06µs +4.15%
token_at_position 341.2±1.51µs 340.9±3.17µs -0.09%
tokens_at_position 3.7±0.06ms 3.6±0.02ms -2.70%
tokens_for_file 401.4±1.99µs 405.0±2.82µs +0.90%
traverse 34.3±0.56ms 34.7±0.75ms +1.17%

Copy link

Benchmark for cd213fd

Click to view benchmark
Test Base PR %
code_action 5.2±0.04ms 5.2±0.20ms 0.00%
code_lens 290.3±8.40ns 289.4±14.90ns -0.31%
compile 2.7±0.05s 4.5±0.08s +66.67%
completion 4.7±0.05ms 4.7±0.16ms 0.00%
did_change_with_caching 2.6±0.04s 4.3±0.03s +65.38%
document_symbol 897.3±27.74µs 913.8±41.64µs +1.84%
format 70.7±0.70ms 71.0±0.79ms +0.42%
goto_definition 333.6±7.07µs 415.3±6.86µs +24.49%
highlight 9.1±0.02ms 9.0±0.03ms -1.10%
hover 485.9±7.05µs 498.8±7.09µs +2.65%
idents_at_position 118.1±0.38µs 118.6±2.07µs +0.42%
inlay_hints 644.5±22.39µs 642.9±18.97µs -0.25%
on_enter 1974.6±90.56ns 2.0±0.08µs +1.29%
parent_decl_at_position 3.7±0.03ms 3.7±0.02ms 0.00%
prepare_rename 336.6±6.24µs 339.9±8.91µs +0.98%
rename 9.4±0.11ms 9.3±0.03ms -1.06%
semantic_tokens 1198.8±10.11µs 1247.3±17.24µs +4.05%
token_at_position 338.6±2.59µs 339.7±2.49µs +0.32%
tokens_at_position 3.7±0.02ms 3.7±0.05ms 0.00%
tokens_for_file 400.2±2.77µs 415.1±2.35µs +3.72%
traverse 34.4±0.66ms 33.8±0.74ms -1.74%

Copy link

Benchmark for d742809

Click to view benchmark
Test Base PR %
code_action 5.3±0.13ms 5.7±0.25ms +7.55%
code_lens 293.5±8.26ns 291.1±10.64ns -0.82%
compile 2.8±0.06s 4.2±0.14s +50.00%
completion 4.8±0.07ms 4.8±0.21ms 0.00%
did_change_with_caching 2.6±0.04s 4.0±0.07s +53.85%
document_symbol 943.6±56.68µs 846.7±21.12µs -10.27%
format 71.2±0.79ms 71.3±1.30ms +0.14%
goto_definition 342.5±6.36µs 339.5±10.72µs -0.88%
highlight 9.2±0.16ms 9.0±0.13ms -2.17%
hover 495.4±5.53µs 492.1±4.85µs -0.67%
idents_at_position 117.8±2.39µs 120.7±0.23µs +2.46%
inlay_hints 649.5±10.79µs 642.5±35.00µs -1.08%
on_enter 1937.3±64.84ns 2.1±0.09µs +8.40%
parent_decl_at_position 3.8±0.14ms 3.7±0.05ms -2.63%
prepare_rename 339.3±8.41µs 339.2±9.17µs -0.03%
rename 9.6±0.37ms 9.6±0.31ms 0.00%
semantic_tokens 1259.6±19.53µs 1243.6±13.39µs -1.27%
token_at_position 337.4±4.78µs 340.5±3.33µs +0.92%
tokens_at_position 3.8±0.09ms 3.7±0.05ms -2.63%
tokens_for_file 406.5±2.50µs 404.0±6.37µs -0.62%
traverse 34.3±0.99ms 34.9±0.91ms +1.75%

Copy link

Benchmark for 26a45eb

Click to view benchmark
Test Base PR %
code_action 5.6±0.06ms 5.1±0.07ms -8.93%
code_lens 284.5±7.42ns 297.3±6.52ns +4.50%
compile 2.7±0.04s 4.1±0.13s +51.85%
completion 4.9±0.07ms 4.6±0.11ms -6.12%
did_change_with_caching 2.6±0.04s 4.0±0.08s +53.85%
document_symbol 922.1±48.44µs 903.1±46.89µs -2.06%
format 70.6±1.13ms 72.1±1.17ms +2.12%
goto_definition 376.3±4.31µs 335.8±10.17µs -10.76%
highlight 9.5±0.04ms 8.7±0.13ms -8.42%
hover 524.0±13.28µs 492.8±6.27µs -5.95%
idents_at_position 116.9±0.41µs 118.1±0.59µs +1.03%
inlay_hints 687.4±20.42µs 633.1±34.65µs -7.90%
on_enter 2.0±0.04µs 2.0±0.04µs 0.00%
parent_decl_at_position 3.8±0.05ms 3.5±0.05ms -7.89%
prepare_rename 385.1±8.18µs 333.3±3.95µs -13.45%
rename 9.8±0.12ms 9.0±0.07ms -8.16%
semantic_tokens 1189.6±34.49µs 1244.3±15.43µs +4.60%
token_at_position 370.9±3.34µs 334.8±4.13µs -9.73%
tokens_at_position 3.8±0.03ms 3.5±0.05ms -7.89%
tokens_for_file 401.9±2.92µs 401.6±4.11µs -0.07%
traverse 34.6±0.96ms 35.0±1.05ms +1.16%

Copy link

Benchmark for 6e2b942

Click to view benchmark
Test Base PR %
code_action 5.1±0.06ms 5.0±0.11ms -1.96%
code_lens 283.8±6.88ns 291.1±23.71ns +2.57%
compile 2.7±0.06s 4.2±0.10s +55.56%
completion 4.6±0.07ms 4.6±0.02ms 0.00%
did_change_with_caching 2.6±0.02s 3.9±0.06s +50.00%
document_symbol 854.0±15.05µs 879.9±20.84µs +3.03%
format 71.2±1.23ms 72.9±2.14ms +2.39%
goto_definition 336.5±4.49µs 341.2±2.89µs +1.40%
highlight 8.8±0.32ms 8.8±0.18ms 0.00%
hover 492.6±4.17µs 490.8±5.26µs -0.37%
idents_at_position 119.4±1.69µs 120.8±0.60µs +1.17%
inlay_hints 636.1±34.68µs 634.6±33.24µs -0.24%
on_enter 2.0±0.05µs 1971.3±89.65ns -1.43%
parent_decl_at_position 3.6±0.07ms 3.6±0.04ms 0.00%
prepare_rename 338.2±4.86µs 338.5±5.66µs +0.09%
rename 9.0±0.03ms 9.0±0.21ms 0.00%
semantic_tokens 1195.1±27.76µs 1209.1±7.57µs +1.17%
token_at_position 338.3±2.98µs 338.0±3.10µs -0.09%
tokens_at_position 3.6±0.05ms 3.6±0.04ms 0.00%
tokens_for_file 398.8±2.40µs 402.0±2.25µs +0.80%
traverse 34.7±0.44ms 34.1±1.31ms -1.73%

Copy link

Benchmark for 943be69

Click to view benchmark
Test Base PR %
code_action 5.8±0.11ms 5.5±0.13ms -5.17%
code_lens 287.4±9.10ns 283.5±9.37ns -1.36%
compile 2.8±0.04s 4.4±0.09s +57.14%
completion 5.1±0.19ms 4.7±0.08ms -7.84%
did_change_with_caching 2.7±0.04s 4.1±0.06s +51.85%
document_symbol 920.1±25.41µs 861.2±10.45µs -6.40%
format 71.6±0.90ms 74.7±1.13ms +4.33%
goto_definition 343.9±5.85µs 356.8±13.06µs +3.75%
highlight 9.3±0.25ms 9.0±0.16ms -3.23%
hover 497.0±8.17µs 508.9±3.57µs +2.39%
idents_at_position 118.1±0.26µs 120.9±2.29µs +2.37%
inlay_hints 650.5±7.17µs 648.1±17.97µs -0.37%
on_enter 2.0±0.09µs 2.0±0.04µs 0.00%
parent_decl_at_position 3.7±0.04ms 3.7±0.03ms 0.00%
prepare_rename 342.9±7.37µs 349.2±6.01µs +1.84%
rename 9.4±0.16ms 9.4±0.16ms 0.00%
semantic_tokens 1226.6±30.49µs 1283.2±13.95µs +4.61%
token_at_position 339.0±2.70µs 333.3±3.03µs -1.68%
tokens_at_position 3.8±0.07ms 3.7±0.03ms -2.63%
tokens_for_file 404.7±3.80µs 400.8±2.55µs -0.96%
traverse 35.6±0.61ms 36.1±0.81ms +1.40%

To fix the Numeric type propagation we now do the type checking of code
blocks twice, first to collect all the unification and second to unify
the types of a previous namespace with the variable declarations types
of the current namespace.

This changes incurs a significative performance drop in the compilation time.
The test in release goes from 3 secs to 5 secs.

This can be improved significatively when we start using LexicalScopes
instead of cloning the whole namespace on each scope.

Fixes #6371
Copy link

Benchmark for b5cd011

Click to view benchmark
Test Base PR %
code_action 5.2±0.17ms 5.3±0.12ms +1.92%
code_lens 285.5±10.56ns 292.5±22.81ns +2.45%
compile 2.8±0.06s 3.5±0.10s +25.00%
completion 4.6±0.20ms 4.8±0.31ms +4.35%
did_change_with_caching 2.7±0.05s 3.4±0.09s +25.93%
document_symbol 887.4±21.72µs 865.0±24.11µs -2.52%
format 71.0±0.56ms 74.4±1.51ms +4.79%
goto_definition 404.1±12.32µs 351.3±7.57µs -13.07%
highlight 8.8±0.19ms 9.1±0.12ms +3.41%
hover 628.1±2.35µs 506.6±3.80µs -19.34%
idents_at_position 118.6±0.40µs 119.0±0.63µs +0.34%
inlay_hints 630.1±24.69µs 655.4±51.62µs +4.02%
on_enter 1982.7±81.09ns 2.0±0.07µs +0.87%
parent_decl_at_position 3.6±0.07ms 3.7±0.03ms +2.78%
prepare_rename 333.3±7.06µs 342.7±8.79µs +2.82%
rename 9.3±0.25ms 9.4±0.22ms +1.08%
semantic_tokens 1198.6±8.13µs 1247.4±23.53µs +4.07%
token_at_position 331.8±2.34µs 335.7±2.24µs +1.18%
tokens_at_position 3.6±0.06ms 3.7±0.06ms +2.78%
tokens_for_file 399.9±2.15µs 399.3±8.98µs -0.15%
traverse 34.8±0.88ms 34.5±1.24ms -0.86%

Copy link

Benchmark for d3b931e

Click to view benchmark
Test Base PR %
code_action 5.0±0.03ms 5.2±0.03ms +4.00%
code_lens 283.1±12.63ns 297.3±9.10ns +5.02%
compile 2.7±0.03s 3.4±0.08s +25.93%
completion 4.6±0.09ms 4.7±0.04ms +2.17%
did_change_with_caching 2.5±0.03s 3.3±0.06s +32.00%
document_symbol 891.5±44.96µs 911.1±29.69µs +2.20%
format 72.5±1.00ms 72.4±0.87ms -0.14%
goto_definition 340.8±5.90µs 415.0±10.30µs +21.77%
highlight 8.7±0.05ms 9.2±0.08ms +5.75%
hover 494.1±3.04µs 493.5±12.35µs -0.12%
idents_at_position 118.9±0.73µs 120.8±0.30µs +1.60%
inlay_hints 634.7±11.73µs 641.0±21.76µs +0.99%
on_enter 1876.8±37.93ns 2.0±0.10µs +6.56%
parent_decl_at_position 3.6±0.03ms 3.7±0.04ms +2.78%
prepare_rename 338.7±6.60µs 341.1±4.82µs +0.71%
rename 9.0±0.19ms 9.4±0.06ms +4.44%
semantic_tokens 1243.7±12.89µs 1245.2±9.21µs +0.12%
token_at_position 338.4±1.41µs 332.2±2.98µs -1.83%
tokens_at_position 3.5±0.02ms 3.7±0.02ms +5.71%
tokens_for_file 396.1±3.14µs 402.5±3.81µs +1.62%
traverse 35.0±0.62ms 34.4±1.23ms -1.71%

IGI-111
IGI-111 previously approved these changes Aug 26, 2024
@IGI-111 IGI-111 requested a review from a team August 26, 2024 20:37
@JoshuaBatty
Copy link
Member

25% performance regression is pretty hard pill to swallow. Is there an open issue for removing the namespace cloning and using LexicalScopes like you mention @esdrubal ? If not, would you mind opening one and linking this PR to the issue so this regression can at least just be temporary?

IGI-111
IGI-111 previously approved these changes Aug 27, 2024
@IGI-111 IGI-111 requested a review from a team August 27, 2024 10:13
Copy link

Benchmark for c505e0a

Click to view benchmark
Test Base PR %
code_action 5.1±0.06ms 5.2±0.10ms +1.96%
code_lens 289.1±8.82ns 297.4±6.81ns +2.87%
compile 2.7±0.06s 3.5±0.22s +29.63%
completion 4.5±0.04ms 5.0±0.52ms +11.11%
did_change_with_caching 2.7±0.16s 3.4±0.07s +25.93%
document_symbol 890.8±106.86µs 896.1±26.63µs +0.59%
format 71.0±0.87ms 73.6±1.00ms +3.66%
goto_definition 334.2±9.15µs 340.1±7.76µs +1.77%
highlight 8.8±0.12ms 9.0±0.12ms +2.27%
hover 487.4±11.91µs 495.1±7.02µs +1.58%
idents_at_position 122.8±9.09µs 123.2±6.00µs +0.33%
inlay_hints 627.4±29.80µs 663.8±80.59µs +5.80%
on_enter 2.0±0.05µs 1998.4±95.93ns -0.08%
parent_decl_at_position 3.7±0.22ms 3.7±0.02ms 0.00%
prepare_rename 337.0±6.62µs 339.0±4.91µs +0.59%
rename 9.0±0.12ms 9.3±0.09ms +3.33%
semantic_tokens 1242.7±12.05µs 1234.9±21.68µs -0.63%
token_at_position 370.3±1.83µs 335.3±3.89µs -9.45%
tokens_at_position 3.8±0.33ms 3.9±0.16ms +2.63%
tokens_for_file 397.6±2.06µs 409.7±13.63µs +3.04%
traverse 40.9±2.25ms 40.0±1.28ms -2.20%

tritao
tritao previously approved these changes Aug 27, 2024
@esdrubal esdrubal dismissed stale reviews from tritao and IGI-111 via 725e553 August 28, 2024 11:32
Copy link

Benchmark for cdd3fdb

Click to view benchmark
Test Base PR %
code_action 5.2±0.19ms 5.0±0.51ms -3.85%
code_lens 283.4±4.59ns 329.1±9.64ns +16.13%
compile 2.8±0.05s 2.9±0.06s +3.57%
completion 4.8±0.22ms 4.5±0.01ms -6.25%
did_change_with_caching 2.8±0.04s 3.0±0.05s +7.14%
document_symbol 856.0±15.17µs 927.7±29.47µs +8.38%
format 72.9±1.99ms 71.8±0.63ms -1.51%
goto_definition 346.0±16.72µs 344.5±6.71µs -0.43%
highlight 8.8±0.08ms 8.6±0.12ms -2.27%
hover 499.1±8.43µs 428.0±7.81µs -14.25%
idents_at_position 119.3±0.66µs 119.7±0.92µs +0.34%
inlay_hints 627.2±22.61µs 631.1±22.36µs +0.62%
on_enter 1895.6±34.85ns 2.0±0.08µs +5.51%
parent_decl_at_position 3.8±0.13ms 3.6±0.05ms -5.26%
prepare_rename 341.9±3.95µs 342.7±5.75µs +0.23%
rename 9.4±0.23ms 8.9±0.12ms -5.32%
semantic_tokens 1195.7±31.90µs 1227.2±13.00µs +2.63%
token_at_position 340.3±2.12µs 332.3±1.96µs -2.35%
tokens_at_position 3.7±0.11ms 3.6±0.07ms -2.70%
tokens_for_file 406.9±2.63µs 402.1±3.53µs -1.18%
traverse 35.5±0.77ms 34.0±0.89ms -4.23%

@esdrubal
Copy link
Contributor Author

This PR is also hitting the recursion limit that #6471 will fix.

@IGI-111 IGI-111 requested a review from tritao August 28, 2024 18:40
@IGI-111 IGI-111 enabled auto-merge (squash) August 28, 2024 18:45
@IGI-111 IGI-111 merged commit 4698d37 into master Aug 28, 2024
36 checks passed
@IGI-111 IGI-111 deleted the esdrubal/6371 branch August 28, 2024 18:57
Copy link

Benchmark for 4b33516

Click to view benchmark
Test Base PR %
code_action 5.1±0.03ms 5.1±0.08ms 0.00%
code_lens 282.1±6.73ns 286.2±6.17ns +1.45%
compile 1667.8±63.48ms 2.1±0.05s +25.91%
completion 4.6±0.40ms 4.5±0.09ms -2.17%
did_change_with_caching 1557.4±33.86ms 1938.9±35.36ms +24.50%
document_symbol 852.3±18.05µs 878.6±19.79µs +3.09%
format 72.0±1.96ms 74.1±0.89ms +2.92%
goto_definition 336.8±4.02µs 340.9±8.67µs +1.22%
highlight 9.0±0.12ms 8.7±0.19ms -3.33%
hover 349.2±6.85µs 356.2±5.76µs +2.00%
idents_at_position 120.1±3.44µs 118.8±1.59µs -1.08%
inlay_hints 636.8±23.40µs 638.2±24.30µs +0.22%
on_enter 2.0±0.04µs 2.0±0.12µs 0.00%
parent_decl_at_position 3.7±0.11ms 3.6±0.04ms -2.70%
prepare_rename 337.9±6.90µs 340.4±6.81µs +0.74%
rename 9.5±0.70ms 9.1±0.19ms -4.21%
semantic_tokens 1174.5±43.94µs 1284.9±10.57µs +9.40%
token_at_position 337.8±8.79µs 338.7±4.37µs +0.27%
tokens_at_position 3.7±0.04ms 3.6±0.12ms -2.70%
tokens_for_file 399.9±7.67µs 401.4±13.36µs +0.38%
traverse 34.3±0.61ms 35.5±0.54ms +3.50%

JoshuaBatty added a commit that referenced this pull request Aug 28, 2024
## Description
As shown in https://fuellabs.github.io/sway-performance-dashboard/ #6461
introduced a bigger memory footprint.

Lexical scope has a shared OrdMap called
`symbols_unique_while_collecting_unifications` that was never cleared.
With this commit, it is cleared on each code block along with the clear
of collected unifications.

## Checklist

- [x] I have linked to any relevant issues.
- [x] I have commented my code, particularly in hard-to-understand
areas.
- [ ] I have updated the documentation where relevant (API docs, the
reference, and the Sway book).
- [ ] If my change requires substantial documentation changes, I have
[requested support from the DevRel
team](https://github.com/FuelLabs/devrel-requests/issues/new/choose)
- [ ] I have added tests that prove my fix is effective or that my
feature works.
- [x] I have added (or requested a maintainer to add) the necessary
`Breaking*` or `New Feature` labels where relevant.
- [x] I have done my best to ensure that my PR adheres to [the Fuel Labs
Code Review
Standards](https://github.com/FuelLabs/rfcs/blob/master/text/code-standards/external-contributors.md).
- [x] I have requested a review from the relevant team or maintainers.

Co-authored-by: João Matos <joao@tritao.eu>
Co-authored-by: Joshua Batty <joshpbatty@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
audit-report Related to the audit report bug Something isn't working P: high Should be looked at if there are no critical issues left
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Type Propagation With Numerics
4 participants