Repository navigation
Description
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_atin the split search rather than hoisted row offsets intoX.data - gini computed over the full label set rather than the classes actually present at the node
right_countsrebuilt with a second full pass instead of being seeded from the node histogram
Constraints on any fix
- Impurity must stay
f32evaluated in ascending class order to remain bit-identical. perf: RandomForestClassifier.fit allocation and split-scan cleanup (roadmap #18) #349 established this: it rules out the incremental sum-of-squares trick, and hand-inlining the impurity helper measured flat (1.196 vs 1.200). _sort_pairsis shared with the DecisionTree canonical row, so a change there moves that row too. perf: RandomForestClassifier.fit allocation and split-scan cleanup (roadmap #18) #349 switched it from halving gaps to Knuth's 3h+1; a bottom-up merge sort measured slower (1.155 vs 1.200) and was dropped.- Whatever lands must keep the fitted trees identical for a given seed, since Reduce Digits DecisionTree predictive disparity and compare learned tree structure #204 and Reduce Digits RandomForest disparity and align bootstrap / per-tree randomness #205 track DecisionTree and RandomForest disparity against sklearn.
- Read Tree nodes are malloc'd without initialisation, so unwritten fields hold nondeterministic garbage #359 first.
tree.flow:216mallocs nodes and leaves some fields unwritten, so any node-structure comparison must skip the fields the builder does not write for that node kind, or it will chase nondeterministic garbage.
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
godofecht commentedon Aug 19, 2026
Profile first: where the plain builder's time actually goes
Before changing anything, I built a harness carrying a verbatim copy of
_build_classifier_treewith per-phase timers, and ran it on the canonicalDecisionTree digits row (1437 x 64, depth 10, criterion gini, the same
benchmarks/split_digits.binsplit and StandardScaler the headline benchmarkuses).
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.
The sort is 69.8% of the builder. Everything the issue lists as remaining
waste sums to about 28%:
matrix_atin the gather: 10.8%right_countsrebuilt with a second full pass: 4.2%are constant at that node and cannot produce a candidate at all. They still
get gathered, sorted and swept.
split nodes, a mean of 3.2 of the 10 labels. 68% of every
_gini_from_countsterm 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 consecutivevalues 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 accumulatesleft_sumandleft_sum_sqin f32 in sorted position order, so a differentorder among equal values changes the rounding and can flip a strict
<comparison. The regressor therefore keeps
_sort_pairs_f32untouched and onlygets the order-neutral wins (constant-column skip, hoisted row offsets).
Plan, in that order, each measured and pushed on its own:
_build_classifier_tree(~28% of the builder)_build_regressor_treeSplit selection is unchanged throughout: features stay scanned in ascending
index order with a strict
<, sotests/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.
test: pin the plain tree builders against the pre-#396 implementation
Merge pull request #432 from godofecht/perf/396-plain-tree-builders