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

perf: Improve performance when reading dynamic properties. #98

Merged
merged 1 commit into from
Jan 10, 2024
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@
using System.Threading;
using System.Threading.Tasks;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Logging.Abstractions;

namespace Chinook.DynamicMvvm
{
Expand Down Expand Up @@ -65,7 +63,40 @@ public static T Get<T>(
this IViewModel viewModel,
T initialValue = default,
[CallerMemberName] string name = null
) => viewModel.Get<T>(viewModel.GetOrCreateDynamicProperty(name, n => viewModel.GetDynamicPropertyFactory().Create(n, initialValue, viewModel)));
)
{
// We don't use GetOrCreateDynamicProperty internally to avoid the performance costs of the lambda and closure.
if (viewModel.IsDisposed)
jeanplevesque marked this conversation as resolved.
Show resolved Hide resolved
{
return default(T);
}

if (viewModel.TryGetDisposable<IDynamicProperty>(name, out var property))
{
return viewModel.Get<T>(property);
}
else
{
property = AddDynamicPropertyFromValue(viewModel, initialValue, name);

return (T)property.Value;
}
}

private static IDynamicProperty AddDynamicPropertyFromValue<T>(IViewModel viewModel, T initialValue, string name)
{
var property = viewModel.GetDynamicPropertyFactory().Create(name, initialValue, viewModel);
property.ValueChanged += OnDynamicPropertyChanged;

viewModel.AddDisposable(name, property);

return property;

void OnDynamicPropertyChanged(IDynamicProperty dynamicProperty)
{
viewModel.RaisePropertyChanged(dynamicProperty.Name);
}
}

/// <summary>
/// Gets or creates a <see cref="IDynamicProperty"/> attached to this <see cref="IViewModel"/>.
Expand All @@ -79,7 +110,40 @@ public static T Get<T>(
this IViewModel viewModel,
Func<T> initialValue,
[CallerMemberName] string name = null
) => viewModel.Get<T>(viewModel.GetOrCreateDynamicProperty(name, n => viewModel.GetDynamicPropertyFactory().Create(n, initialValue(), viewModel)));
)
{
// We don't use GetOrCreateDynamicProperty internally to avoid the performance costs of the lambda and closure.
if (viewModel.IsDisposed)
{
return default(T);
}

if (viewModel.TryGetDisposable<IDynamicProperty>(name, out var property))
{
return viewModel.Get<T>(property);
}
else
{
property = AddDynamicPropertyFromValue(viewModel, initialValue, name);

return (T)property.Value;
}
}

private static IDynamicProperty AddDynamicPropertyFromValue<T>(IViewModel viewModel, Func<T> initialValue, string name)
{
var property = viewModel.GetDynamicPropertyFactory().Create(name, initialValue(), viewModel);
property.ValueChanged += OnDynamicPropertyChanged;

viewModel.AddDisposable(name, property);

return property;

void OnDynamicPropertyChanged(IDynamicProperty dynamicProperty)
{
viewModel.RaisePropertyChanged(dynamicProperty.Name);
}
}

/// <summary>
/// Gets or creates a <see cref="IDynamicProperty"/> attached to this <see cref="IViewModel"/>.
Expand All @@ -95,7 +159,40 @@ public static T GetFromTask<T>(
Func<CancellationToken, Task<T>> source,
T initialValue = default,
[CallerMemberName] string name = null
) => viewModel.Get<T>(viewModel.GetOrCreateDynamicProperty(name, n => viewModel.GetDynamicPropertyFactory().CreateFromTask(n, source, initialValue, viewModel)));
)
{
// We don't use GetOrCreateDynamicProperty internally to avoid the performance costs of the lambda and closure.
if (viewModel.IsDisposed)
{
return default(T);
}

if (viewModel.TryGetDisposable<IDynamicProperty>(name, out var property))
{
return viewModel.Get<T>(property);
}
else
{
property = AddDynamicPropertyTask(viewModel, source, initialValue, name);

return (T)property.Value;
}
}

private static IDynamicProperty AddDynamicPropertyTask<T>(IViewModel viewModel, Func<CancellationToken, Task<T>> source, T initialValue, string name)
{
var property = viewModel.GetDynamicPropertyFactory().CreateFromTask(name, source, initialValue, viewModel);
property.ValueChanged += OnDynamicPropertyChanged;

viewModel.AddDisposable(name, property);

return property;

void OnDynamicPropertyChanged(IDynamicProperty dynamicProperty)
{
viewModel.RaisePropertyChanged(dynamicProperty.Name);
}
}

/// <summary>
/// Gets or creates a <see cref="IDynamicProperty"/> attached to this <see cref="IViewModel"/>.
Expand All @@ -111,7 +208,40 @@ public static T GetFromObservable<T>(
IObservable<T> source,
T initialValue = default,
[CallerMemberName] string name = null
) => viewModel.Get<T>(viewModel.GetOrCreateDynamicProperty(name, n => viewModel.GetDynamicPropertyFactory().CreateFromObservable(n, source, initialValue, viewModel)));
)
{
// We don't use GetOrCreateDynamicProperty internally to avoid the performance costs of the lambda and closure.
if (viewModel.IsDisposed)
{
return default(T);
}

if (viewModel.TryGetDisposable<IDynamicProperty>(name, out var property))
{
return viewModel.Get<T>(property);
}
else
{
property = AddDynamicPropertyFromObservable(viewModel, source, initialValue, name);

return (T)property.Value;
}
}

private static IDynamicProperty AddDynamicPropertyFromObservable<T>(IViewModel viewModel, IObservable<T> source, T initialValue, string name)
{
var property = viewModel.GetDynamicPropertyFactory().CreateFromObservable(name, source, initialValue, viewModel);
property.ValueChanged += OnDynamicPropertyChanged;

viewModel.AddDisposable(name, property);

return property;

void OnDynamicPropertyChanged(IDynamicProperty dynamicProperty)
{
viewModel.RaisePropertyChanged(dynamicProperty.Name);
}
}

/// <summary>
/// Sets the value of a property.
Expand Down