← Back to Pull requests

GH-55399: [C++][Parquet] Minor optimize level counting with CountLevels #55408

Closed
unknown closed this pull request without merging on Nov 23, 2026 16:34 UTC
View on GitHub
unknown unknown commented on Nov 05, 2026 05:19 UTC
Author

Rationale for this change

std::count over int16_t levels widens every comparison result to a 64-bit accumulator, preventing efficient 16-bit lane vectorization. Accumulating in uint16_t chunks keeps the reduction in 16-bit lanes without overflowing:

1. x86: https://godbolt.org/z/Gqc7fxe55
2. ARM: https://godbolt.org/z/sGhG8crYa
3. Fast-path vs out-of-line chunked fallback (-O2 x86 AVX2 & ARM NEON): https://godbolt.org/z/rvobWjrfa

Before x86:

vmovq      (%rdi,%rax,2), %xmm6      # Load 8 bytes = 4 levels
vpcmpeqw   %xmm1, %xmm6, %xmm6
vpmovzxwq  %xmm6, %ymm6              # 8×16 bits -> 4×64 bits, only 4 lanes
vpand      %ymm2, %ymm6, %ymm6       # & 1
vpaddq     %ymm6, %ymm0, %ymm0       # 64-bit adds

After x86:

vpcmpeqw   (%rdi,%r8,2), %ymm1, %ymm5   # compare 16 levels
vpsubw     %ymm5, %ymm0, %ymm0          # add 16 lanes

Before ARM:

ldp q18,q19,[x8,#-16]      ; 2 cmeq 16 bits mask
cmeq v18.8h, v18.8h, v16.8h
cmeq v19.8h, v19.8h, v16.8h
ushll2 v20.4s, v18.8h, #0  ; 16->32 bits
ushll  v18.4s, v18.4h, #0
...
and    v22.16b, v22.16b, v17.16b
add    v4.2d, v4.2d, v20.2d
subs x14,x14,#16 ; b.ne

After ARM:

ldp q3,q4,[x9,#-16]
cmeq v3.8h, v3.8h, v2.8h
cmeq v4.8h, v4.8h, v2.8h
sub  v0.8h, v0.8h, v3.8h
sub  v1.8h, v1.8h, v4.8h
subs/add/b.ne

Some notes:
1. Inputs larger than uint16_t::max() (65535) use an out-of-line chunked fallback (CountLevelsIfChunked), so the common single-page path avoids outer-loop LICM hoisting of vector broadcasts (vpbroadcastw + vzeroupper on x86 AVX2 / dup.8h on NEON) on short inputs.
2. Non-template inline wrappers (CountLevels and CountLevelsGreaterEqual) are provided on top of CountLevelsIf to keep call sites concise and ensure a single shared closure type per wrapper across translation units (so the linker ODR-deduplicates CountLevelsIfChunked to one copy per wrapper across physical-type template instantiations and TUs).
3. Optimizes both read and write paths (though level counting is only a small part of end-to-end Parquet I/O).

What changes are included in this PR?

  • Add internal helpers CountLevels, CountLevelsGreaterEqual, CountLevelsIf, and out-of-line fallback CountLevelsIfChunked in cpp/src/parquet/level_comparison_internal.h.

  • Use CountLevels / CountLevelsGreaterEqual in cpp/src/parquet/column_reader.cc, cpp/src/parquet/column_writer.cc, and cpp/src/parquet/file_reader.cc.

  • Add unit tests in cpp/src/parquet/level_conversion_test.cc covering predicates, tail lengths, and uint16_t::max() chunk boundaries.
  • Are these changes tested?

    Yes, unit tests in cpp/src/parquet/level_conversion_test.cc and covered by existing Parquet reader/writer tests.

    Are there any user-facing changes?

    No

    Was AI used for this PR?

    PR code and description written by:

  • Human

  • AI
  • Reviewed before submission by:

  • Human
  • GitHub Issue: #55399
  • Diff: 368 lines (+276 / -92) across 4 files Reviewer: pitrou Review duration: 26594m
    All Vouch review confidence checks passed
    Residual risk 0.18 = Change Risk (0.43) × (1 − Review Confidence (0.57)). Review depth and reviewer attention state are adequate.
    Model 1 · Change Risk (XGBoost)
    Score: 0.43 · 368 lines diff across 4 files
    0.43
    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.354
    Shallow Review Depth shallow_review_depth
    0.289
    Real PR Conversation Timeline
    1 comments
    "Review active on #55408"
    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: 42.8% (MEDIUM) · Higher = larger blast radius
    42.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: 18.5% · Safe to merge
    18.5%
    Repository