carmineos / rage-toolkit

Other
19 stars 6 forks source link

ResourceHelpers.GetBlocks: try to reduce allocations #9

Open carmineos opened 3 years ago

carmineos commented 3 years ago

Try to reduce allocations when GetReferences and GetParts are invoked.

carmineos commented 2 years ago

A possible change could be editing both GetParts and GetReferences to take directly an HashSet as parameter

public override void GetParts(HashSet<Tuple<long, IResourceBlock>> blocks)
{
    base.GetParts(blocks);
    // Add parts to the hashset
}

public override void GetReferences(HashSet<IResourceBlock> blocks)
{
    base.GetReferences(blocks);
    // Add references to the hashset
}

which would also require the setter of BlockPosition to create a temporary HashSet

public virtual long BlockPosition
{
    get
    {
        return position;
    }
    set
    {
        position = value;
        var parts = new HashSet<Tuple<long, IResourceBlock>>();
        GetParts(parts);
        foreach (var part in parts)
        {
            part.Item2.BlockPosition = value + part.Item1;
        }
    }
}