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

Implement Typed Module Caching #6280

Merged
merged 32 commits into from
Aug 15, 2024
Merged

Implement Typed Module Caching #6280

merged 32 commits into from
Aug 15, 2024

Conversation

JoshuaBatty
Copy link
Member

@JoshuaBatty JoshuaBatty commented Jul 19, 2024

Description

This PR implements a typed module caching system for the compiler when interacted with from the language server. The main goal is to improve performance by caching typed modules and reusing them when possible, reducing unnecessary recompilation.

Key Changes

  • Introduced CowCache for efficient, thread-safe caching with copy-on-write semantics
  • Implemented typed module caching in the QueryEngine
  • Updated ServerState and Session to utilize the new caching system

Performance Improvements

These changes show promising performance improvements in processing didChange events, particularly for larger projects. Here are some benchmarks using the FUSD libraries Sway project (19 Sway files):

Build Before After Improvement
Debug 448.25ms 123.73ms 72.4% faster
Release 103.83ms 53.59ms 48.4% faster

These improvements should lead to a more responsive development experience, especially when making frequent small changes.

closes #6228
improves: FuelLabs/sway-vscode-plugin#172

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.

@JoshuaBatty JoshuaBatty requested review from a team as code owners July 19, 2024 01:30
@JoshuaBatty JoshuaBatty marked this pull request as draft July 19, 2024 01:30
@JoshuaBatty JoshuaBatty self-assigned this Jul 23, 2024
Copy link

Benchmark for 03149bf

Click to view benchmark
Test Base PR %
code_action 5.0±0.09ms 5.0±0.03ms 0.00%
code_lens 337.8±5.79ns 277.5±9.39ns -17.85%
compile 2.6±0.05s 2.7±0.05s +3.85%
completion 4.5±0.03ms 4.5±0.02ms 0.00%
did_change_with_caching 2.6±0.02s 2.7±0.03s +3.85%
document_symbol 893.0±42.34µs 883.8±13.93µs -1.03%
format 69.1±1.00ms 73.2±0.85ms +5.93%
goto_definition 358.7±4.25µs 339.3±4.48µs -5.41%
highlight 8.6±0.12ms 8.6±0.14ms 0.00%
hover 508.7±10.57µs 494.5±5.10µs -2.79%
idents_at_position 120.5±0.21µs 119.4±0.67µs -0.91%
inlay_hints 631.8±9.93µs 643.1±40.26µs +1.79%
on_enter 465.8±13.91ns 475.1±15.79ns +2.00%
parent_decl_at_position 3.6±0.02ms 3.6±0.03ms 0.00%
prepare_rename 353.1±2.67µs 342.7±5.76µs -2.95%
rename 8.9±0.17ms 9.0±0.16ms +1.12%
semantic_tokens 1251.3±11.03µs 1256.8±11.55µs +0.44%
token_at_position 358.7±2.57µs 338.4±2.27µs -5.66%
tokens_at_position 3.6±0.02ms 3.6±0.02ms 0.00%
tokens_for_file 408.7±2.64µs 405.8±4.77µs -0.71%
traverse 38.1±1.13ms 39.4±0.77ms +3.41%

Copy link

Benchmark for 10fc3e6

Click to view benchmark
Test Base PR %
code_action 5.0±0.06ms 5.2±0.07ms +4.00%
code_lens 337.5±5.28ns 289.4±11.36ns -14.25%
compile 2.7±0.06s 2.7±0.05s 0.00%
completion 4.5±0.07ms 4.7±0.08ms +4.44%
did_change_with_caching 2.6±0.01s 2.7±0.04s +3.85%
document_symbol 852.3±16.59µs 930.9±11.89µs +9.22%
format 69.1±0.79ms 71.7±0.86ms +3.76%
goto_definition 349.4±6.38µs 338.6±6.34µs -3.09%
highlight 8.6±0.02ms 8.9±0.10ms +3.49%
hover 502.3±5.55µs 491.8±7.04µs -2.09%
idents_at_position 118.6±0.52µs 119.8±0.23µs +1.01%
inlay_hints 675.3±24.99µs 639.2±21.28µs -5.35%
on_enter 466.8±6.15ns 474.0±11.44ns +1.54%
parent_decl_at_position 3.5±0.02ms 3.7±0.02ms +5.71%
prepare_rename 346.6±4.09µs 339.2±3.26µs -2.14%
rename 8.9±0.15ms 9.3±0.03ms +4.49%
semantic_tokens 1244.4±8.87µs 1255.7±12.12µs +0.91%
token_at_position 338.3±3.66µs 342.6±1.97µs +1.27%
tokens_at_position 3.5±0.02ms 3.7±0.05ms +5.71%
tokens_for_file 400.7±3.01µs 399.2±2.39µs -0.37%
traverse 38.5±0.84ms 37.2±0.91ms -3.38%

