Skip to content
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

Reduce allocations during checksum creation. #76524

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
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
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ internal readonly partial record struct Checksum

private static readonly ObjectPool<XxHash128> s_incrementalHashPool =
new(() => new(), size: 20);
private static readonly ObjectPool<ObjectWriter> s_objectWriterPool =
new(() => new(SerializableBytes.CreateWritableStream(), leaveOpen: true, writeValidationBytes: true), size: 4);

public static Checksum Create(IEnumerable<string?> values)
{
Expand Down Expand Up @@ -57,15 +59,24 @@ public static Checksum Create(Stream stream)

public static Checksum Create<T>(T @object, Action<T, ObjectWriter> writeObject)
{
using var stream = SerializableBytes.CreateWritableStream();
// Obtain a writer from the pool
var objectWriter = s_objectWriterPool.Allocate();
var stream = objectWriter.BaseStream;

using (var objectWriter = new ObjectWriter(stream, leaveOpen: true))
{
writeObject(@object, objectWriter);
}
// Invoke the callback to Write object into objectWriter
writeObject(@object, objectWriter);

// Include validation bytes in the new checksum from the stream
stream.Position = 0;
return Create(stream);
var newChecksum = Create(stream);

// Reset object writer back to it's initial state
objectWriter.Reset();

// Release the writer back to the pool
s_objectWriterPool.Free(objectWriter);

return newChecksum;
}

public static Checksum Create(Checksum checksum1, Checksum checksum2)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,12 @@ public readonly void Dispose()
}
}

public void Reset()
{
_valueToIdMap.Clear();
_nextId = 0;
}

public bool TryGetReferenceId(string value, out int referenceId)
=> _valueToIdMap.TryGetValue(value, out referenceId);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,19 @@ public void Dispose()
public void WriteUInt16(ushort value) => _writer.Write(value);
public void WriteString(string? value) => WriteStringValue(value);

public Stream BaseStream => _writer.BaseStream;

public void Reset(bool includeValidationBytes = true)
{
_stringReferenceMap.Reset();
_writer.BaseStream.Position = includeValidationBytes ? 2 : 0;

if (_writer.BaseStream is SerializableBytes.ReadWriteStream pooledStream)
pooledStream.SetLength(_writer.BaseStream.Position, truncate: false);
else
_writer.BaseStream.SetLength(_writer.BaseStream.Position);
}

/// <summary>
/// Used so we can easily grab the low/high 64bits of a guid for serialization.
/// </summary>
Expand Down
Loading