| | | 1 | | //======================================================================= |
| | | 2 | | // Pools.cs |
| | | 3 | | //======================================================================= |
| | | 4 | | // MIT License, Copyright (c) 2024–present David Oravsky (mrdav30) |
| | | 5 | | // See LICENSE file in the project root for full license information. |
| | | 6 | | //======================================================================= |
| | | 7 | | |
| | | 8 | | using GridForge.Grids.Storage; |
| | | 9 | | using GridForge.Spatial; |
| | | 10 | | using SwiftCollections; |
| | | 11 | | using SwiftCollections.Pool; |
| | | 12 | | |
| | | 13 | | namespace GridForge.Grids; |
| | | 14 | | |
| | | 15 | | internal static class Pools |
| | | 16 | | { |
| | | 17 | | #region Grid Pooling |
| | | 18 | | |
| | | 19 | | /// <summary> |
| | | 20 | | /// Object pool for reusing <see cref="VoxelGrid"/> instances. |
| | | 21 | | /// </summary> |
| | 1 | 22 | | public static readonly SwiftObjectPool<VoxelGrid> GridPool = new( |
| | 1 | 23 | | createFunc: () => new VoxelGrid(), |
| | 1 | 24 | | actionOnRelease: grid => grid.Reset() |
| | 1 | 25 | | ); |
| | | 26 | | |
| | | 27 | | /// <summary> |
| | | 28 | | /// Object pool for reusing <see cref="Voxel"/> instances. |
| | | 29 | | /// </summary> |
| | 1 | 30 | | public static readonly SwiftObjectPool<Voxel> VoxelPool = new( |
| | 1 | 31 | | createFunc: () => new Voxel(), |
| | 1 | 32 | | actionOnRelease: voxel => voxel.Reset() |
| | 1 | 33 | | ); |
| | | 34 | | |
| | 1 | 35 | | public static readonly SwiftObjectPool<SwiftSparseMap<ScanCell>> ScanCellMapPool = new( |
| | 1 | 36 | | createFunc: () => new SwiftSparseMap<ScanCell>(), |
| | 1 | 37 | | actionOnRelease: map => map.Clear() |
| | 1 | 38 | | ); |
| | | 39 | | |
| | 1 | 40 | | public static readonly SwiftObjectPool<SwiftSparseMap<SparseVoxelBlock>> SparseVoxelBlockMapPool = new( |
| | 1 | 41 | | createFunc: () => new SwiftSparseMap<SparseVoxelBlock>(), |
| | 1 | 42 | | actionOnRelease: map => map.Clear() |
| | 1 | 43 | | ); |
| | | 44 | | |
| | 1 | 45 | | public static readonly SwiftObjectPool<SparseVoxelBlock> SparseVoxelBlockPool = new( |
| | 1 | 46 | | createFunc: () => new SparseVoxelBlock(), |
| | 1 | 47 | | actionOnRelease: block => block.Clear() |
| | 1 | 48 | | ); |
| | | 49 | | |
| | 1 | 50 | | public static readonly SwiftDictionaryPool<int, int> SparseVoxelBlockCapacityPool = new(); |
| | | 51 | | |
| | | 52 | | /// <summary> |
| | | 53 | | /// Object pool for reusing <see cref="ScanCell"/> instances. |
| | | 54 | | /// </summary> |
| | 1 | 55 | | public static readonly SwiftObjectPool<ScanCell> ScanCellPool = new( |
| | 1 | 56 | | createFunc: () => new ScanCell(), |
| | 1 | 57 | | actionOnRelease: cell => cell.Reset() |
| | 1 | 58 | | ); |
| | | 59 | | |
| | | 60 | | #endregion |
| | | 61 | | |
| | | 62 | | #region Node Pooling |
| | | 63 | | |
| | 1 | 64 | | public static readonly SwiftDictionaryPool<WorldVoxelIndex, SwiftBucket<IVoxelOccupant>> VoxelOccupantDictionaryPool |
| | | 65 | | |
| | 1 | 66 | | public static readonly SwiftObjectPool<SwiftBucket<IVoxelOccupant>> VoxelOccupantBucketPool = new( |
| | 1 | 67 | | createFunc: () => new SwiftBucket<IVoxelOccupant>(), |
| | 1 | 68 | | actionOnRelease: bucket => bucket.Clear() |
| | 1 | 69 | | ); |
| | | 70 | | |
| | | 71 | | #endregion |
| | | 72 | | |
| | | 73 | | public static void ClearPools() |
| | | 74 | | { |
| | 2 | 75 | | GridPool.Clear(); |
| | 2 | 76 | | VoxelPool.Clear(); |
| | 2 | 77 | | ScanCellMapPool.Clear(); |
| | 2 | 78 | | SparseVoxelBlockMapPool.Clear(); |
| | 2 | 79 | | SparseVoxelBlockPool.Clear(); |
| | 2 | 80 | | SparseVoxelBlockCapacityPool.Clear(); |
| | 2 | 81 | | ScanCellPool.Clear(); |
| | 2 | 82 | | VoxelOccupantDictionaryPool.Clear(); |
| | 2 | 83 | | VoxelOccupantBucketPool.Clear(); |
| | 2 | 84 | | } |
| | | 85 | | } |