using System.Security.Cryptography; namespace AcDream.Bake; /// /// Offline-only exact-byte deduplication catalog. SHA-256 narrows candidates; /// a complete byte comparison is still mandatory before any alias is emitted. /// internal sealed class ExactPayloadAliasCatalog { private readonly Dictionary> _entriesByDigest = new(StringComparer.Ordinal); public bool TryFind(ReadOnlySpan payload, out ulong primaryKey) { string digest = Digest(payload); if (_entriesByDigest.TryGetValue(digest, out List? candidates)) { foreach (Entry candidate in candidates) { if (payload.SequenceEqual(candidate.Payload)) { primaryKey = candidate.PrimaryKey; return true; } } } primaryKey = 0; return false; } public void Add(ulong primaryKey, byte[] payload) { ArgumentNullException.ThrowIfNull(payload); string digest = Digest(payload); if (!_entriesByDigest.TryGetValue(digest, out List? entries)) { entries = []; _entriesByDigest.Add(digest, entries); } entries.Add(new Entry(primaryKey, payload)); } private static string Digest(ReadOnlySpan payload) => Convert.ToHexString(SHA256.HashData(payload)); private sealed record Entry(ulong PrimaryKey, byte[] Payload); }