Copy parsed JSON values into objects and arrays instead of moving them #12796
All Vouch review confidence checks passed
Residual risk 0.18 = Change Risk (0.45) × (1 − Review Confidence (0.60)).
Review depth and reviewer attention state are adequate.
Model 1 · Change Risk (XGBoost)
Score: 0.45 · 415 lines diff across 5 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.361
Shallow Review Depth
shallow_review_depth
0.289
Real PR Conversation Timeline
1 comments
"Review active on #12796"
general
weight: 0.35
RAMitchell
Follow-up to #12311 and #12317. The JSON text reader now copies each parsed value into its array or object instead of moving it. Moving a
Jsonis the more expensive of the two, because its move constructor allocates a null value. For a 1000-tree model,Json::Loadof the JSON file gets 24% faster andXGBoosterLoadModel21% faster. Single-row inplace prediction gets 6.8% faster.What
JsonReader::ParseArrayandJsonReader::ParseObject(src/common/json.cc) now copy the parsedJsoninto the container. Before, they moved it. Nothing else changes.Why
Json(Json&&)swaps with a freshly constructedptr_{new JsonNull}. So every move into a container allocates aJsonNullfor the local, and thatJsonNullis freed when the local goes out of scope. A copy only increments the reference count, and the local releases its reference at the end of the iteration. The stored value ends up with a single owner either way.This reverses the "move into the container" part of #12317, which expected a move to be cheaper than a copy. For
Jsonit is not, because of the allocation above.Measurements
Apple M4 Pro, Apple clang 21.0.0, CMake
Release, no OpenMP,nthread=1. Master f109cc0 against this branch, same flags. Runs interleaved, 8 per side. Each run is the median of 5 repeats. Tables show the median of the runs, with the range in brackets.| Case | master | this PR | Change |
|---|---|---|---|
|
XGBoosterPredictFromDense, 1 row × 127 features, 50 trees (#12311) | 3628 ns [3586–4706] | 3383 ns [3344–3414] | -6.8% || Same, 10k rows | 3.626 ms [3.612–3.676] | 3.629 ms [3.609–3.647] | +0.1% (noise) |
|
Json::Loadof a 1.5 MB JSON model (1000 trees, depth 6) | 15.26 ms [15.12–15.93] | 11.66 ms [11.58–11.82] | -23.6% ||
XGBoosterLoadModelfrom that JSON file | 16.38 ms [15.83–16.82] | 12.97 ms [12.78–13.20] | -20.8% ||
XGBoosterLoadModelfrom the same model as UBJSON | 2.65 ms [2.58–2.84] | 2.67 ms [2.64–2.73] | +0.6% (noise; UBJSON has its own reader) ||
XGBoosterSaveJsonConfig+XGBoosterLoadJsonConfig| 137.0 µs [135.3–140.4] | 135.7 µs [135.0–137.2] | -0.9% |Predictions are bit-identical on both sides.
How to test
Json.TestParseObjectnow checks that the last duplicate key wins when replacing a nested container. The standaloneJson.ParseContainerValuestest was removed; existing parser and round-trip tests cover the remaining behavior.Json,UBJson,Learner, serialization,Tree, predictor and DMatrix gtest suites (210 tests), plustests/python/test_basic_models.py,test_model_io.py,test_model_compatibility.py,test_config.py,test_predict.py,test_dmatrix.py,test_basic.pyandtest_with_pandas.py.Risk and revert
The parsed result is the same: each container ends up owning one reference to each value, just as before. What changes is the cost per value, which is now an atomic increment and decrement instead of a
mallocandfree. The production change touches 4 lines of code in one file. To revert the optimization, restore the two container insertions insrc/common/json.cc.