Repository navigation
[controls] improve performance of {Binding} - #14830
Merged
jonathanpeppers merged 1 commit intoMay 2, 2023
Merged
Conversation
I wrote a benchmark for `Binding`, such as:
[Benchmark]
public void BindName()
{
var binding = new Binding("Name", source: Source);
Target.SetBinding(MyObject.NameProperty, binding);
}
Attaching `dotnet-trace`, I found ~53% of the time was spent in
`MethodInfo.GetParameters()`:
core.benchmarks!Microsoft.Maui.Benchmarks.BindingBenchmarker.BindName()
...
microsoft.maui.controls!Microsoft.Maui.Controls.BindingExpression.SetupPart()
System.Private.CoreLib.il!System.Reflection.RuntimeMethodInfo.GetParameters()
Reviewing the code, it was basically calling:
System.Reflection.PropertyInfo.SetMethod.GetParameters()[0].ParameterType;
We can just call `PropertyInfo.PropertyType` instead, to avoid
allocating a `ParameterInfo[]` and indexing into it.
The results of this change in my benchmarks:
| Method | Mean | Error | StdDev | Gen0 | Gen1 | Allocated |
|------------------- |---------:|---------:|---------:|-------:|-------:|----------:|
| --BindName | 18.82 us | 0.336 us | 0.471 us | 1.2817 | 1.2512 | 10.55 KB |
| ++BindName | 18.80 us | 0.371 us | 0.555 us | 1.2512 | 1.2207 | 10.23 KB |
| --BindChild | 27.47 us | 0.542 us | 0.827 us | 2.0142 | 1.9836 | 16.56 KB |
| ++BindChild | 26.71 us | 0.516 us | 0.652 us | 1.9226 | 1.8921 | 15.94 KB |
| --BindChildIndexer | 58.39 us | 1.113 us | 1.143 us | 3.1738 | 3.1128 | 26.17 KB |
| ++BindChildIndexer | 58.00 us | 1.055 us | 1.295 us | 3.1128 | 3.0518 | 25.47 KB |
-- Before
++ After
This should help scrolling performance such as in dotnet#12130, where bindings
are updating while scrolling.
mattleibow
approved these changes
Apr 29, 2023
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
Sign up for free
to subscribe to this conversation on GitHub.
Already have an account?
Sign in.
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.
I wrote a benchmark for
Binding, such as:Attaching
dotnet-trace, I found ~53% of the time was spent inMethodInfo.GetParameters():Reviewing the code, it was basically calling:
We can just call
PropertyInfo.PropertyTypeinstead, to avoid allocating aParameterInfo[]and indexing into it.The results of this change in my benchmarks:
-- Before
++ After
This should help scrolling performance such as in #12130, where bindings are updating while scrolling.