← Back to Pull requests

perf: reuse expression selectors to reduce CPU and memory usage #20450

Closed
unknown closed this pull request without merging on Oct 14, 2026 06:05 UTC
View on GitHub
unknown unknown commented on Sep 29, 2026 06:05 UTC
Author

Description

On-heap incremental ingestion creates a new aggregator for every new rollup key. For expression-based aggregators, each factorization previously rebuilt the expression bindings and row-value selector even when the selector factory and memoized parsed Expr were unchanged.

This repeated work is significant for ingestion workloads with many rollup keys and expression metrics.

<details>
<summary>A live profiling for a task that has 6 expressions in the metric spec shows that Expression.plan accounts for 30% of all samples.</summary>

<img width="738" height="651" alt="Profile" src="https://github.com/user-attachments/assets/39c564a6-829f-465d-8fbc-9ddd3cf3c384" />

</details>

The investigation also found an existing correctness issue in RowBasedExpressionColumnValueSelector: transformed expressions were cached using only the hash code of the array-binding list. Distinct binding lists such as ["Aa"] and ["BB"] have the same Java hash code and could therefore reuse the wrong transformed expression.

Changes

  • Add an ExprEvalSelectorCache contract and implement it in OnheapIncrementalIndex.CachingColumnSelectorFactory.

  • Cache completed ColumnValueSelector<ExprEval> instances by the identity of the original parsed expression.

  • Keep aggregator instances and their mutable aggregation state independent; only the row-value selector is shared.

  • Use explicit get, construct, and put operations because selector construction can re-enter the same factory while resolving input columns.

  • Change the transformed-expression cache from a hash-code key to the complete array-binding list, allowing normal list equality to distinguish hash collisions.

  • Add ExpressionIngestionBenchmark as a reproducible JMH benchmark for this ingestion path.
  • Scope

  • Selector caching is limited to the on-heap incremental-ingestion selector factory.

  • Query-time selector factories are unchanged.

  • IncrementalIndex.add remains single-threaded or externally synchronized; this change does not introduce concurrent selector sharing.

  • ExpressionLambdaAggregatorFactory is unchanged because it plans its expressions directly.

  • The transformed-expression cache fix is included so selector reuse exercises the correct combined path.

  • The benchmark covers incremental-index ingestion with many new rollup keys and expression metrics. It does not measure Kafka networking, JSON parsing, compression, persistence, concurrent task activity, or end-to-end production ingestion throughput.
  • Benchmark

    ExpressionIngestionBenchmark measures on-heap ingestion of 100,000 rows into 25,000 rollup keys using three expression metrics. Input-row construction is excluded from the measured invocation.

    Each variant was measured with JMH 1.37 on JDK 25 using three JVM forks, three single-shot warmup iterations, seven single-shot measurement iterations, a 1 GiB heap, and the JMH GC profiler.

    | Variant | Mean elapsed | Approx. throughput | Allocation |
    |---|---:|---:|---:|
    | PR merge-base d2bff3c9ec | 364.981 ± 16.502 ms | 273,987 rows/s | 985.00 MiB/op |
    | Expression-selector cache abe0fc5847 | 129.557 ± 4.759 ms | 771,860 rows/s | 162.00 MiB/op |

    Selector caching reduced elapsed time by 64.5%, increased approximate throughput by 2.82x, and reduced allocation by 83.6% in this workload.

    <details>
    <summary>CPU usage of previous same ingestion task dropped from 10% to 5%, and heap never exeeds 6GiB for the same ingestion task in live env</summary>

    <img width="780" height="990" alt="image" src="https://github.com/user-attachments/assets/926c5c3f-8d86-457b-a96f-766520614c17" />
    </details>

    Diff: 461 lines (+345 / -116) across 5 files Reviewer: GWphua Review duration: 21600m
    All Vouch review confidence checks passed
    Residual risk 0.18 = Change Risk (0.46) × (1 − Review Confidence (0.60)). Review depth and reviewer attention state are adequate.
    Model 1 · Change Risk (XGBoost)
    Score: 0.46 · 461 lines diff across 5 files
    0.46
    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.367
    Shallow Review Depth shallow_review_depth
    0.289
    Real PR Conversation Timeline
    1 comments
    "Review active on #20450"
    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: 45.6% (MEDIUM) · Higher = larger blast radius
    45.6%
    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: 18% · Safe to merge
    18%
    Repository