Skip to content

GH-55448: [C++][Parquet] Optimize ALP and make vector sizes configurable - #55449

Open
wgtmac wants to merge 5 commits into
apache:mainfrom
wgtmac:alp_improve
Open

wgtmac wants to merge 5 commits into
apache:mainfrom
wgtmac:alp_improve

Conversation

@wgtmac

@wgtmac wgtmac commented Nov 9, 2026 •

Copy link
Copy Markdown
Member

Rationale for this change

This PR addresses follow-up items from #48345 to improve ALP performance, format compatibility, and writer configuration.

What changes are included in this PR?

  • Add AlpConfig and WriterProperties::Builder::alp_config() to configure the vector size and sampling window.
  • Accept unsorted and duplicate exception positions while validating every position. Preserve the existing compiler optimization hints when positions are strictly increasing.
  • Load encoding and decoding multipliers before value loops.
  • Serialize each vector immediately instead of retaining all encoded vectors and a separate offset array.
  • Use stack storage for second-level sampling, avoiding a sample allocation for each vector parameter search.
  • Cache a column-chunk preset when the configured sampling window is reached.
  • Size PutSpaced() temporary storage by the non-null count and avoid allocation for empty or all-null input.

Are these changes tested?

Yes.

Are there any user-facing changes?

Yes. Writers can configure ALP through AlpConfig:

  • vector_size: defaults to 1,024; supported values are powers of two from 8 through 32,768.
  • sampling_window_size: defaults to 122,880 non-null values and must be positive. The preset is cached at the end of the page that reaches the configured count.

Was AI used for this PR?

AI assisted with implementation, review, test execution, and benchmarking.

PR code and description written by:

  • Human
  • AI

Reviewed before submission by:

  • Human
  • AI
  • Not reviewed

@wgtmac
wgtmac requested review from HuaHuaY and pitrou as code owners November 9, 2026 06:05
Copilot AI balanced review requested due to automatic review settings November 9, 2026 06:05

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota. Try requesting a review at a lower effort level.

Copilot AI balanced review requested due to automatic review settings November 9, 2026 06:56

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota. Try requesting a review at a lower effort level.

Copilot AI balanced review requested due to automatic review settings November 9, 2026 07:39

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota. Try requesting a review at a lower effort level.

@pitrou pitrou left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just some API notes

Comment thread cpp/src/parquet/encoding.h Resolved
Comment thread cpp/src/parquet/properties.h Resolved
Copilot AI balanced review requested due to automatic review settings November 10, 2026 07:51

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota. Try requesting a review at a lower effort level.

@pitrou pitrou left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I must think a bit more about the EncodingOptions API but otherwise this is looking rather good.

if (worse_total_bits_counter == AlpSamplingOptions::kEarlyExitThreshold) {
break;
}
continue;

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unrelated to this PR, but what's the point of the "early exit" thing?
Why would we not test all pre-selected combinations (especially as kMaxCombinations = 5)?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is a related discussion at #48345 (comment).

The ALP paper uses early exit with at most 5 candidates and early exit threshold of 2. Our values are 5 and 4 respectively, so we cannot actually skip any candidates.

Increasing the candidate count or lowering the threshold could make it useful, but the greedy search may miss a better candidate and hurt compression ratio. In the future, we can turn these sampling constants to configurable options to make them work.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't understand why increasing the candidate count would hurt compression ratio. It can only improve the compression ratio, right?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry, I didn't explain it clearly. I meant that the greedy early exit itself would hurt the compression ratio compared to not using early exit.

My suggestion is to keep the early exit algorithm as consistent with the ALP paper and make it tunable after we have more production experience.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok, but my question remains: why the early exit at all? Is it just to save a bit of time when compressing?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, the ALP has designed this early exit to save some time but it doesn't tell how much time can be saved from this.

Comment on lines +537 to +538
// The encoder emits strictly increasing positions. Use the vectorized
// fast path for that common case.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Was the "vectorized fast path" measured to bring substantial benefits? After all, there should not be many exceptions...

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

#48345 (comment) mentioned testing unroll factors of 2, 4 and 8, but provided no measurements specific to exception patching.

When I made this change, I have verified that the code path has been vectorized by gcc on godbolt. But I didn't verify its performance gain because I used Clang locally which failed to do this :/

Comment on lines 111 to 112
chunk_samples_.push_back(std::move(chunk_sample));
chunks_sampled_++;

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm curious, is chunk_samples_ unbounded, or does it stop accumulating after a certain number of samples? It's not clear by reading this code.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The sampler itself is unbounded but the encoder will stop sampling after processing sampling_window_size values to get the preset.

Comment thread cpp/src/parquet/encoder.cc Outdated

EncodingPreset page_preset;
if (!preset_.has_value()) {
// Use a page-specific sampler to avoid affecting the global preset.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why do we not use a page-specific sampler for all pages? Is it too expensive?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To clarify, before my change in this PR, every page performs a complete two-pass sampling and encoding strategy as described in #55449 (comment). I think this is expensive so I introduce a column-chunk-level shared sampler to avoid repeating this process over and over again.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this is expensive

Did you validate this and your approach using benchmarks?

Also, IMHO we should be careful to not pessimize the compression efficiency in search for encoding speed improvements.

This might not be a problem on synthetic random data, but reduce compression efficiency on real-world data with non-random ordering.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good question. I've only benchmarked the speedup but not yet checked the compression ratio. I asked AI to do a quick check and indeed found several regressions. Since this is a naive approach and has regression, let me revert this part from the PR.

Comment thread cpp/src/parquet/encoder.cc Resolved
Comment thread cpp/src/parquet/encoder.cc Resolved
Comment thread cpp/src/parquet/encoder.cc Resolved
Copilot AI balanced review requested due to automatic review settings November 12, 2026 05:40

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota. Try requesting a review at a lower effort level.

Copilot AI balanced review requested due to automatic review settings November 12, 2026 14:58

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota. Try requesting a review at a lower effort level.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants