You signed in with another tab or window.Reload to refresh your session.You signed out in another tab or window.Reload to refresh your session.You switched accounts on another tab or window.Reload to refresh your session.Dismiss alert
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
Reduces allocs and increases CPU perf ofActivatorUtilities.CreateInstance()
The alloc reduction is significant and depends on number of constructors and the number of constructor parameters (e.g. 4.6x reduction for 3 constructors).
The CPU case depends on whether[ActivatorUtilitiesConstructor] is used. If used, the perf improvements are much better:
When used, CPU improvement of ~2x depending on number of constructor parameters.
Leveraging theConstructorInvoker class added in v8; this allows use ofSpan<object> over stack-allocated objects (if <= 4 parameters) or the ability to re-use an allocation ofobject[] over varying number of constructor parameters.
.NET Framework is also slightly improved for both CPU and allocs:
.NET Framework 4.8.1 Benchmarks
BEFORE| Method | Mean | Error | StdDev | Median | Min | Max | Gen0 | Allocated ||------------------------------- |---------:|----------:|----------:|---------:|---------:|----------:|-------:|----------:|| CreateInstance_1 | 1.391 us | 0.0269 us | 0.0309 us | 1.385 us | 1.355 us | 1.447 us | 0.0650 | 417 B || CreateInstance_3 | 4.355 us | 0.0778 us | 0.0690 us | 4.348 us | 4.264 us | 4.464 us | 0.1575 | 1043 B || CreateInstance_5 | 9.039 us | 0.1724 us | 0.1771 us | 8.978 us | 8.799 us | 9.430 us | 0.2889 | 1926 B || CreateInstance_1_WithAttrFirst | 1.993 us | 0.0234 us | 0.0208 us | 1.989 us | 1.957 us | 2.038 us | 0.0788 | 538 B || CreateInstance_3_WithAttrFirst | 5.031 us | 0.0971 us | 0.0909 us | 5.004 us | 4.898 us | 5.216 us | 0.1800 | 1164 B || CreateInstance_5_WithAttrFirst | 9.790 us | 0.1928 us | 0.1980 us | 9.701 us | 9.551 us | 10.255 us | 0.3104 | 2046 B || CreateInstance_1_WithAttrLast | 2.046 us | 0.0354 us | 0.0347 us | 2.033 us | 1.996 us | 2.117 us | 0.0808 | 538 B || CreateInstance_3_WithAttrLast | 5.405 us | 0.2172 us | 0.2414 us | 5.372 us | 5.155 us | 5.935 us | 0.1829 | 1163 B || CreateInstance_5_WithAttrLast | 9.787 us | 0.0755 us | 0.0630 us | 9.780 us | 9.700 us | 9.877 us | 0.3179 | 2046 B |AFTER| Method | Mean | Error | StdDev | Median | Min | Max | Gen0 | Allocated ||------------------------------- |---------:|----------:|----------:|---------:|---------:|---------:|-------:|----------:|| CreateInstance_1 | 1.257 us | 0.0148 us | 0.0124 us | 1.254 us | 1.246 us | 1.290 us | 0.0601 | 393 B || CreateInstance_3 | 4.120 us | 0.0342 us | 0.0320 us | 4.124 us | 4.073 us | 4.181 us | 0.1464 | 947 B || CreateInstance_5 | 8.723 us | 0.0864 us | 0.0808 us | 8.708 us | 8.611 us | 8.867 us | 0.2405 | 1725 B || CreateInstance_1_WithAttrFirst | 1.925 us | 0.0215 us | 0.0201 us | 1.920 us | 1.904 us | 1.959 us | 0.0767 | 514 B || CreateInstance_3_WithAttrFirst | 4.909 us | 0.0628 us | 0.0588 us | 4.895 us | 4.835 us | 5.012 us | 0.1582 | 1067 B || CreateInstance_5_WithAttrFirst | 9.309 us | 0.0847 us | 0.0751 us | 9.278 us | 9.199 us | 9.470 us | 0.2623 | 1846 B || CreateInstance_1_WithAttrLast | 1.931 us | 0.0288 us | 0.0269 us | 1.926 us | 1.898 us | 1.975 us | 0.0804 | 514 B || CreateInstance_3_WithAttrLast | 4.885 us | 0.0635 us | 0.0594 us | 4.862 us | 4.815 us | 5.022 us | 0.1576 | 1067 B || CreateInstance_5_WithAttrLast | 9.392 us | 0.1023 us | 0.0957 us | 9.360 us | 9.277 us | 9.575 us | 0.2593 | 1846 B |
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading.Please reload this page.
Reduces allocs and increases CPU perf of
ActivatorUtilities.CreateInstance()The alloc reduction is significant and depends on number of constructors and the number of constructor parameters (e.g. 4.6x reduction for 3 constructors).
The CPU case depends on whether
[ActivatorUtilitiesConstructor]is used. If used, the perf improvements are much better:The optimizations include:
[ActivatorUtilitiesConstructor]is used. We can now ignore most processing of other constructors.ConstructorInvokerclass added in v8; this allows use ofSpan<object>over stack-allocated objects (if <= 4 parameters) or the ability to re-use an allocation ofobject[]over varying number of constructor parameters.Benchmarks
.NET Framework is also slightly improved for both CPU and allocs:
.NET Framework 4.8.1 Benchmarks