Browse Source

Compact generated XAML identifiers in Release builds (#22674)

* Compact generated XAML identifiers in Release builds

* Use direct XamlX generated identifier interface

* Select XamlX identifier generators in XAML build task

* Update XamlX to the main branch

---------

Co-authored-by: Julien Lebosquain <julien@lebosquain.net>
pull/22676/head
Nikita Tsukanov 6 days ago
committed by GitHub
parent
commit
dc2253658c
No known key found for this signature in database GPG Key ID: B5690EEEBB952194
  1. 2
      external/XamlX
  2. 3
      packages/Avalonia/AvaloniaBuildTasks.targets
  3. 4
      src/Avalonia.Build.Tasks/CompileAvaloniaXamlTask.cs
  4. 12
      src/Avalonia.Build.Tasks/DeterministicIdGenerator.cs
  5. 11
      src/Avalonia.Build.Tasks/XamlCompilerTaskExecutor.cs
  6. 2
      src/Markup/Avalonia.Markup.Xaml.Loader/CompilerExtensions/XamlIlBindingPathHelper.cs
  7. 32
      src/Markup/Avalonia.Markup.Xaml.Loader/CompilerExtensions/XamlIlClrPropertyInfoHelper.cs

2
external/XamlX

@ -1 +1 @@
Subproject commit 7ef6aef496ab6e8dcf3df04bef697be49db37c04
Subproject commit f28a1415b91e1161e78f3caeee4705ba38e4f730

3
packages/Avalonia/AvaloniaBuildTasks.targets

@ -157,6 +157,8 @@
<AvaloniaXamlVerboseExceptions Condition="'$(AvaloniaXamlVerboseExceptions)' == ''">false</AvaloniaXamlVerboseExceptions>
<AvaloniaXamlCreateSourceInfo Condition="'$(AvaloniaXamlCreateSourceInfo)' == '' AND '$(Configuration)' == 'Debug'">true</AvaloniaXamlCreateSourceInfo>
<AvaloniaXamlCreateSourceInfo Condition="'$(AvaloniaXamlCreateSourceInfo)' == '' AND '$(Configuration)' != 'Debug'">false</AvaloniaXamlCreateSourceInfo>
<AvaloniaXamlIlCompactHelperNames Condition="'$(AvaloniaXamlIlCompactHelperNames)' == '' AND '$(Configuration)' == 'Release'">true</AvaloniaXamlIlCompactHelperNames>
<AvaloniaXamlIlCompactHelperNames Condition="'$(AvaloniaXamlIlCompactHelperNames)' == ''">false</AvaloniaXamlIlCompactHelperNames>
</PropertyGroup>
<ItemGroup>
@ -187,6 +189,7 @@
SkipXamlCompilation="$(_AvaloniaSkipXamlCompilation)"
DebuggerLaunch="$(AvaloniaXamlIlDebuggerLaunch)"
CreateSourceInfo="$(AvaloniaXamlCreateSourceInfo)"
CompactHelperNames="$(AvaloniaXamlIlCompactHelperNames)"
DefaultCompileBindings="$(AvaloniaUseCompiledBindingsByDefault)"
VerboseExceptions="$(AvaloniaXamlVerboseExceptions)"
AnalyzerConfigFiles="@(EditorConfigFiles)"/>

4
src/Avalonia.Build.Tasks/CompileAvaloniaXamlTask.cs

@ -32,7 +32,7 @@ namespace Avalonia.Build.Tasks
ProjectDirectory, VerifyIl, DefaultCompileBindings, outputImportance,
new XamlCompilerDiagnosticsFilter(AnalyzerConfigFiles),
(SignAssembly && !DelaySign && !PublicSign) ? AssemblyOriginatorKeyFile : null,
SkipXamlCompilation, DebuggerLaunch, VerboseExceptions, CreateSourceInfo);
SkipXamlCompilation, DebuggerLaunch, VerboseExceptions, CreateSourceInfo, CompactHelperNames);
if (res.Success && !res.WrittenFile)
{
@ -102,6 +102,8 @@ namespace Avalonia.Build.Tasks
public bool CreateSourceInfo { get; set; }
public bool CompactHelperNames { get; set; }
public bool VerboseExceptions { get; set; }
public ITaskItem[] AnalyzerConfigFiles { get; set; }

12
src/Avalonia.Build.Tasks/DeterministicIdGenerator.cs

@ -1,12 +0,0 @@
using System;
using XamlX.Transform;
namespace Avalonia.Build.Tasks
{
public class DeterministicIdGenerator : IXamlIdentifierGenerator
{
private int _nextId = 1;
public string GenerateIdentifierPart() => (_nextId++).ToString();
}
}

11
src/Avalonia.Build.Tasks/XamlCompilerTaskExecutor.cs

@ -52,7 +52,8 @@ namespace Avalonia.Build.Tasks
string[] references, string projectDirectory,
bool verifyIl, bool defaultCompileBindings, MessageImportance logImportance,
XamlCompilerDiagnosticsFilter diagnosticsFilter, string strongNameKey,
bool skipXamlCompilation, bool debuggerLaunch, bool verboseExceptions, bool createSourceInfo)
bool skipXamlCompilation, bool debuggerLaunch, bool verboseExceptions, bool createSourceInfo,
bool compactHelperNames)
{
try
{
@ -67,7 +68,7 @@ namespace Avalonia.Build.Tasks
var compileRes = CompileCore(
engine, typeSystem, projectDirectory, verifyIl,
defaultCompileBindings, logImportance, diagnosticsFilter,
debuggerLaunch, verboseExceptions, createSourceInfo);
debuggerLaunch, verboseExceptions, createSourceInfo, compactHelperNames);
if (compileRes == null)
return new CompileResult(true);
if (compileRes == false)
@ -108,7 +109,7 @@ namespace Avalonia.Build.Tasks
XamlCompilerDiagnosticsFilter diagnosticsFilter,
bool debuggerLaunch,
bool verboseExceptions,
bool createSourceInfo)
bool createSourceInfo, bool compactHelperNames)
{
if (debuggerLaunch)
{
@ -204,7 +205,7 @@ namespace Avalonia.Build.Tasks
new XamlIlClrPropertyInfoEmitter(typeSystem.CreateTypeBuilder(clrPropertiesDef)),
new XamlIlPropertyInfoAccessorFactoryEmitter(typeSystem.CreateTypeBuilder(indexerAccessorClosure)),
new XamlIlTrampolineBuilder(typeSystem.CreateTypeBuilder(trampolineBuilder)),
new DeterministicIdGenerator(),
compactHelperNames ? new CompactIdentifierGenerator() : new SequentialIdentifierGenerator(),
diagnosticsHandler);
@ -675,7 +676,7 @@ namespace Avalonia.Build.Tasks
return false;
avares.Save();
}
loaderDispatcherMethod.Body.Instructions.Add(Instruction.Create(OpCodes.Ldnull));
loaderDispatcherMethod.Body.Instructions.Add(Instruction.Create(OpCodes.Ret));
return true;

2
src/Markup/Avalonia.Markup.Xaml.Loader/CompilerExtensions/XamlIlBindingPathHelper.cs

@ -865,7 +865,7 @@ namespace Avalonia.Markup.Xaml.XamlIl.CompilerExtensions
else
{
// In this case, we need to emit our own delegate type.
string delegateTypeName = context.Configuration.IdentifierGenerator.GenerateIdentifierPart();
string delegateTypeName = context.Configuration.IdentifierGenerator.CreateIdentifierFromPrefix("");
specificDelegateType = newDelegateTypeBuilder = context.DeclaringType.DefineDelegateSubType(
delegateTypeName,
XamlVisibility.Private,

32
src/Markup/Avalonia.Markup.Xaml.Loader/CompilerExtensions/XamlIlClrPropertyInfoHelper.cs

@ -6,6 +6,7 @@ using XamlX.Ast;
using XamlX.TypeSystem;
using XamlX.IL;
using XamlX.Emit;
using XamlX.Transform;
namespace Avalonia.Markup.Xaml.XamlIl.CompilerExtensions
{
@ -36,8 +37,12 @@ namespace Avalonia.Markup.Xaml.XamlIl.CompilerExtensions
if (_boxedTrue is null || _boxedFalse is null)
{
var wellKnownTypes = context.Configuration.WellKnownTypes;
_boxedTrue = _builder.DefineField(wellKnownTypes.Object, "BooleanBox!True", XamlVisibility.Private, true);
_boxedFalse = _builder.DefineField(wellKnownTypes.Object, "BooleanBox!False", XamlVisibility.Private, true);
_boxedTrue = _builder.DefineField(wellKnownTypes.Object,
context.Configuration.IdentifierGenerator.CreateIdentifierFromPreferredName("BooleanBox!True", 0),
XamlVisibility.Private, true);
_boxedFalse = _builder.DefineField(wellKnownTypes.Object,
context.Configuration.IdentifierGenerator.CreateIdentifierFromPreferredName("BooleanBox!False", 0),
XamlVisibility.Private, true);
var cctor = _builder.DefineConstructor(true);
cctor.Generator
@ -148,9 +153,9 @@ namespace Avalonia.Markup.Xaml.XamlIl.CompilerExtensions
if (GetCachedPropertyInfoMethod(_fields, key, property, out var lst) is { } cached)
return cached;
var name = lst.Count == 0 ? key : key + "_" + context.Configuration.IdentifierGenerator.GenerateIdentifierPart();
var name = context.Configuration.IdentifierGenerator.CreateIdentifierFromPreferredName(key, lst.Count);
var field = _builder.DefineField(types.IPropertyInfo, name + "!Field", XamlVisibility.Private, true);
var field = _builder.DefineField(types.IPropertyInfo, name.AppendSuffix("!Field"), XamlVisibility.Private, true);
void Load(IXamlMethod m, IXamlILEmitter cg, bool passThis)
{
@ -173,7 +178,7 @@ namespace Avalonia.Markup.Xaml.XamlIl.CompilerExtensions
var getter = property.Getter == null ?
null :
_builder.DefineMethod(types.XamlIlTypes.Object,
new[] {types.XamlIlTypes.Object}, name + "!Getter", XamlVisibility.Private, true, false);
new[] {types.XamlIlTypes.Object}, name.AppendSuffix("!Getter"), XamlVisibility.Private, true, false);
if (getter != null)
{
Load(property.Getter!, getter.Generator, !property.Getter!.IsStatic);
@ -199,7 +204,7 @@ namespace Avalonia.Markup.Xaml.XamlIl.CompilerExtensions
null :
_builder.DefineMethod(types.XamlIlTypes.Void,
new[] {types.XamlIlTypes.Object, types.XamlIlTypes.Object},
name + "!Setter", XamlVisibility.Private, true, false);
name.AppendSuffix("!Setter"), XamlVisibility.Private, true, false);
if (setter != null)
{
Load(property.Setter!, setter.Generator, !property.Setter!.IsStatic);
@ -217,7 +222,7 @@ namespace Avalonia.Markup.Xaml.XamlIl.CompilerExtensions
}
var get = _builder.DefineMethod(types.IPropertyInfo, Array.Empty<IXamlType>(),
name + "!Property", XamlVisibility.Public, true, false);
name.AppendSuffix("!Property"), XamlVisibility.Public, true, false);
var ctor = types.ClrPropertyInfo.Constructors.First(c =>
@ -275,15 +280,14 @@ namespace Avalonia.Markup.Xaml.XamlIl.CompilerExtensions
var funcType = context.Configuration.WellKnownTypes.GetFuncOfT(2).MakeGenericType(sourceType, valueType);
var actionType = context.Configuration.WellKnownTypes.GetActionOfT(2).MakeGenericType(sourceType, valueType);
var name = lst.Count == 0
? key + "!Typed"
: key + "!Typed_" + context.Configuration.IdentifierGenerator.GenerateIdentifierPart();
var name = context.Configuration.IdentifierGenerator.CreateIdentifierFromPreferredName(
key + "!Typed", lst.Count);
var field = _builder.DefineField(typedPropertyInfoType, name + "!Field", XamlVisibility.Private, true);
var field = _builder.DefineField(typedPropertyInfoType, name.AppendSuffix("!Field"), XamlVisibility.Private, true);
var getter = property.Getter == null
? null
: _builder.DefineMethod(valueType, new[] { sourceType }, name + "!Getter", XamlVisibility.Private, true, false);
: _builder.DefineMethod(valueType, new[] { sourceType }, name.AppendSuffix("!Getter"), XamlVisibility.Private, true, false);
if (getter != null)
{
if (!property.Getter!.IsStatic)
@ -293,7 +297,7 @@ namespace Avalonia.Markup.Xaml.XamlIl.CompilerExtensions
var setter = property.Setter == null
? null
: _builder.DefineMethod(types.XamlIlTypes.Void, new[] { sourceType, valueType }, name + "!Setter", XamlVisibility.Private, true, false);
: _builder.DefineMethod(types.XamlIlTypes.Void, new[] { sourceType, valueType }, name.AppendSuffix("!Setter"), XamlVisibility.Private, true, false);
if (setter != null)
{
if (!property.Setter!.IsStatic)
@ -303,7 +307,7 @@ namespace Avalonia.Markup.Xaml.XamlIl.CompilerExtensions
}
var get = _builder.DefineMethod(typedPropertyInfoType, Array.Empty<IXamlType>(),
name + "!Property", XamlVisibility.Public, true, false);
name.AppendSuffix("!Property"), XamlVisibility.Public, true, false);
var ctor = typedClrPropertyInfoType.Constructors.First(c =>
c.Parameters.Count == 3 && !c.IsStatic);

Loading…
Cancel
Save