Skip to content

Commit

Permalink
perf: use a ref inside the new array and reduce code size
Browse files Browse the repository at this point in the history
... from my previous attempt, still a bit larger than the original.

             | Original | Previous Commit   | Current Commit    |
-------------|----------|-------------------|-------------------|
Status       | crash    | works             | works             |
Fields       | 2046     | 3 (-2043)         | 4 (+1)            |
Locals       | 2048     | 2048              | 2049 (+1)         |
IL Code Size | 179,715  | 215,349 (+35,634) | 183,429 (-31,920) |

Also generate code that is easier to read.
  • Loading branch information
spouliot committed Dec 29, 2022
1 parent c1c1a20 commit af90eb7
Showing 1 changed file with 17 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -594,38 +594,41 @@ private bool IsOverride(IMethodSymbol? methodDefinition)
private void GenerateTypeTable(IndentedStringBuilder writer)
{
var types = _typeMap.Where(k => !k.Key.IsGenericType);
writer.AppendLineIndented($"private global::Uno.UI.DataBinding.IBindableType[] _bindableTypes = new global::Uno.UI.DataBinding.IBindableType[{types.Count()}];");
writer.AppendLineIndented($"private readonly global::Uno.UI.DataBinding.IBindableType[] _bindableTypes = new global::Uno.UI.DataBinding.IBindableType[{types.Count()}];");
writer.AppendLineIndented($"private static global::Uno.UI.DataBinding.IBindableType _null;");

using (writer.BlockInvariant("public global::Uno.UI.DataBinding.IBindableType GetBindableTypeByFullName(string fullName)"))
{
writer.AppendLineIndented("ref global::Uno.UI.DataBinding.IBindableType element = ref _null;");
using (writer.BlockInvariant(@"switch(fullName)"))
{
foreach (var type in types)
{
_cancellationToken.ThrowIfCancellationRequested();

var typeIndexString = $"{type.Value.Index:000}";
var typeIndex = type.Value.Index;

writer.AppendLineIndented($"case \"{type.Key}\":");
using (writer.BlockInvariant($"if(_bindableTypes[{typeIndexString}] == null)"))
using (writer.BlockInvariant($"case \"{type.Key}\":"))
{
if (_xamlResourcesTrimming && type.Key.GetAllInterfaces().Any(i => SymbolEqualityComparer.Default.Equals(i, _dependencyObjectSymbol)))
writer.AppendLineIndented($"element = ref _bindableTypes[{typeIndex}];");
using (writer.BlockInvariant("if(element == null)"))
{
var linkerHintsClassName = LinkerHintsHelpers.GetLinkerHintsClassName(_defaultNamespace);
var safeTypeName = LinkerHintsHelpers.GetPropertyAvailableName(type.Key.GetFullMetadataName());
if (_xamlResourcesTrimming && type.Key.GetAllInterfaces().Any(i => SymbolEqualityComparer.Default.Equals(i, _dependencyObjectSymbol)))
{
var linkerHintsClassName = LinkerHintsHelpers.GetLinkerHintsClassName(_defaultNamespace);
var safeTypeName = LinkerHintsHelpers.GetPropertyAvailableName(type.Key.GetFullMetadataName());

writer.AppendLineIndented($"if(global::{linkerHintsClassName}.{safeTypeName})");
}

writer.AppendLineIndented($"if(global::{linkerHintsClassName}.{safeTypeName})");
writer.AppendLineIndented($"element = MetadataBuilder_{typeIndex:000}.Build();");
}

writer.AppendLineIndented($"_bindableTypes[{typeIndexString}] = MetadataBuilder_{typeIndexString}.Build();");
writer.AppendLineIndented("break;");
}

writer.AppendLineIndented($"return _bindableTypes[{typeIndexString}];");
}

writer.AppendLineIndented("default:");
writer.AppendLineIndented(@"return null;");
}
writer.AppendLineIndented("return element;");
}

using (writer.BlockInvariant("public global::Uno.UI.DataBinding.IBindableType GetBindableTypeByType(Type type)"))
Expand Down

0 comments on commit af90eb7

Please sign in to comment.