Experiment with pooled xfer buffer.

This commit is contained in:
Eugene Wang 2023-04-04 22:22:02 -04:00
parent 059b8dd60e
commit a2c4d5a455
2 changed files with 53 additions and 0 deletions

View File

@ -0,0 +1,46 @@
using System;
using System.Buffers;
namespace NTwain
{
public class DataTransferredEventArgs : EventArgs, IDisposable
{
/// <summary>
/// Ctor for pooled data and the pool to clean it up.
/// </summary>
/// <param name="pool"></param>
/// <param name="data"></param>
internal DataTransferredEventArgs(ArrayPool<byte>? pool, byte[]? data)
{
_pool = pool;
Data = data;
}
bool _disposed;
private readonly ArrayPool<byte>? _pool;
/// <summary>
/// The complete file data if the transfer was done
/// through memory. IMPORTANT: The data held
/// in this array will no longer be valid once
/// this event arg has been disposed.
/// </summary>
public byte[]? Data { get; private set; }
public void Dispose()
{
if (!_disposed)
{
if (_pool != null && Data != null)
{
_pool.Return(Data);
Data = null;
}
_disposed = true;
}
}
}
}

View File

@ -1,6 +1,7 @@
using NTwain.Data; using NTwain.Data;
using NTwain.Triplets; using NTwain.Triplets;
using System; using System;
using System.Buffers;
using System.Collections.Concurrent; using System.Collections.Concurrent;
using System.Collections.Generic; using System.Collections.Generic;
using System.Diagnostics; using System.Diagnostics;
@ -12,6 +13,12 @@ namespace NTwain
partial class TwainAppSession partial class TwainAppSession
{ {
// experiment using array pool for things transferred in memory.
// this can pool up to a "normal" max of legal size paper in 24 bit at 300 dpi (~31MB)
// so the array max is made with 32 MB. Typical usage should be a lot less.
static readonly ArrayPool<byte> XferMemPool = ArrayPool<byte>.Create(32505856, 4);
/// <summary> /// <summary>
/// Start the transfer loop. /// Start the transfer loop.
/// This should be called after receiving /// This should be called after receiving