Change Apple RLE algorithm for a faster version.

This commit is contained in:
2021-10-17 01:12:06 +01:00
parent 3e30153b6c
commit bc2a7acea5
4 changed files with 77 additions and 81 deletions

View File

@@ -31,8 +31,6 @@
// Copyright © 2018-2019 David Ryskalczyk // Copyright © 2018-2019 David Ryskalczyk
// ****************************************************************************/ // ****************************************************************************/
using System.IO;
namespace Aaru6.Compression namespace Aaru6.Compression
{ {
/// <summary>Implements the Apple version of RLE</summary> /// <summary>Implements the Apple version of RLE</summary>
@@ -40,63 +38,57 @@ namespace Aaru6.Compression
{ {
const uint DART_CHUNK = 20960; const uint DART_CHUNK = 20960;
readonly Stream _inStream; /// <summary>Decodes a buffer compressed with Apple RLE</summary>
int _count; /// <param name="source">Encoded buffer</param>
bool _nextA; // true if A, false if B /// <param name="destination">Buffer where to write the decoded data</param>
byte _repeatedByteA, _repeatedByteB; /// <returns>The number of decoded bytes</returns>
bool _repeatMode; // true if we're repeating, false if we're just copying public static int DecodeBuffer(byte[] source, byte[] destination)
/// <summary>Initializes a decompressor for the specified stream</summary>
/// <param name="stream">Stream containing the compressed data</param>
public AppleRle(Stream stream)
{ {
_inStream = stream; int count = 0;
Reset(); bool nextA = true; // true if A, false if B
byte repeatedByteA = 0, repeatedByteB = 0;
bool repeatMode = false; // true if we're repeating, false if we're just copying
int inPosition = 0, outPosition = 0;
while(inPosition <= source.Length &&
outPosition <= destination.Length)
{
switch(repeatMode)
{
case true when count > 0:
{
count--;
if(nextA)
{
nextA = false;
destination[outPosition++] = repeatedByteA;
continue;
} }
void Reset() nextA = true;
{
_repeatedByteA = _repeatedByteB = 0; destination[outPosition++] = repeatedByteB;
_count = 0;
_nextA = true; continue;
_repeatMode = false; }
case false when count > 0:
count--;
destination[outPosition++] = source[inPosition++];
continue;
} }
/// <summary>Decompresses a byte</summary> if(inPosition == source.Length)
/// <returns>Decompressed byte</returns> break;
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) while(true)
{ {
byte b1 = (byte)_inStream.ReadByte(); byte b1 = source[inPosition++];
byte b2 = (byte)_inStream.ReadByte(); byte b2 = source[inPosition++];
short s = (short)((b1 << 8) | b2); short s = (short)((b1 << 8) | b2);
if(s == 0 || if(s == 0 ||
@@ -106,23 +98,27 @@ namespace Aaru6.Compression
if(s < 0) if(s < 0)
{ {
_repeatMode = true; repeatMode = true;
_repeatedByteA = (byte)_inStream.ReadByte(); repeatedByteA = source[inPosition++];
_repeatedByteB = (byte)_inStream.ReadByte(); repeatedByteB = source[inPosition++];
_count = (-s * 2) - 1; count = (-s * 2) - 1;
_nextA = false; nextA = false;
return _repeatedByteA; destination[outPosition++] = repeatedByteA;
break;
} }
if(s <= 0) repeatMode = false;
continue; count = (s * 2) - 1;
_repeatMode = false; destination[outPosition++] = source[inPosition++];
_count = (s * 2) - 1;
return _inStream.ReadByte(); break;
} }
}
return outPosition;
} }
} }
} }

View File

@@ -12,7 +12,7 @@ namespace AaruBenchmark
public void Aaru() => Compression.Aaru.AppleRle(); public void Aaru() => Compression.Aaru.AppleRle();
[Benchmark] [Benchmark]
public void Aaru6() => Compression.Aaru6.AppleRle(); public void Aaru6() => Aaru6Compressions.AppleRle();
} }
[SimpleJob(RuntimeMoniker.Net60)] [SimpleJob(RuntimeMoniker.Net60)]
@@ -22,7 +22,7 @@ namespace AaruBenchmark
public void Aaru() => Compression.Aaru.TeleDiskLzh(); public void Aaru() => Compression.Aaru.TeleDiskLzh();
[Benchmark] [Benchmark]
public void Aaru6() => Compression.Aaru6.TeleDiskLzh(); public void Aaru6() => Aaru6Compressions.TeleDiskLzh();
} }
[SimpleJob(RuntimeMoniker.Net60)] [SimpleJob(RuntimeMoniker.Net60)]

View File

@@ -4,11 +4,11 @@ using Aaru6.Compression;
namespace AaruBenchmark.Compression namespace AaruBenchmark.Compression
{ {
public class Aaru6 public class Aaru6Compressions
{ {
public static void AppleRle() public static void AppleRle()
{ {
const int bufferSize = 20960; const int bufferSize = 32768;
byte[] input = new byte[1102]; byte[] input = new byte[1102];
var fs = new FileStream(Path.Combine(Program.Folder, "apple_rle.bin"), FileMode.Open, FileAccess.Read); var fs = new FileStream(Path.Combine(Program.Folder, "apple_rle.bin"), FileMode.Open, FileAccess.Read);
@@ -19,12 +19,12 @@ namespace AaruBenchmark.Compression
byte[] output = new byte[bufferSize]; byte[] output = new byte[bufferSize];
var rle = new AppleRle(new MemoryStream(input)); int realSize = Aaru6.Compression.AppleRle.DecodeBuffer(input, output);
for(int i = 0; i < bufferSize; i++) if(realSize != 20960)
output[i] = (byte)rle.ProduceByte(); throw new InvalidDataException("Incorrect decompressed size");
string crc = Crc32Context.Data(output, out _); string crc = Crc32Context.Data(output, (uint)realSize, out _);
if(crc != "3525ef06") if(crc != "3525ef06")
throw new InvalidDataException("Incorrect decompressed checksum"); throw new InvalidDataException("Incorrect decompressed checksum");

Binary file not shown.