Optimize BufferedSubStream.ReadByte#912
Merged
adamhathcock merged 1 commit intoadamhathcock:masterfrom Mar 25, 2025
Merged
Conversation
jdpurcell
commented
Mar 25, 2025
| public override void Flush() { } | ||
|
|
||
| public override long Length => BytesLeftToRead; | ||
| public override long Length => BytesLeftToRead + _cacheLength - _cacheOffset; |
Contributor
Author
There was a problem hiding this comment.
It seems weird that Length in a stream refers only to the remaining portion, but since that's the way it already worked I assume it's for a reason.
Owner
There was a problem hiding this comment.
maybe that's not the contract but I "meant" to do it 😬
jdpurcell
commented
Mar 25, 2025
|
|
||
| private void RefillCache() | ||
| { | ||
| var count = (int)Math.Min(BytesLeftToRead, _cache.Length); |
Contributor
Author
There was a problem hiding this comment.
This is a new bit of logic here; the old code would always request _cache.Length bytes when refilling. It probably wasn't hurting anything but I figure it's better to request only what we need.
adamhathcock
approved these changes
Mar 25, 2025
| public override void Flush() { } | ||
|
|
||
| public override long Length => BytesLeftToRead; | ||
| public override long Length => BytesLeftToRead + _cacheLength - _cacheOffset; |
Owner
There was a problem hiding this comment.
maybe that's not the contract but I "meant" to do it 😬
This was referenced Aug 1, 2025
This was referenced Nov 24, 2025
This was referenced Jan 5, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
In
BufferedSubStream, by changing whatBytesLeftToReadand_cacheLengthtrack, we can simplify the work to be done when reading a byte.BytesLeftToReadnow tracks the amount of completely unread/uncached data, and_cacheLengthtracks the total cache length including the consumed portion. Then we only need to update the cache offset when reading a byte. It also moves the logic to refresh the cache into its own method. Interestingly, only about half of the performance increase comes from simplifying the variable usage. The other half comes from moving out the cache refresh logic, presumably because it makes theReadBytemethod small enough for callers to inline.Core i7-6700k (1.7% reduction):
Apple M3 (2.8% reduction):
Time to extract a 14MB 7z. The benchmarks are with .NET 8.0 but it also helped slightly with .NET Framework 4.8 (~1.1% reduction).
I wish I could keep going with these optimization PRs but I think I'm out of ideas after this one 😄.