Skip to content

Use the ArrayPool API in .NET Standard 2.1 or later to reduce the creation of LOH objects. #1812

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 16 commits into from
Apr 25, 2025
Merged
Changes from 6 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
93 changes: 83 additions & 10 deletions src/Magick.NET/Helpers/ByteArrayWrapper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,34 +3,76 @@

using System;
using System.IO;
#if NETSTANDARD2_1_OR_GREATER
using System.Buffers;
#endif

namespace ImageMagick;

internal sealed unsafe class ByteArrayWrapper
{
#if NETSTANDARD2_1_OR_GREATER

private static readonly ArrayPool<byte> _pool = ArrayPool<byte>.Create(1024 * 1024 * 64, 1024);

private byte[] _bytes = _pool.Rent(1024 * 1024);

#else

private byte[] _bytes = new byte[8192];
private int _offset = 0;
private int _length = 0;

#endif
private int _offset;

private int _length;

#if NETSTANDARD2_1_OR_GREATER

public byte[] GetBytes()
{
var result = new byte[_length];
Array.Copy(_bytes, result, Math.Min(_bytes.Length, _length));
_pool.Return(_bytes, true);
return result;
}

~ByteArrayWrapper()
{
if (_bytes is not null)
{
_pool.Return(_bytes, true);
}
}
#else

public byte[] GetBytes()
{
ResizeBytes(_length);

return _bytes;
}

#endif
public long Read(IntPtr data, UIntPtr count, IntPtr user_data)
{
if (data == IntPtr.Zero)
{
return 0;
}

var total = (int)count;

if (total == 0)
{
return 0;
}

var length = Math.Min(total, _length - _offset);

if (length != 0)
{
var destination = (byte*)data.ToPointer();

fixed (byte* source = _bytes)
{
NativeMemory.Copy(source + _offset, destination, length);
Expand All @@ -45,42 +87,53 @@ public long Read(IntPtr data, UIntPtr count, IntPtr user_data)
public long Seek(long offset, IntPtr whence, IntPtr user_data)
{
var newOffset = (int)((SeekOrigin)whence switch
{
SeekOrigin.Begin => offset,
SeekOrigin.Current => _offset + offset,
SeekOrigin.End => _length - offset,
_ => -1,
});
{
SeekOrigin.Begin => offset,
SeekOrigin.Current => _offset + offset,
SeekOrigin.End => _length - offset,
_ => -1,
});

if (_offset == newOffset)
{
return _offset;
}

if (newOffset < 0)
{
return -1;
}

_offset = newOffset;

return _offset;
}

public long Tell(IntPtr user_data)
=> _offset;
{
return _offset;
}

public long Write(IntPtr data, UIntPtr count, IntPtr user_data)
{
if (data == IntPtr.Zero)
{
return 0;
}

var total = (int)count;

if (total == 0)
{
return 0;
}

var newOffset = _offset + total;

EnsureLength(newOffset);

var source = (byte*)data.ToPointer();

fixed (byte* destination = _bytes)
{
NativeMemory.Copy(source, destination + _offset, total);
Expand All @@ -94,17 +147,37 @@ public long Write(IntPtr data, UIntPtr count, IntPtr user_data)
private void EnsureLength(int length)
{
if (length < _length)
{
return;
}

_length = length;

if (_length < _bytes.Length)
{
return;
}

var newLength = Math.Max(_bytes.Length * 2, _length);
ResizeBytes(newLength);
}

#if NETSTANDARD2_1_OR_GREATER

private void ResizeBytes(int length)
{
byte[] byte2 = _pool.Rent(length);
Array.Copy(_bytes, byte2, Math.Min(_bytes.Length, length));
_pool.Return(_bytes, true);
_bytes = byte2;
}

#else

private void ResizeBytes(int length)
=> Array.Resize(ref _bytes, length);
{
Array.Resize(ref _bytes, length);
}

#endif
}