Browse Source

Merge pull request #4020 from MarchingCube/perf-inpc

Benchmark and simple optimization for INPC accessor.
pull/4077/head
Dariusz Komosiński 6 years ago
committed by GitHub
parent
commit
d6693dbfd4
No known key found for this signature in database GPG Key ID: 4AEE18F83AFDEB23
  1. 8
      src/Avalonia.Base/Data/Core/Plugins/InpcPropertyAccessorPlugin.cs
  2. 57
      tests/Avalonia.Benchmarks/Data/PropertyAccessorBenchmarks.cs

8
src/Avalonia.Base/Data/Core/Plugins/InpcPropertyAccessorPlugin.cs

@ -1,7 +1,5 @@
using System;
using System.ComponentModel;
using System.Linq;
using System.Reactive.Linq;
using System.Reflection;
using Avalonia.Utilities;
@ -31,7 +29,11 @@ namespace Avalonia.Data.Core.Plugins
Contract.Requires<ArgumentNullException>(propertyName != null);
reference.TryGetTarget(out object instance);
var p = instance.GetType().GetRuntimeProperties().FirstOrDefault(x => x.Name == propertyName);
const BindingFlags bindingFlags = BindingFlags.NonPublic | BindingFlags.Public |
BindingFlags.Static | BindingFlags.Instance;
var p = instance.GetType().GetProperty(propertyName, bindingFlags);
if (p != null)
{

57
tests/Avalonia.Benchmarks/Data/PropertyAccessorBenchmarks.cs

@ -0,0 +1,57 @@
using System;
using System.ComponentModel;
using System.Runtime.CompilerServices;
using Avalonia.Data.Core.Plugins;
using BenchmarkDotNet.Attributes;
using JetBrains.Annotations;
namespace Avalonia.Benchmarks.Data
{
[MemoryDiagnoser, InProcess]
public class PropertyAccessorBenchmarks
{
private readonly InpcPropertyAccessorPlugin _plugin = new InpcPropertyAccessorPlugin();
private readonly TestObject _targetStrongRef = new TestObject();
private readonly WeakReference<object> _targetWeakRef;
public PropertyAccessorBenchmarks()
{
_targetWeakRef = new WeakReference<object>(_targetStrongRef);
}
[Benchmark]
public void InpcAccessor()
{
_plugin.Start(_targetWeakRef, nameof(TestObject.Test));
}
private class TestObject : INotifyPropertyChanged
{
private string _test;
public string Test
{
get => _test;
set
{
if (_test == value)
{
return;
}
_test = value;
OnPropertyChanged();
}
}
public event PropertyChangedEventHandler PropertyChanged;
[NotifyPropertyChangedInvocator]
protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
}
}
}
Loading…
Cancel
Save