-
Notifications
You must be signed in to change notification settings - Fork 4.1k
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
Changes from all commits
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 |
---|---|---|
|
@@ -23,6 +23,11 @@ internal readonly partial record struct Checksum | |
private static readonly ObjectPool<XxHash128> s_incrementalHashPool = | ||
new(() => new(), size: 20); | ||
|
||
// Pool of ObjectWriters to reduce allocations. The pool size is intentionally small as the writers are used for such | ||
// a short period that concurrent usage of different items from the pool is infrequent. | ||
private static readonly ObjectPool<ObjectWriter> s_objectWriterPool = | ||
new(() => new(SerializableBytes.CreateWritableStream(), leaveOpen: true, writeValidationBytes: true), size: 4); | ||
|
||
public static Checksum Create(IEnumerable<string?> values) | ||
{ | ||
using var pooledHash = s_incrementalHashPool.GetPooledObject(); | ||
|
@@ -57,15 +62,25 @@ 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(); | ||
|
||
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 | ||
var stream = objectWriter.BaseStream; | ||
stream.Position = 0; | ||
return Create(stream); | ||
var newChecksum = Create(stream); | ||
|
||
// Reset object writer back to it's initial state, including the validation bytes | ||
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. |
||
objectWriter.WriteValidationBytes(); | ||
|
||
// Release the writer back to the pool | ||
s_objectWriterPool.Free(objectWriter); | ||
|
||
return newChecksum; | ||
} | ||
|
||
public static Checksum Create(Checksum checksum1, Checksum checksum2) | ||
|
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.