-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
57 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} | ||
} | ||
} |