AnnulusGames / LitMotion

Lightning-fast and Zero Allocation Tween Library for Unity.

Home Page:https://annulusgames.github.io/LitMotion/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Introduce Struct Wrapper (without pooling)

Akeit0 opened this issue · comments

Related to #33
On delegate binding, struct wrapper class will reduce allocation even if it is not pooled.
Because non static lambda allocates an anonymous wrapper and a delegate.
Furthermore, as for int types, you can share the ones you use most often.

public class ReadOnlyIntBox{
    public readonly int Value;
    public ReadOnlyIntBox(int value){
        Value=value;
    }
   static ReadOnlyIntBox[] sharedBoxes=new ReadOnlyIntBox[sharedSize] ;
    const sharedSize=16;
    public static Create(int value){
        if(0<=value&&value<sharedSize){
             ref var shared=ref sharedBoxes[value];
             if(shared==null) shared=new ReadOnlyIntBox(value);
             return shared;
        }
        return new ReadOnlyIntBox(value);
    }
}