Skip to content
Merged
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
37 changes: 29 additions & 8 deletions src/Filee.Engines/Archives/Bzip2BlockEncoder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@
// bits or less, which every bzip2 decoder takes. The result is a bit string (Bzip2Bits) that starts with the block
// magic; blocks are not byte aligned, so the writer joins them bit by bit.

using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Runtime.Intrinsics;

namespace Filee.Engines.Archives;

/// <summary>Encodes bzip2 blocks; reusable, one block at a time.</summary>
Expand All @@ -28,6 +32,10 @@ internal sealed class Bzip2BlockEncoder
private readonly uint[][] _codes = CreateTables<uint>();
private readonly int[][] _tableFrequencies = CreateTables<int>();

// Per symbol, its code length in each table side by side (eight lanes, the unused ones too dear to be chosen), so
// a group's cost in every table is one vector sum.
private readonly Vector128<ushort>[] _packedLengths = new Vector128<ushort>[MaxAlphabet];

/// <summary>The last block's bits, from its magic to its last Huffman code.</summary>
public Bzip2Bits Output { get; } = new();

Expand Down Expand Up @@ -231,28 +239,41 @@ private void ChooseTables(int count, int alphabet, int tables, int selectorCount
remaining -= sum;
}

Span<int> cost = stackalloc int[MaxTables];
Span<ushort> lanes = stackalloc ushort[Vector128<ushort>.Count];
var symbols = _symbols;
if (count > symbols.Length)
throw new ArgumentOutOfRangeException(nameof(count));
ref var symbol0 = ref MemoryMarshal.GetArrayDataReference(symbols);
ref var packed = ref MemoryMarshal.GetArrayDataReference(_packedLengths);
for (var pass = 0; pass < passes; pass++)
{
for (var t = 0; t < tables; t++)
Array.Clear(_tableFrequencies[t], 0, alphabet);
for (var v = 0; v < alphabet; v++)
{
// A group holds at most 50 symbols of at most 17 bits: 255 a lane never wins and never overflows.
lanes.Fill(255);
for (var t = 0; t < tables; t++)
lanes[t] = _lengths[t][v];
_packedLengths[v] = Vector128.Create<ushort>(lanes);
}
for (var g = 0; g < selectorCount; g++)
{
var start = g * GroupSize;
var end = Math.Min(count, start + GroupSize);
cost.Clear();
var cost = Vector128<ushort>.Zero;
for (var i = start; i < end; i++)
{
var symbol = symbols[i];
for (var t = 0; t < tables; t++)
cost[t] += _lengths[t][symbol];
}
cost += Unsafe.Add(ref packed, Unsafe.Add(ref symbol0, i)); // symbols are below the alphabet
var best = 0;
var bestCost = cost.GetElement(0);
for (var t = 1; t < tables; t++)
{
if (cost[t] < cost[best])
var c = cost.GetElement(t);
if (c < bestCost)
{
best = t;
bestCost = c;
}
}
_selectors[g] = (byte)best;
var chosen = _tableFrequencies[best];
Expand Down
6 changes: 3 additions & 3 deletions src/Filee.Engines/Archives/Bzip2Writer.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// Writes .bz2 files with Filee's own encoder, from the bzip2 format: the "BZh9" stream header, blocks of at most
// 900,000 bytes, and the end marker with the CRC of all block CRCs. The input goes through bzip2's first run-length
// step here (runs of 4 to 255 equal bytes become 4 bytes and a count) while the block CRC is taken over the bytes as
// they came; full blocks are encoded (Bzip2BlockEncoder) on up to four threads at once, and written in order. Memory
// they came; full blocks are encoded (Bzip2BlockEncoder) on up to eight threads at once, and written in order. Memory
// stays bounded by the number of blocks in flight (about 20 MB each while sorting). It is a write-only stream, so a TAR
// can be compressed while it is written (.tar.bz2) without a temporary file.

Expand Down Expand Up @@ -45,8 +45,8 @@ private sealed class Slot
public Task? Work { get; set; }
}

/// <summary>Blocks encoded at once by default: a few cores, leaving the rest to the machine.</summary>
internal static int DefaultThreads => Math.Clamp(Environment.ProcessorCount / 2, 1, 4);
/// <summary>Blocks encoded at once by default: one per processor, at most eight (about 20 MB each while sorting).</summary>
internal static int DefaultThreads => Math.Clamp(Environment.ProcessorCount, 1, 8);

/// <param name="threads">Blocks encoded at once (1 encodes on the writing thread).</param>
public Bzip2Writer(Stream output, ArchiveLevel level, CancellationToken cancellationToken = default, bool leaveOpen = false,
Expand Down
3 changes: 2 additions & 1 deletion src/Filee.Engines/Archives/CompressedFile.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,8 @@ public static void Decompress(string archive, string format, string outputFile,
try
{
long declared = -1;
using (var decoder = Open(format, input, name, ref declared))
// Decoded on a thread of its own while this one writes the file.
using (var decoder = ReadAheadStream.Wrap(Open(format, input, name, ref declared), declared, cancellationToken))
using (var output = new FileStream(outputFile, FileMode.CreateNew, FileAccess.Write, FileShare.None, 1 << 16))
{
// Past the room on the drive is "too large"; past a header's size (within that room) is damage.
Expand Down
135 changes: 110 additions & 25 deletions src/Filee.Engines/Archives/Lzma/Lzma2Parallel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@
// What a segment decodes to is held in memory until it is read, so the threads and the size of a segment are
// bounded: at most one segment per processor, all of them in an eighth of the memory, none past 512 MiB. A segment
// that fails on its thread is decoded again, from its start, on the reader's thread as it is read: so damaged data
// gives the very bytes and the very error, at the same place, as the one-thread decoder.
// gives the very bytes and the very error, at the same place, as the one-thread decoder. Workers hand out what they
// decoded as they go (Decoded.Publish), so the segment being read streams out while the next ones decode; after a
// failure the reader's own decoder skips what was handed out already.

using System.Runtime.ExceptionServices;

Expand Down Expand Up @@ -158,39 +160,111 @@ public static int Workers(long unpacked, long packed, uint dictionary)
return segments;
}

/// <summary>What a worker decoded: the output, how much of it is good, and the error that stopped it, if any.</summary>
/// <summary>
/// What a worker decodes: the output, how much of it is good, and the error that stopped it, if any. The worker
/// publishes the output as it goes, so the reader hands out the start of a segment while the rest is decoded.
/// </summary>
internal sealed class Decoded(byte[] output)
{
/// <summary>The worker publishes after every this many bytes.</summary>
public const int Step = 1 << 20;

private readonly object _gate = new();
private int _ready;
private bool _done;

public byte[] Output { get; } = output;

/// <summary>Bytes the worker decoded so far (its own count; readers go by <see cref="WaitBeyond"/>).</summary>
public int Count { get; set; }

public ExceptionDispatchInfo? Error { get; set; }

/// <summary>Called by the worker: the first <see cref="Count"/> bytes of the output are final.</summary>
public void Publish()
{
lock (_gate)
{
_ready = Count;
Monitor.PulseAll(_gate);
}
}

/// <summary>Called by the worker when it stops, with <see cref="Error"/> set if it failed.</summary>
public void Finish()
{
lock (_gate)
{
if (Error is null)
_ready = Count;
_done = true;
Monitor.PulseAll(_gate);
}
}

/// <summary>
/// Waits until more than <paramref name="position"/> bytes are published or the worker stopped; returns the
/// bytes published (all that will be when <paramref name="done"/>).
/// </summary>
public int WaitBeyond(int position, out bool done)
{
lock (_gate)
{
while (_ready <= position && !_done)
Monitor.Wait(_gate);
done = _done;
return _ready;
}
}
}

/// <summary>
/// Decodes one segment: <paramref name="length"/> bytes of chunks followed by the end byte (the caller puts it at
/// <c>packed[length]</c>), expected to give <paramref name="unpacked"/> bytes.
/// Decodes one segment into <paramref name="result"/>: <paramref name="length"/> bytes of chunks followed by the
/// end byte (the caller puts it at <c>packed[length]</c>), expected to give <paramref name="unpacked"/> bytes.
/// </summary>
public static Decoded DecodeSegment(byte[] packed, int length, uint dictionary, int unpacked, CancellationToken cancellationToken)
public static void DecodeSegment(Decoded result, byte[] packed, int length, uint dictionary, int unpacked, CancellationToken cancellationToken)
{
var result = new Decoded(GC.AllocateUninitializedArray<byte>(unpacked));
try
{
var reader = new Lzma2Reader(new ByteInput(new MemoryStream(packed, 0, length + 1, writable: false)), dictionary, unpacked,
cancellationToken);
var output = result.Output;
while (result.Count < unpacked)
{
var read = reader.Read(output.AsSpan(result.Count));
var read = reader.Read(output.AsSpan(result.Count, Math.Min(Decoded.Step, unpacked - result.Count)));
if (read == 0)
throw new InvalidDataException("the LZMA2 chunk is damaged");
result.Count += read;
result.Publish();
}
}
catch (Exception ex)
{
result.Error = ExceptionDispatchInfo.Capture(ex);
}
return result;
finally
{
result.Finish();
}
}

/// <summary>
/// Reads and drops <paramref name="count"/> bytes of <paramref name="reader"/>: data decoded again after a worker
/// failed, up to where the reader had got. <paramref name="seen"/> gets them on the way.
/// </summary>
public static void Skip(Lzma2Reader reader, long count, Action<ReadOnlySpan<byte>>? seen = null)
{
if (count <= 0)
return;
var scratch = new byte[Math.Min(count, 1 << 16)];
while (count > 0)
{
var read = reader.Read(scratch.AsSpan(0, (int)Math.Min(count, scratch.Length)));
if (read == 0)
return;
seen?.Invoke(scratch.AsSpan(0, read));
count -= read;
}
}

/// <summary>The segments of a 7z LZMA2 coder, decoded ahead on worker threads and read in order.</summary>
Expand All @@ -201,10 +275,11 @@ private sealed class SegmentStream : Stream
private readonly uint _dictionary;
private readonly int _workers;
private readonly CancellationTokenSource _cancel;
private readonly Queue<(Segment Segment, Task<Decoded> Work)> _ahead = new();
private readonly Queue<(Segment Segment, Decoded Work)> _ahead = new();
private int _next;
private Decoded? _current;
private (Segment Segment, Decoded Work)? _current;
private int _position;
private int _ready;
private Lzma2Reader? _replay; // a failed segment, decoded again here

public SegmentStream(PackedWindow packed, List<Segment> segments, uint dictionary, int workers, CancellationToken cancellationToken)
Expand Down Expand Up @@ -234,29 +309,34 @@ public override int Read(Span<byte> buffer)
}
if (_current is { } current)
{
if (_position < current.Count)
if (_position < _ready)
{
var count = Math.Min(buffer.Length, current.Count - _position);
current.Output.AsSpan(_position, count).CopyTo(buffer);
var count = Math.Min(buffer.Length, _ready - _position);
current.Work.Output.AsSpan(_position, count).CopyTo(buffer);
_position += count;
return count;
}
_ready = current.Work.WaitBeyond(_position, out var done);
if (_position < _ready || !done)
continue;
_current = null;
if (current.Work.Error is { } error)
{
// Decoded again here, as the one-thread reader reads it, from where the worker's output ran out.
if (error.SourceException is OperationCanceledException)
error.Throw();
_replay = Open(current.Segment, _cancel.Token);
Skip(_replay, _position);
}
continue;
}
_cancel.Token.ThrowIfCancellationRequested();
Start();
if (!_ahead.TryDequeue(out var next))
return 0;
var decoded = next.Work.GetAwaiter().GetResult();
if (decoded.Error is { } error)
{
if (error.SourceException is OperationCanceledException)
error.Throw();
_replay = Open(next.Segment, _cancel.Token);
continue;
}
_current = decoded;
_current = next;
_position = 0;
_ready = 0;
}
}

Expand All @@ -269,7 +349,9 @@ private void Start()
var packed = ReadPacked(segment);
var dictionary = _dictionary;
var token = _cancel.Token;
_ahead.Enqueue((segment, Task.Run(() => DecodeSegment(packed, segment.Length, dictionary, segment.Unpacked, token), CancellationToken.None)));
var decoded = new Decoded(GC.AllocateUninitializedArray<byte>(segment.Unpacked));
_ = Task.Run(() => DecodeSegment(decoded, packed, segment.Length, dictionary, segment.Unpacked, token), CancellationToken.None);
_ahead.Enqueue((segment, decoded));
}
}

Expand Down Expand Up @@ -321,7 +403,10 @@ public int ReadAt(long at, Span<byte> target)
return 0;
if (target.Length > length - at)
target = target[..(int)(length - at)];
shared.Position = offset + at;
return shared.ReadAtLeast(target, target.Length, throwOnEndOfStream: false);
lock (shared) // as SharedWindowStream: other coders of the folder may read on other threads
{
shared.Position = offset + at;
return shared.ReadAtLeast(target, target.Length, throwOnEndOfStream: false);
}
}
}
Loading
Loading