-
Notifications
You must be signed in to change notification settings - Fork 4.2k
Reduce allocations during checksum creation. #76524
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
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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) | ||
{ | ||
|
@@ -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; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. consider moving this till after writing out hte object. |
||
|
||
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(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. does There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Changed in commit 2 to just rewrite the validation bytes after the reset instead of having the reset set the position to after the validation bytes. Probably easier to understand that way. |
||
|
||
// Release the writer back to the pool | ||
s_objectWriterPool.Free(objectWriter); | ||
|
||
return newChecksum; | ||
} | ||
|
||
public static Checksum Create(Checksum checksum1, Checksum checksum2) | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. hrmm... why is this optional. that seems... onfusing. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. no one seems to ever call this with a value. so just remove and always include the bytes? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yeah, probably shouldn't have worried about supporting that case |
||
{ | ||
_stringReferenceMap.Reset(); | ||
_writer.BaseStream.Position = includeValidationBytes ? 2 : 0; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. might want to doc this. perhaps that the bytes are already there, and we want to move after it. alterantively, hard reset to 0 and write the bytes again. |
||
|
||
if (_writer.BaseStream is SerializableBytes.ReadWriteStream pooledStream) | ||
pooledStream.SetLength(_writer.BaseStream.Position, truncate: false); | ||
else | ||
_writer.BaseStream.SetLength(_writer.BaseStream.Position); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. def find this whole part unclear. considering doc'ing. |
||
} | ||
|
||
/// <summary> | ||
/// Used so we can easily grab the low/high 64bits of a guid for serialization. | ||
/// </summary> | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
why size:4?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's because the object is used for such a short period, that it's actually pretty unlikely to have concurrent usage of different items from the pool. I'll add a comment indicating such.