Skip to content
This repository has been archived by the owner on Jan 23, 2023. It is now read-only.
/ corefx Public archive

Adding Buffer Pooling to System.IO #5742

Closed
wants to merge 1 commit into from
Closed

Adding Buffer Pooling to System.IO #5742

wants to merge 1 commit into from

Conversation

jonmill
Copy link

@jonmill jonmill commented Jan 27, 2016

Starting to buffer all the things from #5598; adding Buffer Pooling to the BufferedStream, BinaryWriter, BinaryReader, and Stream classes.

Thanks to @ericstj for helping get the project.json and csproj files correct

@stephentoub @weshaggard

if (_charBytes != null)
ArrayPool<byte>.Shared.Return(_charBytes);
if (_charBuffer != null)
ArrayPool<char>.Shared.Return(_charBuffer);
Copy link
Member

Choose a reason for hiding this comment

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

Would it make sense to return the buffer before calling dispose on the stream? This would ensure the buffers are returned even if stream.Dispose throws.

Copy link
Member

Choose a reason for hiding this comment

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

Won't this end up returning these buffers twice if it's Dispose'd twice?

Copy link
Member

Choose a reason for hiding this comment

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

Yeah, the code should guard against double return either by checking _isDisposed, or setting _buffer to null after the first clear. Also, this should be thread-safe.

Copy link
Author

Choose a reason for hiding this comment

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

Fixed

@stephentoub
Copy link
Member

Thanks for working on this.

@stephentoub
Copy link
Member

@sokket, before doing further work on this, see the discussion here: #5954 (comment)

@stephentoub
Copy link
Member

cc: @jkotas

@jonmill
Copy link
Author

jonmill commented Feb 9, 2016

@stephentoub I didn't see a conclusion to that thread...should I back out this and the compression changes?

@stephentoub
Copy link
Member

@sokket, thanks for doing this. Based on the discussion at the design meeting today, I'm closing this. In cases where these types are misused, we can't guarantee in these cases that we won't continue using the buffer even after it's been returned, e.g. BinaryReader passes its _buffer to the underlying Stream, which may be buggy and hold onto and use that byte[] well after it's been returned to the pool.

@karelz karelz modified the milestone: 1.0.0-rtm Dec 3, 2016
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

7 participants