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

PropertyHelper: Reduce closure allocation #34

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 32 additions & 20 deletions src/Nito.Mvvm.CalculatedProperties/PropertyHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,14 +35,7 @@ public PropertyHelper(Action<PropertyChangedEventArgs> onPropertyChanged)
/// <param name="propertyName">The name of the property to retrieve or create.</param>
public TriggerProperty<T> GetOrAddTriggerProperty<T>(Func<T>? calculateInitialValue = null, IEqualityComparer<T>? comparer = null, [CallerMemberName] string propertyName = null!)
{
IProperty result;
if (!_properties.TryGetValue(propertyName, out result))
{
result = new TriggerProperty<T>(_onPropertyChanged, calculateInitialValue == null ? default! : calculateInitialValue(), comparer);
_properties.Add(propertyName, result);
}

return (TriggerProperty<T>) result;
return GetTriggerProperty<T>(propertyName) ?? AddTriggerProperty(calculateInitialValue, comparer, propertyName);
}

/// <summary>
Expand All @@ -53,22 +46,15 @@ public TriggerProperty<T> GetOrAddTriggerProperty<T>(Func<T>? calculateInitialVa
/// <param name="propertyName">The name of the property to retrieve or create.</param>
public CalculatedProperty<T> GetOrAddCalculatedProperty<T>(Func<T> calculateValue, [CallerMemberName] string propertyName = null!)
{
IProperty result;
if (!_properties.TryGetValue(propertyName, out result))
{
result = new CalculatedProperty<T>(_onPropertyChanged, calculateValue);
_properties.Add(propertyName, result);
}

return (CalculatedProperty<T>) result;
return GetCalculatedProperty<T>(propertyName) ?? AddCalculatedProperty(calculateValue, propertyName);
}

/// <summary>
/// Retrieves the specified trigger property if it exists; otherwise, returns <c>null</c>.
/// </summary>
/// <typeparam name="T">The type of the property value.</typeparam>
/// <param name="propertyName">The name of the property to retrieve.</param>
public TriggerProperty<T> GetTriggerProperty<T>(string propertyName)
public TriggerProperty<T>? GetTriggerProperty<T>(string propertyName)
{
IProperty result;
_properties.TryGetValue(propertyName, out result);
Expand All @@ -80,7 +66,7 @@ public TriggerProperty<T> GetTriggerProperty<T>(string propertyName)
/// </summary>
/// <typeparam name="T">The type of the property value.</typeparam>
/// <param name="propertyName">The name of the property to retrieve.</param>
public CalculatedProperty<T> GetCalculatedProperty<T>(string propertyName)
public CalculatedProperty<T>? GetCalculatedProperty<T>(string propertyName)
{
IProperty result;
_properties.TryGetValue(propertyName, out result);
Expand Down Expand Up @@ -108,7 +94,13 @@ public T Get<T>(Func<T> calculateInitialValue, IEqualityComparer<T>? comparer =
/// <param name="propertyName">The name of the property.</param>
public T Get<T>(T initialValue, IEqualityComparer<T>? comparer = null, [CallerMemberName] string propertyName = null!)
{
return Get(() => initialValue, comparer, propertyName);
var triggerProperty = GetTriggerProperty<T>(propertyName);
if (triggerProperty == null)
{
var initialValueCopy = initialValue;
triggerProperty = AddTriggerProperty(() => initialValueCopy, comparer, propertyName);
}
return triggerProperty.GetValue(propertyName);
}

/// <summary>
Expand All @@ -120,7 +112,13 @@ public T Get<T>(T initialValue, IEqualityComparer<T>? comparer = null, [CallerMe
/// <param name="propertyName">The name of the property.</param>
public void Set<T>(T value, IEqualityComparer<T>? comparer = null, [CallerMemberName] string propertyName = null!)
{
GetOrAddTriggerProperty(() => value, comparer, propertyName).SetValue(value, propertyName);
var triggerProperty = GetTriggerProperty<T>(propertyName);
if (triggerProperty == null)
{
var valueCopy = value;
triggerProperty = AddTriggerProperty(() => valueCopy, comparer, propertyName);
}
triggerProperty.SetValue(value, propertyName);
}

/// <summary>
Expand All @@ -134,6 +132,20 @@ public T Calculated<T>(Func<T> calculateValue, [CallerMemberName] string propert
return GetOrAddCalculatedProperty(calculateValue, propertyName).GetValue(propertyName);
}

private TriggerProperty<T> AddTriggerProperty<T>(Func<T>? calculateInitialValue, IEqualityComparer<T>? comparer, string propertyName)
{
TriggerProperty<T> result = new TriggerProperty<T>(_onPropertyChanged, calculateInitialValue == null ? default! : calculateInitialValue(), comparer);
_properties.Add(propertyName, result);
return result;
}

private CalculatedProperty<T> AddCalculatedProperty<T>(Func<T> calculateValue, string propertyName)
{
CalculatedProperty<T> result = new CalculatedProperty<T>(_onPropertyChanged, calculateValue);
_properties.Add(propertyName, result);
return result;
}

[DebuggerNonUserCode]
private string DebuggerDisplay
{
Expand Down