Fast unsafe collections for memory reuse by stack type. Adding elements without overhead when increasing Capacity. Can also be used in as classic collection with resizing or on a custom memory allocator.
Allows you to allocate memory for a method / class and place all sets of variables in it. Avoid repeated copying of structures when placing them in collections. And other use cases.
The generated code uses .Net 6 features. So use only with .Net 6+.
Supported collections:
- Stack
- List
- Queue
Usage:
unsafe
{using(varmemory=newStruct.StackMemory(sizeof(int)*100))//Allocate memory for all your collections 400 byte.{{usingvarlistOfInt32=newStruct.ListOfInt32(2,&memory);//2 * 4 = 8 bytefor(inti=0;i<48;i++){listOfInt32.Add(ini);//+ 4 byte, shift index memory.Current by 4 byte: memory.Current +=4;}//listOfInt32 is 200 bytelist.ExpandCapacity(50);// + 200 byte: memory.Current +=200;list.TrimExcess();// - 200 byte: memory.Current -=200;//Do whatever you want with list of Int32 items}//return memoryvarlistOfInt64=newStruct.ListOfInt64(50,&memory);//get memory 400 byte//Do whatever you want with list of Int64 items}//free all memory}In our example, we will allocate a list of 50 elements on this memory. Then we increase the capacity to the 100 elements. No copying or reallocation. Then we free old collection and allocate new collection of Int64 on the same memory.
In the future(TODO), you can compress memory if there are areas that are no longer used, thereby not sealing the collection. This can be useful for allocating memory for an entire method if we know approximately how much memory it can consume at the maximum.
Stack of composite type example:
//Marking a class/struct with attributes is all that is required of you.[GenerateStack][GenerateWrapper]publicstructSimpleStruct{publicSimpleStruct(intint32,longint64){Int32=int32;Int64=int64;}publiclongInt64;publicintInt32;}[GenerateList][GenerateWrapper]publicclassSimpleClass{publicSimpleClass(intint32,longint64){Int32=int32;Int64=int64;}publiclongInt64;publicintInt32;}//Stack of pointers
unsafe
{using(varmemory=newStruct.StackMemory(SimpleStructHelper.SizeOf+(nuint)sizeof(IntPtr))){usingvarstack=newStruct.StackOfIntPtr(1,&memory);{varitem=newStruct.SimpleStructWrapper(&memory);item.Int32=456;*stack.TopFuture()=newIntPtr(item.Ptr);stack.PushFuture();}varitem2=newStruct.SimpleStructWrapper(stack.Top().ToPointer());//item2 point to same memory as is item}}//All alocate memory = SimpleStructHelper.SizeOf * 100 = 12* 100 = 1200 byte
unsafe
{using(varmemory=newStruct.StackMemory(JobStructHelper.SizeOf*(nuint)100))//allocate memory{{varitem=newStruct.SimpleStructWrapper(memory.Start,false);usingvarstackOfSimpleStruct=newStruct.StackOfSimpleStruct((nuint)100,&memory);//get memoryfor(inti=0;i<100;i++){item.ChangePtr(stackOfSimpleStruct.TopFuture());item.Int32=i;item.Int64=i*2;stackOfSimpleStruct.PushFuture();}//Do whatever you want with stack}//return memoryvaritem=newStruct.SimpleClassWrapper(memory.Start,false);varlistOfSimpleClass=newStruct.ListOfSimpleClass((nuint)100,&memory);//get memoryfor(inti=0;i<100;i++){item.ChangePtr(listOfSimpleClass.GetFuture());item.Int32=i;item.Int64=i*2;listOfSimpleClass.AddFuture();}}//free all memory}