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

Add ToReadOnlySequence() to ByteString #7491

Open
wants to merge 6 commits into
base: dev
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -3805,6 +3805,7 @@ namespace Akka.IO
public Akka.IO.ByteString Slice(int index) { }
public Akka.IO.ByteString Slice(int index, int count) { }
public byte[] ToArray() { }
public System.Buffers.ReadOnlySequence<byte> ToReadOnlySequence() { }
public System.ReadOnlySpan<byte> ToReadOnlySpan() { }
public override string ToString() { }
public string ToString(System.Text.Encoding encoding) { }
Expand All @@ -3815,6 +3816,11 @@ namespace Akka.IO
public static Akka.IO.ByteString op_Explicit(byte[] bytes) { }
public static byte[] op_Explicit(Akka.IO.ByteString byteString) { }
public static bool !=(Akka.IO.ByteString x, Akka.IO.ByteString y) { }
[System.Diagnostics.DebuggerDisplayAttribute("(RunningIndex = {RunningIndex}, Length = {Memory.Length})}")]
public sealed class ByteStringReadOnlySequenceSegment : System.Buffers.ReadOnlySequenceSegment<byte>
{
public static System.Buffers.ReadOnlySequence<byte> CreateSequence(Akka.IO.ByteString bs) { }
}
}
[Akka.Annotations.InternalApiAttribute()]
public class ConnectException : System.Exception
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3795,6 +3795,7 @@ namespace Akka.IO
public Akka.IO.ByteString Slice(int index) { }
public Akka.IO.ByteString Slice(int index, int count) { }
public byte[] ToArray() { }
public System.Buffers.ReadOnlySequence<byte> ToReadOnlySequence() { }
public System.ReadOnlySpan<byte> ToReadOnlySpan() { }
public override string ToString() { }
public string ToString(System.Text.Encoding encoding) { }
Expand All @@ -3805,6 +3806,11 @@ namespace Akka.IO
public static Akka.IO.ByteString op_Explicit(byte[] bytes) { }
public static byte[] op_Explicit(Akka.IO.ByteString byteString) { }
public static bool !=(Akka.IO.ByteString x, Akka.IO.ByteString y) { }
[System.Diagnostics.DebuggerDisplayAttribute("(RunningIndex = {RunningIndex}, Length = {Memory.Length})}")]
public sealed class ByteStringReadOnlySequenceSegment : System.Buffers.ReadOnlySequenceSegment<byte>
{
public static System.Buffers.ReadOnlySequence<byte> CreateSequence(Akka.IO.ByteString bs) { }
}
}
[Akka.Annotations.InternalApiAttribute()]
public class ConnectException : System.Exception
Expand Down
59 changes: 59 additions & 0 deletions src/core/Akka.Tests/Util/ByteStringSpec.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
//-----------------------------------------------------------------------

using System;
using System.Buffers;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
Expand Down Expand Up @@ -97,6 +98,64 @@ public void A_ByteString_ToReadOnlySpan_compacted_must_have_identical_memory_ref
var concatSpan4 = compacted.ToReadOnlySpan();
(concatSpan3 == concatSpan4).Should().BeTrue();
}

[Fact]
public void A_ByteString_ToReadOnlySequence_must_have_correct_size()
{
Prop.ForAll((ByteString a, ByteString b) =>
{
a.ToReadOnlySequence().Length.Should().Be(a.Count);
b.ToReadOnlySequence().Length.Should().Be(b.Count);
var concat = a + b;
var spanConcat = concat.ToReadOnlySequence();
return spanConcat.Length == concat.Count;
}).QuickCheckThrowOnFailure();
}

[Fact]
public void A_ByteString_ToReadOnlySequence_compacted_must_have_identical_contents_to_original()
{
var bytesA = new byte[] { 1, 2, 3, 4, 5, 6, 7, 8, 9 };
var bytesB = new byte[] { 10, 11, 12, 13, 14, 15, 16, 17, 18 };
var a = ByteString.FromBytes(bytesA);
var b = ByteString.FromBytes(bytesB);

var concat = a + b;

var concatSpan = concat.ToReadOnlySequence();
var concatSpan2 = concat.ToReadOnlySequence();
// not compact, returns a new span
(concatSpan.ToArray().SequenceEqual(concatSpan2.ToArray())).Should().BeTrue();

// compact, will return same span
var compacted = concat.Compact();
var concatSpan3 = compacted.ToReadOnlySequence();
(concatSpan.ToArray().SequenceEqual(concatSpan3.ToArray())).Should().BeTrue();
}

[Fact]
public void A_ByteString_ToReadOnlySequence_compacted_must_have_identical_contents_to_original_with_many_Segments()
{
var bytesA = new byte[] { 1, 2, 3, 4, 5, 6, 7, 8, 9 };
var bytesB = new byte[] { 10, 11, 12, 13, 14, 15, 16, 17, 18 };
var bytesC = new byte[] { 19, 20, 21, 22, 23, 24, 25, 26, 27 };
var a = ByteString.FromBytes(bytesA);
var b = ByteString.FromBytes(bytesB);
var c = ByteString.FromBytes(bytesC);
var concat = a + b +c;

var concatSpan = concat.ToReadOnlySequence();
var concatSpan2 = concat.ToReadOnlySequence();
// not compact, returns a new span
(concatSpan.ToArray().SequenceEqual(concatSpan2.ToArray())).Should().BeTrue();

// compact, will return same span
var compacted = concat.Compact();
var concatSpan3 = compacted.ToReadOnlySequence();
(concatSpan.ToArray().SequenceEqual(concatSpan3.ToArray())).Should().BeTrue();

(concatSpan.ToArray().SequenceEqual(compacted.ToArray())).Should().BeTrue();
}

[Fact]
public void A_ByteString_must_have_correct_size_when_slicing_from_index()
Expand Down
62 changes: 62 additions & 0 deletions src/core/Akka/Util/ByteString.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
//-----------------------------------------------------------------------

using System;
using System.Buffers;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
Expand All @@ -28,6 +29,40 @@ 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);
var prior = first;
for (int i = 1; i < bArr.Length; i++)
{
var item = bArr[i];
var curr = new ByteStringReadOnlySequenceSegment(item, prior.RunningIndex + prior.Memory.Length);
prior.Next = curr;
prior = curr;
}

return new ReadOnlySequence<byte>(first, 0, prior, prior.Memory.Length);
}
}

#region creation methods

/// <summary>
Expand Down Expand Up @@ -528,6 +563,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
Expand Down
Loading