我有一个性能至关重要的二进制决策树,我想将这个问题集中在一行代码上。下面是二叉树迭代器的代码,其中包含针对它进行性能分析的结果。
        public ScTreeNode GetNodeForState(int rootIndex, float[] inputs)
        {
0.2%        ScTreeNode node = RootNodes[rootIndex].TreeNode;
24.6%       while (node.BranchData != null)
            {
0.2%            BranchNodeData b = node.BranchData;
0.5%            node = b.Child2;
12.8%           if (inputs[b.SplitInputIndex] <= b.SplitValue)
0.8%                node = b.Child1;
            }
0.4%        return node;
        }BranchData是一个字段,而不是属性。我这样做是为了防止不被内联的风险。
BranchNodeData类如下:
public sealed class BranchNodeData
{
    /// <summary>
    /// The index of the data item in the input array on which we need to split
    /// </summary>
    internal int SplitInputIndex = 0;
    /// <summary>
    /// The value that we should split on
    /// </summary>
    internal float SplitValue = 0;
    /// <summary>
    /// The nodes children
    /// </summary>
    internal ScTreeNode Child1;
    internal ScTreeNode Child2;
}如您所见,while循环/空值检查对性能造成了巨大影响。那棵树很大,所以我希望寻找一片叶子需要一段时间,但是我想了解在那条线上花费的时间不成比例。
我试过了:
- 将Null检查与一会儿分开-正是Null检查才是命中。
- 向对象添加一个布尔字段并进行检查,这没有什么区别。所比较的内容无关紧要,而是比较是问题所在。
这是分支预测问题吗?如果是这样,我该怎么办?如果有什么?
我不会假装理解CIL,但我会把它发布给任何人,以便他们可以尝试从中获取一些信息。
.method public hidebysig
instance class OptimalTreeSearch.ScTreeNode GetNodeForState (
    int32 rootIndex,
    float32[] inputs
) cil managed
{
    // Method begins at RVA 0x2dc8
    // Code size 67 (0x43)
    .maxstack 2
    .locals init (
        [0] class OptimalTreeSearch.ScTreeNode node,
        [1] class OptimalTreeSearch.BranchNodeData b
    )
    IL_0000: ldarg.0
    IL_0001: ldfld class [mscorlib]System.Collections.Generic.List`1<class OptimalTreeSearch.ScRootNode> OptimalTreeSearch.ScSearchTree::RootNodes
    IL_0006: ldarg.1
    IL_0007: callvirt instance !0 class [mscorlib]System.Collections.Generic.List`1<class OptimalTreeSearch.ScRootNode>::get_Item(int32)
    IL_000c: ldfld class OptimalTreeSearch.ScTreeNode OptimalTreeSearch.ScRootNode::TreeNode
    IL_0011: stloc.0
    IL_0012: br.s IL_0039
    // loop start (head: IL_0039)
        IL_0014: ldloc.0
        IL_0015: ldfld class OptimalTreeSearch.BranchNodeData OptimalTreeSearch.ScTreeNode::BranchData
        IL_001a: stloc.1
        IL_001b: ldloc.1
        IL_001c: ldfld class OptimalTreeSearch.ScTreeNode OptimalTreeSearch.BranchNodeData::Child2
        IL_0021: stloc.0
        IL_0022: ldarg.2
        IL_0023: ldloc.1
        IL_0024: ldfld int32 OptimalTreeSearch.BranchNodeData::SplitInputIndex
        IL_0029: ldelem.r4
        IL_002a: ldloc.1
        IL_002b: ldfld float32 OptimalTreeSearch.BranchNodeData::SplitValue
        IL_0030: bgt.un.s IL_0039
        IL_0032: ldloc.1
        IL_0033: ldfld class OptimalTreeSearch.ScTreeNode OptimalTreeSearch.BranchNodeData::Child1
        IL_0038: stloc.0
        IL_0039: ldloc.0
        IL_003a: ldfld class OptimalTreeSearch.BranchNodeData OptimalTreeSearch.ScTreeNode::BranchData
        IL_003f: brtrue.s IL_0014
    // end loop
    IL_0041: ldloc.0
    IL_0042: ret
} // end of method ScSearchTree::GetNodeForState编辑:我决定做一个分支预测测试,如果在一段时间内,我添加了一个相同的,所以我们有
while (node.BranchData != null)和
if (node.BranchData != null)在里面。然后,我对此进行了性能分析,执行第一次比较所需的时间是执行总是返回true的第二次比较的时间的六倍。因此,看来这确实是分支预测的问题-我猜我对此无能为力吗?
另一个编辑
如果必须从RAM加载while.check的node.BranchData,则也会发生上述结果-然后将其缓存为if语句。
这是我关于类似主题的第三个问题。这次,我只关注一行代码。关于这个问题,我的其他问题是:
while(true) { /* current body */ if(node.BranchData == null) return node; }。它会改变什么吗?
                while(true) { BranchNodeData b = node.BranchData; if(ReferenceEquals(b, null)) return node; node = b.Child2; if (inputs[b.SplitInputIndex] <= b.SplitValue) node = b.Child1; }检索node. BranchData一次。
                
BranchNode属性的实现。请尝试更换node.BranchData != nullReferenceEquals(node.BranchData, null)。有什么区别吗?