Skip to content

Commit

Permalink
Merge pull request #428 from Washi1337/feature/owned-lazyvars
Browse files Browse the repository at this point in the history
Migrate to LazyVariable<TOwner, TValue> to avoid delegate closure allocation
  • Loading branch information
Washi1337 authored Apr 29, 2023
2 parents 0945321 + 89f934b commit 2007f94
Show file tree
Hide file tree
Showing 85 changed files with 876 additions and 800 deletions.
8 changes: 4 additions & 4 deletions src/AsmResolver.DotNet/AssemblyDefinition.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public class AssemblyDefinition : AssemblyDescriptor, IModuleProvider, IHasSecur
{
private IList<ModuleDefinition>? _modules;
private IList<SecurityDeclaration>? _securityDeclarations;
private readonly LazyVariable<byte[]?> _publicKey;
private readonly LazyVariable<AssemblyDefinition, byte[]?> _publicKey;
private byte[]? _publicKeyToken;

/// <summary>
Expand Down Expand Up @@ -98,7 +98,7 @@ public static AssemblyDefinition FromImage(IPEImage peImage, ModuleReaderParamet
protected AssemblyDefinition(MetadataToken token)
: base(token)
{
_publicKey = new LazyVariable<byte[]?>(GetPublicKey);
_publicKey = new LazyVariable<AssemblyDefinition, byte[]?>(x => x.GetPublicKey());
}

/// <summary>
Expand Down Expand Up @@ -169,10 +169,10 @@ public IList<SecurityDeclaration> SecurityDeclarations
/// </remarks>
public byte[]? PublicKey
{
get => _publicKey.Value;
get => _publicKey.GetValue(this);
set
{
_publicKey.Value = value;
_publicKey.SetValue(value);
_publicKeyToken = null;
}
}
Expand Down
16 changes: 8 additions & 8 deletions src/AsmResolver.DotNet/AssemblyDescriptor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ public abstract class AssemblyDescriptor : MetadataMember, IHasCustomAttribute,
{
private const int PublicKeyTokenLength = 8;

private readonly LazyVariable<Utf8String?> _name;
private readonly LazyVariable<Utf8String?> _culture;
private readonly LazyVariable<AssemblyDescriptor, Utf8String?> _name;
private readonly LazyVariable<AssemblyDescriptor, Utf8String?> _culture;
private IList<CustomAttribute>? _customAttributes;

/// <summary>
Expand All @@ -30,8 +30,8 @@ public abstract class AssemblyDescriptor : MetadataMember, IHasCustomAttribute,
protected AssemblyDescriptor(MetadataToken token)
: base(token)
{
_name = new LazyVariable<Utf8String?>(GetName);
_culture = new LazyVariable<Utf8String?>(GetCulture);
_name = new LazyVariable<AssemblyDescriptor, Utf8String?>(x => x.GetName());
_culture = new LazyVariable<AssemblyDescriptor, Utf8String?>(x => x.GetCulture());
Version = new Version(0, 0, 0, 0);
}

Expand All @@ -43,8 +43,8 @@ protected AssemblyDescriptor(MetadataToken token)
/// </remarks>
public Utf8String? Name
{
get => _name.Value;
set => _name.Value = value;
get => _name.GetValue(this);
set => _name.SetValue(value);
}

string? INameProvider.Name => Name;
Expand Down Expand Up @@ -175,8 +175,8 @@ protected virtual IList<CustomAttribute> GetCustomAttributes() =>
/// </remarks>
public Utf8String? Culture
{
get => _culture.Value;
set => _culture.Value = value;
get => _culture.GetValue(this);
set => _culture.SetValue(value);
}

/// <summary>
Expand Down
16 changes: 8 additions & 8 deletions src/AsmResolver.DotNet/AssemblyReference.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ public class AssemblyReference :
IOwnedCollectionElement<ModuleDefinition>,
IImplementation
{
private readonly LazyVariable<byte[]?> _publicKeyOrToken;
private readonly LazyVariable<byte[]?> _hashValue;
private readonly LazyVariable<AssemblyReference, byte[]?> _publicKeyOrToken;
private readonly LazyVariable<AssemblyReference, byte[]?> _hashValue;
private byte[]? _publicKeyToken;

/// <summary>
Expand All @@ -26,8 +26,8 @@ public class AssemblyReference :
protected AssemblyReference(MetadataToken token)
: base(token)
{
_publicKeyOrToken = new LazyVariable<byte[]?>(GetPublicKeyOrToken);
_hashValue = new LazyVariable<byte[]?>(GetHashValue);
_publicKeyOrToken = new LazyVariable<AssemblyReference, byte[]?>(x => x.GetPublicKeyOrToken());
_hashValue = new LazyVariable<AssemblyReference, byte[]?>(x => x.GetHashValue());
}

/// <summary>
Expand Down Expand Up @@ -107,17 +107,17 @@ public ModuleDefinition? Module
/// </remarks>
public byte[]? PublicKeyOrToken
{
get => _publicKeyOrToken.Value;
set => _publicKeyOrToken.Value = value;
get => _publicKeyOrToken.GetValue(this);
set => _publicKeyOrToken.SetValue(value);
}

/// <summary>
/// Gets or sets the hash value of the assembly reference.
/// </summary>
public byte[]? HashValue
{
get => _hashValue.Value;
set => _hashValue.Value = value;
get => _hashValue.GetValue(this);
set => _hashValue.SetValue(value);
}

/// <inheritdoc />
Expand Down
10 changes: 5 additions & 5 deletions src/AsmResolver.DotNet/Bundles/BundleFile.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ namespace AsmResolver.DotNet.Bundles
/// </summary>
public class BundleFile : IOwnedCollectionElement<BundleManifest>
{
private readonly LazyVariable<ISegment> _contents;
private readonly LazyVariable<BundleFile, ISegment> _contents;

/// <summary>
/// Creates a new empty bundle file.
Expand All @@ -20,7 +20,7 @@ public class BundleFile : IOwnedCollectionElement<BundleManifest>
public BundleFile(string relativePath)
{
RelativePath = relativePath;
_contents = new LazyVariable<ISegment>(GetContents);
_contents = new LazyVariable<BundleFile, ISegment>(x => x.GetContents());
}

/// <summary>
Expand All @@ -44,7 +44,7 @@ public BundleFile(string relativePath, BundleFileType type, ISegment contents)
{
RelativePath = relativePath;
Type = type;
_contents = new LazyVariable<ISegment>(contents);
_contents = new LazyVariable<BundleFile, ISegment>(contents);
}

/// <summary>
Expand Down Expand Up @@ -102,8 +102,8 @@ public bool IsCompressed
/// </summary>
public ISegment Contents
{
get => _contents.Value;
set => _contents.Value = value;
get => _contents.GetValue(this);
set => _contents.SetValue(value);
}

/// <summary>
Expand Down
8 changes: 4 additions & 4 deletions src/AsmResolver.DotNet/ClassLayout.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ namespace AsmResolver.DotNet
/// </summary>
public class ClassLayout : MetadataMember
{
private readonly LazyVariable<TypeDefinition?> _parent;
private readonly LazyVariable<ClassLayout, TypeDefinition?> _parent;

/// <summary>
/// Initializes the class layout with a metadata token.
Expand All @@ -16,7 +16,7 @@ public class ClassLayout : MetadataMember
protected ClassLayout(MetadataToken token)
: base(token)
{
_parent = new LazyVariable<TypeDefinition?>(GetParent);
_parent = new LazyVariable<ClassLayout, TypeDefinition?>(x => x.GetParent());
}

/// <summary>
Expand Down Expand Up @@ -58,8 +58,8 @@ public uint ClassSize
/// </summary>
public TypeDefinition? Parent
{
get => _parent.Value;
internal set => _parent.Value = value;
get => _parent.GetValue(this);
internal set => _parent.SetValue(value);
}

/// <summary>
Expand Down
16 changes: 8 additions & 8 deletions src/AsmResolver.DotNet/Constant.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ namespace AsmResolver.DotNet
/// </summary>
public class Constant : MetadataMember
{
private readonly LazyVariable<IHasConstant?> _parent;
private readonly LazyVariable<DataBlobSignature?> _value;
private readonly LazyVariable<Constant, IHasConstant?> _parent;
private readonly LazyVariable<Constant, DataBlobSignature?> _value;

/// <summary>
/// Initializes the constant with a metadata token.
Expand All @@ -19,8 +19,8 @@ public class Constant : MetadataMember
protected Constant(MetadataToken token)
: base(token)
{
_parent = new LazyVariable<IHasConstant?>(GetParent);
_value = new LazyVariable<DataBlobSignature?>(GetValue);
_parent = new LazyVariable<Constant, IHasConstant?>(x => x.GetParent());
_value = new LazyVariable<Constant, DataBlobSignature?>(x => x.GetValue());
}

/// <summary>
Expand Down Expand Up @@ -50,17 +50,17 @@ public ElementType Type
/// </summary>
public IHasConstant? Parent
{
get => _parent.Value;
internal set => _parent.Value = value;
get => _parent.GetValue(this);
internal set => _parent.SetValue(value);
}

/// <summary>
/// Gets or sets the serialized literal value.
/// </summary>
public DataBlobSignature? Value
{
get => _value.Value;
set => _value.Value = value;
get => _value.GetValue(this);
set => _value.SetValue(value);
}

/// <summary>
Expand Down
24 changes: 12 additions & 12 deletions src/AsmResolver.DotNet/CustomAttribute.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@ namespace AsmResolver.DotNet
/// </summary>
public class CustomAttribute : MetadataMember, IOwnedCollectionElement<IHasCustomAttribute>
{
private readonly LazyVariable<IHasCustomAttribute?> _parent;
private readonly LazyVariable<ICustomAttributeType?> _constructor;
private readonly LazyVariable<CustomAttributeSignature?> _signature;
private readonly LazyVariable<CustomAttribute, IHasCustomAttribute?> _parent;
private readonly LazyVariable<CustomAttribute, ICustomAttributeType?> _constructor;
private readonly LazyVariable<CustomAttribute, CustomAttributeSignature?> _signature;

/// <summary>
/// Initializes an empty custom attribute.
Expand All @@ -20,9 +20,9 @@ public class CustomAttribute : MetadataMember, IOwnedCollectionElement<IHasCusto
protected CustomAttribute(MetadataToken token)
: base(token)
{
_parent = new LazyVariable<IHasCustomAttribute?>(GetParent);
_constructor = new LazyVariable<ICustomAttributeType?>(GetConstructor);
_signature = new LazyVariable<CustomAttributeSignature?>(GetSignature);
_parent = new LazyVariable<CustomAttribute, IHasCustomAttribute?>(x => x.GetParent());
_constructor = new LazyVariable<CustomAttribute, ICustomAttributeType?>(x => x.GetConstructor());
_signature = new LazyVariable<CustomAttribute, CustomAttributeSignature?>(x => x.GetSignature());
}

/// <summary>
Expand Down Expand Up @@ -53,8 +53,8 @@ public CustomAttribute(ICustomAttributeType? constructor, CustomAttributeSignatu
/// </summary>
public IHasCustomAttribute? Parent
{
get => _parent.Value;
private set => _parent.Value = value;
get => _parent.GetValue(this);
private set => _parent.SetValue(value);
}

IHasCustomAttribute? IOwnedCollectionElement<IHasCustomAttribute>.Owner
Expand All @@ -68,17 +68,17 @@ public IHasCustomAttribute? Parent
/// </summary>
public ICustomAttributeType? Constructor
{
get => _constructor.Value;
set => _constructor.Value = value;
get => _constructor.GetValue(this);
set => _constructor.SetValue(value);
}

/// <summary>
/// Gets or sets the signature containing the arguments passed onto the attribute's constructor.
/// </summary>
public CustomAttributeSignature? Signature
{
get => _signature.Value;
set => _signature.Value = value;
get => _signature.GetValue(this);
set => _signature.SetValue(value);
}

/// <summary>
Expand Down
24 changes: 12 additions & 12 deletions src/AsmResolver.DotNet/EventDefinition.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@ public class EventDefinition :
IHasCustomAttribute,
IOwnedCollectionElement<TypeDefinition>
{
private readonly LazyVariable<Utf8String?> _name;
private readonly LazyVariable<TypeDefinition?> _declaringType;
private readonly LazyVariable<ITypeDefOrRef?> _eventType;
private readonly LazyVariable<EventDefinition, Utf8String?> _name;
private readonly LazyVariable<EventDefinition, TypeDefinition?> _declaringType;
private readonly LazyVariable<EventDefinition, ITypeDefOrRef?> _eventType;
private IList<MethodSemantics>? _semantics;
private IList<CustomAttribute>? _customAttributes;

Expand All @@ -31,9 +31,9 @@ public class EventDefinition :
protected EventDefinition(MetadataToken token)
: base(token)
{
_name = new LazyVariable<Utf8String?>(() => GetName());
_eventType = new LazyVariable<ITypeDefOrRef?>(GetEventType);
_declaringType = new LazyVariable<TypeDefinition?>(GetDeclaringType);
_name = new LazyVariable<EventDefinition, Utf8String?>(x => x.GetName());
_eventType = new LazyVariable<EventDefinition, ITypeDefOrRef?>(x => x.GetEventType());
_declaringType = new LazyVariable<EventDefinition, TypeDefinition?>(x => x.GetDeclaringType());
}

/// <summary>
Expand Down Expand Up @@ -87,8 +87,8 @@ public bool IsRuntimeSpecialName
/// </remarks>
public Utf8String? Name
{
get => _name.Value;
set => _name.Value = value;
get => _name.GetValue(this);
set => _name.SetValue(value);
}

string? INameProvider.Name => Name;
Expand All @@ -101,8 +101,8 @@ public Utf8String? Name
/// </summary>
public ITypeDefOrRef? EventType
{
get => _eventType.Value;
set => _eventType.Value = value;
get => _eventType.GetValue(this);
set => _eventType.SetValue(value);
}

/// <inheritdoc />
Expand All @@ -113,8 +113,8 @@ public ITypeDefOrRef? EventType
/// </summary>
public TypeDefinition? DeclaringType
{
get => _declaringType.Value;
private set => _declaringType.Value = value;
get => _declaringType.GetValue(this);
private set => _declaringType.SetValue(value);
}

ITypeDescriptor? IMemberDescriptor.DeclaringType => DeclaringType;
Expand Down
Loading

0 comments on commit 2007f94

Please sign in to comment.