Copy link

Benchmark for 6c45d0a

Click to view benchmark
Test Base PR %
code_action 5.1±0.07ms 5.2±0.05ms +1.96%
code_lens 337.6±9.45ns 295.3±7.42ns -12.53%
compile 2.7±0.08s 2.7±0.03s 0.00%
completion 4.5±0.02ms 4.7±0.08ms +4.44%
did_change_with_caching 2.6±0.03s 2.6±0.04s 0.00%
document_symbol 853.1±35.60µs 850.4±7.32µs -0.32%
format 70.4±2.12ms 72.5±0.78ms +2.98%
goto_definition 333.6±6.41µs 337.3±3.64µs +1.11%
highlight 8.7±0.18ms 8.9±0.06ms +2.30%
hover 491.3±5.63µs 491.4±4.04µs +0.02%
idents_at_position 119.0±1.38µs 117.6±2.06µs -1.18%
inlay_hints 625.2±27.31µs 633.2±9.97µs +1.28%
on_enter 457.6±14.76ns 465.0±14.52ns +1.62%
parent_decl_at_position 3.6±0.06ms 3.7±0.03ms +2.78%
prepare_rename 334.6±2.88µs 340.5±5.17µs +1.76%
rename 8.9±0.06ms 9.3±0.19ms +4.49%
semantic_tokens 1250.8±12.70µs 1261.6±12.60µs +0.86%
token_at_position 336.4±2.03µs 338.7±2.82µs +0.68%
tokens_at_position 3.5±0.03ms 3.7±0.02ms +5.71%
tokens_for_file 401.2±3.63µs 428.3±7.35µs +6.75%
traverse 38.5±0.62ms 35.9±0.61ms -6.75%

Copy link

Benchmark for 3878468

Click to view benchmark
Test Base PR %
code_action 5.0±0.04ms 5.0±0.16ms 0.00%
code_lens 336.5±5.51ns 289.7±4.38ns -13.91%
compile 2.7±0.04s 2.6±0.05s -3.70%
completion 4.5±0.10ms 4.5±0.04ms 0.00%
did_change_with_caching 2.6±0.02s 2.6±0.08s 0.00%
document_symbol 856.5±18.11µs 842.4±6.06µs -1.65%
format 69.5±1.01ms 72.7±1.23ms +4.60%
goto_definition 337.5±8.50µs 348.7±6.10µs +3.32%
highlight 8.6±0.03ms 8.7±0.02ms +1.16%
hover 493.2±5.66µs 502.9±5.47µs +1.97%
idents_at_position 121.1±0.39µs 117.3±0.47µs -3.14%
inlay_hints 639.9±38.22µs 672.6±19.56µs +5.11%
on_enter 463.6±10.66ns 473.5±15.20ns +2.14%
parent_decl_at_position 3.5±0.05ms 3.5±0.02ms 0.00%
prepare_rename 338.5±6.97µs 347.2±6.69µs +2.57%
rename 8.9±0.12ms 9.0±0.25ms +1.12%
semantic_tokens 1268.6±10.66µs 1254.8±14.13µs -1.09%
token_at_position 332.9±3.70µs 331.6±1.68µs -0.39%
tokens_at_position 3.5±0.02ms 3.5±0.03ms 0.00%
tokens_for_file 405.9±2.39µs 407.1±2.45µs +0.30%
traverse 38.6±0.62ms 35.9±0.69ms -6.99%

