Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why size:4?

Copy link
Contributor Author

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.


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;
Copy link
Member

Choose a reason for hiding this comment

The 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();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

does writeValidationBytes then work properly? do we basically check if we're at pos0 and wrrite out those bytes (i genuinely don't remember).

Copy link
Contributor Author

Choose a reason for hiding this comment

The 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)
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)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

hrmm... why is this optional. that seems... onfusing.

Copy link
Member

Choose a reason for hiding this comment

The 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?

Copy link
Contributor Author

Choose a reason for hiding this comment

The 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;
Copy link
Member

Choose a reason for hiding this comment

The 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);
Copy link
Member

Choose a reason for hiding this comment

The 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>
Expand Down
Loading