aspnet / Routing

[Archived] Middleware for routing requests to application logic. Project moved to https://github.com/aspnet/AspNetCore
Apache License 2.0
272 stars 122 forks source link

Improve performance and reduce allocations in TemplateBinder ctor #902

Closed drieseng closed 5 years ago

drieseng commented 6 years ago

Improves performance and reduce allocations in TemplateBinder ctors:

Result:

Method Version Mean Error StdDev Op/s Gen 0 Allocated
ParametersAndDefaultsAndRequiredKeysAndPolicies master 768.0 ns 5.927 ns 5.544 ns 1,302,060.6 0.0105 968 B
ParametersAndDefaultsAndRequiredKeysAndPolicies PR - List\ 589.1 ns 5.514 ns 5.158 ns 1,697,611.7 0.0095 840 B
ParametersAndDefaultsAndRequiredKeysAndPolicies PR - ArrayBuilder\ 565.9 ns 1.284 ns 1.072 ns 1,767,084.3 0.0076 760 B
ParametersAndNoDefaultsAndRequiredKeysAndPolicies master 637.8 ns 6.036 ns 5.351 ns 1,567,837.6 0.0095 824 B
ParametersAndNoDefaultsAndRequiredKeysAndPolicies PR - List\ 371.5 ns 3.651 ns 3.415 ns 2,691,922.5 0.0076 696 B
ParametersAndNoDefaultsAndRequiredKeysAndPolicies PR - ArrayBuilder\ 349.1 ns 2.952 ns 2.616 ns 2,864,878.1 0.0072 616 B
NoParametersAndDefaultsAndRequiredKeysAndPolicies master 690.6 ns 3.484 ns 3.089 ns 1,448,016.1 0.0105 984 B
NoParametersAndDefaultsAndRequiredKeysAndPolicies PR - List\ 483.5 ns 9.588 ns 8.968 ns 2,068,154.3 0.0095 856 B
NoParametersAndDefaultsAndRequiredKeysAndPolicies PR - ArrayBuilder\ 458.9 ns 3.747 ns 3.505 ns 2,179,081.6 0.0086 776 B
NoParametersAndNoDefaultsAndRequiredKeysAndNoPolicies master 386.1 ns 2.847 ns 2.523 ns 2,590,204.2 0.0048 424 B
NoParametersAndNoDefaultsAndRequiredKeysAndNoPolicies PR - List\ 316.5 ns 3.476 ns 3.252 ns 3,159,387.1 0.0052 504 B
NoParametersAndNoDefaultsAndRequiredKeysAndNoPolicies PR - ArrayBuilder\ 300.6 ns 2.025 ns 1.795 ns 3,326,396.0 0.0043 424 B

Benchmark is available here.

drieseng commented 6 years ago

@JamesNK Do you only ever intend to use the first parameter transformer? In DefaultLinkGenerator.CreateTemplateBinder(RouteEndpoint endpoint) we only pass the first one to the TemplateBinder ctor.

Also, why not pass the constraints and transformer(s) separately to the TemplateBinder ctor? Right now, we do the work of extracting them from the list of parameter policies twice.

JamesNK commented 6 years ago

Do you only ever intend to use the first parameter transformer?

Yes, for simplicity.

Also, why not pass the constraints and transformer(s) separately to the TemplateBinder ctor? Right now, we do the work of extracting them from the list of parameter policies twice.

It is a public constructor. If additional usages of policies in the future then the constructor would need to change.

natemcmaster commented 5 years ago

@rynowak @JamesNK We're merging Routing today into aspnet/AspNetCore. If you can merge this in the next 30 minutes or so, we can bring this along. Otherwise, you'll have to open a new PR against https://github.com/aspnet/AspNetCore.