You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
HashData stream one-shots can special case some stream types #64173
The new HashData one shots that accept a Stream as input can special case some stream types by deferring to the ReadOnlySpan<byte> one shots where the stream is known to be backed by contiguous memory. For example, MemoryStream's TryGetBuffer.
Doing this would give a few benefits:
They would be truly non-allocating.
Throughput will be higher since it boils down a single p/invoke (usually) instead of reading from the stream in chunks and updating.
This issue is to track the implementation of that.
I also considered UnmanagedMemoryStream which we could do with PositionPointer, but I do not think that one is typical. I found zero occurrence of that throughout GitHub. So I don't think that one is worth pursuing.
I think my biggest "iffy" on doing this actually is that it will have the observable affect of not advancing the stream if we use TryGetBuffer, unless we explicitly set Position after using TryGetBuffer.
I think my biggest "iffy" on doing this actually is that it will have the observable affect of not advancing the stream if we use TryGetBuffer, unless we explicitly set Position after using TryGetBuffer.
You would need to set the position after. You'd also need to constrain it to exactly typeof(MemoryStream) and not any streams derived from it.
I'm not convinced this is a worthwhile change. All of the cases you cited involve a client that has a strongly-typed MemoryStream and can just as easily call GetBuffer and call the ROS-based HashData.
Activity
stephentoub commentedon Jan 24, 2022
It can. Should it? How common will it be to use those streams with this method?
vcsjones commentedon Jan 24, 2022
I think hashing a
MemoryStreamis not uncommon and some places are already making this optimization usingGetBuffer. Searching GitHub, some examples:(that's only looking at the first few pages).
I also considered
UnmanagedMemoryStreamwhich we could do withPositionPointer, but I do not think that one is typical. I found zero occurrence of that throughout GitHub. So I don't think that one is worth pursuing.vcsjones commentedon Jan 24, 2022
I would agree though that the majority of uses are a
FileStreamor some kind of network stream.vcsjones commentedon Jan 24, 2022
I think my biggest "iffy" on doing this actually is that it will have the observable affect of not advancing the stream if we use
TryGetBuffer, unless we explicitly setPositionafter usingTryGetBuffer.stephentoub commentedon Jan 24, 2022
You would need to set the position after. You'd also need to constrain it to exactly typeof(MemoryStream) and not any streams derived from it.
I'm not convinced this is a worthwhile change. All of the cases you cited involve a client that has a strongly-typed MemoryStream and can just as easily call GetBuffer and call the ROS-based HashData.
vcsjones commentedon Jan 24, 2022
Fair enough, I'll close this out then. We can re-open it if any new evidence emerges that this is worth pursuing.