This is an automated email from the ASF dual-hosted git repository.
blankensteiner pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/pulsar-dotpulsar.git
The following commit(s) were added to refs/heads/master by this push:
new 20f46b3 Fix performance issue with ReadUint32 (#102)
20f46b3 is described below
commit 20f46b30bfbd1d07cd7dd3cc0f687f6478393ba3
Author: Kristian Andersen <[email protected]>
AuthorDate: Wed Apr 20 09:03:40 2022 +0200
Fix performance issue with ReadUint32 (#102)
* Fix performance issue with ReadUint32
The loop would not correctly break after reading 4 bytes.
* Unit tests for these cases were already present.
---
src/DotPulsar/Internal/Extensions/ReadOnlySequenceExtensions.cs | 8 ++++----
.../Internal/Extensions/ReadOnlySequenceExtensionsTests.cs | 8 ++++----
2 files changed, 8 insertions(+), 8 deletions(-)
diff --git a/src/DotPulsar/Internal/Extensions/ReadOnlySequenceExtensions.cs
b/src/DotPulsar/Internal/Extensions/ReadOnlySequenceExtensions.cs
index 6f66461..0ed4613 100644
--- a/src/DotPulsar/Internal/Extensions/ReadOnlySequenceExtensions.cs
+++ b/src/DotPulsar/Internal/Extensions/ReadOnlySequenceExtensions.cs
@@ -65,22 +65,22 @@ public static class ReadOnlySequenceExtensions
var span = memory.Span;
- for (var i = (int) start; i < span.Length; ++i, ++read)
+ for (var i = (int) start; i < span.Length && read < 4; ++i, ++read)
{
switch (read)
{
case 0:
if (reverse) union.B0 = span[i];
else union.B3 = span[i];
- continue;
+ break;
case 1:
if (reverse) union.B1 = span[i];
else union.B2 = span[i];
- continue;
+ break;
case 2:
if (reverse) union.B2 = span[i];
else union.B1 = span[i];
- continue;
+ break;
case 3:
if (reverse) union.B3 = span[i];
else union.B0 = span[i];
diff --git
a/tests/DotPulsar.Tests/Internal/Extensions/ReadOnlySequenceExtensionsTests.cs
b/tests/DotPulsar.Tests/Internal/Extensions/ReadOnlySequenceExtensionsTests.cs
index ef128e5..7ae3fb6 100644
---
a/tests/DotPulsar.Tests/Internal/Extensions/ReadOnlySequenceExtensionsTests.cs
+++
b/tests/DotPulsar.Tests/Internal/Extensions/ReadOnlySequenceExtensionsTests.cs
@@ -101,7 +101,7 @@ public class ReadOnlySequenceExtensionsTests
}
[Fact]
- public void
ReadUInt32_GivenSequenceWithSingleSegment_ShouldGiveExceptedResult()
+ public void
ReadUInt32_GivenSequenceWithSingleSegment_ShouldGiveExpectedResult()
{
//Arrange
var sequence = new SequenceBuilder<byte>().Append(new byte[] { 0x00,
0x01, 0x02, 0x03 }).Build();
@@ -115,7 +115,7 @@ public class ReadOnlySequenceExtensionsTests
}
[Fact]
- public void
ReadUInt32_GivenSequenceWithSingleSegmentAndNonZeroStart_ShouldGiveExceptedResult()
+ public void
ReadUInt32_GivenSequenceWithSingleSegmentAndNonZeroStart_ShouldGiveExpectedResult()
{
//Arrange
var sequence = new SequenceBuilder<byte>().Append(new byte[] { 0x09,
0x00, 0x01, 0x02, 0x03 }).Build();
@@ -146,7 +146,7 @@ public class ReadOnlySequenceExtensionsTests
[InlineData(new byte[] { 0x02 }, new byte[] { 0x03, 0x04, 0x05 }, new
byte[] { 0x09 })]
[InlineData(new byte[] { 0x02 }, new byte[] { 0x03 }, new byte[] { 0x04,
0x05 }, new byte[] { 0x09 })]
#pragma warning restore xUnit1025 // InlineData should be unique within the
Theory it belongs to
- public void
ReadUInt32_GivenSequenceWithMultipleSegments_ShouldGiveExceptedResult(params
byte[][] testPath)
+ public void
ReadUInt32_GivenSequenceWithMultipleSegments_ShouldGiveExpectedResult(params
byte[][] testPath)
{
//Arrange
var sequenceBuilder = new SequenceBuilder<byte>();
@@ -166,7 +166,7 @@ public class ReadOnlySequenceExtensionsTests
[InlineData(2, new byte[] { 0x09, 0x09, 0x02 }, new byte[] { 0x03, 0x04,
0x05 }, new byte[] { 0x09, 0x09, 0x09 })]
[InlineData(3, new byte[] { 0x09, 0x09, 0x09 }, new byte[] { 0x02, 0x03,
0x04 }, new byte[] { 0x05, 0x09, 0x09 })]
[InlineData(4, new byte[] { 0x09, 0x09, 0x09 }, new byte[] { 0x09, 0x02,
0x03 }, new byte[] { 0x04, 0x05, 0x09 })]
- public void
ReadUInt32_GivenSequenceWithMultipleSegmentsAndNonZeroStart_ShouldGiveExceptedResult(long
start, params byte[][] testPath)
+ public void
ReadUInt32_GivenSequenceWithMultipleSegmentsAndNonZeroStart_ShouldGiveExpectedResult(long
start, params byte[][] testPath)
{
//Arrange
var sequenceBuilder = new SequenceBuilder<byte>();