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

Lower reflection overhead #2839

Merged
merged 23 commits into from
May 30, 2024
Merged

Lower reflection overhead #2839

merged 23 commits into from
May 30, 2024

Conversation

nohwnd
Copy link
Member

@nohwnd nohwnd commented May 10, 2024

This PR simplifies the way we access reflection in discovery and run. By improving the attribute cache and using it whenever possible.

It also simplifies ReflectHelper code, by removing dependencies between methods, and giving them more descriptive names. To make it much easier to reason about the current and desired behavior.

I am keeping the existing non-consistent behavior, to avoid breaking changes.

  • IsAttributeDefined => IsNonDerivedAttributeDefined

  • GetCustomAttribute => GetFirstDerivedAttributeOrDefault + inherit: true - when used with attributes that allow only one instance

  • GetCustomAttributes -> GetDerivedAttributes + inherit: true - for attributes that allow multiple.

Before

Initial measurements, on SDK 3.3.1.

Discovery of 10k empty tests:
discovered: 10000 tests in 1992,1448 ms, sent them in 289,5684 ms, total: 2281,7132
discovered: 10000 tests in 899,1829 ms, sent them in 212,4562 ms, total: 1111,6391
discovered: 10000 tests in 552,0988 ms, sent them in 321,4334 ms, total: 873,5322

Run 10k empty tests:

image

discovery: 1327ms
Microsoft(R) Testing Platform Execution Command Line Tool
Version: 1.1.0+8c0a8fd8e (UTC 2024/04/03)
RuntimeInformation: win-x64 - .NET 8.0.5
Copyright(c) Microsoft Corporation. All rights reserved.
Passed! - Failed: 0, Passed: 10000, Skipped: 0, Total: 10000, Duration: 2s 272ms - mstest133.dll (win-x64 - .NET 8.0.5)
run: 2616ms

After:

After

Discover 10 empty tests:
discovered: 10000 tests in 1219,4222 ms, sent them in 227,5277 ms, total: 1446,9499 <<<
2088
discovered: 10000 tests in 324,3993 ms, sent them in 193,5436 ms, total: 517,9429 <<<
1024
discovered: 10000 tests in 259,1427 ms, sent them in 291,6613 ms, total: 550,804 <<<
discovered: discovery alloc: 50 MB send alloc: 72 MB
971

Run 10k empty tests:

image

discovery: 999ms
discovered: 10000 tests in 279,4147 ms, sent them in 333,1555 ms, total: 612,5702 <<<
discovered: discovery alloc: 49 MB send alloc: 72 MB

.NET Testing Platform v1.3.0-preview.24277.3+3e1e5aebb (UTC 2024/05/27) [win-x64 - .NET 8.0.5]
discovered: 10000 tests in 271,2012 ms, sent them in 186,4562 ms, total: 457,65739999999994 <<<
discovered: discovery alloc: 49 MB send alloc: 52 MB
Passed! - Failed: 0, Passed: 10000, Skipped: 0, Total: 10000, Duration: 1s 120ms - mstest133.dll (win-x64 - .NET 8.0.5)
run: 1475ms

Conclusion

The overhead is cut to about half of what it was before, both in memory allocations and speed.

Fix #2763

@nohwnd nohwnd marked this pull request as ready for review May 28, 2024 13:10
@nohwnd nohwnd requested a review from Evangelink as a code owner May 28, 2024 13:10
Copy link
Member

@Evangelink Evangelink left a comment

Choose a reason for hiding this comment

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

I'll need another round of review and maybe playing a little with the code.

@Evangelink Evangelink merged commit 6ebb3d6 into main May 30, 2024
6 checks passed
@Evangelink Evangelink deleted the reflection-perf branch May 30, 2024 07:37
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.

Improve algorithm for reflection
2 participants