Skip to content

Commit

Permalink
perf: Reduce generated code size for BindableMetadata and DependencyP…
Browse files Browse the repository at this point in the history
…roperty
  • Loading branch information
jeromelaban committed Sep 29, 2020
1 parent aad95fe commit 9296796
Show file tree
Hide file tree
Showing 6 changed files with 110 additions and 74 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -227,7 +227,7 @@ private void GenerateProviderTable(IEnumerable<INamedTypeSymbol> q, IndentedStri
{
using (writer.BlockInvariant(@"lock(_knownMissingTypes)"))
{
using (writer.BlockInvariant(@"if(!_knownMissingTypes.Contains(type) || !type.IsGenericType)"))
using (writer.BlockInvariant(@"if(!_knownMissingTypes.Contains(type) && !type.IsGenericType)"))
{
writer.AppendLineInvariant(@"_knownMissingTypes.Add(type);");
writer.AppendLineInvariant(@"Debug.WriteLine($""The Bindable attribute is missing and the type [{{type.FullName}}] is not known by the MetadataProvider. Reflection was used instead of the binding engine and generated static metadata. Add the Bindable attribute to prevent this message and performance issues."");");
Expand Down Expand Up @@ -474,15 +474,9 @@ where field.IsStatic

if (getMethod != null)
{
var getter = $"{XamlConstants.Types.DependencyObjectExtensions}.GetValue(instance, {ownerTypeName}.{propertyName}Property, precedence)";
var setter = $"{XamlConstants.Types.DependencyObjectExtensions}.SetValue(instance, {ownerTypeName}.{propertyName}Property, value, precedence)";

var propertyType = GetGlobalQualifier(getMethod.ReturnType) + SanitizeTypeName(getMethod.ReturnType.ToString());

writer.AppendLineInvariant($@"bindableType.AddProperty(""{propertyName}"", typeof({propertyType}), Get{propertyName}, Set{propertyName});");

postWriter.AppendLineInvariant($@"private static object Get{propertyName}(object instance, Windows.UI.Xaml.DependencyPropertyValuePrecedences? precedence) => {getter};");
postWriter.AppendLineInvariant($@"private static void Set{propertyName}(object instance, object value, Windows.UI.Xaml.DependencyPropertyValuePrecedences? precedence) => {setter};");
writer.AppendLineInvariant($@"bindableType.AddProperty({ownerType.ToDisplayString(SymbolDisplayFormat.FullyQualifiedFormat)}.{dependencyProperty});");
}
}

Expand Down
19 changes: 15 additions & 4 deletions src/Uno.UI/DataBinding/BindableProperty.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
using System;
#nullable enable

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Windows.UI.Xaml;

namespace Uno.UI.DataBinding
{
Expand All @@ -11,17 +14,25 @@ namespace Uno.UI.DataBinding
/// </summary>
public class BindableProperty : IBindableProperty
{
public BindableProperty(Type propertyType, PropertyGetterHandler getter, PropertySetterHandler setter)
public BindableProperty(DependencyProperty property)
{
DependencyProperty = property;
PropertyType = property.Type;
}

public BindableProperty(Type propertyType, PropertyGetterHandler getter, PropertySetterHandler? setter)
{
Getter = getter;
Setter = setter;
PropertyType = propertyType;
}

public PropertyGetterHandler Getter { get; }
public PropertyGetterHandler? Getter { get; }

public PropertySetterHandler Setter { get; }
public PropertySetterHandler? Setter { get; }

public Type PropertyType { get; }

public DependencyProperty? DependencyProperty { get; }
}
}
29 changes: 18 additions & 11 deletions src/Uno.UI/DataBinding/BindableType.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
using System;
#nullable enable

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
Expand All @@ -15,9 +17,9 @@ namespace Uno.UI.DataBinding
public class BindableType : IBindableType
{
private readonly Hashtable _properties;
private StringIndexerGetterDelegate _stringIndexerGetter;
private StringIndexerSetterDelegate _stringIndexerSetter;
private ActivatorDelegate _activator;
private StringIndexerGetterDelegate? _stringIndexerGetter;
private StringIndexerSetterDelegate? _stringIndexerSetter;
private ActivatorDelegate? _activator;

/// <summary>
/// Builds a new BindableType.
Expand All @@ -32,12 +34,12 @@ public BindableType(int estimatedPropertySize, Type sourceType)

public Type Type { get; }

public ActivatorDelegate CreateInstance()
public ActivatorDelegate? CreateInstance()
{
return _activator;
}

public IBindableProperty GetProperty(string name)
public IBindableProperty? GetProperty(string name)
{
var property = _properties[name] as IBindableProperty;

Expand All @@ -47,7 +49,7 @@ public IBindableProperty GetProperty(string name)

if (prop != null && prop.OwnerType.IsAssignableFrom(Type))
{
property = GetProperty(prop?.Name);
property = GetProperty(prop.Name);
}
}

Expand All @@ -59,12 +61,17 @@ public void AddActivator(ActivatorDelegate activator)
_activator = activator;
}

public void AddProperty<T>(string name, PropertyGetterHandler getter, PropertySetterHandler setter = null)
public void AddProperty<T>(string name, PropertyGetterHandler getter, PropertySetterHandler? setter = null)
{
_properties[name] = new BindableProperty(typeof(T), getter, setter);
}

public void AddProperty(string name, Type propertyType, PropertyGetterHandler getter, PropertySetterHandler setter = null)
public void AddProperty(DependencyProperty property)
{
_properties[property.Name] = new BindableProperty(property);
}

public void AddProperty(string name, Type propertyType, PropertyGetterHandler getter, PropertySetterHandler? setter = null)
{
_properties[name] = new BindableProperty(propertyType, getter, setter);
}
Expand All @@ -75,12 +82,12 @@ public void AddIndexer(StringIndexerGetterDelegate getter, StringIndexerSetterDe
_stringIndexerSetter = setter;
}

public StringIndexerGetterDelegate GetIndexerGetter()
public StringIndexerGetterDelegate? GetIndexerGetter()
{
return _stringIndexerGetter;
}

public StringIndexerSetterDelegate GetIndexerSetter()
public StringIndexerSetterDelegate? GetIndexerSetter()
{
return _stringIndexerSetter;
}
Expand Down
Loading

0 comments on commit 9296796

Please sign in to comment.