Skip to content

Conversation

xunilrj
Copy link
Contributor

@xunilrj xunilrj commented Apr 16, 2025

Description

This PR is a prequel to #7015, trying to optimize the compiler to alleviate the hit of having more items, impl etc...

We have here two optimizations:
1 - We spend a lot of time counting newlines when converting byte offsets to LineCol. Now, we calculate line offsets just once, and use binary search later to find which line a byte offset is at.
2 - QueryEngine::get_programs_cache_entry was cloning the whole TyProgram. That is why did_change_with_caching was always getting worse, as the program was increasing in size. Now all compilation stages are behind Arc, which makes the clone free.

Analysis

Putting a dbg!(...) like the image below, and calling counts (https://github.com/nnethercote/counts).

cargo bench -- traverse 2>&1 | grep "sway-types/src/span.rs:29:9" | counts

image

I get the following results:

972102 counts
(  1)   156720 (16.1%, 16.1%): [sway-types/src/span.rs:29:9] self.pos = 0
(  2)    15900 ( 1.6%, 17.8%): [sway-types/src/span.rs:29:9] self.pos = 104
(  3)    15840 ( 1.6%, 19.4%): [sway-types/src/span.rs:29:9] self.pos = 107
(  4)     2280 ( 0.2%, 19.6%): [sway-types/src/span.rs:29:9] self.pos = 19281
(  5)     2280 ( 0.2%, 19.9%): [sway-types/src/span.rs:29:9] self.pos = 19285
(  6)     2280 ( 0.2%, 20.1%): [sway-types/src/span.rs:29:9] self.pos = 19287
(  7)     2280 ( 0.2%, 20.3%): [sway-types/src/span.rs:29:9] self.pos = 19292
(  8)     2280 ( 0.2%, 20.6%): [sway-types/src/span.rs:29:9] self.pos = 19323
(  9)     2280 ( 0.2%, 20.8%): [sway-types/src/span.rs:29:9] self.pos = 19327
( 10)     2280 ( 0.2%, 21.0%): [sway-types/src/span.rs:29:9] self.pos = 19329
( 11)     2280 ( 0.2%, 21.3%): [sway-types/src/span.rs:29:9] self.pos = 19334
( 12)      870 ( 0.1%, 21.4%): [sway-types/src/span.rs:29:9] self.pos = 4285
...

This means that line_col is being called 972k times. 16% is for position zero, which should be trivial. The rest will iterate the whole file source code to count number of lines. Making the real complexity of the work here something like O(qty * self.pos). And some values of self.pos are not trivial at all.

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.

@xunilrj xunilrj force-pushed the xunilrj/sway-optimizations-2 branch from 9c37556 to 103a146 Compare April 16, 2025 18:16
@xunilrj xunilrj self-assigned this Apr 16, 2025
Copy link

codspeed-hq bot commented Apr 16, 2025

CodSpeed Performance Report

Merging #7093 will improve performances by ×140

Comparing xunilrj/sway-optimizations-2 (8fed389) with master (7cb7809)

Summary

⚡ 11 improvements
✅ 11 untouched benchmarks

Benchmarks breakdown

Benchmark BASE HEAD Change
did_change_with_caching 520.9 ms 3.8 ms ×140
traverse 230.9 ms 48.3 ms ×4.8
code_action 28.3 ms 7.2 ms ×3.9
completion 21.1 ms 5.3 ms ×3.9
document_symbol 19.1 ms 3.1 ms ×6.1
find_all_references 47.4 ms 5.2 ms ×9.1
highlight 44.7 ms 5.8 ms ×7.7
inlay_hints 18.7 ms 2.2 ms ×8.6
rename 47.4 ms 5.2 ms ×9.1
parent_decl_at_position 18.8 ms 3.1 ms ×6.1
tokens_at_position 18.8 ms 3.1 ms ×6.1

JoshuaBatty
JoshuaBatty previously approved these changes Apr 17, 2025
Copy link
Member

@JoshuaBatty JoshuaBatty left a comment

Choose a reason for hiding this comment

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

This is really neat. Amazing performance increase. Hats off!

@tritao
Copy link
Contributor

tritao commented Apr 17, 2025

Crazy speedup, amazing 🚀

Just to note that "compile" and "did_change_with_caching" steps on LSP benchmark seem to have gotten around 7-9% slower though, seems unlikely to have been caused by the changes though given the speedups?

@xunilrj
Copy link
Contributor Author

xunilrj commented Apr 17, 2025

Crazy speedup, amazing 🚀

Just to note that "compile" and "did_change_with_caching" steps on LSP benchmark seem to have gotten around 7-9% slower though, seems unlikely to have been caused by the changes though given the speedups?

I also managed to optimize "did_change_with_caching".

@xunilrj xunilrj marked this pull request as ready for review April 17, 2025 22:07
@xunilrj xunilrj requested review from a team as code owners April 17, 2025 22:07
@xunilrj xunilrj enabled auto-merge (squash) April 17, 2025 22:08
@Voxelot
Copy link
Member

Voxelot commented Apr 18, 2025

Screenshot 2025-04-18 at 12 47 15 PM

wut 🤯

@xunilrj xunilrj merged commit f607a67 into master Apr 21, 2025
41 checks passed
@xunilrj xunilrj deleted the xunilrj/sway-optimizations-2 branch April 21, 2025 19:36
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants