Micro optimisations iterating through a tree in C#

前端 未结 3 876
情歌与酒
情歌与酒 2021-01-14 16:29

I\'m working on a massive number crunching project. I\'ve been optimising everything since the start as I knew it would be important. Doing performance anal

相关标签:
3条回答
  • 2021-01-14 17:12

    Given the points made in the other answer about caching, but not in relation to the null check, try ordering the references to the BranchNodeData fields so that the first reference allows all of the following fields to be loaded into the cache.

    That is, I assume the Jitter, or the CPU, is not smart enough to load "backwards" to cache SplitInputIndex, SplitValue and Child1 when Child2 is referenced first in the current code.

    So either change the order of the fields in the BranchNodeData class, or change the set; if ... overwrite; to an if ... else.

    0 讨论(0)
  • 2021-01-14 17:19

    Just some code rewrite. It might help because it avoids at least two jumps.

    public ScTreeNode GetNodeForState(int rootIndex, float[] inputs)
    {
    
        ScTreeNode node = RootNodes[rootIndex].TreeNode;
    
        while (node.BranchData != null)
        {
            BranchNodeData b = node.BranchData;
            node = b.Child2;
            if (inputs[b.SplitInputIndex] <= b.SplitValue))
                node = b.Child1;
        }
    
        return node;
    
    }
    
    0 讨论(0)
  • 2021-01-14 17:22

    BranchNodeData looks like a reference type. its only 0.2% of your runtime because its just making a pointer to the data that already exists, not actually copying or assigning anything.

    You're probably getting such a hit on the null check because the CLR is having to do a cast in order to check the sealed class you've pasted in. Checking for nullity there isn't necessarily what you're after. There are a ton of ways to modify that class to give you a boolean to check against that wouldn't require as much computing power. I'd honestly go the route of having that be something that your ScTreeNode class can provide.

    0 讨论(0)
提交回复
热议问题