made 'property change detector' that uses prop getter delegate rather than PropertyInfo.GetValue for better performance
This commit is contained in:
1 parent
7388472f6a
commit
60c5159a4a
1 file changed
+112
-12
@@ -1,5 +1,7 @@
|
||||
using bts.api.core;
|
||||
using mbts;
|
||||
using mbts.ui.shell;
|
||||
using mbts.utils;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.ComponentModel;
|
||||
@@ -8,11 +10,25 @@ using System.Reactive.Linq;
|
||||
using System.Reflection;
|
||||
|
||||
namespace bts.utils {
|
||||
static class ObjectSnapshot {
|
||||
public static IObservable<TableUpdate<(string name, object current, object original), string>> property_table<T>(T instance, params string[] prop_names) where T : INotifyPropertyChanged =>
|
||||
property_table(instance, Array.ConvertAll(prop_names, Impl<T>.type.GetProperty));
|
||||
interface IPropertyChangedDetector<T> where T : IRaiseNotifyPropertyChanged {
|
||||
IDisposable detect_changes(T target);
|
||||
}
|
||||
|
||||
public static IObservable<TableUpdate<(string name, object current, object original), string>> property_table<T>(T instance, params PropertyInfo[] props) where T : INotifyPropertyChanged =>
|
||||
static class ObjectSnapshot {
|
||||
public static IEnumerable<Func<TSource, TValue>> property_delegates_of_type<TSource, TValue>() where TSource : class where TValue : class {
|
||||
var tval = typeof(TValue);
|
||||
return typeof(TSource).GetProperties(BindingFlags.Public | BindingFlags.Instance)
|
||||
.Where(p => tval.IsAssignableFrom(p.PropertyType))
|
||||
.Select(_prop_getter<TSource, TValue>);
|
||||
}
|
||||
|
||||
public static IObservable<TableUpdate<(string name, object current, object original), string>> property_table<T>(T instance, params string[] prop_names) where T : INotifyPropertyChanged =>
|
||||
_property_table(instance, Array.ConvertAll(prop_names, PropertyInfoStore<T>.type.GetProperty));
|
||||
|
||||
public static IObservable<TableUpdate<(string name, object current, object original), string>> property_table(INotifyPropertyChanged instance, params string[] prop_names) =>
|
||||
_property_table(instance, Array.ConvertAll(prop_names, instance.GetType().GetProperty));
|
||||
|
||||
static IObservable<TableUpdate<(string name, object current, object original), string>> _property_table(INotifyPropertyChanged instance, params PropertyInfo[] props) =>
|
||||
Observable.Create<TableUpdate<(string name, object current, object original), string>>(o => {
|
||||
var cache = props.ToDictionary(p => p.Name, p => new PropNode(p, p.GetValue(instance)));
|
||||
var table = TableUpdate<(string name, object current, object original), string>.mk_empty(t => t.name).with_added(cache.Values.Select(pn => (pn.prop.Name, pn.current, pn.original)));
|
||||
@@ -29,11 +45,20 @@ namespace bts.utils {
|
||||
}
|
||||
});
|
||||
|
||||
public static IPropertyChangedDetector<T> build_detector<T>(params string[] props) where T : IRaiseNotifyPropertyChanged =>
|
||||
new DiscretePropertyChangedDetector<T>(props);
|
||||
|
||||
public static IPropertyChangedDetector<T> build_detector_except<T>(params string[] except_props) where T : IRaiseNotifyPropertyChanged {
|
||||
// convert to a hashset for quicker lookups
|
||||
var exclude = new HashSet<string>(except_props);
|
||||
return new DiscretePropertyChangedDetector<T>(PropertyInfoStore<T>.all_props.Where(p => !exclude.Contains(p.Name)).Select(p => p.Name).ToArray());
|
||||
}
|
||||
|
||||
public static IObservable<bool> dirty_monitor<T>(T instance, params string[] prop_names) where T : INotifyPropertyChanged =>
|
||||
dirty_monitor(instance, Array.ConvertAll(prop_names, Impl<T>.type.GetProperty));
|
||||
dirty_monitor(instance, Array.ConvertAll(prop_names, PropertyInfoStore<T>.type.GetProperty));
|
||||
|
||||
public static IObservable<bool> dirty_monitor<T>(T instance, out IDisposable snapshot, params string[] prop_names) where T : INotifyPropertyChanged =>
|
||||
dirty_monitor(instance, out snapshot, Array.ConvertAll(prop_names, Impl<T>.type.GetProperty));
|
||||
dirty_monitor(instance, out snapshot, Array.ConvertAll(prop_names, PropertyInfoStore<T>.type.GetProperty));
|
||||
|
||||
public static IObservable<bool> dirty_monitor<T>(T instance, out IDisposable snapshot, params PropertyInfo[] props) where T : INotifyPropertyChanged {
|
||||
var prop_map = props.ToDictionary(p => p.Name, p => new PropNode(p, p.GetValue(instance)));
|
||||
@@ -68,7 +93,7 @@ namespace bts.utils {
|
||||
});
|
||||
|
||||
public static IDisposable create<T>(T instance) =>
|
||||
create(instance, Impl<T>.mutable_props);
|
||||
create(instance, PropertyInfoStore<T>.mutable_props);
|
||||
|
||||
public static IDisposable create<T>(T instance, PropertyInfo[] props) {
|
||||
var snapshot = Array.ConvertAll(props, p => p.GetValue(instance));
|
||||
@@ -78,13 +103,63 @@ namespace bts.utils {
|
||||
});
|
||||
}
|
||||
|
||||
static class Impl<T> {
|
||||
static Func<T, TValue> _prop_getter<T, TValue>(PropertyInfo prop) =>
|
||||
(Func<T, TValue>)prop.GetGetMethod().CreateDelegate(typeof(Func<T, TValue>));
|
||||
|
||||
static class PropertyInfoStore<T> {
|
||||
public static readonly Type type = typeof(T);
|
||||
|
||||
public static readonly PropertyInfo[] mutable_props =
|
||||
type.GetProperties(BindingFlags.Public | BindingFlags.Instance)
|
||||
.Where(p => p.GetSetMethod() != null)
|
||||
.ToArray();
|
||||
public static readonly PropertyInfo[]
|
||||
all_props = type.GetProperties(BindingFlags.Public | BindingFlags.Instance),
|
||||
mutable_props;
|
||||
|
||||
static PropertyInfoStore() =>
|
||||
mutable_props = all_props.Where(p => p.GetGetMethod() != null).ToArray();
|
||||
}
|
||||
|
||||
// DelegateStore maintains a cache of property getter/setter delegates,
|
||||
// which perform much better than the corresponding PropertyInfo.GetValue/SetValue.
|
||||
static class DelegateStore<T> {
|
||||
public static readonly Type type = typeof(T);
|
||||
|
||||
static readonly IReadOnlyDictionary<string, (Func<T, object> get, Action<T, object> set)> _property_delegates;
|
||||
|
||||
public static Func<T, object> getter(string prop_name) =>
|
||||
_property_delegates.TryGetValue(prop_name, out var dels) ? dels.get : null;
|
||||
|
||||
public static Action<T, object> setter(string prop_name) =>
|
||||
_property_delegates.TryGetValue(prop_name, out var dels) ? dels.set : null;
|
||||
|
||||
static DelegateStore() {
|
||||
var prop_dels = new Dictionary<string, (Func<T, object> get, Action<T, object> set)>();
|
||||
foreach (var p in PropertyInfoStore<T>.all_props) {
|
||||
Func<T, object> wrapped_getter = null;
|
||||
Action<T, object> wrapped_setter = null;
|
||||
var getter = p.GetGetMethod();
|
||||
var setter = p.GetSetMethod();
|
||||
if (getter == null && setter == null)
|
||||
continue;
|
||||
var del_types = (typeof(Func<,>).MakeGenericType(typeof(T), p.PropertyType), typeof(Action<,>).MakeGenericType(typeof(T), p.PropertyType));
|
||||
var twrapper = typeof(Wrapper<>).MakeGenericType(typeof(T), p.PropertyType);
|
||||
if (getter != null) {
|
||||
wrapped_getter = (Func<T, object>)
|
||||
twrapper.GetMethod(nameof(Wrapper<T>.getter), BindingFlags.Static | BindingFlags.Public)
|
||||
.Invoke(null, new[] { getter.CreateDelegate(del_types.Item1) });
|
||||
}
|
||||
if (setter != null) {
|
||||
wrapped_setter = (Action<T, object>)
|
||||
twrapper.GetMethod(nameof(Wrapper<T>.setter), BindingFlags.Static | BindingFlags.Public)
|
||||
.Invoke(null, new[] { setter.CreateDelegate(del_types.Item2) });
|
||||
}
|
||||
prop_dels[p.Name] = (wrapped_getter, wrapped_setter);
|
||||
}
|
||||
_property_delegates = prop_dels;
|
||||
}
|
||||
|
||||
static class Wrapper<TValue> {
|
||||
public static Func<T, object> getter(Func<T, TValue> getter) => src => getter(src);
|
||||
public static Action<T, object> setter(Action<T, TValue> setter) => (src, val) => setter(src, (TValue)val);
|
||||
}
|
||||
}
|
||||
|
||||
class PropNode {
|
||||
@@ -100,5 +175,30 @@ namespace bts.utils {
|
||||
public object original { get; set; }
|
||||
public object current { get; set; }
|
||||
}
|
||||
|
||||
class DiscretePropertyChangedDetector<T> : IPropertyChangedDetector<T> where T : IRaiseNotifyPropertyChanged {
|
||||
static readonly Converter<string, (string name, Func<T, object> get)> _realize_prop =
|
||||
(prop_name) => (prop_name, DelegateStore<T>.getter(prop_name));
|
||||
|
||||
readonly (string name, Func<T, object> get)[] _target_props;
|
||||
|
||||
public DiscretePropertyChangedDetector()
|
||||
: this(Array.ConvertAll(PropertyInfoStore<T>.all_props, p => p.Name)) { }
|
||||
|
||||
public DiscretePropertyChangedDetector(params string[] props) =>
|
||||
_target_props = Array.ConvertAll(props, _realize_prop);
|
||||
|
||||
public IDisposable detect_changes(T target) {
|
||||
var get_target_val = new Converter<(string name, Func<T, object> get), object>(tup => tup.get(target));
|
||||
var orig_state = Array.ConvertAll(_target_props, get_target_val);
|
||||
return Disposable.Create(() => {
|
||||
var curr_state = Array.ConvertAll(_target_props, get_target_val);
|
||||
for (int i = 0; i < curr_state.Length; i++) {
|
||||
if (!Equals(curr_state[i], orig_state[i]))
|
||||
target.raise_notify_property_changed(_target_props[i].name);
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user