Copy link

Benchmark for efbe942

Click to view benchmark
Test Base PR %
code_action 5.1±0.12ms 5.2±0.12ms +1.96%
code_lens 339.3±8.37ns 280.3±4.28ns -17.39%
compile 2.7±0.05s 2.7±0.05s 0.00%
completion 4.6±0.11ms 4.7±0.03ms +2.17%
did_change_with_caching 2.7±0.02s 2.6±0.03s -3.70%
document_symbol 921.8±17.02µs 915.1±39.42µs -0.73%
format 69.8±1.10ms 68.9±0.54ms -1.29%
goto_definition 341.4±6.46µs 344.8±6.30µs +1.00%
highlight 8.7±0.15ms 9.0±0.12ms +3.45%
hover 496.6±7.54µs 504.0±3.83µs +1.49%
idents_at_position 119.5±0.36µs 118.7±0.40µs -0.67%
inlay_hints 631.0±16.69µs 646.4±24.93µs +2.44%
on_enter 457.5±6.98ns 477.0±11.54ns +4.26%
parent_decl_at_position 3.6±0.04ms 3.7±0.04ms +2.78%
prepare_rename 340.1±4.20µs 344.2±6.14µs +1.21%
rename 9.1±0.16ms 9.4±0.20ms +3.30%
semantic_tokens 1271.9±16.32µs 1307.8±14.22µs +2.82%
token_at_position 334.4±2.41µs 337.3±2.32µs +0.87%
tokens_at_position 3.5±0.02ms 3.7±0.03ms +5.71%
tokens_for_file 398.9±2.30µs 405.6±3.30µs +1.68%
traverse 38.3±0.84ms 35.7±0.76ms -6.79%

Copy link

Benchmark for 2c7ca9c

Click to view benchmark
Test Base PR %
code_action 5.1±0.09ms 5.2±0.06ms +1.96%
code_lens 339.6±12.56ns 283.4±4.62ns -16.55%
compile 2.7±0.06s 2.7±0.03s 0.00%
completion 4.5±0.03ms 4.8±0.12ms +6.67%
did_change_with_caching 2.6±0.03s 2.7±0.04s +3.85%
document_symbol 916.1±9.98µs 918.8±45.84µs +0.29%
format 71.0±2.92ms 69.7±1.09ms -1.83%
goto_definition 337.6±2.69µs 345.4±4.26µs +2.31%
highlight 8.7±0.05ms 9.1±0.04ms +4.60%
hover 507.0±6.34µs 497.0±3.41µs -1.97%
idents_at_position 120.9±0.35µs 117.2±2.40µs -3.06%
inlay_hints 628.9±21.63µs 649.2±8.82µs +3.23%
on_enter 476.8±14.87ns 475.6±14.04ns -0.25%
parent_decl_at_position 3.5±0.04ms 3.7±0.02ms +5.71%
prepare_rename 346.2±49.15µs 344.2±5.70µs -0.58%
rename 9.1±0.25ms 9.4±0.07ms +3.30%
semantic_tokens 1231.0±15.19µs 1277.6±11.41µs +3.79%
token_at_position 340.4±3.63µs 338.7±4.52µs -0.50%
tokens_at_position 3.5±0.06ms 3.7±0.04ms +5.71%
tokens_for_file 400.4±2.78µs 404.5±2.97µs +1.02%
traverse 38.7±0.51ms 36.1±0.81ms -6.72%

@JoshuaBatty JoshuaBatty added language server LSP server compiler: frontend Everything to do with type checking, control flow analysis, and everything between parsing and IRgen performance Everything related to performance, speed wise or memory wise. labels Aug 1, 2024
Copy link

github-actions bot commented Aug 1, 2024

Benchmark for ff415cc

