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
4 changes: 4 additions & 0 deletions src/Filee.Engines/Archives/Bzip2BlockEncoder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ private static T[][] CreateTables<T>()
/// <param name="block">The block after the first run-length step (1 … 900,000 bytes).</param>
/// <param name="crc">The block CRC of the bytes before that step.</param>
/// <param name="passes">How often the Huffman tables are refined (1 or more).</param>
[MethodImpl(MethodImplOptions.AggressiveOptimization)]
public void Encode(ReadOnlySpan<byte> block, uint crc, int passes, CancellationToken cancellationToken)
{
var n = block.Length;
Expand Down Expand Up @@ -170,6 +171,7 @@ public void Encode(ReadOnlySpan<byte> block, uint crc, int passes, CancellationT
/// bijective base 2 with RUNA (1) and RUNB (2), lowest digit first; position j is symbol j + 1. Ends with the
/// end-of-block symbol. Returns the number of symbols.
/// </summary>
[MethodImpl(MethodImplOptions.AggressiveOptimization)]
private int MoveToFront(ReadOnlySpan<byte> bwt, ReadOnlySpan<byte> number, int inUse, int endOfBlock, Span<int> frequencies)
{
Span<byte> list = stackalloc byte[256];
Expand Down Expand Up @@ -220,6 +222,7 @@ private int WriteRun(int run, int count, Span<int> frequencies)
}

/// <summary>Picks the selectors and the tables' code lengths.</summary>
[MethodImpl(MethodImplOptions.AggressiveOptimization)]
private void ChooseTables(int count, int alphabet, int tables, int selectorCount, ReadOnlySpan<int> frequencies, int passes)
{
// Start: table t is cheap for one slice of the symbols (by number), dear for the rest.
Expand Down Expand Up @@ -308,6 +311,7 @@ internal static class Bzip2Huffman
/// Code lengths (1 … <paramref name="maxLength"/>) for <paramref name="frequencies"/>; every symbol gets one, unused
/// ones as if seen once. A tree deeper than the limit is built again from flattened frequencies.
/// </summary>
[MethodImpl(MethodImplOptions.AggressiveOptimization)]
public static void Lengths(ReadOnlySpan<int> frequencies, Span<byte> lengths, int maxLength)
{
var n = frequencies.Length;
Expand Down
9 changes: 9 additions & 0 deletions src/Filee.Engines/Archives/Bzip2Sort.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,21 @@
// Equal rows (a periodic block) may come in any order among themselves; the transform is the same.

using System.Buffers;
using System.Runtime.CompilerServices;

namespace Filee.Engines.Archives;

/// <summary>Sorts the rotations of a block and writes its Burrows-Wheeler transform.</summary>
internal static class Bzip2Sort
{
// The sort's methods are compiled fully optimised on first use (AggressiveOptimization): each runs for a whole
// block per call, so a job of a few dozen blocks never left their ReadyToRun code, which made it about half as long
// again.
/// <summary>
/// Writes the last column of the sorted rotations of <paramref name="block"/> to <paramref name="output"/> and returns
/// the row of the unrotated block (bzip2's origin pointer).
/// </summary>
[MethodImpl(MethodImplOptions.AggressiveOptimization)]
public static int Transform(ReadOnlySpan<byte> block, Span<byte> output, CancellationToken cancellationToken)
{
var n = block.Length;
Expand Down Expand Up @@ -70,6 +75,7 @@ public static int Transform(ReadOnlySpan<byte> block, Span<byte> output, Cancell
/// smallest rotation is never dropped. A periodic text has two or more: the candidates end up on two of them and
/// match all the way round. A text with one runs out of candidates before.
/// </remarks>
[MethodImpl(MethodImplOptions.AggressiveOptimization)]
internal static int SmallestRotation(ReadOnlySpan<byte> text, out bool periodic)
{
var n = text.Length;
Expand Down Expand Up @@ -100,6 +106,7 @@ internal static int SmallestRotation(ReadOnlySpan<byte> text, out bool periodic)
/// The length of the shortest piece <paramref name="text"/> is a whole number of copies of (its length when it is
/// none), from the prefix function, kept in <paramref name="scratch"/>.
/// </summary>
[MethodImpl(MethodImplOptions.AggressiveOptimization)]
private static int RootLength(ReadOnlySpan<byte> text, int[] scratch)
{
var n = text.Length;
Expand Down Expand Up @@ -152,6 +159,7 @@ private readonly struct IntText(int[] text) : IText
/// the suffix array.
/// The string of names is sorted the same way when names repeat; its alphabet is at most half the size.
/// </remarks>
[MethodImpl(MethodImplOptions.AggressiveOptimization)]
private static void SuffixArray<T>(T text, int n, int alphabet, int[] sa, CancellationToken cancellationToken)
where T : struct, IText
{
Expand Down Expand Up @@ -311,6 +319,7 @@ private static void SuffixArray<T>(T text, int n, int alphabet, int[] sa, Cancel
/// suffix whose predecessor is L-type has nothing to place in the S scan, one whose predecessor is S-type has. The
/// S scan leaves every entry as the plain position. 0 is both a gap and suffix 0, which never places anything.
/// </remarks>
[MethodImpl(MethodImplOptions.AggressiveOptimization)]
private static void Induce<T>(T text, int n, int[] sa, int[] starts, int[] ends, int[] fill, int alphabet)
where T : struct, IText
{
Expand Down
3 changes: 3 additions & 0 deletions src/Filee.Engines/Archives/Bzip2Writer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
// 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.

using System.Runtime.CompilerServices;
using Filee.Core.Presets;

namespace Filee.Engines.Archives;
Expand Down Expand Up @@ -71,6 +72,7 @@ public Bzip2Writer(Stream output, ArchiveLevel level, CancellationToken cancella

public override void Write(byte[] buffer, int offset, int count) => Write(buffer.AsSpan(offset, count));

[MethodImpl(MethodImplOptions.AggressiveOptimization)]
public override void Write(ReadOnlySpan<byte> buffer)
{
ObjectDisposedException.ThrowIf(_finished, this);
Expand Down Expand Up @@ -253,6 +255,7 @@ public void Clear()
}

/// <summary>Writes the low <paramref name="count"/> (0 … 32) bits of <paramref name="value"/>, which has no others set.</summary>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void Write(int count, uint value)
{
_pending = (_pending << count) | value;
Expand Down
17 changes: 17 additions & 0 deletions src/Filee.Engines/Archives/Lzma/Lzma2Reader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
// An LZMA chunk unpacks to at most 2 MiB from at most 64 KiB, with its own range coder, so each is read whole
// before it is decoded and nothing in it can claim more than those limits.

using System.Buffers.Binary;

namespace Filee.Engines.Archives.Lzma;

/// <summary>Buffered reading from a stream that counts the bytes it hands out.</summary>
Expand Down Expand Up @@ -35,6 +37,21 @@ public void ReadExactly(Span<byte> target)
throw new InvalidDataException("the data ends too early");
}

/// <summary>The next four bytes as a big-endian number; the stream ending first is damage.</summary>
public uint ReadUInt32BigEndian()
{
if (_end - _position >= 4)
{
var value = BinaryPrimitives.ReadUInt32BigEndian(_buffer.AsSpan(_position));
_position += 4;
Consumed += 4;
return value;
}
Span<byte> bytes = stackalloc byte[4];
ReadExactly(bytes);
return BinaryPrimitives.ReadUInt32BigEndian(bytes);
}

/// <summary>Reads as much of <paramref name="target"/> as the stream still has.</summary>
public int ReadAtMost(Span<byte> target)
{
Expand Down
10 changes: 9 additions & 1 deletion src/Filee.Engines/Archives/Lzma/Lzma2Writer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
// threads of its own (MatchFinder.StartAhead, the trees split between them by hash), which gives the same bytes too.

using System.Numerics;
using System.Runtime.CompilerServices;
using Filee.Core.Presets;

namespace Filee.Engines.Archives.Lzma;
Expand All @@ -31,7 +32,8 @@ public static Lzma2Settings For(ArchiveLevel level, long expectedSize = -1)
var settings = level switch
{
ArchiveLevel.Store => new Lzma2Settings(1 << 12, default, Store: true),
ArchiveLevel.Fast => new Lzma2Settings(1 << 18, new LzmaEncoderSettings(Depth: 8, Nice: 32, Optimal: false, Tree: false), false),
// Six chain candidates: against eight, about 4 % less work for 0.25 % larger output.
ArchiveLevel.Fast => new Lzma2Settings(1 << 18, new LzmaEncoderSettings(Depth: 6, Nice: 32, Optimal: false, Tree: false), false),
ArchiveLevel.Maximum => new Lzma2Settings(1 << 26, new LzmaEncoderSettings(Depth: 64, Nice: 64, Optimal: true, Tree: true), false),
_ => new Lzma2Settings(1 << 24, new LzmaEncoderSettings(Depth: 32, Nice: 32, Optimal: true, Tree: true), false),
};
Expand Down Expand Up @@ -346,6 +348,12 @@ public void Encode(CancellationToken cancellationToken, int searchThreads = 0)
}
}

// Compiled fully optimised on first use, with the fast level's per-symbol code (LzmaEncoder.Encode and EncodeOne
// and the helpers marked AggressiveInlining) inlined, as the tiered compiler ends up doing; the methods it calls
// stay separate and are compiled the same way. A chunk then runs at full speed from the first one, where the
// ReadyToRun and instrumented code made a fast job of a few dozen megabytes about a third longer, more so with many
// threads.
[MethodImpl(MethodImplOptions.AggressiveOptimization)]
private void EncodeChunk(int end, CancellationToken cancellationToken)
{
cancellationToken.ThrowIfCancellationRequested();
Expand Down
10 changes: 8 additions & 2 deletions src/Filee.Engines/Archives/Lzma/LzmaDecoder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -317,10 +317,14 @@ public void AppendUncompressed(ReadOnlySpan<byte> data)
}
}

// The decoding loops are compiled fully optimised on first use (AggressiveOptimization) instead of starting from
// the ReadyToRun code and going through the instrumented tier: those ran the start of every job slower, and a job
// is often over before the tiered code would come.
/// <summary>
/// Decodes until <paramref name="output"/> is full or, with <paramref name="allowEndMarker"/>, the end marker
/// comes. Returns the bytes written.
/// </summary>
[MethodImpl(MethodImplOptions.AggressiveOptimization)]
public int Decode(RangeDecoder rc, Span<byte> output, bool allowEndMarker)
{
// The literal coders come with the properties; without them the probabilities below would not be there.
Expand Down Expand Up @@ -365,6 +369,7 @@ public int Decode(RangeDecoder rc, Span<byte> output, bool allowEndMarker)
/// hand over when the IsMatch bit changes. Each keeps the range coder in registers for its whole run: one method
/// for both had more live values than the CPU has registers, and the JIT spilled the coder in the literal loop.
/// </remarks>
[MethodImpl(MethodImplOptions.AggressiveOptimization)]
private bool Run<TInput>(RangeDecoder rc, int stop, bool allowEndMarker) where TInput : struct, IInputMode
{
var start = _pos;
Expand Down Expand Up @@ -412,7 +417,7 @@ private int NextSymbol<TInput>(RangeDecoder rc) where TInput : struct, IInputMod
/// Decodes literals (the first one's IsMatch bit was read) until a match comes (<see cref="MatchNext"/>) or Run
/// has to stop (<see cref="Stopped"/>).
/// </summary>
[MethodImpl(MethodImplOptions.NoInlining)]
[MethodImpl(MethodImplOptions.NoInlining | MethodImplOptions.AggressiveOptimization)]
private int LiteralRun<TInput>(RangeDecoder rc) where TInput : struct, IInputMode
{
ref var win = ref MemoryMarshal.GetArrayDataReference(_window);
Expand Down Expand Up @@ -502,7 +507,7 @@ private int LiteralRun<TInput>(RangeDecoder rc) where TInput : struct, IInputMod
/// (the rest stays pending), until a literal comes (<see cref="LiteralNext"/>), Run has to stop
/// (<see cref="Stopped"/>) or the end marker (<see cref="EndMarker"/>).
/// </summary>
[MethodImpl(MethodImplOptions.NoInlining)]
[MethodImpl(MethodImplOptions.NoInlining | MethodImplOptions.AggressiveOptimization)]
private int MatchRun<TInput>(RangeDecoder rc, bool allowEndMarker) where TInput : struct, IInputMode
{
var window = _window;
Expand Down Expand Up @@ -677,6 +682,7 @@ private static unsafe void Prefetch(byte[] window, int source, int count)
}

/// <summary>Does the first <paramref name="count"/> copies MatchRun put off, in order.</summary>
[MethodImpl(MethodImplOptions.AggressiveOptimization)]
private void CopyDeferred(byte[] window, int count)
{
ref var deferred = ref MemoryMarshal.GetArrayDataReference(_deferred);
Expand Down
9 changes: 9 additions & 0 deletions src/Filee.Engines/Archives/Lzma/LzmaEncoder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ public LzmaEncoder(MatchFinder mf, LzmaProperties properties, LzmaEncoderSetting
/// Codes symbols from the cursor until <paramref name="end"/> (no match crosses it) or until the coder's output
/// would pass <paramref name="packedLimit"/> bytes.
/// </summary>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void Encode(RangeEncoder rc, int end, long packedLimit)
{
while (_mf.Position < end && rc.FlushedSize < packedLimit)
Expand Down Expand Up @@ -112,6 +113,7 @@ public void Shifted(int shift)
_nextAt -= shift;
}

[MethodImpl(MethodImplOptions.AggressiveInlining)]
private void EncodeOne(RangeEncoder rc, int end)
{
var buffer = _mf.Buffer;
Expand Down Expand Up @@ -207,6 +209,7 @@ private void EncodeOne(RangeEncoder rc, int end)
private static bool FarNearer(uint near, uint far) => far >> 7 > near;

/// <summary>The longest repeat of one of the last four distances at <paramref name="pos"/> (0 when none of 2 or more).</summary>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
private int LongestRep(byte[] buffer, int pos, int available, out int repIndex)
{
repIndex = 0;
Expand All @@ -232,6 +235,7 @@ private int LongestRep(byte[] buffer, int pos, int available, out int repIndex)
}

/// <summary>True when one of the last four distances repeats at least <paramref name="length"/> bytes at <paramref name="pos"/> (one byte past the cursor).</summary>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
private bool RepReaches(byte[] buffer, int pos, int available, int length)
{
if (available < length)
Expand Down Expand Up @@ -523,6 +527,7 @@ private void RefreshPrices()
[MethodImpl(MethodImplOptions.AggressiveInlining)]
private uint Rep(int index) => index switch { 0 => Rep0, 1 => Rep1, 2 => Rep2, _ => Rep3 };

[MethodImpl(MethodImplOptions.AggressiveOptimization)]
private void EmitLiteralOrShortRep(RangeEncoder rc)
{
var buffer = _mf.Buffer;
Expand All @@ -543,6 +548,7 @@ private void EmitLiteralOrShortRep(RangeEncoder rc)
EmitLiteral(rc);
}

[MethodImpl(MethodImplOptions.AggressiveOptimization)]
private void EmitShortRep(RangeEncoder rc)
{
var state2 = (State << 4) + ((int)_total & PbMask);
Expand All @@ -554,6 +560,7 @@ private void EmitShortRep(RangeEncoder rc)
Advance(1);
}

[MethodImpl(MethodImplOptions.AggressiveInlining)]
private void EmitLiteral(RangeEncoder rc)
{
var buffer = _mf.Buffer;
Expand Down Expand Up @@ -599,6 +606,7 @@ private int LiteralPrice(int pos, long total, int state, uint rep0)
return price;
}

[MethodImpl(MethodImplOptions.AggressiveOptimization)]
private void EmitMatch(RangeEncoder rc, int length, uint distance)
{
var posState = (int)_total & PbMask;
Expand All @@ -615,6 +623,7 @@ private void EmitMatch(RangeEncoder rc, int length, uint distance)
Advance(length);
}

[MethodImpl(MethodImplOptions.AggressiveOptimization)]
private void EmitRep(RangeEncoder rc, int index, int length)
{
var posState = (int)_total & PbMask;
Expand Down
3 changes: 2 additions & 1 deletion src/Filee.Engines/Archives/Lzma/MatchFinder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -338,7 +338,7 @@ private int Search(int pos, int maxLength, int[]? lengths, uint[]? distances, ou
/// the same tables, candidates and lists. A candidate is only measured when its two bytes ending at the best length
/// so far agree with the cursor's (a match longer than the best has them), which takes one load per candidate.
/// </summary>
[MethodImpl(MethodImplOptions.NoInlining)]
[MethodImpl(MethodImplOptions.NoInlining | MethodImplOptions.AggressiveOptimization)]
private int ChainSearch(int pos, int maxLength, int[] lengths, uint[] distances)
{
System.Diagnostics.Debug.Assert(pos >= _nextInsert, "a position is searched twice");
Expand Down Expand Up @@ -548,6 +548,7 @@ public void SkipTo(int pos)
Insert(pos);
}

[MethodImpl(MethodImplOptions.AggressiveOptimization)]
private void Insert(int pos)
{
var buffer = Buffer;
Expand Down
5 changes: 5 additions & 0 deletions src/Filee.Engines/Archives/Lzma/RangeEncoder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ private void Bit(ref ushort probability, uint bit, ref ulong low, ref uint range
}

/// <summary>A literal (8 bits through the tree at <paramref name="probabilities"/>, most significant first).</summary>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void EncodeLiteral(ref ushort probabilities, uint symbol)
{
var low = _low;
Expand All @@ -103,6 +104,7 @@ public void EncodeLiteral(ref ushort probabilities, uint symbol)
/// A literal after a match: while its bits agree with <paramref name="matchByte"/>'s, the probabilities picked by
/// that byte's bit (0x100 or 0x200 on) are used, after the first difference the plain ones.
/// </summary>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void EncodeMatchedLiteral(ref ushort probabilities, uint symbol, uint matchByte)
{
var low = _low;
Expand Down Expand Up @@ -147,6 +149,7 @@ public void EncodeBit(ref ushort probability, int bit)
}

/// <summary>The low <paramref name="count"/> bits of <paramref name="value"/> at probability one half, most significant first.</summary>
[MethodImpl(MethodImplOptions.AggressiveOptimization)]
public void EncodeDirectBits(uint value, int count)
{
var low = _low;
Expand All @@ -166,6 +169,7 @@ public void EncodeDirectBits(uint value, int count)
}

/// <summary>A <paramref name="bits"/>-bit symbol through a binary tree of probabilities, most significant bit first.</summary>
[MethodImpl(MethodImplOptions.AggressiveOptimization)]
public void EncodeTree(Span<ushort> probabilities, int bits, uint symbol)
{
if (probabilities.Length < 1 << bits)
Expand All @@ -185,6 +189,7 @@ public void EncodeTree(Span<ushort> probabilities, int bits, uint symbol)
}

/// <summary>Like <see cref="EncodeTree"/>, least significant bit first.</summary>
[MethodImpl(MethodImplOptions.AggressiveOptimization)]
public void EncodeReverseTree(Span<ushort> probabilities, int bits, uint symbol)
{
if (probabilities.Length < 1 << bits)
Expand Down
Loading
Loading