Skip to content

Commit

Permalink
Reversed elements in a faster way.
Browse files Browse the repository at this point in the history
  • Loading branch information
MeltyPlayer committed Jul 9, 2024
1 parent 1804b7d commit 9a45e71
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 6 deletions.
3 changes: 2 additions & 1 deletion Benchmarks/Benchmarks.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="BenchmarkDotNet" Version="0.13.8" />
<PackageReference Include="BenchmarkDotNet" Version="0.13.12" />
<PackageReference Include="CommunityToolkit.HighPerformance" Version="8.2.0" />
<PackageReference Include="ZString" Version="2.5.1" />
</ItemGroup>

Expand Down
4 changes: 3 additions & 1 deletion Benchmarks/Main.cs
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
using BenchmarkDotNet.Configs;
using BenchmarkDotNet.Diagnosers;
using BenchmarkDotNet.Running;

namespace benchmarks {
public class Program {
public static void Main(string[] args) {
var summary = BenchmarkRunner.Run<StringBuilderComparison>(
var summary = BenchmarkRunner.Run<ReadingValues>(

Check failure on line 8 in Benchmarks/Main.cs

View workflow job for this annotation

GitHub Actions / build

The type or namespace name 'ReadingValues' could not be found (are you missing a using directive or an assembly reference?)

Check failure on line 8 in Benchmarks/Main.cs

View workflow job for this annotation

GitHub Actions / build

The type or namespace name 'ReadingValues' could not be found (are you missing a using directive or an assembly reference?)
ManualConfig
.Create(DefaultConfig.Instance)
.AddDiagnoser(new MemoryDiagnoser(new MemoryDiagnoserConfig(true)))
.WithOptions(ConfigOptions.DisableOptimizationsValidator));
}
}
Expand Down
3 changes: 2 additions & 1 deletion Schema/Schema.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,8 @@

<ItemGroup>
<!-- Generator dependencies -->
<PackageReference Include="CommunityToolkit.HighPerformance" Version="8.2.0" GeneratePathProperty="true" PrivateAssets="all" />
<PackageReference Include="CommunityToolkit.HighPerformance" Version="8.2.2" GeneratePathProperty="true" PrivateAssets="all" />
<PackageReference Include="Tedd.BitUtils" Version="1.0.7" />
</ItemGroup>

<PropertyGroup>
Expand Down
33 changes: 30 additions & 3 deletions Schema/src/binary/reader/EndianBinaryBufferedStream.cs
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
using System;
using System.Buffers.Binary;
using System.Runtime.CompilerServices;

using CommunityToolkit.HighPerformance;

using schema.src.util;
using schema.util.streams;

using Tedd;

namespace schema.binary {
public interface ISpanElementReverser {
void Reverse(Span<byte> span);
Expand All @@ -15,9 +18,33 @@ public interface ISpanElementReverser {
public class SpanElementReverser : ISpanElementReverser {
public void Reverse(Span<byte> span) => span.Reverse();

public void ReverseElements(Span<byte> span, int stride) {
for (var i = 0; i < span.Length; i += stride) {
span.Slice(i, stride).Reverse();
public void ReverseElements(Span<byte> bytes, int stride) {
if (stride == 2) {
var shorts = bytes.Cast<byte, short>();
for (var i = 0; i < shorts.Length; ++i) {
shorts[i].ReverseEndianness();
}
return;
}

if (stride == 4) {
var ints = bytes.Cast<byte, int>();
for (var i = 0; i < ints.Length; ++i) {
ints[i].ReverseEndianness();
}
return;
}

if (stride == 8) {
var longs = bytes.Cast<byte, long>();
for (var i = 0; i < longs.Length; ++i) {
longs[i].ReverseEndianness();
}
return;
}

for (var i = 0; i < bytes.Length; i += stride) {
bytes.Slice(i, stride).Reverse();
}
}
}
Expand Down

0 comments on commit 9a45e71

Please sign in to comment.