← Back to Pull requests

[RF] Avoid quadratic copying in CodegenContext::buildArg(std::span<const double>) #24297

Closed
unknown closed this pull request without merging on Nov 23, 2026 13:12 UTC
View on GitHub
unknown unknown commented on Nov 23, 2026 08:56 UTC
Author

[RF] Avoid quadratic copying in CodegenContext::buildArg(std::span<const double>)

Changes or fixes:


buildArg() called _xlArr.reserve(_xlArr.size() + n) before appending each array. Reserving exactly the new size defeats the vector's geometric growth, so every call reallocated and copied the whole constants array. The total cost was quadratic in the number of calls and the array size.

This shows up for large HistFactory models with codegen, where each sample appends its nominal and variation arrays. For an ATLAS model with ~36 channels, "Function JIT time" grew from 29 s (18 channels) to 35 min (36 channels). A backtrace showed the time spent in memcpy under vector::reserve() with _xlArr holding 24.6 M doubles (~200 MB), i.e. copying that much on every call.

The fix appends with _xlArr.insert(_xlArr.end(), arr.begin(), arr.end()), without the reserve. The generated code and the contents of xlArr are unchanged.

Assisted-by: claude:claude-opus-5-5. The AI wrote the commits and their messages and I tested them locally.

Checklist:

  • tested changes locally

  • updated the docs (if necessary)
  • Timing studies

    I studied the time spent in roofit codegen for different numbers of regions in the workspace (the full workspace has 150 regions). I observed that the time for the function JIT time has a strong non-linear scaling.

    The timings are plotted in the following figure. The points for fits with 6, 18 and 36 regions are without this fix, the points with 72 and 150 regions are with this fix applied.

    <img width="764" height="562" alt="image" src="https://github.com/user-attachments/assets/54a2e497-a2eb-44cb-bb71-9628838c0531" />

    Diff: 210 lines (+157 / -53) across 2 files Reviewer: guitargeek Review duration: 255m
    All Vouch review confidence checks passed
    Residual risk 0.16 = Change Risk (0.41) × (1 − Review Confidence (0.60)). Review depth and reviewer attention state are adequate.
    Model 1 · Change Risk (XGBoost)
    Score: 0.41 · 210 lines diff across 2 files
    0.41
    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.320
    Shallow Review Depth shallow_review_depth
    0.289
    Ai Authored ai_authored
    0.050
    Real PR Conversation Timeline
    1 comments
    "Review active on #24297"
    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: 41.5% (MEDIUM) · Higher = larger blast radius
    41.5%
    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: 16.4% · Safe to merge
    16.4%