Skip to content

Improve signature generation algorithm #193

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

Merged
merged 2 commits into from
Jun 3, 2025
Merged
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
53 changes: 22 additions & 31 deletions MetadataProcessor.Shared/Tables/nanoSignaturesTable.cs
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,10 @@ static nanoSignaturesTable()
/// </summary>
private readonly IDictionary<byte[], ushort> _idsBySignatures =
new Dictionary<byte[], ushort>(new ByteArrayComparer());
/// <summary>
/// The signatures as they are written to the output stream.
/// </summary>
private readonly List<byte> _signatureTable = new List<byte>();

/// <summary>
/// Assembly tables context - contains all tables used for building target assembly.
Expand All @@ -116,11 +120,6 @@ static nanoSignaturesTable()

private readonly bool _verbose = false;

/// <summary>
/// Last available signature id (offset in resulting table).
/// </summary>
private ushort _lastAvailableId;

/// <summary>
/// Creates new instance of <see cref="nanoSignaturesTable"/> object.
/// </summary>
Expand Down Expand Up @@ -468,12 +467,7 @@ public void WriteDataTypeForTypeDef(TypeDefinition typeDefinition, nanoBinaryWri
public void Write(
nanoBinaryWriter writer)
{
foreach (var signature in _idsBySignatures
.OrderBy(item => item.Value)
.Select(item => item.Key))
{
writer.WriteBytes(signature);
}
writer.WriteBytes(_signatureTable.ToArray());
}

private byte[] GetSignature(
Expand Down Expand Up @@ -736,18 +730,29 @@ private ushort GetOrCreateSignatureIdImpl(
return id;
}

var fullSignatures = GetFullSignaturesArray();
for (var i = 0; i <= fullSignatures.Length - signature.Length; ++i)
for (int i = 0; i < _signatureTable.Count - signature.Length; i++)
{
if (signature.SequenceEqual(fullSignatures.Skip(i).Take(signature.Length)))
bool found = true;
for (int j = 0; j < signature.Length; ++j)
{
return (ushort)i;
if (_signatureTable[i + j] != signature[j])
{
found = false;
break;
}
}

if (found)
{
id = (ushort)i;
_idsBySignatures.Add(signature, id);
return id;
}
}

id = _lastAvailableId;
id = (ushort)_signatureTable.Count;
_idsBySignatures.Add(signature, id);
_lastAvailableId += (ushort)signature.Length;
_signatureTable.AddRange(signature);

return id;
}
Expand Down Expand Up @@ -775,20 +780,6 @@ private void WriteTypeInfo(
}
}

private byte[] GetFullSignaturesArray()
{
return _idsBySignatures
.OrderBy(item => item.Value)
.Select(item => item.Key)
.Aggregate(new List<byte>(),
(current, item) =>
{
current.AddRange(item);
return current;
})
.ToArray();
}

private void WriteSubTypeInfo(TypeReference typeDefinition, nanoBinaryWriter writer)
{
ushort referenceId;
Expand Down