Browse Source

inlining microoptimizations

af/merge-core
Anton Firszov 9 years ago
parent
commit
1bb014dcba
  1. 68
      src/ImageSharp.Formats.Jpeg/Components/Decoder/Bits.cs
  2. 4
      src/ImageSharp.Formats.Jpeg/Components/Decoder/Bytes.cs
  3. 6
      src/ImageSharp.Formats.Jpeg/JpegDecoderCore.cs
  4. 8
      src/ImageSharp.Formats.Jpeg/project.json
  5. 26
      tests/ImageSharp.Tests/Formats/Jpg/JpegDecoderTests.cs

68
src/ImageSharp.Formats.Jpeg/Components/Decoder/Bits.cs

@ -5,6 +5,7 @@
namespace ImageSharp.Formats.Jpg
{
using System;
using System.Runtime.CompilerServices;
/// <summary>
@ -53,39 +54,60 @@ namespace ImageSharp.Formats.Jpg
/// <param name="n">The number of bits to ensure.</param>
/// <param name="decoder">Jpeg decoder</param>
/// <returns>Error code</returns>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
//[MethodImpl(MethodImplOptions.AggressiveInlining)]
public DecoderErrorCode EnsureNBitsUnsafe(int n, JpegDecoderCore decoder)
{
while (true)
{
// Grab the decode bytes, use them and then set them
// back on the decoder.
Bytes decoderBytes = decoder.Bytes;
byte c;
DecoderErrorCode errorCode = decoderBytes.ReadByteStuffedByteUnsafe(decoder.InputStream, out c);
decoder.Bytes = decoderBytes;
if (errorCode != DecoderErrorCode.NoError)
DecoderErrorCode errorCode = this.EnsureBitsStepImpl(decoder);
if (errorCode != DecoderErrorCode.NoError || this.UnreadBits >= n)
{
return errorCode;
}
}
}
this.Accumulator = (this.Accumulator << 8) | c;
this.UnreadBits += 8;
if (this.Mask == 0)
{
this.Mask = 1 << 7;
}
else
{
this.Mask <<= 8;
}
/// <summary>
/// Unrolled version of <see cref="EnsureNBitsUnsafe"/> for n==8
/// </summary>
/// <param name="decoder"></param>
/// <returns></returns>
public DecoderErrorCode Ensure8BitsUnsafe(JpegDecoderCore decoder)
{
return this.EnsureBitsStepImpl(decoder);
}
if (this.UnreadBits >= n)
{
return DecoderErrorCode.NoError;
}
/// <summary>
/// Unrolled version of <see cref="EnsureNBitsUnsafe"/> for n==1
/// </summary>
/// <param name="decoder"></param>
/// <returns></returns>
public DecoderErrorCode Ensure1BitUnsafe(JpegDecoderCore decoder)
{
return this.EnsureBitsStepImpl(decoder);
}
private DecoderErrorCode EnsureBitsStepImpl(JpegDecoderCore decoder)
{
byte c;
DecoderErrorCode errorCode = decoder.Bytes.ReadByteStuffedByteUnsafe(decoder.InputStream, out c);
if (errorCode != DecoderErrorCode.NoError)
{
return errorCode;
}
this.Accumulator = (this.Accumulator << 8) | c;
this.UnreadBits += 8;
if (this.Mask == 0)
{
this.Mask = 1 << 7;
}
else
{
this.Mask <<= 8;
}
return errorCode;
}
/// <summary>

4
src/ImageSharp.Formats.Jpeg/Components/Decoder/Bytes.cs

@ -144,7 +144,7 @@ namespace ImageSharp.Formats.Jpg
/// <param name="inputStream">Input stream</param>
/// <param name="result">The result <see cref="byte"/> as out parameter</param>
/// <returns>The <see cref="DecoderErrorCode"/></returns>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
//[MethodImpl(MethodImplOptions.AggressiveInlining)]
public DecoderErrorCode ReadByteUnsafe(Stream inputStream, out byte result)
{
DecoderErrorCode errorCode = DecoderErrorCode.NoError;
@ -184,7 +184,7 @@ namespace ImageSharp.Formats.Jpg
/// </summary>
/// <param name="inputStream">Input stream</param>
/// <returns>The <see cref="DecoderErrorCode"/></returns>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
//[MethodImpl(MethodImplOptions.AggressiveInlining)]
public DecoderErrorCode FillUnsafe(Stream inputStream)
{
if (this.I != this.J)

6
src/ImageSharp.Formats.Jpeg/JpegDecoderCore.cs

@ -229,7 +229,8 @@ namespace ImageSharp.Formats
{
if (this.Bits.UnreadBits == 0)
{
DecoderErrorCode errorCode = this.Bits.EnsureNBitsUnsafe(1, this);
//DecoderErrorCode errorCode = this.Bits.EnsureNBitsUnsafe(1, this);
DecoderErrorCode errorCode = this.Bits.Ensure1BitUnsafe(this);
if (errorCode != DecoderErrorCode.NoError)
{
result = false;
@ -338,7 +339,8 @@ namespace ImageSharp.Formats
if (this.Bits.UnreadBits < 8)
{
DecoderErrorCode errorCode = this.Bits.EnsureNBitsUnsafe(8, this);
//DecoderErrorCode errorCode = this.Bits.EnsureNBitsUnsafe(8, this);
DecoderErrorCode errorCode = this.Bits.Ensure8BitsUnsafe(this);
if (errorCode == DecoderErrorCode.NoError)
{

8
src/ImageSharp.Formats.Jpeg/project.json

@ -42,10 +42,10 @@
"target": "project",
"version": "1.0.0-*"
},
"StyleCop.Analyzers": {
"version": "1.1.0-beta001",
"type": "build"
},
//"StyleCop.Analyzers": {
// "version": "1.1.0-beta001",
// "type": "build"
//},
"System.Buffers": "4.0.0",
"System.Runtime.CompilerServices.Unsafe": "4.0.0"
},

26
tests/ImageSharp.Tests/Formats/Jpg/JpegDecoderTests.cs

@ -40,31 +40,5 @@ namespace ImageSharp.Tests
provider.Utility.SaveTestOutputFile(image, "bmp");
}
unsafe struct Buzisag
{
public int Value;
public delegate void BlockAction(Buzisag* b);
public static void Foo(Buzisag* buzisag)
{
Bar(buzisag, b => b->Value++);
}
public static void Bar(Buzisag* buzisag, BlockAction action)
{
action(buzisag);
}
}
[Fact]
public unsafe void Kabbe()
{
Buzisag b = default(Buzisag);
Buzisag.Foo(&b);
Assert.Equal(1, b.Value);
}
}
}
Loading…
Cancel
Save