From 94df8fc1ad8833c912e19f642df78d49cca091b8 Mon Sep 17 00:00:00 2001 From: Brian Popow Date: Mon, 1 Nov 2021 14:33:46 +0100 Subject: [PATCH] Small bitreader improvements: - Make bitmask static readonly - Add aggresive inlining - Change Guard to DebugGuard in ReadValue --- .../Formats/Webp/BitReader/Vp8LBitReader.cs | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/src/ImageSharp/Formats/Webp/BitReader/Vp8LBitReader.cs b/src/ImageSharp/Formats/Webp/BitReader/Vp8LBitReader.cs index 601336fa4b..07423e3127 100644 --- a/src/ImageSharp/Formats/Webp/BitReader/Vp8LBitReader.cs +++ b/src/ImageSharp/Formats/Webp/BitReader/Vp8LBitReader.cs @@ -28,7 +28,7 @@ namespace SixLabors.ImageSharp.Formats.Webp.BitReader /// private const int Wbits = 32; - private readonly uint[] bitMask = + private static readonly uint[] BitMask = { 0, 0x000001, 0x000003, 0x000007, 0x00000f, @@ -125,13 +125,14 @@ namespace SixLabors.ImageSharp.Formats.Webp.BitReader /// /// The number of bits to read (should not exceed 16). /// A ushort value. + [MethodImpl(InliningOptions.ShortMethod)] public uint ReadValue(int nBits) { - Guard.MustBeGreaterThan(nBits, 0, nameof(nBits)); + DebugGuard.MustBeGreaterThan(nBits, 0, nameof(nBits)); if (!this.Eos && nBits <= Vp8LMaxNumBitRead) { - ulong val = this.PrefetchBits() & this.bitMask[nBits]; + ulong val = this.PrefetchBits() & BitMask[nBits]; this.bitPos += nBits; this.ShiftBytes(); return (uint)val; @@ -169,6 +170,7 @@ namespace SixLabors.ImageSharp.Formats.Webp.BitReader /// /// Advances the read buffer by 4 bytes to make room for reading next 32 bits. /// + [MethodImpl(InliningOptions.ShortMethod)] public void FillBitWindow() { if (this.bitPos >= Wbits) @@ -181,7 +183,8 @@ namespace SixLabors.ImageSharp.Formats.Webp.BitReader /// Returns true if there was an attempt at reading bit past the end of the buffer. /// /// True, if end of buffer was reached. - public bool IsEndOfStream() => this.Eos || ((this.pos == this.len) && (this.bitPos > Lbits)); + [MethodImpl(InliningOptions.ShortMethod)] + public bool IsEndOfStream() => this.Eos || (this.pos == this.len && this.bitPos > Lbits); [MethodImpl(InliningOptions.ShortMethod)] private void DoFillBitWindow() => this.ShiftBytes(); @@ -189,6 +192,7 @@ namespace SixLabors.ImageSharp.Formats.Webp.BitReader /// /// If not at EOS, reload up to Vp8LLbits byte-by-byte. /// + [MethodImpl(InliningOptions.ShortMethod)] private void ShiftBytes() { System.Span dataSpan = this.Data.Memory.Span;