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

Make MarkupExtension.ProvideValue method inlinable for StaticResourceExtension and ResolveByNameExtension #17659

Merged
merged 1 commit into from
Dec 3, 2024
Merged
Show file tree
Hide file tree
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
@@ -1,4 +1,5 @@
using System;
using System.Runtime.CompilerServices;
using Avalonia.Controls;
using Avalonia.Data.Core;

Expand All @@ -13,14 +14,17 @@ public ResolveByNameExtension(string name)

public string Name { get; }

public object? ProvideValue(IServiceProvider serviceProvider)
public object? ProvideValue(IServiceProvider serviceProvider) => ProvideValue(serviceProvider, Name);

[MethodImpl(MethodImplOptions.NoInlining)]
private static object? ProvideValue(IServiceProvider serviceProvider, string name)
{
var nameScope = serviceProvider.GetService<INameScope>();

if (nameScope is null)
return null;
var value = nameScope.FindAsync(Name);

var value = nameScope.FindAsync(name);

if(value.IsCompleted)
return value.GetResult();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System;
using System.Collections.Generic;
using System.Reflection;
using System.Runtime.CompilerServices;
using Avalonia.Controls;
using Avalonia.Markup.Data;
using Avalonia.Markup.Xaml.Converters;
Expand All @@ -22,9 +23,14 @@ public StaticResourceExtension(object resourceKey)

public object? ResourceKey { get; set; }

public object? ProvideValue(IServiceProvider serviceProvider)
// Keep instance method ProvideValue as simple as possible, increasing chance to inline it.
// With modern runtimes, inlining this method also helps to eliminate extension allocation completely.
public object? ProvideValue(IServiceProvider serviceProvider) => ProvideValue(serviceProvider, ResourceKey);

[MethodImpl(MethodImplOptions.NoInlining)]
private static object? ProvideValue(IServiceProvider serviceProvider, object? resourceKey)
{
if (ResourceKey is not { } resourceKey)
if (resourceKey is null)
{
throw new ArgumentException("StaticResourceExtension.ResourceKey must be set.");
}
Expand Down Expand Up @@ -87,20 +93,16 @@ public StaticResourceExtension(object resourceKey)
{
// This is stored locally to avoid allocating closure in the outer scope.
var localTargetType = targetType;
var localInstance = this;
var localKeyInstance = resourceKey;

DelayedBinding.Add(target, property, x => localInstance.GetValue(x, localTargetType));
DelayedBinding.Add(target, property, x =>
ColorToBrushConverter.Convert(x.FindResource(localKeyInstance), localTargetType));
return AvaloniaProperty.UnsetValue;
}

throw new KeyNotFoundException($"Static resource '{resourceKey}' not found.");
}

private object? GetValue(StyledElement control, Type? targetType)
{
return ColorToBrushConverter.Convert(control.FindResource(ResourceKey!), targetType);
}

internal static ThemeVariant? GetDictionaryVariant(IAvaloniaXamlIlParentStackProvider? stack)
{
switch (stack)
Expand Down
Loading