Skip to content

Plain decision tree builders still carry the waste that #349 removed from the RF variant #396

Closed

Description

@godofecht

Found while measuring #234 and #237 (BaggingClassifier.fit, BaggingRegressor.fit — PRs #389, #390). Filing separately because the fix belongs in tree.flow, not in the ensemble wrappers.

The measurement that motivates it

Both Bagging fit issues were optimized the same way PR #349 optimized RandomForest fit: bootstrap scratch allocated once, row-major memcpy instead of ~115k matrix_set/matrix_at pairs per estimator. That phase got 22x-27x faster in isolation.

End-to-end it made no difference, because the phase is 1-3% of fit:

bootstrap phase share of fit end-to-end
BaggingClassifier, digits 1797x64 5.611 -> 0.253 ms (22x) 1.10% 440.9 -> 421.6 ms, repeats span 0.99x-1.07x
BaggingRegressor, digits 1797x64 4.987 -> 0.187 ms (27x) 1.30% 18.07 -> 17.65 ms (1.024x)

97-99% of both fits is decision_tree_classifier_fit and _build_regressor_tree in tree.flow.

What is actually left on the table

PR #349 improved _build_classifier_tree_rf, the random-forest-specific builder. The plain builders that Bagging, DecisionTreeClassifier and DecisionTreeRegressor all use did not get the same treatment and still have:

  • the constant-column scan retained (candidate columns that cannot yield a split are still evaluated)
  • per-element matrix_at in the split search rather than hoisted row offsets into X.data
  • gini computed over the full label set rather than the classes actually present at the node
  • right_counts rebuilt with a second full pass instead of being seeded from the node histogram

Constraints on any fix

This is a bigger win than any of the ensemble-level work: it is the 97%, and it is shared by every tree-based estimator in the library.

Activity

  1. godofecht commented on Aug 19, 2026

    @godofecht
    OwnerAuthor

    Profile first: where the plain builder's time actually goes

    Before changing anything, I built a harness carrying a verbatim copy of
    _build_classifier_tree with per-phase timers, and ran it on the canonical
    DecisionTree digits row (1437 x 64, depth 10, criterion gini, the same
    benchmarks/split_digits.bin split and StandardScaler the headline benchmark
    uses). FLOW_HOST=python FLOW_OPT_LEVEL=0, load average 6.5.

    The instrumented copy reproduces the canonical accuracy 0.833333313 and the
    canonical 245 nodes, so it is the real builder, not an approximation.

    library fit  37.923 ms  accuracy 0.833333313  n_nodes 245
    instrumented 34.301 ms  accuracy 0.833333313  n_nodes 245
    
    phase attribution (instrumented total 34.301 ms)
      setup (class_idx, parent gini, allocs)     0.368 ms    1.1%
      gather (matrix_at per element)             3.713 ms   10.8%
      sort (_sort_pairs)                        23.940 ms   69.8%
      hist seed (second full pass)               1.434 ms    4.2%
      scan (threshold sweep + gini)              4.427 ms   12.9%
      partition + frees                          0.153 ms    0.4%
      leaf nodes                                 0.032 ms    0.1%
    
    counts
      split nodes            177
      leaf nodes             68
      feature scans          11328
      constant columns       3512
      sum n_indices (splits) 11791
      impurity evaluations   49148
      sum classes present    574  (of 1770 possible)
    

    The sort is 69.8% of the builder. Everything the issue lists as remaining
    waste sums to about 28%:

    • per-element matrix_at in the gather: 10.8%
    • the threshold scan, including gini over the full label set: 12.9%
    • right_counts rebuilt with a second full pass: 4.2%
    • the constant-column scan: 3512 of 11328 feature scans, 31%, are columns that
      are constant at that node and cannot produce a candidate at all. They still
      get gathered, sorted and swept.
    • gini over the full label set: nodes hold 574 distinct classes summed over 177
      split nodes, a mean of 3.2 of the 10 labels. 68% of every _gini_from_counts
      term is a guaranteed zero, evaluated 49148 x 2 times.

    So the #349 techniques are worth having, and they are worth roughly 28% of the
    builder combined. The other 70% is the shell sort, and #349 could not touch it
    because a bottom-up merge sort measured slower there.

    The sort is however replaceable, and the reason is already written down in
    tree.flow: the split scan reads counts only at boundaries where consecutive
    values differ, and at such a boundary the set of samples on the left is fixed by
    the value sequence, not by the order among equal values. For the classifier the
    counts are exact integers, so any correct ascending sort yields a bit-identical
    split. That means the per-node sort can be replaced by carrying each feature's
    sorted order down the tree: sort every column once at the root, then split a
    node's order into the two children with an order-preserving partition, O(n) per
    feature per node instead of O(n log n).

    That argument does NOT hold for _build_regressor_tree. Its scan accumulates
    left_sum and left_sum_sq in f32 in sorted position order, so a different
    order among equal values changes the rounding and can flip a strict <
    comparison. The regressor therefore keeps _sort_pairs_f32 untouched and only
    gets the order-neutral wins (constant-column skip, hoisted row offsets).

    Plan, in that order, each measured and pushed on its own:

    1. the perf: RandomForestClassifier.fit allocation and split-scan cleanup (roadmap #18) #349 techniques applied to _build_classifier_tree (~28% of the builder)
    2. presorted column order carried down the tree, removing the 70% sort
    3. the order-neutral subset applied to _build_regressor_tree

    Split selection is unchanged throughout: features stay scanned in ascending
    index order with a strict <, so tests/test_tree_split_tiebreak.flow (#426)
    keeps passing, and the fitted trees are compared element-wise against the
    pre-change builder rather than only by accuracy.

  2. added 2 commits that reference this issue on Aug 19, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions