Skip to content

Avoid allocations in more cases #9788

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

Merged
merged 2 commits into from
Apr 27, 2019
Merged

Avoid allocations in more cases #9788

merged 2 commits into from
Apr 27, 2019

Conversation

rynowak
Copy link
Member

@rynowak rynowak commented Apr 26, 2019

Updates to DFA Matcher to avoid allocations in more cases. This makes
the matcher more pay-for-play.

  • Avoid allocating an RVD while matching if possible
  • Avoid allocating the candidate set unless necessary

First, avoid creating the RVD unless there are parameters or
constraints. This means that the candidate set can contain null route
value dictionaries. This seems fine because selectors are already
low-level. The route values feature will allocate an RVD when accessed,
so code in MVC or middleware won't even see a null RVD.

Secondly, avoid creating the candidate set unless there are selectors.
This will save an allocation most of the time since we won't need to run
selectors is really common cases. The candidate set is needed because
selectors mix mutability and async, so it needs to be a reference type.
However the default case is that we don't need to run selectors. The
impact of this is that we make a bunch of methods have an instance
variant and a static variant that operates on the array.


I ran some perf tests on this, and it helps the case where you have multiple endpoints that match but no policies. We don't have any tests that directly benchmark this scenario, but it's somewhat common when you use parameters and literals in the same position, or use constraints.

Numbers from GithubMatcherBenchmark - this benchmark has some cases like this, and there's a marginal improvement across the aggregate.

Before

BenchmarkDotNet=v0.10.13, OS=Windows 10.0.17763
Intel Core i7-8650U CPU 1.90GHz (Kaby Lake R), 1 CPU, 8 logical cores and 4 physical cores
.NET Core SDK=3.0.100-preview6-011546
  [Host]     : .NET Core 3.0.0-preview6-27623-04 (CoreCLR 4.6.27622.74, CoreFX 4.700.19.22212), 64bit RyuJIT
  Job-VZXIJQ : .NET Core 3.0.0-preview6-27623-04 (CoreCLR 4.6.27622.74, CoreFX 4.700.19.22212), 64bit RyuJIT

Runtime=Core  Server=True  Toolchain=.NET Core 3.0  
RunStrategy=Throughput  
Method Mean Error StdDev Op/s Scaled ScaledSD Gen 0 Allocated
Baseline 147.4 ns 2.075 ns 1.839 ns 6,783,660.9 1.00 0.00 1.7090 9.49 KB
Dfa 354.7 ns 4.589 ns 3.583 ns 2,819,073.1 2.41 0.04 10.8643 57.07 KB

After

BenchmarkDotNet=v0.10.13, OS=Windows 10.0.17763
Intel Core i7-8650U CPU 1.90GHz (Kaby Lake R), 1 CPU, 8 logical cores and 4 physical cores
.NET Core SDK=3.0.100-preview6-011546
  [Host]     : .NET Core 3.0.0-preview6-27623-04 (CoreCLR 4.6.27622.74, CoreFX 4.700.19.22212), 64bit RyuJIT
  Job-IRODAN : .NET Core 3.0.0-preview6-27623-04 (CoreCLR 4.6.27622.74, CoreFX 4.700.19.22212), 64bit RyuJIT

Runtime=Core  Server=True  Toolchain=.NET Core 3.0  
RunStrategy=Throughput  
Method Mean Error StdDev Op/s Scaled ScaledSD Gen 0 Allocated
Baseline 144.1 ns 1.231 ns 1.152 ns 6,940,674.8 1.00 0.00 1.8311 9.49 KB
Dfa 338.6 ns 2.982 ns 2.490 ns 2,953,168.6 2.35 0.02 9.5215 52.12 KB

@Eilon Eilon added the area-mvc Includes: MVC, Actions and Controllers, Localization, CORS, most templates label Apr 26, 2019
Copy link
Member

@davidfowl davidfowl left a comment

Choose a reason for hiding this comment

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

I approve this message

@davidfowl
Copy link
Member

What allocations are left? Just the selector context?

@rynowak
Copy link
Member Author

rynowak commented Apr 26, 2019

What allocations are left? Just the selector context?

Selector context - AND the CandiateState[] that we allocate.

Ryan Nowak added 2 commits April 26, 2019 15:58
Updates to DFA Matcher to avoid allocations in more cases. This makes
the matcher more pay-for-play.

- Avoid allocating an RVD while matching if possible
- Avoid allocating the candidate set unless necessary

First, avoid creating the RVD unless there are parameters or
constraints. This means that the candidate set can contain null route
value dictionaries. This seems fine because selectors are already
low-level. The route values feature will allocate an RVD when accessed,
so code in MVC or middleware won't even see a null RVD.

Secondly, avoid creating the candidate set unless there are selectors.
This will save an allocation most of the time since we won't need to run
selectors is really common cases. The candidate set is needed because
selectors mix mutability and async, so it needs to be a reference type.
However the default case is that we *don't* need to run selectors. The
impact of this is that we make a bunch of methods have an instance
variant and a static variant that operates on the array.
@rynowak rynowak force-pushed the rynowak/noop-harder branch from 8f21d41 to 510fa81 Compare April 26, 2019 23:11
@rynowak
Copy link
Member Author

rynowak commented Apr 27, 2019

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-mvc Includes: MVC, Actions and Controllers, Localization, CORS, most templates
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants