-
Notifications
You must be signed in to change notification settings - 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 4 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,51 @@ namespace Akka.IO | |
[DebuggerDisplay("(Count = {_count}, Buffers = {_buffers})")] | ||
public sealed class ByteString : IEquatable<ByteString>, IEnumerable<byte> | ||
{ | ||
/// <summary> | ||
/// A <see cref="ReadOnlySequenceSegment{T}"/> to encapsulate | ||
/// Parts of a <see cref="ByteString"/> in a <see cref="ReadOnlySequence{T}"/> | ||
/// </summary> | ||
[DebuggerDisplay("(RunningIndex = {RunningIndex}, Length = {Memory.Length})}")] | ||
public sealed class ByteStringReadOnlySequenceSegment : ReadOnlySequenceSegment<byte> | ||
{ | ||
private 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); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. IDK if I like this less or more than just 'going backwards on the array' and just building the Segments from last to first. Happy for feedback (I suppose main thing being the pattern here vs setting next explicitly in ctor could impact JIT optimizations... but probably overthinking things a bit.) |
||
} | ||
} | ||
|
||
#region creation methods | ||
|
||
/// <summary> | ||
|
@@ -246,7 +292,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 +574,33 @@ public ReadOnlySpan<byte> ToReadOnlySpan() | |
|
||
return new ReadOnlySpan<byte>(ToArray()); | ||
} | ||
|
||
/// <summary> | ||
/// Returns a <see cref="ReadOnlySequence{T}"/> of <see cref="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 <see cref="ReadOnlySequence{T}"/> of <see cref="byte"/> | ||
/// over the data in the <see cref="ByteString"/> | ||
/// </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.
TBH this might be premature optimization....
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.
Yes it is, please remove