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

Merged
merged 2 commits into from
Jan 3, 2025

Conversation

ToddGrun
Copy link
Contributor

@ToddGrun ToddGrun commented Dec 19, 2024

From the csharp editing scrolling speedometer, I see about 0.3% of total allocations in the VS process occurring in Checksum.Create. A large part of these allocations are stream/writer constructions. Instead, this PR attempts to use a small pool to reuse and reset these objects.

*** before image showing % of VS allocations due to Checksum.Create ***
image

*** after image showing % of VS allocations due to Checksum.Create ***
image

*** before image showing types allocated under Checksum.Create ***
image

*** after image showing types allocated under Checksum.Create ***
image

Initially creating this PR as a draft PR to get speedometer results to see if this actually improves things.

From the csharp editing scrolling speedometer, I see about 0.3% of total allocations in the VS process occurring in Checksum.Create. A large part of these allocations are stream/writer constructions. Instead, this PR attempts to use a small pool to reuse and reset these objects.
@dotnet-issue-labeler dotnet-issue-labeler bot added Area-IDE untriaged Issues and PRs which have not yet been triaged by a lead labels Dec 19, 2024
@ToddGrun
Copy link
Contributor Author

ToddGrun commented Dec 19, 2024

Test insertion PR: https://dev.azure.com/devdiv/DevDiv/_git/VS/pullrequest/599592

Speedometer results look good, elevating out of draft status

@ToddGrun ToddGrun marked this pull request as ready for review December 20, 2024 14:45
@ToddGrun ToddGrun requested a review from a team as a code owner December 20, 2024 14:45
@ToddGrun
Copy link
Contributor Author

ToddGrun commented Jan 2, 2025

@CyrusNajmabadi -- ptal

@CyrusNajmabadi
Copy link
Member

What have you done for me lately??

@@ -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.

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.

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.

@@ -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

public void Reset(bool includeValidationBytes = true)
{
_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.

Copy link
Member

@CyrusNajmabadi CyrusNajmabadi left a comment

Choose a reason for hiding this comment

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

preemptively signing off. i have no problem with the concept. it just seems a little confusing/complex. if the complexity is necessary, i'd like beefing up the docs on why it works the way it does.

@ToddGrun ToddGrun merged commit fd40510 into dotnet:main Jan 3, 2025
25 checks passed
@dotnet-policy-service dotnet-policy-service bot added this to the Next milestone Jan 3, 2025
Copy link
Member

@CyrusNajmabadi CyrusNajmabadi left a comment

Choose a reason for hiding this comment

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

This looks great. Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Area-IDE untriaged Issues and PRs which have not yet been triaged by a lead VSCode
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants