From 57ed74368715e764a438783d2ff2c3a6bc82650e Mon Sep 17 00:00:00 2001 From: routersys Date: Fri, 9 Oct 2026 21:52:55 +0900 Subject: [PATCH] =?UTF-8?q?=E3=82=AB=E3=83=BC=E3=83=8D=E3=83=AB=E3=81=AE?= =?UTF-8?q?=E3=82=B9=E3=83=9A=E3=82=AF=E3=83=88=E3=83=AB=E3=81=AE=E4=BD=9C?= =?UTF-8?q?=E6=A5=AD=E9=A0=98=E5=9F=9F=E3=82=92=E5=85=B1=E6=9C=89=E3=81=AE?= =?UTF-8?q?=E8=B2=B8=E3=81=97=E5=87=BA=E3=81=97=E3=81=AB=E3=81=99=E3=82=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit ・カーネルのスペクトルの計算用メモリをエフェクト間で共有して、使用メモリを削減-M --- .../SpectralConvolution/KernelSpectrum.cs | 13 ++++- .../SpectralConvolution/ScratchPool.cs | 48 +++++++++++++++++++ 2 files changed, 59 insertions(+), 2 deletions(-) create mode 100644 SpectralConvolution/SpectralConvolution/ScratchPool.cs diff --git a/SpectralConvolution/SpectralConvolution/KernelSpectrum.cs b/SpectralConvolution/SpectralConvolution/KernelSpectrum.cs index 26f4d96..fdc5db2 100644 --- a/SpectralConvolution/SpectralConvolution/KernelSpectrum.cs +++ b/SpectralConvolution/SpectralConvolution/KernelSpectrum.cs @@ -4,8 +4,8 @@ namespace SpectralConvolution; internal sealed class KernelSpectrum { - readonly ComplexLines rows = new(); - readonly ComplexLines column = new(); + static readonly ScratchPool Scratches = new(Environment.ProcessorCount); + double[] kernel = []; Float2[] twiddles = []; Float2[] spectrum = []; @@ -45,6 +45,9 @@ internal sealed class KernelSpectrum Radius = 0; if (kernel.Length < kernelArea) kernel = new double[kernelArea]; + using var lease = Scratches.Rent(); + var rows = lease.Value.Rows; + var column = lease.Value.Column; rows.Ensure(rowArea); column.Ensure(size); if (spectrum.Length < size * size) @@ -112,4 +115,10 @@ internal sealed class KernelSpectrum } public static float Flush(float value) => float.IsSubnormal(value) ? float.CopySign(0f, value) : value; + + sealed class TransformScratch + { + public readonly ComplexLines Rows = new(); + public readonly ComplexLines Column = new(); + } } diff --git a/SpectralConvolution/SpectralConvolution/ScratchPool.cs b/SpectralConvolution/SpectralConvolution/ScratchPool.cs new file mode 100644 index 0000000..d57b68d --- /dev/null +++ b/SpectralConvolution/SpectralConvolution/ScratchPool.cs @@ -0,0 +1,48 @@ +namespace SpectralConvolution; + +internal sealed class ScratchPool(int capacity) where T : class, new() +{ + readonly T?[] slots = new T?[capacity]; + readonly object gate = new(); + + public Lease Rent() + { + T? item = null; + lock (gate) + { + for (var index = 0; index < slots.Length; index++) + { + if (slots[index] is not { } found) + continue; + + item = found; + slots[index] = null; + break; + } + } + + return new Lease(this, item ?? new T()); + } + + void Return(T item) + { + lock (gate) + { + for (var index = 0; index < slots.Length; index++) + { + if (slots[index] is not null) + continue; + + slots[index] = item; + return; + } + } + } + + public readonly struct Lease(ScratchPool pool, T value) : IDisposable + { + public T Value { get; } = value; + + public void Dispose() => pool.Return(Value); + } +}