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

Streams #27

Open
wants to merge 17 commits into
base: master
Choose a base branch
from
Open

Streams #27

wants to merge 17 commits into from

Conversation

mgravell
Copy link
Owner

@mgravell mgravell commented Mar 14, 2019

Adds a new SequenceStream / ReadOnlySequenceStream pair - similar to MemoryStream, but:

  • GetBuffer() returns [ReadOnly]Sequence<byte>
  • when created as dynamically resizeable, it leases segments from the ArrayPool<byte>.Shared and returns them when trimmed/disposed

Benefits:

  • buffer allocations are pooled leases
  • because non-contiguous memory is the norm, there's no need to copy any data around when expanding the underlying buffer (in this use-case, SequenceStream owns the sequence, so it can play with the chain; it does not mutate the chain if an existing one is passed in)
  • allows you to treat a Sequence<byte> or ReadOnlySequence<byte> as a Stream with very low overhead
  • (marginally) out-performs MemoryStream anyway (I'd have settled for "equal or slightly worse")

Possible additions:

  • should we allow a specific ArrayPool<byte> to be specified? or is that unnecessary?
  • if so, should we actually be exposing/using MemoryPool<byte>[.Shared] instead of ArrayPool<byte>?
  • should we allow arena-based allocation as an optional opt-in?

Performance:

BenchmarkDotNet=v0.11.4, OS=Windows 10.0.17134.648 (1803/April2018Update/Redstone4)
Intel Core i7-5930K CPU 3.50GHz (Broadwell), 1 CPU, 12 logical and 6 physical cores
Frequency=3416973 Hz, Resolution=292.6567 ns, Timer=TSC
  [Host]     : .NET Framework 4.7.2 (CLR 4.0.30319.42000), 64bit RyuJIT-v4.7.3362.0
  Job-FQKGZY : .NET Framework 4.7.2 (CLR 4.0.30319.42000), 64bit RyuJIT-v4.7.3362.0
  Job-UGUEYK : .NET Core 2.2.2 (CoreCLR 4.6.27317.07, CoreFX 4.6.27318.02), 64bit RyuJIT
Method Runtime Toolchain Mean Error StdDev Median Ratio RatioSD Gen 0/1k Op Gen 1/1k Op Gen 2/1k Op Allocated Memory/Op
MemoryStreamDefault Clr net472 8.933 ms 0.1760 ms 0.3515 ms 9.013 ms 1.07 0.05 390.6250 390.6250 390.6250 1399112 B
MemoryStreamPreSize Clr net472 8.263 ms 0.1633 ms 0.3480 ms 8.466 ms 1.00 0.04 140.6250 140.6250 140.6250 515976 B
RecyclableMemoryStreamDefault Clr net472 7.570 ms 0.0628 ms 0.0491 ms 7.552 ms 0.92 0.01 - - - 1024 B
RecyclableMemoryStreamPreSize Clr net472 7.899 ms 0.1538 ms 0.2206 ms 7.903 ms 0.95 0.03 - - - 960 B
SequenceStreamDefault Clr net472 7.863 ms 0.1568 ms 0.1743 ms 7.797 ms 0.96 0.02 - - - 448 B
SequenceStreamPreSize Clr net472 8.606 ms 0.2415 ms 0.7120 ms 8.407 ms 0.98 0.04 - - - 512 B
MemoryStreamDefault Core netcoreapp2.2 8.234 ms 0.1316 ms 0.1167 ms 8.193 ms 1.00 0.00 390.6250 390.6250 390.6250 1397576 B
MemoryStreamPreSize Core netcoreapp2.2 8.250 ms 0.1647 ms 0.2927 ms 8.199 ms 1.01 0.04 140.6250 140.6250 140.6250 515048 B
RecyclableMemoryStreamDefault Core netcoreapp2.2 7.999 ms 0.1564 ms 0.2389 ms 8.036 ms 0.97 0.04 - - - 920 B
RecyclableMemoryStreamPreSize Core netcoreapp2.2 7.888 ms 0.1541 ms 0.2575 ms 7.861 ms 0.97 0.03 - - - 920 B
SequenceStreamDefault Core netcoreapp2.2 8.282 ms 0.1627 ms 0.3359 ms 8.334 ms 1.00 0.05 - - - 400 B
SequenceStreamPreSize Core netcoreapp2.2 8.227 ms 0.1629 ms 0.3364 ms 8.253 ms 1.01 0.03 - - - 400 B

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant