-
Couldn't load subscription status.
- Fork 1.1k
Add ToReadOnlySequence() to ByteString #7491
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
base: dev
Are you sure you want to change the base?
Changes from 2 commits
c3a3240
c280650
ecf4b3f
adbbdb1
516d807
cf63b0b
435a9c2
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -6,6 +6,7 @@ | |
| //----------------------------------------------------------------------- | ||
|
|
||
| using System; | ||
| using System.Buffers; | ||
| using System.Collections; | ||
| using System.Collections.Generic; | ||
| using System.Diagnostics; | ||
|
|
@@ -28,6 +29,46 @@ namespace Akka.IO | |
| [DebuggerDisplay("(Count = {_count}, Buffers = {_buffers})")] | ||
| public sealed class ByteString : IEquatable<ByteString>, IEnumerable<byte> | ||
| { | ||
| public class ByteStringReadOnlySequenceSegment : ReadOnlySequenceSegment<byte> | ||
| { | ||
| public ByteStringReadOnlySequenceSegment(ReadOnlyMemory<byte> memory, long runningIndex) | ||
| { | ||
| Memory = memory; | ||
| RunningIndex = runningIndex; | ||
| } | ||
|
|
||
| /// <remarks> | ||
| /// This is here because <see cref="ReadOnlySequenceSegment{T}"/> | ||
| /// has predefined properties with Protected Setters. | ||
| /// </remarks> | ||
| public static ReadOnlySequence<byte> CreateSequence(ByteString bs) | ||
| { | ||
| var bArr = bs._buffers; | ||
| var first = new ByteStringReadOnlySequenceSegment(bArr[0],0); | ||
| ByteBuffer item = default; | ||
| ByteStringReadOnlySequenceSegment last = first; | ||
| if (bArr.Length == 2) | ||
| { | ||
| item = bArr[1]; | ||
| last = new ByteStringReadOnlySequenceSegment(item, bs.Count-item.Count); | ||
| first.Next = last; | ||
| } | ||
|
||
| else | ||
| { | ||
| var prior = first; | ||
| for (int i = 1; i < bArr.Length; i++) | ||
| { | ||
| item = bArr[i]; | ||
| var curr = new ByteStringReadOnlySequenceSegment(item, prior.RunningIndex+prior.Memory.Length); | ||
| prior.Next = curr; | ||
| prior = curr; | ||
| } | ||
| last = prior; | ||
| } | ||
| return new ReadOnlySequence<byte>(first,0,last,last.Memory.Length); | ||
|
||
| } | ||
| } | ||
|
|
||
| #region creation methods | ||
|
|
||
| /// <summary> | ||
|
|
@@ -246,7 +287,7 @@ public static ByteString FromString(string str, Encoding encoding) | |
|
|
||
| private readonly int _count; | ||
| private readonly ByteBuffer[] _buffers; | ||
|
|
||
| private ByteString(ByteBuffer[] buffers, int count) | ||
| { | ||
| _buffers = buffers; | ||
|
|
@@ -528,6 +569,30 @@ public ReadOnlySpan<byte> ToReadOnlySpan() | |
|
|
||
| return new ReadOnlySpan<byte>(ToArray()); | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Returns a ReadOnlySequence<byte/> over the contents of this ByteString. | ||
| /// This is not a copying operation and zero-alloc when ByteString is compact | ||
| /// Otherwise N <see cref="ByteStringReadOnlySequenceSegment"/>s will be allocated | ||
| /// Where N is the number of arrays currently internally held by the ByteString | ||
| /// </summary> | ||
| /// <returns>A ReadOnlySpan<byte/> over the byte data.</returns> | ||
| public ReadOnlySequence<byte> ToReadOnlySequence() | ||
| { | ||
| if (_count == 0) | ||
| { | ||
| return ReadOnlySequence<byte>.Empty; | ||
| } | ||
| else if (_buffers.Length == 1) | ||
| { | ||
| //Happy path, we can just pass ArraySegment here and avoid alloc. | ||
| return new ReadOnlySequence<byte>(_buffers[0]); | ||
| } | ||
| else | ||
| { | ||
| return ByteStringReadOnlySequenceSegment.CreateSequence(this); | ||
| } | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Appends <paramref name="other"/> <see cref="ByteString"/> at the tail | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not sure where to put in class (or maybe even move to partial/etc?)
It's a nested class for the sake of being able to directly access
_buffersin the create. OTOH if we 'flipped' the construction (see other comment) we wouldn't need to nest it and could just build in.ToReadOnlySequence().... Either way this should be
sealedthough