Skip to content

Commit

Permalink
FFTCache pooling
Browse files Browse the repository at this point in the history
  • Loading branch information
VoidXH committed Oct 17, 2023
1 parent 03c41a5 commit 9106aba
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 1 deletion.
2 changes: 1 addition & 1 deletion Cavern/Utilities/FFTCache.cs
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ public FFTCache(int size) {
}

for (int i = 0, c = QMath.Log2(size); i < c; i++) {
Interlocked.Increment(ref refcounts[i]);
if (cos[i] != null) {
continue;
}
Expand All @@ -89,7 +90,6 @@ public FFTCache(int size) {
thisSin[j + 1] = sinValue;
}
}
Interlocked.Increment(ref refcounts[i]);
}

float[] maskSource = new float[Vector<float>.Count + 1];
Expand Down
56 changes: 56 additions & 0 deletions Cavern/Utilities/FFTCachePool.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
using System;
using System.Collections.Generic;

namespace Cavern.Utilities {
/// <summary>
/// When performing a large amount of FFTs across multiple threads, use this pool for optimizing allocation performance
/// by reusing caches that are not used anymore by their thread.
/// </summary>
public class FFTCachePool : IDisposable {
/// <summary>
/// Caches not currently leased.
/// </summary>
readonly Stack<FFTCache> caches = new Stack<FFTCache>();

/// <summary>
/// Size of the used FFTs.
/// </summary>
readonly int size;

/// <summary>
/// Create an <see cref="FFTCache"/> pool for this FFT size.
/// </summary>
public FFTCachePool(int size) => this.size = size;

/// <summary>
/// Get an <see cref="FFTCache"/> to work with.
/// </summary>
public FFTCache Lease() {
lock (this) {
if (caches.Count == 0) {
return new ThreadSafeFFTCache(size);
} else {
return caches.Pop();
}
}
}

/// <summary>
/// Store the <paramref name="cache"/> for later reuse.
/// </summary>
public void Return(FFTCache cache) {
lock (this) {
caches.Push(cache);
}
}

/// <summary>
/// Free all resources used by the allocated <see cref="caches"/>.
/// </summary>
public void Dispose() {
while (caches.Count != 0) {
caches.Pop().Dispose();
}
}
}
}

0 comments on commit 9106aba

Please sign in to comment.