Browse Source

- Use memory manager in Bytes

pull/431/head
Lauri Kotilainen 8 years ago
parent
commit
26155568f6
  1. 26
      src/ImageSharp/Formats/Jpeg/GolangPort/Components/Decoder/Bytes.cs
  2. 11
      src/ImageSharp/Formats/Jpeg/GolangPort/Components/Decoder/InputProcessor.cs
  3. 2
      src/ImageSharp/Formats/Jpeg/GolangPort/OrigJpegDecoderCore.cs

26
src/ImageSharp/Formats/Jpeg/GolangPort/Components/Decoder/Bytes.cs

@ -6,6 +6,8 @@ using System.Buffers;
using System.IO; using System.IO;
using System.Runtime.CompilerServices; using System.Runtime.CompilerServices;
using SixLabors.ImageSharp.Memory;
namespace SixLabors.ImageSharp.Formats.Jpeg.GolangPort.Components.Decoder namespace SixLabors.ImageSharp.Formats.Jpeg.GolangPort.Components.Decoder
{ {
/// <summary> /// <summary>
@ -25,12 +27,12 @@ namespace SixLabors.ImageSharp.Formats.Jpeg.GolangPort.Components.Decoder
/// buffer[i:j] are the buffered bytes read from the underlying /// buffer[i:j] are the buffered bytes read from the underlying
/// stream that haven't yet been passed further on. /// stream that haven't yet been passed further on.
/// </summary> /// </summary>
public byte[] Buffer; public Buffer<byte> Buffer;
/// <summary> /// <summary>
/// Values of <see cref="Buffer"/> converted to <see cref="int"/>-s /// Values of <see cref="Buffer"/> converted to <see cref="int"/>-s
/// </summary> /// </summary>
public int[] BufferAsInt; public Buffer<int> BufferAsInt;
/// <summary> /// <summary>
/// Start of bytes read /// Start of bytes read
@ -48,20 +50,17 @@ namespace SixLabors.ImageSharp.Formats.Jpeg.GolangPort.Components.Decoder
/// </summary> /// </summary>
public int UnreadableBytes; public int UnreadableBytes;
private static readonly ArrayPool<byte> BytePool = ArrayPool<byte>.Create(BufferSize, 50);
private static readonly ArrayPool<int> IntPool = ArrayPool<int>.Create(BufferSize, 50);
/// <summary> /// <summary>
/// Creates a new instance of the <see cref="Bytes"/>, and initializes it's buffer. /// Creates a new instance of the <see cref="Bytes"/>, and initializes it's buffer.
/// </summary> /// </summary>
/// <param name="memoryManager">The <see cref="MemoryManager"/> to use for buffer allocations.</param>
/// <returns>The bytes created</returns> /// <returns>The bytes created</returns>
public static Bytes Create() public static Bytes Create(MemoryManager memoryManager)
{ {
return new Bytes return new Bytes
{ {
Buffer = BytePool.Rent(BufferSize), Buffer = memoryManager.Allocate<byte>(BufferSize),
BufferAsInt = IntPool.Rent(BufferSize) BufferAsInt = memoryManager.Allocate<int>(BufferSize)
}; };
} }
@ -70,11 +69,8 @@ namespace SixLabors.ImageSharp.Formats.Jpeg.GolangPort.Components.Decoder
/// </summary> /// </summary>
public void Dispose() public void Dispose()
{ {
if (this.Buffer != null) this.Buffer?.Dispose();
{ this.BufferAsInt?.Dispose();
BytePool.Return(this.Buffer);
IntPool.Return(this.BufferAsInt);
}
this.Buffer = null; this.Buffer = null;
this.BufferAsInt = null; this.BufferAsInt = null;
@ -244,7 +240,7 @@ namespace SixLabors.ImageSharp.Formats.Jpeg.GolangPort.Components.Decoder
} }
// Fill in the rest of the buffer. // Fill in the rest of the buffer.
int n = inputStream.Read(this.Buffer, this.J, this.Buffer.Length - this.J); int n = inputStream.Read(this.Buffer.Array, this.J, this.Buffer.Length - this.J);
if (n == 0) if (n == 0)
{ {
return OrigDecoderErrorCode.UnexpectedEndOfStream; return OrigDecoderErrorCode.UnexpectedEndOfStream;

11
src/ImageSharp/Formats/Jpeg/GolangPort/Components/Decoder/InputProcessor.cs

@ -5,6 +5,8 @@ using System;
using System.IO; using System.IO;
using System.Runtime.CompilerServices; using System.Runtime.CompilerServices;
using SixLabors.ImageSharp.Memory;
namespace SixLabors.ImageSharp.Formats.Jpeg.GolangPort.Components.Decoder namespace SixLabors.ImageSharp.Formats.Jpeg.GolangPort.Components.Decoder
{ {
/// <summary> /// <summary>
@ -26,12 +28,13 @@ namespace SixLabors.ImageSharp.Formats.Jpeg.GolangPort.Components.Decoder
/// <summary> /// <summary>
/// Initializes a new instance of the <see cref="InputProcessor"/> struct. /// Initializes a new instance of the <see cref="InputProcessor"/> struct.
/// </summary> /// </summary>
/// <param name="memoryManager">The <see cref="MemoryManager"/> to use for buffer allocations.</param>
/// <param name="inputStream">The input <see cref="Stream"/></param> /// <param name="inputStream">The input <see cref="Stream"/></param>
/// <param name="temp">Temporal buffer, same as <see cref="OrigJpegDecoderCore.Temp"/></param> /// <param name="temp">Temporal buffer, same as <see cref="OrigJpegDecoderCore.Temp"/></param>
public InputProcessor(Stream inputStream, byte[] temp) public InputProcessor(MemoryManager memoryManager, Stream inputStream, byte[] temp)
{ {
this.Bits = default(Bits); this.Bits = default(Bits);
this.Bytes = Bytes.Create(); this.Bytes = Bytes.Create(memoryManager);
this.InputStream = inputStream; this.InputStream = inputStream;
this.Temp = temp; this.Temp = temp;
this.LastErrorCode = OrigDecoderErrorCode.NoError; this.LastErrorCode = OrigDecoderErrorCode.NoError;
@ -155,13 +158,13 @@ namespace SixLabors.ImageSharp.Formats.Jpeg.GolangPort.Components.Decoder
{ {
if (this.Bytes.J - this.Bytes.I >= length) if (this.Bytes.J - this.Bytes.I >= length)
{ {
Array.Copy(this.Bytes.Buffer, this.Bytes.I, data, offset, length); Array.Copy(this.Bytes.Buffer.Array, this.Bytes.I, data, offset, length);
this.Bytes.I += length; this.Bytes.I += length;
length -= length; length -= length;
} }
else else
{ {
Array.Copy(this.Bytes.Buffer, this.Bytes.I, data, offset, this.Bytes.J - this.Bytes.I); Array.Copy(this.Bytes.Buffer.Array, this.Bytes.I, data, offset, this.Bytes.J - this.Bytes.I);
offset += this.Bytes.J - this.Bytes.I; offset += this.Bytes.J - this.Bytes.I;
length -= this.Bytes.J - this.Bytes.I; length -= this.Bytes.J - this.Bytes.I;
this.Bytes.I += this.Bytes.J - this.Bytes.I; this.Bytes.I += this.Bytes.J - this.Bytes.I;

2
src/ImageSharp/Formats/Jpeg/GolangPort/OrigJpegDecoderCore.cs

@ -210,7 +210,7 @@ namespace SixLabors.ImageSharp.Formats.Jpeg.GolangPort
{ {
this.MetaData = new ImageMetaData(); this.MetaData = new ImageMetaData();
this.InputStream = stream; this.InputStream = stream;
this.InputProcessor = new InputProcessor(stream, this.Temp); this.InputProcessor = new InputProcessor(this.configuration.MemoryManager, stream, this.Temp);
// Check for the Start Of Image marker. // Check for the Start Of Image marker.
this.InputProcessor.ReadFull(this.Temp, 0, 2); this.InputProcessor.ReadFull(this.Temp, 0, 2);

Loading…
Cancel
Save