Repository navigation
perf(variant): avoid bracket path string allocations - #11232
Conversation
|
Thanks @DKER2, could you add a separate PR with the bench you used to measure the perf? I'd like to have it before we proceed here. |
Co-Authored-By: Claude Opus 5.5 <[email protected]>
Covers dot notation, bracket fields, indexes, wildcards and escaped bracket fields so changes to the path parser can be measured. Co-Authored-By: Claude Opus 5.5 <[email protected]>
|
run benchmark variant_path |
|
Hi @DKER2, thanks for the request (#11232 (comment)). Only whitelisted users can trigger benchmarks. Allowed users: 2010YOUY01, Dandandan, Fokko, HippoBaro, Jefffrey, Omega359, Rachelint, Rich-T-kid, adriangb, alamb, asubiotto, avantgardnerio, brunal, buraksenn, cetra3, codephage2020, coderfender, comphead, erenavsarogullari, etseidl, friendlymatthew, gabotechs, geoffreyclaude, grtlr, haohuaijin, jayzhan211, jonathanc-n, kevinjqliu, klion26, kosiew, kumarUjjawal, kunalsinghdadhwal, liamzwbao, mbutrovich, mkleen, mzabaluev, neilconway, rluvaton, sdf-jkl, timsaucer, xudong963, zhuqi-lucas. File an issue against this benchmark runner |
Moved to a separate PR (apache#11508) so the benchmark lands on main first. Co-Authored-By: Claude Opus 5.5 <[email protected]>
# Which issue does this PR close? - Part of apache#9493. # Rationale for this change apache#11232 changes how `VariantPath` strings are parsed, and there is currently no benchmark covering path parsing. As requested in apache#11232 (comment), this adds the benchmark on its own so it is available on `main` as a baseline before that PR proceeds. # What changes are included in this PR? A new Criterion target, `variant_path`, in `parquet-variant`. It parses six path strings with `VariantPath::try_from`: | Case | Path | |---|---| | `dot_fields` | `foo.bar.baz` | | `bracket_quoted_fields` | `['foo']['bar']['baz']` | | `bracket_indexes` | `[0][1][2]` | | `bracket_wildcards` | `[*][*][*]` | | `bracket_escaped_field` | `['escaped\]field']` | | `mixed` | `a['b.c'].d[2]['3']` | Run with `cargo bench -p parquet-variant --bench variant_path`. For reference, results on `main` (aarch64 macOS): | Case | Time | |---|---:| | `dot_fields` | 30.6 ns | | `bracket_quoted_fields` | 259 ns | | `bracket_indexes` | 140 ns | | `bracket_wildcards` | 137 ns | | `bracket_escaped_field` | 144 ns | | `mixed` | 293 ns | # Are these changes tested? This PR only adds a benchmark; no library code changes. # Are there any significant user-facing changes? No. # AI usage Claude Code was used to write the benchmark and run it. I reviewed the result and can own and debug the change. 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-authored-by: Claude Opus 5.5 <[email protected]>
|
run benchmark variant_path |
|
🤖 Benchmark starting (GKE) | trigger Target: variant_path Sharding factor: 1 (1 workers). Comparing perf/variant-path-lazy-bracket-allocation (371aeb4) to 8f4bb0c (merge-base) diff Run configurationrun benchmark variant_path
shards: 1
Results will be posted when all workers finish. File an issue against this benchmark runner |
|
🤖 Benchmark completed (GKE) | trigger Target: variant_path Comparing perf/variant-path-lazy-bracket-allocation (371aeb4) to 8f4bb0c (merge-base) diff Run configurationrun benchmark variant_path
shards: 1
DetailsPer-runner informationvariant_path — shard 1/1Node: gk3-benchmark-cluster-nap-1kma86l2-48dd0048-2jmx Instance: c4a-highmem-16 (12 vCPU / 65 GiB) uname: BENCH_COMMAND: cargo bench --features=arrow,async,test_common,experimental,object_store --bench variant_pathCPU Details (lscpu)Resource Usagebase (merge-base)
branch
File an issue against this benchmark runner |
| match &path[0] { | ||
| VariantPathElement::Field { | ||
| name: Cow::Borrowed(name), | ||
| } => assert_eq!(*name, ""), | ||
| other => panic!("expected borrowed field, got {other:?}"), | ||
| } | ||
|
|
||
| match &path[1] { | ||
| VariantPathElement::Field { | ||
| name: Cow::Borrowed(name), | ||
| } => assert_eq!(*name, "plain"), | ||
| other => panic!("expected borrowed field, got {other:?}"), | ||
| } | ||
|
|
||
| match &path[2] { | ||
| VariantPathElement::Field { | ||
| name: Cow::Owned(name), | ||
| } => assert_eq!(name, "escaped]field"), | ||
| other => panic!("expected owned field, got {other:?}"), | ||
| } | ||
| } |
There was a problem hiding this comment.
can move the three asserts into one
There was a problem hiding this comment.
Smth like these here could do:
use VariantPathElement::Field;
assert!(matches!(
&path[..],
[
Field { name: Cow::Borrowed("") },
Field { name: Cow::Borrowed("plain") },
Field { name: Cow::Owned(escaped) },
Field { name: Cow::Borrowed("a]b") },
] if escaped == "escaped]field"
));| quote = None; | ||
| unescaped.push(c); | ||
| if let Some(unescaped) = &mut unescaped { | ||
| unescaped.push(c); |
There was a problem hiding this comment.
Instead of pushing each char to the unescaped one by one we can count the offsets where the string starts and ends or encounters a new backslash.
It shows improvement when I prototyped locally
There was a problem hiding this comment.
that make sense for speed performance, change the implementation to count the offset instead. But the improvement is mainly on CPU utilization as we still need to introduce additional memory (which is small for int) for a cursor to tracks how far we’ve copied into unescaped.
There was a problem hiding this comment.
a case like ['abc\]xyz'] need this
|
Thanks lemme take a look |
Address review feedback: instead of pushing each char into the unescaped buffer, track where the current run of literal chars starts and copy it with `push_str` on each escape and at the closing bracket. Also collapse the bracket field ownership asserts into one and cover multiple escapes between multi-byte chars. Co-Authored-By: Claude Opus 5.5 <[email protected]>
Co-Authored-By: Claude Opus 5.5 <[email protected]>
|
@sdf-jkl updated the implementation, can you help to run the benchmark if needed |
|
run benchmark variant_path |
|
🤖 Benchmark starting (GKE) | trigger Target: variant_path Sharding factor: 1 (1 workers). Comparing perf/variant-path-lazy-bracket-allocation (4431da4) to 9a95c6d (merge-base) diff Run configurationrun benchmark variant_path
shards: 1
Results will be posted when all workers finish. File an issue against this benchmark runner |
|
🤖 Benchmark completed (GKE) | trigger Target: variant_path Comparing perf/variant-path-lazy-bracket-allocation (4431da4) to 9a95c6d (merge-base) diff Run configurationrun benchmark variant_path
shards: 1
DetailsPer-runner informationvariant_path — shard 1/1Node: gk3-benchmark-cluster-nap-1qhpgbsz-2dfa3b45-h8h6 Instance: c4a-highmem-16 (12 vCPU / 65 GiB) uname: BENCH_COMMAND: cargo bench --features=arrow,async,test_common,experimental,object_store --bench variant_pathCPU Details (lscpu)Resource Usagebase (merge-base)
branch
File an issue against this benchmark runner |
|
🤖 Benchmark starting (GKE) | trigger Target: variant_path Sharding factor: 1 (1 workers). Comparing 7347a4e (7347a4e) to 371aeb4 diff Run configurationrun benchmark variant_path
baseline:
ref: 371aeb4bcceb1306d4c5a4e0bc9211ebf01f9188
changed:
ref: 7347a4e2927be901111c1ff827f4ef273ce4ac20
shards: 1
Results will be posted when all workers finish. File an issue against this benchmark runner |
|
🤖 Benchmark completed (GKE) | trigger Target: variant_path Comparing 7347a4e (7347a4e) to 371aeb4 diff Run configurationrun benchmark variant_path
baseline:
ref: 371aeb4bcceb1306d4c5a4e0bc9211ebf01f9188
changed:
ref: 7347a4e2927be901111c1ff827f4ef273ce4ac20
shards: 1
DetailsPer-runner informationvariant_path — shard 1/1Node: gk3-benchmark-cluster-nap-akit9iic-f7d08e37-fgpr Instance: c4a-highmem-16 (12 vCPU / 65 GiB) uname: BENCH_COMMAND: cargo bench --features=arrow,async,test_common,experimental,object_store --bench variant_pathCPU Details (lscpu)Resource Usagebase (merge-base)
branch
File an issue against this benchmark runner |
|
Hey @sdf-jkl, reverted |
|
Run benchmark variant_path |
|
🤖 Benchmark starting (GKE) | trigger Target: variant_path Sharding factor: 1 (1 workers). Comparing perf/variant-path-lazy-bracket-allocation (b8c3169) to 87b8013 (merge-base) diff Run configurationrun benchmark variant_path
shards: 1
Results will be posted when all workers finish. File an issue against this benchmark runner |
|
🤖 Benchmark completed (GKE) | trigger Target: variant_path Comparing perf/variant-path-lazy-bracket-allocation (b8c3169) to 87b8013 (merge-base) diff Run configurationrun benchmark variant_path
shards: 1
DetailsPer-runner informationvariant_path — shard 1/1Node: gk3-benchmark-cluster-nap-dg6by4rp-0606c2db-t44r Instance: c4a-highmem-16 (12 vCPU / 65 GiB) uname: BENCH_COMMAND: cargo bench --features=arrow,async,test_common,experimental,object_store --bench variant_pathCPU Details (lscpu)Resource Usagebase (merge-base)
branch
File an issue against this benchmark runner |
| match &path[0] { | ||
| VariantPathElement::Field { | ||
| name: Cow::Borrowed(name), | ||
| } => assert_eq!(*name, ""), | ||
| other => panic!("expected borrowed field, got {other:?}"), | ||
| } | ||
|
|
||
| match &path[1] { | ||
| VariantPathElement::Field { | ||
| name: Cow::Borrowed(name), | ||
| } => assert_eq!(*name, "plain"), | ||
| other => panic!("expected borrowed field, got {other:?}"), | ||
| } | ||
|
|
||
| match &path[2] { | ||
| VariantPathElement::Field { | ||
| name: Cow::Owned(name), | ||
| } => assert_eq!(name, "escaped]field"), | ||
| other => panic!("expected owned field, got {other:?}"), | ||
| } | ||
| } |
There was a problem hiding this comment.
Smth like these here could do:
use VariantPathElement::Field;
assert!(matches!(
&path[..],
[
Field { name: Cow::Borrowed("") },
Field { name: Cow::Borrowed("plain") },
Field { name: Cow::Owned(escaped) },
Field { name: Cow::Borrowed("a]b") },
] if escaped == "escaped]field"
));|
Sorry for overlooking this one earlier @sdf-jkl - collapsed the four match blocks into a single matches! |
|
Thanks for bearing with me @DKER2 🎉 |
Which issue does this PR close?
Rationale for this change
parse_in_bracketcurrently allocates aStringfor every bracket expression, including indexes, wildcards, and quoted fields that require no unescaping.VariantPathElement::Fieldalready storesCow<str>, so unescaped field names can borrow directly from the input path.What changes are included in this PR?
Stringuntil the first backslash is encountered.Cow::Borrowedwhile escaped fields useCow::Owned.This branch includes
mainwith #11231 (closing brackets inside quoted fields). The quote tracking added there relied on the eagerly built buffer, so it is adapted here: an opening quote is now detected by its offset in the bracket content rather than by the buffer being empty, and quoted fields containing](e.g.['a]b']) still borrow.Are these changes tested?
Yes. The existing path parsing tests cover behavior, and a new test asserts the ownership of each case: empty, plain, and
]-containing quoted fields are borrowed, and an escaped field is owned.The benchmark for this change is in #11508 (
cargo bench -p parquet-variant --bench variant_path), split out as requested so it is available onmainas a baseline. I will mergemainhere once it lands so the numbers can be reproduced.Earlier local measurements, taken with a temporary Criterion harness on aarch64 macOS before #11231 was merged in:
Are there any user-facing changes?
No semantic or public API changes. Parsed paths remain the same; unescaped bracket fields now borrow their names instead of allocating owned strings.
AI usage
OpenAI Codex was used to inspect the parser, help implement the lazy allocation path, construct ownership tests, and run validation and benchmarks. Claude Code was used to adapt the change to #11231 after merging
main. I reviewed the resulting code and behavior and can own and debug the change.