← Back to Pull requests

Reserve the tree view vector in GBTreeModelView #12807

Closed
unknown closed this pull request without merging on Nov 06, 2026 15:37 UTC
View on GitHub
unknown unknown commented on Nov 06, 2026 03:06 UTC
Author

Related to #12311.

What

GBTreeModelView now reserves its vector of tree views before filling it. The existing CHECK_GE(tree_end, tree_begin) moves above the loop, so the tree count is checked before reserve uses it.

Why

The view is rebuilt on every predict call, and the vector grew one emplace_back at 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 a sample profile of the loop below, building the model view was ~23% of XGBoosterPredictFromDense.

This does not cache anything between calls; it only makes the per-call rebuild cheaper.

How to test

Benchmark: a C loop over XGBoosterPredictFromDense with 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, alternating master and 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>

macOS
master 2855.2 ns 58912 KB
fix 2468.5 ns 58880 KB
master 2839.2 ns 58960 KB
fix 2509.4 ns 58880 KB
master 2843.0 ns 58992 KB
fix 2512.8 ns 58848 KB
master 2884.7 ns 59008 KB
fix 2507.3 ns 58768 KB

Linux
master 3005.6 ns 55248 KB
fix 2662.6 ns 55320 KB
master 2977.7 ns 55316 KB
fix 2623.2 ns 55188 KB
master 2964.6 ns 55252 KB
fix 2635.4 ns 55384 KB
master 3048.5 ns 55252 KB
fix 2636.4 ns 55368 KB

</details>

tests/python/test_predict.py, test_basic_models.py and test_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 host std::vector and the device copy size is unchanged. Revert is the change in src/predictor/gbtree_view.h.

Diff: 342 lines (+256 / -86) across 4 files Reviewer: RAMitchell Review duration: 751m
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
0.42
Model 2 · Review Depth (DistilBERT)
Depth Score: 0.17 · 1 substantive comment(s)
0.17
Model 3 · Review Timing & Attention
Session adequacy: 100% · Reviewer familiarity: 35%
56%
Bedrock Claude 3 · Risk Narration
(disconnected)
Groq
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
Reviewers
Assignees
Unassigned
Labels
risk: low confidence: 56.8% reviewed
Vouch Risk Breakdown
AI-powered review risk analysis
Change Risk
Change Risk · Model 1 (XGBoost)
Predicted defect probability (0.00–1.00) based on diff churn, modified sensitive files, and author risk history.
Score: 42.3% (MEDIUM) · Higher = larger blast radius
42.3%
Review Confidence
Review Confidence · Models 2 & 3
Scrutiny score (0.00–1.00) combining DistilBERT semantic review depth with reviewer familiarity and session dwell time.
Score: 56.8% · Lower = rushed or shallow review
56.8%
Residual Risk
Residual Risk · Core Equation
Residual = Change × (1 − Confidence)
Unmitigated risk surviving review. ≥ 0.65 triggers an automated re-queue to protect production.
Score: 18.3% · Safe to merge
18.3%
Repository