Add benchmarks for Apple RLE decompression.

This commit is contained in:
2021-10-14 03:12:02 +01:00
parent 2376652e88
commit 7446741746
10 changed files with 228 additions and 0 deletions

View File

@@ -0,0 +1,9 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
</Project>

View File

@@ -0,0 +1,128 @@
// /***************************************************************************
// Aaru Data Preservation Suite
// ----------------------------------------------------------------------------
//
// Filename : AppleRle.cs
// Author(s) : Natalia Portillo <claunia@claunia.com>
//
// Component : Compression algorithms.
//
// --[ Description ] ----------------------------------------------------------
//
// Decompress Apple variant of RLE.
//
// --[ License ] --------------------------------------------------------------
//
// This library is free software; you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as
// published by the Free Software Foundation; either version 2.1 of the
// License, or (at your option) any later version.
//
// This library is distributed in the hope that it will be useful, but
// WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public
// License along with this library; if not, see <http://www.gnu.org/licenses/>.
//
// ----------------------------------------------------------------------------
// Copyright © 2011-2021 Natalia Portillo
// Copyright © 2018-2019 David Ryskalczyk
// ****************************************************************************/
using System.IO;
namespace Aaru6.Compression
{
/// <summary>Implements the Apple version of RLE</summary>
public class AppleRle
{
const uint DART_CHUNK = 20960;
readonly Stream _inStream;
int _count;
bool _nextA; // true if A, false if B
byte _repeatedByteA, _repeatedByteB;
bool _repeatMode; // true if we're repeating, false if we're just copying
/// <summary>Initializes a decompressor for the specified stream</summary>
/// <param name="stream">Stream containing the compressed data</param>
public AppleRle(Stream stream)
{
_inStream = stream;
Reset();
}
void Reset()
{
_repeatedByteA = _repeatedByteB = 0;
_count = 0;
_nextA = true;
_repeatMode = false;
}
/// <summary>Decompresses a byte</summary>
/// <returns>Decompressed byte</returns>
public int ProduceByte()
{
if(_repeatMode && _count > 0)
{
_count--;
if(_nextA)
{
_nextA = false;
return _repeatedByteA;
}
_nextA = true;
return _repeatedByteB;
}
if(!_repeatMode &&
_count > 0)
{
_count--;
return _inStream.ReadByte();
}
if(_inStream.Position == _inStream.Length)
return -1;
while(true)
{
byte b1 = (byte)_inStream.ReadByte();
byte b2 = (byte)_inStream.ReadByte();
short s = (short)((b1 << 8) | b2);
if(s == 0 ||
s >= DART_CHUNK ||
s <= -DART_CHUNK)
continue;
if(s < 0)
{
_repeatMode = true;
_repeatedByteA = (byte)_inStream.ReadByte();
_repeatedByteB = (byte)_inStream.ReadByte();
_count = (-s * 2) - 1;
_nextA = false;
return _repeatedByteA;
}
if(s <= 0)
continue;
_repeatMode = false;
_count = (s * 2) - 1;
return _inStream.ReadByte();
}
}
}
}

View File

@@ -4,6 +4,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "AaruBenchmark", "AaruBenchm
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Aaru6.Checksums", "Aaru6.Checksums\Aaru6.Checksums.csproj", "{CC48B324-A532-4A45-87A6-6F91F7141E8D}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Aaru6.Compression", "Aaru6.Compression\Aaru6.Compression.csproj", "{7B9BAFE0-E9D9-4EF4-B009-007F727DAB68}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
@@ -18,5 +20,9 @@ Global
{CC48B324-A532-4A45-87A6-6F91F7141E8D}.Debug|Any CPU.Build.0 = Debug|Any CPU
{CC48B324-A532-4A45-87A6-6F91F7141E8D}.Release|Any CPU.ActiveCfg = Release|Any CPU
{CC48B324-A532-4A45-87A6-6F91F7141E8D}.Release|Any CPU.Build.0 = Release|Any CPU
{7B9BAFE0-E9D9-4EF4-B009-007F727DAB68}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{7B9BAFE0-E9D9-4EF4-B009-007F727DAB68}.Debug|Any CPU.Build.0 = Debug|Any CPU
{7B9BAFE0-E9D9-4EF4-B009-007F727DAB68}.Release|Any CPU.ActiveCfg = Release|Any CPU
{7B9BAFE0-E9D9-4EF4-B009-007F727DAB68}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
EndGlobal

