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: cache attribute symbols #478

Merged
merged 1 commit into from
Jun 6, 2023
Merged

Conversation

TimothyMakkison
Copy link
Collaborator

@TimothyMakkison TimothyMakkison commented Jun 2, 2023

Description
See #475

Add a Dictionary<Type, INamedTypeSymbol?> cache to WellKnownTypes to optimize AttributeDataAccessor.

Benchmarks
Benefits mappers with many mapping methods, saves 8ms for LargeCompile and saved 300 KB of memory. Doesn't look like Compile is effected.

Old:

Method Mean Error StdDev Gen0 Gen1 Allocated
Compile 1.563 ms 0.0261 ms 0.0244 ms 82.0313 19.5313 169.32 KB
LargeCompile 59.450 ms 1.1351 ms 1.1148 ms 1428.5714 285.7143 9280.05 KB
Method Mean Error StdDev Gen0 Gen1 Allocated
Compile 1.536 ms 0.0238 ms 0.0211 ms 82.0313 19.5313 170.23 KB
LargeCompile 57.525 ms 0.5249 ms 0.5155 ms 1428.5714 285.7143 9276.68 KB
Method Mean Error StdDev Gen0 Gen1 Allocated
Compile 1.571 ms 0.0240 ms 0.0225 ms 78.1250 15.6250 169.85 KB
LargeCompile 58.310 ms 0.9858 ms 1.2819 ms 1428.5714 285.7143 9272.75 KB

New:

Method Mean Error StdDev Gen0 Gen1 Allocated
Compile 1.594 ms 0.0303 ms 0.0384 ms 78.1250 15.6250 168.44 KB
LargeCompile 51.705 ms 0.8099 ms 1.1616 ms 1375.0000 375.0000 8934.55 KB
Method Mean Error StdDev Gen0 Gen1 Allocated
Compile 1.575 ms 0.0284 ms 0.0304 ms 89.8438 23.4375 168.49 KB
LargeCompile 51.513 ms 0.7022 ms 1.1924 ms 1333.3333 333.3333 8965.35 KB
Method Mean Error StdDev Gen0 Gen1 Allocated
Compile 1.559 ms 0.0296 ms 0.0277 ms 78.1250 19.5313 169.52 KB
LargeCompile 51.809 ms 0.9939 ms 1.0207 ms 1375.0000 375.0000 8925.2 KB

@codecov
Copy link

codecov bot commented Jun 2, 2023

Codecov Report

Merging #478 (5ab47d0) into main (b17f666) will increase coverage by 0.39%.
The diff coverage is 95.41%.

❗ Current head 5ab47d0 differs from pull request most recent head beb7a53. Consider uploading reports for the commit beb7a53 to get more accurate results

@@            Coverage Diff             @@
##             main     #478      +/-   ##
==========================================
+ Coverage   92.08%   92.48%   +0.39%     
==========================================
  Files         135      139       +4     
  Lines        4282     4455     +173     
  Branches      591      569      -22     
==========================================
+ Hits         3943     4120     +177     
+ Misses        225      221       -4     
  Partials      114      114              
Impacted Files Coverage Δ
...scriptors/InlineExpressionMappingBuilderContext.cs 95.12% <ø> (ø)
...appingBodyBuilders/UserMethodMappingBodyBuilder.cs 90.47% <ø> (ø)
...Riok.Mapperly/Descriptors/MappingBuilderContext.cs 98.14% <ø> (ø)
...erly/Descriptors/MappingBuilders/MappingBuilder.cs 100.00% <ø> (ø)
src/Riok.Mapperly/Descriptors/MappingCollection.cs 100.00% <ø> (ø)
...iok.Mapperly/Descriptors/Mappings/MethodMapping.cs 95.00% <ø> (ø)
...Mappings/UserDefinedExistingTargetMethodMapping.cs 87.17% <ø> (ø)
...serMappings/UserDefinedNewInstanceMethodMapping.cs 100.00% <ø> (ø)
...pings/UserMappings/UserImplementedMethodMapping.cs 90.90% <ø> (ø)
...riptors/MappingBuilders/QueryableMappingBuilder.cs 68.42% <50.00%> (ø)
... and 29 more

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

Copy link
Contributor

@latonz latonz left a comment

Choose a reason for hiding this comment

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

Thank you for this improvement 😊 The performance improvement is dependent on how many times attributes where used in the mapper declaration, therefore mappers with more attributes will benefit a lot more.

@TimothyMakkison
Copy link
Collaborator Author

TimothyMakkison commented Jun 3, 2023

Removed all fields from WellKnownTypes, removed nullables and added TryGetTypeSymbol for DateOnly and TimeOnly. WellKnownTypes is now instantiated in MapperGenerator and shared between instances saving another 400KB for LargeCompile.

The DI passes although I'm a little concerned I've broken .NET Framework by removing some of the null checks.

Ran the Benchmarks a couple of times, both appear to be slower than the previous benchmarks. I wonder if the one of the more recent versions of mapperly have made it slower.

Old:

Method Mean Error StdDev Gen0 Gen1 Allocated
Compile 1.544 ms 0.0306 ms 0.0301 ms 78.1250 15.6250 170.25 KB
LargeCompile 59.269 ms 1.0096 ms 1.7144 ms 1500.0000 500.0000 9276.7 KB
Method Mean Error StdDev Median Gen0 Gen1 Allocated
Compile 1.564 ms 0.0301 ms 0.0358 ms 1.554 ms 82.0313 15.6250 168.29 KB
LargeCompile 60.973 ms 1.2988 ms 3.5774 ms 59.640 ms 1428.5714 428.5714 9274.68 KB
Method Mean Error StdDev Gen0 Gen1 Allocated
Compile 1.553 ms 0.0285 ms 0.0267 ms 89.8438 19.5313 172.33 KB
LargeCompile 58.498 ms 0.9764 ms 1.1991 ms 1428.5714 285.7143 9266.14 KB

New:

Method Mean Error StdDev Median Gen0 Gen1 Allocated
Compile 1.852 ms 0.0990 ms 0.2856 ms 1.721 ms 78.1250 19.5313 171.06 KB
LargeCompile 48.584 ms 0.9688 ms 0.8589 ms 48.693 ms 1333.3333 333.3333 8531.57 KB
Method Mean Error StdDev Median Gen0 Gen1 Allocated
Compile 2.059 ms 0.1961 ms 0.5783 ms 1.740 ms 70.3125 15.6250 168.78 KB
LargeCompile 63.516 ms 6.1086 ms 18.0113 ms 57.057 ms 1285.7143 285.7143 8532.13 KB
Method Mean Error StdDev Median Gen0 Gen1 Allocated
Compile 2.168 ms 0.1911 ms 0.5634 ms 1.989 ms 85.9375 15.6250 170.77 KB
LargeCompile 49.377 ms 0.9812 ms 2.0482 ms 49.311 ms 1333.3333 500.0000 8534.1 KB

@TimothyMakkison
Copy link
Collaborator Author

TimothyMakkison commented Jun 6, 2023

Thanks for the review! I've moved the properties, resued the logic between Get and TryGet and used the generic overload where possible.

@github-actions
Copy link

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

The release is available on:

Your semantic-release bot 📦🚀

@latonz latonz added this to the v2.9.0 milestone Jun 14, 2023
@github-actions
Copy link

github-actions bot commented Aug 7, 2023

🎉 This PR is included in version 3.0.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.

2 participants