Giter VIP home page Giter VIP logo

softstonedevelop / stackmemorycollections Goto Github PK

View Code? Open in Web Editor NEW
5.0 1.0 0.0 2.51 MB

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.

License: MIT License

C# 100.00%
collections memory-management stack analysis roslyn-analyzer roslyn-generator list queue unsafe-code netcore allocator memory-allocation stackmemorycollections csharp csharp-sourcegenerator roslyn source-generator fast-collections

stackmemorycollections's Introduction

StackMemoryCollections

Nuget Downloads Stars License

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 (var memory = new Struct.StackMemory(sizeof(int) * 100))//Allocate memory for all your collections 400 byte.
    {   
        {
            using var listOfInt32 = new Struct.ListOfInt32(2, &memory);//2 * 4 = 8 byte
            for(int i = 0; i< 48; i++)
            {
              listOfInt32.Add(in i);//+ 4 byte, shift index memory.Current by 4 byte: memory.Current +=4;
            }
            //listOfInt32 is 200 byte
            list.ExpandCapacity(50);// + 200 byte: memory.Current +=200;
            list.TrimExcess();// - 200 byte: memory.Current -=200;
            //Do whatever you want with list of Int32 items
        }//return memory

        var listOfInt64 = new Struct.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]
public struct SimpleStruct
{
    public SimpleStruct(
        int int32,
        long int64
        )
    {
        Int32 = int32;
        Int64 = int64;
    }

    public long Int64;
    public int Int32;
}

[GenerateList]
[GenerateWrapper]
public class SimpleClass
{
    public SimpleClass(
        int int32,
        long int64
        )
    {
        Int32 = int32;
        Int64 = int64;
    }

    public long Int64;
    public int Int32;
}
//Stack of pointers
unsafe
{
    using (var memory = new Struct.StackMemory(SimpleStructHelper.SizeOf + (nuint)sizeof(IntPtr)))
    {
        using var stack = new Struct.StackOfIntPtr(1, &memory);
        {
            var item = new Struct.SimpleStructWrapper(&memory);
            item.Int32 = 456;
            *stack.TopFuture() = new IntPtr(item.Ptr);
            stack.PushFuture();
        }
        var item2 = new Struct.SimpleStructWrapper(stack.Top().ToPointer());
        //item2 point to same memory as is item
    }
}
//All alocate memory = SimpleStructHelper.SizeOf * 100 = 12* 100 = 1200 byte
unsafe
{
    using (var memory = new Struct.StackMemory(JobStructHelper.SizeOf * (nuint)100))//allocate memory
    {       
        {
            var item = new Struct.SimpleStructWrapper(memory.Start, false);
            using var stackOfSimpleStruct = new Struct.StackOfSimpleStruct((nuint)100, &memory);//get memory
            for (int i = 0; i < 100; i++)
            {
                item.ChangePtr(stackOfSimpleStruct.TopFuture());
                item.Int32 = i;
                item.Int64 = i * 2;
                stackOfSimpleStruct.PushFuture();
            }
        
            //Do whatever you want with stack
        }//return memory

        var item = new Struct.SimpleClassWrapper(memory.Start, false);
        var listOfSimpleClass = new Struct.ListOfSimpleClass((nuint)100, &memory);//get memory
        for (int i = 0; i < 100; i++)
        {
            item.ChangePtr(listOfSimpleClass.GetFuture());
            item.Int32 = i;
            item.Int64 = i * 2;
            listOfSimpleClass.AddFuture();
        }
    }//free all memory
}

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.