View File

@@ -7,6 +7,7 @@
<ItemGroup>
<PackageReference Include="Aaru.Checksums" Version="5.3.0"/>
<PackageReference Include="Aaru.Compression" Version="5.3.0"/>
<PackageReference Include="BenchmarkDotNet" Version="0.13.1"/>
<PackageReference Include="DotNetZip" Version="1.15.0"/>
<PackageReference Include="SharpCompress" Version="0.29.0"/>
@@ -25,10 +26,15 @@
<Content Include="data\bzip2.bz2">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</Content>
<None Remove="data\apple_rle.bin"/>
<Content Include="data\apple_rle.bin">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</Content>
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\Aaru6.Checksums\Aaru6.Checksums.csproj"/>
<ProjectReference Include="..\Aaru6.Compression\Aaru6.Compression.csproj"/>
</ItemGroup>
<ItemGroup>

View File

@@ -5,6 +5,16 @@ using BenchmarkDotNet.Jobs;
namespace AaruBenchmark
{
[SimpleJob(RuntimeMoniker.Net60)]
public class AppleRleBenchs
{
[Benchmark(Baseline = true)]
public void Aaru() => Compression.Aaru.AppleRle();
[Benchmark]
public void Aaru6() => Compression.Aaru6.AppleRle();
}
[SimpleJob(RuntimeMoniker.Net60)]
public class GzipBenchs
{

View File

@@ -0,0 +1,34 @@
using System.IO;
using Aaru.Compression;
using Aaru6.Checksums;
namespace AaruBenchmark.Compression
{
public class Aaru
{
const int BUFFER_SIZE = 20960;
public static void AppleRle()
{
byte[] input = new byte[1102];
var fs = new FileStream(Path.Combine(Program.Folder, "apple_rle.bin"), FileMode.Open, FileAccess.Read);
fs.Read(input, 0, input.Length);
fs.Close();
fs.Dispose();
byte[] output = new byte[BUFFER_SIZE];
var rle = new AppleRle(new MemoryStream(input));
for(int i = 0; i < BUFFER_SIZE; i++)
output[i] = (byte)rle.ProduceByte();
string crc = Crc32Context.Data(output, out _);
if(crc != "3525ef06")
throw new InvalidDataException("Incorrect decompressed checksum");
}
}
}

View File

@@ -0,0 +1,34 @@
using System.IO;
using Aaru6.Checksums;
using Aaru6.Compression;
namespace AaruBenchmark.Compression
{
public class Aaru6
{
const int BUFFER_SIZE = 20960;
public static void AppleRle()
{
byte[] input = new byte[1102];
var fs = new FileStream(Path.Combine(Program.Folder, "apple_rle.bin"), FileMode.Open, FileAccess.Read);
fs.Read(input, 0, input.Length);
fs.Close();
fs.Dispose();
byte[] output = new byte[BUFFER_SIZE];
var rle = new AppleRle(new MemoryStream(input));
for(int i = 0; i < BUFFER_SIZE; i++)
output[i] = (byte)rle.ProduceByte();
string crc = Crc32Context.Data(output, out _);
if(crc != "3525ef06")
throw new InvalidDataException("Incorrect decompressed checksum");
}
}
}

View File

@@ -13,6 +13,7 @@ namespace AaruBenchmark
{
var config = ManualConfig.Create(DefaultConfig.Instance);
BenchmarkRunner.Run<AppleRleBenchs>(config);
BenchmarkRunner.Run<GzipBenchs>(config);
BenchmarkRunner.Run<Bzip2Benchs>(config);
BenchmarkRunner.Run<Adler32Benchs>(config);

Binary file not shown.

Binary file not shown.