This is smmalloc allocator version for Unity Engine with support of AllocatorManager.IAllocator and Unity.Collections.
smmalloc is a fast and efficient "proxy" allocator designed to handle many small allocations/deallocations in heavy multithreaded scenarios. The allocator created for using in applications where the performance is critical such as video games. Designed to speed up the typical memory allocation pattern in C++ such as many small allocations and deletions. This is a proxy allocator means that smmalloc handles only specific allocation sizes and pass-through all other allocations to generic heap allocator.
- Unity Burst: 1.8.8
- Unity Collections: 2.1.4
- Windows x64
// 8 buckets, 4 BM each
SmAllocatorUtility.Create(8, 4 * 1024 * 1024, out var allocator);
SmAllocatorUtility.Destroy(ref allocator);
// buffer with 100 int elements (400 bytes memory block)
var buffer = SmAllocatorUtility.Malloc<int>(100, UnsafeUtility.SizeOf<int>(), UnsafeUtility.AlignOf<int>(),
allocator.Handle);
P.S.: Don't forget unsafe word!
SmAllocatorUtility.Free(100, buffer, allocator.Handle);
P.S.: Don't forget unsafe word!
// NativeArray<T> with int 100 elements
var array = SmAllocatorUtility.AllocateArray<int>(100, ref allocator);
SmAllocatorUtility.DisposeArray(ref array);
For example NativeList:
// allocate
var nativeList = new NativeList<int>(allocator.Handle);
// deallocate
nativeList.Dispose();
Or NativeHashSet:
// allocate with 10 initial capacity
var nativeHashSet = new NativeHashSet<int>(10, allocator.Handle);
// deallocate
nativeHashSet.Dispose();
// create cache config with description (2048 bytes) for each allocator block
var cacheConfig = Enumerable.Repeat(2048u, 8).ToArray();
// create thread cache
allocator.CreateThreadCache(cacheConfig, CacheWarmupOptions.CacheHot);
allocator.DestroyThreadCache();