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: Implement custom source formatting to improve performance #706

Merged
merged 1 commit into from
Sep 1, 2023
Merged

feat: Implement custom source formatting to improve performance #706

merged 1 commit into from
Sep 1, 2023

Conversation

latonz
Copy link
Contributor

@latonz latonz commented Aug 31, 2023

Implement custom source formatting to improve performance

Description

Implements custom source formatting during the creation of the syntax nodes.
This PR slightly changes the formatting of a few things to improve performance / readability:

  • After a block no empty line is inserted
  • Initializer blocks: each assignment is on its own line (even for older Roslyn versions) and has a trailing comma
Performance current main
Method Mean Error StdDev Gen0 Gen1 Allocated
Compile 582.8 us 11.66 us 29.24 us 48.8281 11.7188 302.52 KB
LargeCompile 31,990.4 us 312.57 us 277.08 us 2125.0000 875.0000 13270.94 KB
Performance with this PR
Method Mean Error StdDev Gen0 Gen1 Allocated
Compile 470.7 us 9.38 us 13.45 us 47.8516 6.8359 298.45 KB
LargeCompile 19,770.2 us 187.93 us 175.79 us 1906.2500 531.2500 11816.03 KB

Improvement of about 38% for our LargeCompile case and small memory improvements 🚀
The PR is pretty big, although a lot of it are just reformatted test files and namespace moves.

Checklist

  • The existing code style is followed
  • The commit message follows our guidelines
  • Performed a self-review of my code
  • Hard-to-understand areas of my code are commented
  • The documentation is updated (as applicable)
  • Unit tests are added/updated
  • Integration tests are added/updated (as applicable, especially if feature/bug depends on roslyn or framework version in use)

@codecov
Copy link

codecov bot commented Aug 31, 2023

Codecov Report

Merging #706 (e2eeb13) into main (23c2adb) will increase coverage by 0.32%.
The diff coverage is 96.56%.

@@            Coverage Diff             @@
##             main     #706      +/-   ##
==========================================
+ Coverage   90.82%   91.15%   +0.32%     
==========================================
  Files         180      198      +18     
  Lines        6313     6555     +242     
  Branches      807      815       +8     
==========================================
+ Hits         5734     5975     +241     
+ Misses        390      389       -1     
- Partials      189      191       +2     
Files Changed Coverage Δ
...iptors/MappingBuilders/EnumerableMappingBuilder.cs 94.80% <ø> (ø)
...Mapperly/Descriptors/Mappings/ArrayCloneMapping.cs 100.00% <ø> (ø)
.../Riok.Mapperly/Descriptors/Mappings/CastMapping.cs 100.00% <ø> (ø)
...ptors/Mappings/Enums/EnumFromStringParseMapping.cs 65.21% <ø> (ø)
...SourceObjectMemberDelegateExistingTargetMapping.cs 100.00% <ø> (ø)
...escriptors/Mappings/ForEachAddEnumerableMapping.cs 95.23% <ø> (ø)
...escriptors/Mappings/ForEachSetDictionaryMapping.cs 100.00% <ø> (ø)
.../Descriptors/Mappings/SourceObjectMemberMapping.cs 100.00% <ø> (ø)
.../Descriptors/Mappings/SourceObjectMethodMapping.cs 100.00% <ø> (ø)
...pperly/Descriptors/Mappings/StaticMethodMapping.cs 100.00% <ø> (ø)
... and 74 more

📣 We’re building smart automated test selection to slash your CI/CD build times. Learn more

@latonz latonz marked this pull request as ready for review August 31, 2023 12:39
Copy link
Collaborator

@TimothyMakkison TimothyMakkison left a comment

Choose a reason for hiding this comment

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

Really impressive stuff, a 38% improvement is crazy good, it's probably on par with a string builder version. IQueryable expressions are way easier to read.
How long did this take 😨

SourceEmitter.Build now takes 15% of the time. Iirc used to take 50%-60% of the runtime.
image

  • After a block no empty line is inserted
  • Initializer blocks each assignment is on its own line (even for older Roslyn versions) and has a trailing comma

Is this intentional or a side effect? I had a similar issue with empty lines in my SourceWriter project

I haven't read the whole thing but I've added a couple of comments

src/Riok.Mapperly/Helpers/StackExtensions.cs Outdated Show resolved Hide resolved
src/Riok.Mapperly/Emit/SourceEmitter.cs Outdated Show resolved Hide resolved
@latonz
Copy link
Contributor Author

latonz commented Sep 1, 2023

@TimothyMakkison thanks for you feedback 😊
The changes on the initializer blocks are intentional and not adding an empty line after code blocks is an intentional performance wise decision. We could add it to every code block pretty easily. But the NormalizeWhitespace implementation only adds it if the next line is not a closing block }. This makes it hard as we cannot add the empty line based on the information of the node itself but would need to analyse the nodes afterwards. And adding it after each block looks strange...

@latonz latonz merged commit 44b5df9 into riok:main Sep 1, 2023
19 checks passed
@latonz latonz deleted the configurable-normalize-whitespace branch September 1, 2023 16:40
@github-actions
Copy link

github-actions bot commented Sep 5, 2023

🎉 This PR is included in version 3.2.0-next.2 🎉

The release is available on:

Your semantic-release bot 📦🚀

@github-actions
Copy link

🎉 This PR is included in version 3.2.0 🎉

The release is available on:

Your semantic-release bot 📦🚀

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.

3 participants