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

Batching for AvaloniaObject property values. #5070

Merged
merged 19 commits into from
Mar 9, 2021

Conversation

grokys
Copy link
Member

@grokys grokys commented Nov 18, 2020

What does the pull request do?

Cuts down the amount of notifications raised when controls' styling is attached/detached:

  • Adds batching to AvaloniaObject by calling BeginBatchUpdate/EndBatchUpdate. During batch update, no property change notifications are sent and bindings are not subscribed
  • Don't subscribe to PropertySetterBindingInstance's inner observable if the containing style instance is not active: this reduces unnecessary resource lookups
  • Lazily evaluate <Template>s in setters

Part of fixing #5027.

Not likely to be ready for 0.10.0 - will need a fair bit of testing.

Benchmarks

before

|       Method |     Mean |     Error |    StdDev |   Gen 0 |   Gen 1 | Gen 2 | Allocated |
|------------- |---------:|----------:|----------:|--------:|--------:|------:|----------:|
| RepeatButton | 1.288 ms | 0.0209 ms | 0.0185 ms | 31.2500 | 13.6719 |     - |  292.1 KB |

after

|       Method |     Mean |   Error |   StdDev |   Median |   Gen 0 | Gen 1 | Gen 2 | Allocated |
|------------- |---------:|--------:|---------:|---------:|--------:|------:|------:|----------:|
| RepeatButton | 391.2 us | 7.77 us | 18.77 us | 382.6 us | 15.1367 |     - |     - |  62.52 KB |

Checklist

  • Added unit tests (if possible)?

We shouldn't subscribe to bindings until needed.
As that's where #5027 was showing up most.
When subscribing to a `PropertySetterBindingInstance`, if the owner style is not active then there's no need to subscribe to the inner observable as this can cause resource lookups etc. Part of fixing #5027.
Cuts down the amount of notifications raised when controls' stying is attached/detached.

Part of fixing #5027.
@grokys grokys marked this pull request as draft November 18, 2020 16:03
@@ -22,6 +23,8 @@ internal class BindingEntry<T> : IBindingEntry, IPriorityValueEntry<T>, IObserve
private readonly IAvaloniaObject _owner;
private IValueSink _sink;
private IDisposable? _subscription;
private bool _isSubscribed;
Copy link
Member Author

Choose a reason for hiding this comment

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

Probably worth packing these into an flags enum to save 4 bytes per value stored.

Copy link
Collaborator

Choose a reason for hiding this comment

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

If only life would be that simple 😄 Depending on the type of the entry alignment of this class will change, for example Sharplab reducing size will do nothing since there is no free space and it will get aligned.

{
private readonly IAvaloniaObject _owner;
private readonly IValueSink _sink;
private readonly List<IPriorityValueEntry<T>> _entries = new List<IPriorityValueEntry<T>>();
private readonly Func<IAvaloniaObject, T, T>? _coerceValue;
private Optional<T> _localValue;
private Optional<T> _value;
private bool _isCalculatingValue;
private bool _batchUpdate;
Copy link
Member Author

Choose a reason for hiding this comment

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

Probably worth packing these into an flags enum to save 4 bytes per value stored.

@grokys grokys mentioned this pull request Jan 25, 2021
3 tasks
Copy link
Collaborator

@MarchingCube MarchingCube left a comment

Choose a reason for hiding this comment

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

I've been running this branch in my app for a few days already. I've definitely noticed that resource lookup count is more manageable and random templates are not being instantiated.

Left two perf comments but mostly for keeping track of it, I can fix it later (such setters are quite rare anyway).

data.result = new PropertySetterLazyInstance<T>(
data.target,
property,
() => (T)template.Build());
Copy link
Collaborator

Choose a reason for hiding this comment

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

perf: eliminate closure and delegate allocation.

data.result = new PropertySetterLazyInstance<T>(
data.target,
property,
() => (T)template.Build());
Copy link
Collaborator

Choose a reason for hiding this comment

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

perf: eliminate closure and delegate allocation.

AssetLoader.RegisterResUriParsers();
return new Styles
{
new StyleInclude(new Uri("avares://Avalonia.Benchmarks"))
Copy link
Collaborator

Choose a reason for hiding this comment

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

I guess this can be replaced now by new FluentTheme()?

@grokys grokys changed the title WIP: Batching for AvaloniaObject property values. Batching for AvaloniaObject property values. Mar 9, 2021
@grokys grokys marked this pull request as ready for review March 9, 2021 08:26
@grokys grokys enabled auto-merge March 9, 2021 08:26
@grokys grokys merged commit 8b34cd3 into master Mar 9, 2021
@grokys grokys deleted the fixes/5027-avaloniaobject-batching branch March 9, 2021 08:42
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.

3 participants