Skip to content

Conversation

@BrennanConroy
Copy link
Member

Fixes #30786

Reuses a BufferSegment if it is unused and a larger memory buffer is requested.

There was already a memory pool test for this type of scenario, so I haven't added a new one.

}

public void ResetMemory()
public void ResetMemory(bool preserveIndex = false)
Copy link
Member

Choose a reason for hiding this comment

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

Nit:

Maybe add a ClearMemory() method and rename this to Reset() and have it call ClearMemory(). Because this is doing more than just resetting memory, it's resetting the RunningIndex and the _next pointer too. It might make sense for ClearMemory() to keep the ResetMemory() name.

Copy link
Member Author

Choose a reason for hiding this comment

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

👍 I was thinking along those lines when writing it. Good to know we're thinking that same thing.

return newSegment;
}

private void SetupSegment(BufferSegment segment, int sizeHint)
Copy link
Member

Choose a reason for hiding this comment

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

Nit:

Suggested change
private void SetupSegment(BufferSegment segment, int sizeHint)
private void RentMemory(BufferSegment segment, int sizeHint)

}

BufferSegment newSegment = AllocateSegment(sizeHint);
if (_writingHead.Length == 0)
Copy link
Member

Choose a reason for hiding this comment

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

I would use _writingHeadBytesBuffered instead (but the logic would need to be reworked a bit)

Copy link
Member Author

Choose a reason for hiding this comment

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

I think I tried using that field initially, but it gets set to 0 in Advance with a non-zero value, so you need to check some other value anyways. What's wrong with _writingHead.Length?

Copy link
Member

Choose a reason for hiding this comment

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

Nothing wrong with it, it just avoids the indirection but this is fine.

@BrennanConroy BrennanConroy added this to the 7.0.0 milestone Jul 21, 2022
@BrennanConroy
Copy link
Member Author

Both test failures have tracking issues and are unrelated to this change, merging.

@BrennanConroy BrennanConroy merged commit 2fc9108 into dotnet:main Jul 21, 2022
@BrennanConroy BrennanConroy deleted the brecon/emptySegment branch July 21, 2022 15:51
@ghost ghost locked as resolved and limited conversation to collaborators Aug 20, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

PipeReader.ReadAsync can return empty segments

3 participants