Click to view benchmark
Test Base PR %
code_action 5.2±0.07ms 5.2±0.13ms 0.00%
code_lens 293.4±7.99ns 342.5±9.95ns +16.73%
compile 2.6±0.04s 2.6±0.03s 0.00%
completion 4.6±0.04ms 4.6±0.05ms 0.00%
did_change_with_caching 2.5±0.02s 2.5±0.03s 0.00%
document_symbol 854.7±6.27µs 863.9±24.31µs +1.08%
format 69.8±1.04ms 69.3±0.75ms -0.72%
goto_definition 343.0±4.54µs 337.2±6.13µs -1.69%
highlight 9.0±0.16ms 9.0±0.09ms 0.00%
hover 502.5±5.31µs 489.9±3.67µs -2.51%
idents_at_position 117.1±0.33µs 117.9±0.28µs +0.68%
inlay_hints 638.8±5.73µs 635.5±22.18µs -0.52%
on_enter 2.1±0.03µs 2.0±0.09µs -4.76%
parent_decl_at_position 3.7±0.02ms 3.7±0.03ms 0.00%
prepare_rename 342.7±4.77µs 336.0±7.58µs -1.96%
rename 9.3±0.01ms 9.2±0.02ms -1.08%
semantic_tokens 1264.9±14.48µs 1289.7±8.01µs +1.96%
token_at_position 340.1±2.24µs 333.8±4.94µs -1.85%
tokens_at_position 3.7±0.03ms 3.7±0.02ms 0.00%
tokens_for_file 405.7±1.47µs 400.5±3.58µs -1.28%
traverse 35.7±0.81ms 33.4±0.89ms -6.44%

@JoshuaBatty JoshuaBatty marked this pull request as ready for review August 1, 2024 03:54
Copy link

github-actions bot commented Aug 1, 2024

Benchmark for bf7b59a

Click to view benchmark
Test Base PR %
code_action 5.4±0.17ms 5.2±0.14ms -3.70%
code_lens 296.9±9.52ns 293.7±9.85ns -1.08%
compile 2.8±0.05s 2.7±0.07s -3.57%
completion 4.9±0.25ms 4.7±0.14ms -4.08%
did_change_with_caching 2.7±0.05s 2.7±0.05s 0.00%
document_symbol 861.2±23.06µs 895.6±28.18µs +3.99%
format 71.2±0.98ms 71.3±0.78ms +0.14%
goto_definition 337.7±8.94µs 337.7±8.03µs 0.00%
highlight 8.8±0.26ms 8.9±0.26ms +1.14%
hover 494.3±6.63µs 493.8±12.46µs -0.10%
idents_at_position 122.3±1.28µs 121.0±0.87µs -1.06%
inlay_hints 635.0±29.40µs 634.8±24.31µs -0.03%
on_enter 1982.0±92.85ns 1989.9±101.72ns +0.40%
parent_decl_at_position 3.6±0.08ms 3.6±0.10ms 0.00%
prepare_rename 341.9±11.45µs 337.9±4.52µs -1.17%
rename 9.3±0.19ms 9.2±0.16ms -1.08%
semantic_tokens 1259.0±22.75µs 1247.8±14.00µs -0.89%
token_at_position 336.7±3.38µs 335.7±3.89µs -0.30%
tokens_at_position 3.6±0.04ms 3.6±0.07ms 0.00%
tokens_for_file 406.5±3.85µs 397.0±3.39µs -2.34%
traverse 39.3±0.28ms 35.7±1.03ms -9.16%

tritao
tritao previously approved these changes Aug 2, 2024
@JoshuaBatty JoshuaBatty requested a review from a team August 2, 2024 23:11
@JoshuaBatty JoshuaBatty enabled auto-merge (squash) August 3, 2024 01:55
IGI-111
IGI-111 previously approved these changes Aug 5, 2024
Copy link

github-actions bot commented Aug 5, 2024

Benchmark for 6cc2233

Click to view benchmark
Test Base PR %
code_action 5.2±0.14ms 5.2±0.05ms 0.00%
code_lens 298.0±8.78ns 287.3±8.37ns -3.59%
compile 2.7±0.07s 2.7±0.07s 0.00%
completion 4.7±0.07ms 4.7±0.02ms 0.00%
did_change_with_caching 2.6±0.06s 2.6±0.03s 0.00%
document_symbol 880.8±34.50µs 902.8±46.41µs +2.50%
format 72.7±1.14ms 72.6±0.77ms -0.14%
goto_definition 344.5±6.64µs 338.1±3.69µs -1.86%
highlight 9.1±0.22ms 9.0±0.08ms -1.10%
hover 499.9±5.44µs 489.9±6.87µs -2.00%
idents_at_position 118.9±0.45µs 120.7±0.55µs +1.51%
inlay_hints 643.4±23.40µs 631.7±25.06µs -1.82%
on_enter 2.0±0.09µs 2.1±0.06µs +5.00%
parent_decl_at_position 3.7±0.02ms 3.7±0.03ms 0.00%
prepare_rename 339.1±5.19µs 336.1±5.77µs -0.88%
rename 9.3±0.13ms 9.3±0.09ms 0.00%
semantic_tokens 1191.9±12.81µs 1212.1±12.05µs +1.69%
token_at_position 335.0±2.40µs 340.7±3.25µs +1.70%
tokens_at_position 3.7±0.02ms 3.7±0.04ms 0.00%
tokens_for_file 403.9±4.13µs 403.5±3.40µs -0.10%
traverse 37.6±1.10ms 34.8±0.92ms -7.45%

sdankel
sdankel previously approved these changes Aug 5, 2024
sway-core/src/query_engine/mod.rs Show resolved Hide resolved
sway-lsp/src/server_state.rs Show resolved Hide resolved
@sdankel sdankel dismissed stale reviews from IGI-111, tritao, and themself via 6676c7f August 15, 2024 21:51
Copy link
Member

@Voxelot Voxelot left a comment

Choose a reason for hiding this comment

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

approving since @tritao already approved, it just got invalidated due to syncing the master branch

@JoshuaBatty JoshuaBatty merged commit 886c0b8 into master Aug 15, 2024
37 checks passed
@JoshuaBatty JoshuaBatty deleted the josh/module_caching branch August 15, 2024 22:05
Copy link

Benchmark for b1e5e80

Click to view benchmark
Test Base PR %
code_action 5.2±0.03ms 5.0±0.10ms -3.85%
code_lens 340.2±17.61ns 282.9±8.72ns -16.84%
compile 2.6±0.07s 2.7±0.03s +3.85%
completion 4.7±0.07ms 4.6±0.11ms -2.13%
did_change_with_caching 2.6±0.02s 2.6±0.04s 0.00%
document_symbol 852.1±15.65µs 933.9±13.50µs +9.60%
format 68.5±1.23ms 67.6±1.58ms -1.31%
goto_definition 340.9±6.98µs 338.5±6.26µs -0.70%
highlight 9.1±0.38ms 8.6±0.03ms -5.49%
hover 497.7±7.45µs 491.7±6.19µs -1.21%
idents_at_position 119.6±0.55µs 117.3±0.76µs -1.92%
inlay_hints 635.2±24.28µs 636.0±59.65µs +0.13%
on_enter 2.1±0.03µs 1996.6±89.60ns -4.92%
parent_decl_at_position 3.7±0.01ms 3.6±0.03ms -2.70%
prepare_rename 340.8±4.56µs 336.2±8.36µs -1.35%
rename 9.3±0.13ms 8.9±0.13ms -4.30%
semantic_tokens 1200.4±14.86µs 1211.9±13.16µs +0.96%
token_at_position 334.7±2.39µs 329.9±2.58µs -1.43%
tokens_at_position 3.7±0.03ms 3.5±0.04ms -5.41%
tokens_for_file 399.9±5.97µs 399.8±5.06µs -0.03%
traverse 37.1±0.37ms 34.5±0.54ms -7.01%

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
compiler: frontend Everything to do with type checking, control flow analysis, and everything between parsing and IRgen language server LSP server performance Everything related to performance, speed wise or memory wise.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Enable module level caching using the QueryEngine
5 participants