Reserve the tree view vector in GBTreeModelView #12807
All Vouch review confidence checks passed
Residual risk 0.18 = Change Risk (0.42) × (1 − Review Confidence (0.57)).
Review depth and reviewer attention state are adequate.
Model 1 · Change Risk (XGBoost)
Score: 0.42 · 342 lines diff across 4 files
Model 2 · Review Depth (DistilBERT)
Depth Score: 0.17 · 1 substantive comment(s)
Model 3 · Review Timing & Attention
Session adequacy: 100% · Reviewer familiarity: 35%
Bedrock Claude 3 · Risk Narration
(disconnected)
Groq Risk Narration
AWS Bedrock is currently disconnected. Generate high-confidence PR triage narration on-demand using Groq.
Model 1 · Change Risk Attribution
SHAP Weights
Diff Size
diff_size
0.349
Shallow Review Depth
shallow_review_depth
0.289
Real PR Conversation Timeline
1 comments
"Review active on #12807"
general
weight: 0.35
RAMitchell
Related to #12311.
What
GBTreeModelViewnow reserves its vector of tree views before filling it. The existingCHECK_GE(tree_end, tree_begin)moves above the loop, so the tree count is checked beforereserveuses it.Why
The view is rebuilt on every predict call, and the vector grew one
emplace_backat a time. For a 50-tree model that is 7 allocations and 6 moves per call. With a single row, that is a visible share of the call: in asampleprofile of the loop below, building the model view was ~23% ofXGBoosterPredictFromDense.This does not cache anything between calls; it only makes the per-call rebuild cheaper.
How to test
Benchmark: a C loop over
XGBoosterPredictFromDensewith a proxy DMatrix. 1 row, 127 features, 50 trees of depth 6,binary:logistic,nthread=1. Each run reports the best ns/call over 5 x 200,000 calls. I ran 4 rounds, alternatingmasterand this PR, and report the median. Peak RAM is the max resident set size of one 200,000-call run.Environments
| | macOS | Linux |
|---|---|---|
| Machine | Apple M4 Pro | same Mac, Docker 29.7.2 (4 CPUs, 6 GB) |
| OS | macOS 26.5.1 | Ubuntu 24.04.5, kernel 7.0.12-linuxkit, aarch64 |
| Compiler | Apple clang 21.0.0 | gcc 13.3.0 |
| Build |
Release, OpenMP on |Release, OpenMP on |Results (median of 4 rounds)
| | macOS master | macOS this PR | Linux master | Linux this PR |
|---|---:|---:|---:|---:|
| Time per call | 2,849 ns | 2,508 ns (-12.0%) | 2,992 ns | 2,636 ns (-11.9%) |
| Peak RAM | 57.6 MB | 57.5 MB (same) | 54.0 MB | 54.0 MB (same) |
Predictions are identical on both builds. The saving is a fixed ~350 ns per call, so it matters for single-row and small-batch prediction; with large batches, tree traversal dominates. I did not measure the GPU predictor, which uses the same constructor.
<details><summary>All rounds (ns/call, peak RSS KB)</summary>
</details>
tests/python/test_predict.py,test_basic_models.pyandtest_shap.py: 39 passed.Risk and revert
Low. A reversed tree range was already rejected by the same
CHECK_GE; it now fails before the loop instead of after it. The CUDA predictors use the same constructor; the temporary is still a hoststd::vectorand the device copy size is unchanged. Revert is the change insrc/predictor/gbtree_view.h.