← Back to Pull requests

perf(arrow-array): build null, map, and dictionary arrays directly in builder finish (#11717) #12353

Closed
unknown closed this pull request without merging on Nov 23, 2026 05:00 UTC
View on GitHub
unknown unknown commented on Nov 20, 2026 03:50 UTC
Author

Which issue does this PR close?

  • Part of #11717

  • Part of #9298
  • Rationale for this change

    #12228 moved the primitive, boolean, byte and fixed-size-binary builders off ArrayData, leaving the remaining builders on the old path:

    values + nulls
        → ArrayDataBuilder
        → ArrayData { buffers: Vec<Buffer> }
        → typed array
    

    Every finish in these builders still pays for a Vec<Buffer>, the ArrayData construction, buffer-count checks, and the Vec teardown. The overhead is a fixed cost per array, so it dominates when callers finish many small arrays.

    NullBuilder was the most extreme case: building a NullArray of any length does not need any buffer at all, yet it went through ArrayData.

    This also removes the last unsafe block that existed only to satisfy ArrayData's invariants rather than an invariant of the typed array itself.

    What changes are included in this PR?

  • NullBuilder::finish / finish_cloned now construct NullArray::new(len) directly.

  • MapBuilder::finish_helper now builds OffsetBuffer and calls MapArray::new_unchecked instead of assembling ArrayData. The now-derivable len parameter was removed from finish_helper and its three callers.

  • The three dictionary builders (PrimitiveDictionaryBuilder, GenericByteDictionaryBuilder, FixedSizeBinaryDictionaryBuilder) now call DictionaryArray::new_unchecked(keys, Arc::new(values)) in finish, finish_cloned, and finish_preserve_values instead of building data types and child data through ArrayDataBuilder.
  • The dictionary builders no longer construct the dictionary DataType themselves; new_unchecked derives it from the keys and values arrays.

    Imports that became unused (arrow_data::ArrayData, ArrayDataBuilder, and production-side DataType) were removed. Tests that still need DataType import it locally.

    This PR intentionally does not change UnionBuilder, which needs dynamic dispatch from DataType to a typed constructor.

    Are these changes tested?

    The existing builder suites cover finish, finish_cloned, finish_preserve_values, builder reuse, null handling, empty maps, and dictionary key overflow. The full arrow-array test suite passes (762 unit tests and 209 doctests), as does the force_validate configuration (750 tests) and the arrow array transform tests (37 tests). Clippy with --all-features --all-targets -- -D warnings and cargo fmt --all -- --check both pass.

    An earlier revision of this PR included a builder_finish benchmark. It was dropped following review feedback on the benchmark PR (#12356): the change reduces work and allocations visibly by inspection, and adding a micro-benchmark per builder risks chasing allocator noise rather than real regressions. The comparison below was measured locally before the benchmark was removed.

    Local numbers (release, criterion, 1 / 8 / 64 elements, whole build-then-finish loop):

    | Bench | Before | After | Change |
    |---|---:|---:|---:|
    | null_builder/len:8 | 45.1 ns | 342 ps | -99% |
    | map_builder/len:1 | 1.018 µs | 586 ns | -42% |
    | map_builder/len:64 | 1.471 µs | 1.048 µs | -29% |
    | primitive_dictionary/len:1 | 381 ns | 210 ns | -45% |
    | primitive_dictionary/len:64 | 808 ns | 628 ns | -22% |
    | byte_dictionary/len:1 | 602 ns | 410 ns | -32% |
    | fixed_size_binary_dictionary/len:1 | 513 ns | 305 ns | -41% |
    | fixed_size_binary_dictionary/len:64 | 1.47 µs | 1.03 µs | -30% |

    These are local measurements and are not required to justify the change; the primary argument is that the code performs strictly less work: one fewer Vec<Buffer> allocation per finished array, no ArrayData construction, no buffer-count validation, and no DataType reconstruction in the dictionary builders.

    Are there any significant user-facing changes?

    No public API changes. The output arrays keep the same data types, values, nulls, and builder-reset behavior. MapBuilder::finish_helper is private, so removing its len parameter is not user-visible.

    AI usage disclosure

    This change and its tests were prepared with AI assistance. I reviewed the implementation and verified the tests, Clippy, and formatting locally.

    Diff: 523 lines (+392 / -131) across 6 files Reviewer: Mola-maker Review duration: 4390m
    All Vouch review confidence checks passed
    Residual risk 0.21 = Change Risk (0.48) × (1 − Review Confidence (0.57)). Review depth and reviewer attention state are adequate.
    Model 1 · Change Risk (XGBoost)
    Score: 0.48 · 523 lines diff across 6 files
    0.48
    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.375
    Shallow Review Depth shallow_review_depth
    0.289
    Real PR Conversation Timeline
    1 comments
    "Review active on #12353"
    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: 47.8% (MEDIUM) · Higher = larger blast radius
    47.8%
    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: 20.7% · Safe to merge
    20.7%