← Back to Pull requests

Copy parsed JSON values into objects and arrays instead of moving them #12796

Closed
unknown closed this pull request without merging on Nov 05, 2026 12:55 UTC
View on GitHub
unknown unknown commented on Nov 04, 2026 20:36 UTC
Author

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 Json is the more expensive of the two, because its move constructor allocates a null value. For a 1000-tree model, Json::Load of the JSON file gets 24% faster and XGBoosterLoadModel 21% faster. Single-row inplace prediction gets 6.8% faster.

What

JsonReader::ParseArray and JsonReader::ParseObject (src/common/json.cc) now copy the parsed Json into the container. Before, they moved it. Nothing else changes.

Why

Json(Json&&) swaps with a freshly constructed ptr_{new JsonNull}. So every move into a container allocates a JsonNull for the local, and that JsonNull is 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 Json it 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::Load of 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% |
| XGBoosterLoadModel from that JSON file | 16.38 ms [15.83–16.82] | 12.97 ms [12.78–13.20] | -20.8% |
| XGBoosterLoadModel from 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

  • The existing Json.TestParseObject now checks that the last duplicate key wins when replacing a nested container. The standalone Json.ParseContainerValues test was removed; existing parser and round-trip tests cover the remaining behavior.

  • After consolidating the test, all 50 selected JSON, UBJSON, learner and CPU serialization gtests pass on Linux CPU, and applicable pre-commit hooks pass.

  • Previously reported validation: the Json, UBJson, Learner, serialization, Tree, predictor and DMatrix gtest suites (210 tests), plus tests/python/test_basic_models.py, test_model_io.py, test_model_compatibility.py, test_config.py, test_predict.py, test_dmatrix.py, test_basic.py and test_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 malloc and free. The production change touches 4 lines of code in one file. To revert the optimization, restore the two container insertions in src/common/json.cc.

    Diff: 415 lines (+311 / -104) across 5 files Reviewer: RAMitchell Review duration: 979m
    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
    0.45
    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%
    60%
    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.361
    Shallow Review Depth shallow_review_depth
    0.289
    Real PR Conversation Timeline
    1 comments
    "Review active on #12796"
    general weight: 0.35
    Reviewers
    Assignees
    Unassigned
    Labels
    risk: low confidence: 60.5% 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: 44.9% (MEDIUM) · Higher = larger blast radius
    44.9%
    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: 60.5% · Lower = rushed or shallow review
    60.5%
    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: 17.8% · Safe to merge
    17.8%
    Repository