Skip to content

refactor(variant): simplify the offset and UTF-8 decoding helpers - #12308

Merged
sdf-jkl merged 1 commit into
apache:mainfrom
pydantic:variant-views-1-refactor
Nov 19, 2026
Merged

sdf-jkl merged 1 commit into
apache:mainfrom
pydantic:variant-views-1-refactor

Conversation

@cetra3

@cetra3 cetra3 commented Nov 18, 2026

Copy link
Copy Markdown
Contributor

Which issue does this PR close?

This is the first of a stack of PRs that split #11511 into smaller pieces, as its reviewers asked.

Rationale for this change

#11511 adds header-only views of encoded variant bytes. The views read offsets and field names straight from the encoded buffer, and these helpers are in the way of that. This PR makes the helper changes on their own, so that the later PRs only show the views.

What changes are included in this PR?

  • OffsetSizeBytes::unpack_u32_at_offset takes self by value. OffsetSizeBytes is Copy.
  • OffsetSizeBytes::unpack_u32 is removed. It was unpack_u32_at_offset with a byte offset of 0, and its callers now call that.
  • str_from_bytes is extracted from string_from_slice, which now calls it.

All of these items are crate-private.

Are these changes tested?

The existing tests cover the changed code. The unpack_u32 tests now call unpack_u32_at_offset.

Are there any user-facing changes?

No.

🤖 Generated with Claude Code

@sdf-jkl

sdf-jkl commented Nov 19, 2026

Copy link
Copy Markdown
Member

run benchmark variant_kernels

@adriangbot

Copy link
Copy Markdown

🤖 Benchmark starting (GKE) | trigger

Target: variant_kernels

Sharding factor: 1 (1 workers).

Comparing variant-views-1-refactor (4b8187e) to e28432b (merge-base) diff

Run configuration
run benchmark variant_kernels
shards: 1

Results will be posted when all workers finish.


File an issue against this benchmark runner

@sdf-jkl sdf-jkl 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.

LGTM, the perf regression you mentioned doesn't reproduce on my x86 machine

Take `OffsetSizeBytes` by value in `unpack_u32_at_offset`, remove
`unpack_u32` in favor of it, and extract `str_from_bytes` from
`string_from_slice`.

Co-Authored-By: Claude Fable 5.2 <[email protected]>
@cetra3
cetra3 force-pushed the variant-views-1-refactor branch from 4b8187e to cd32a6f Compare November 19, 2026 04:21
@adriangbot

Copy link
Copy Markdown

🤖 Benchmark completed (GKE) | trigger

Target: variant_kernels

Comparing variant-views-1-refactor (4b8187e) to e28432b (merge-base) diff

Run configuration
run benchmark variant_kernels
shards: 1
Details
group                                                                                             base                                   changed
-----                                                                                             ----                                   -------
batch_json_string_to_variant duplicate_key_100k (100014 bytes per document, 10 rows)              1.00    143.8±1.87µs        ? ?/sec    1.00    143.8±1.54µs        ? ?/sec
batch_json_string_to_variant escaped_strings (58 bytes per document, 8000 rows)                   1.00      7.0±0.05ms        ? ?/sec    1.00      7.0±0.05ms        ? ?/sec
batch_json_string_to_variant fixed_point_decimals (65 bytes per document, 8000 rows)              1.00      6.6±0.01ms        ? ?/sec    1.01      6.7±0.01ms        ? ?/sec
batch_json_string_to_variant int32 (16 bytes per document, 8000 rows)                             1.00      2.9±0.02ms        ? ?/sec    1.00      3.0±0.02ms        ? ?/sec
batch_json_string_to_variant json_list 8k string                                                  1.00     19.9±0.22ms        ? ?/sec    1.01     20.1±0.22ms        ? ?/sec
batch_json_string_to_variant large_array_1024 (4011 bytes per document, 261 rows)                 1.00      9.2±0.23ms        ? ?/sec    1.01      9.3±0.20ms        ? ?/sec
batch_json_string_to_variant large_string_8k (8214 bytes per document, 127 rows)                  1.00    267.4±0.86µs        ? ?/sec    1.00    266.9±0.78µs        ? ?/sec
batch_json_string_to_variant object - 1 depth(100 fields) random_json(2436 bytes per document)    1.00    149.0±0.46ms        ? ?/sec    1.00    149.5±0.85ms        ? ?/sec
batch_json_string_to_variant object - 1 depth(200 fields) random_json(4871 bytes per document)    1.00    294.8±1.37ms        ? ?/sec    1.00    293.9±1.09ms        ? ?/sec
batch_json_string_to_variant random_json(2633 bytes per document)                                 1.00    224.1±1.16ms        ? ?/sec    1.00    224.3±0.95ms        ? ?/sec
batch_json_string_to_variant repeated_struct 8k string                                            1.00      6.4±0.02ms        ? ?/sec    1.02      6.5±0.05ms        ? ?/sec
shred_variant_partial_object_8k_rows                                                              1.00     21.6±0.07ms        ? ?/sec    1.00     21.5±0.07ms        ? ?/sec
shred_variant_unmatched_object_8k_rows                                                            1.00     27.0±0.08ms        ? ?/sec    1.01     27.1±0.09ms        ? ?/sec
unshred_variant_list                                                                              1.02     10.2±0.08ms        ? ?/sec    1.00     10.1±0.07ms        ? ?/sec
unshred_variant_list_of_objects                                                                   1.00     22.9±0.29ms        ? ?/sec    1.00     22.9±0.27ms        ? ?/sec
unshred_variant_nested_fallback                                                                   1.03    155.9±0.45ms        ? ?/sec    1.00    151.9±0.31ms        ? ?/sec
unshred_variant_object                                                                            1.02      5.6±0.10ms        ? ?/sec    1.00      5.5±0.04ms        ? ?/sec
unshred_variant_partial_object                                                                    1.00     44.6±0.25ms        ? ?/sec    1.00     44.4±0.12ms        ? ?/sec
unshred_variant_partial_object_unsorted_metadata                                                  1.02    201.4±1.15ms        ? ?/sec    1.00    197.7±1.63ms        ? ?/sec
unshred_variant_primitive                                                                         1.00    438.7±3.06µs        ? ?/sec    1.00    438.4±4.57µs        ? ?/sec
variant_array_builder_build_262k_small_values                                                     1.00  1096.8±92.20µs        ? ?/sec    1.10  1208.5±180.87µs        ? ?/sec
variant_get_binary_from_string                                                                    1.01    482.4±0.63µs        ? ?/sec    1.00    475.3±1.57µs        ? ?/sec
variant_get_binary_from_unshredded_binary                                                         1.01    464.9±3.02µs        ? ?/sec    1.00    461.2±2.50µs        ? ?/sec
variant_get_decimal_double_to_decimal128(20,3)_non_finite_8192_safe                               1.03    512.5±1.72µs        ? ?/sec    1.00    496.4±1.66µs        ? ?/sec
variant_get_decimal_double_to_decimal128(20,3)_valid_8192_safe                                    1.02    527.1±1.84µs        ? ?/sec    1.00    516.5±4.06µs        ? ?/sec
variant_get_decimal_double_to_decimal128(20,3)_valid_8192_strict                                  1.02    550.9±1.15µs        ? ?/sec    1.00    539.9±2.70µs        ? ?/sec
variant_get_decimal_double_to_decimal256(40,-2)_scale_down_8192_safe                              1.04   1005.1±4.47µs        ? ?/sec    1.00   969.8±11.43µs        ? ?/sec
variant_get_decimal_double_to_decimal64(3,0)_precision_overflow_8192_safe                         1.02    508.7±0.76µs        ? ?/sec    1.00    497.1±1.74µs        ? ?/sec
variant_get_decimal_float_to_decimal32(7,2)_valid_8192_safe                                       1.02    470.5±1.33µs        ? ?/sec    1.00    461.8±1.73µs        ? ?/sec
variant_get_decimal_float_to_decimal32(7,2)_valid_8192_strict                                     1.02    513.8±1.60µs        ? ?/sec    1.00    502.3±1.53µs        ? ?/sec
variant_get_decimal_float_to_decimal64(3,0)_precision_overflow_8192_safe                          1.03    520.2±1.19µs        ? ?/sec    1.00    505.8±1.48µs        ? ?/sec
variant_get_decimal_int32_to_decimal32(8,0)_precision_overflow_8192_safe                          1.03    485.6±1.18µs        ? ?/sec    1.00    471.3±1.68µs        ? ?/sec
variant_get_decimal_int32_to_decimal32(9,0)_valid_8192_safe                                       1.02    469.6±1.14µs        ? ?/sec    1.00    462.1±1.81µs        ? ?/sec
variant_get_decimal_int32_to_decimal32(9,0)_valid_8192_strict                                     1.03    510.2±2.46µs        ? ?/sec    1.00    496.8±2.41µs        ? ?/sec
variant_get_decimal_int64_to_decimal128(38,2)_scale_up_8192_safe                                  1.03    516.9±2.57µs        ? ?/sec    1.00   500.8±15.94µs        ? ?/sec
variant_get_decimal_int64_to_decimal128(38,2)_scale_up_8192_strict                                1.04    568.5±3.25µs        ? ?/sec    1.00   548.8±17.78µs        ? ?/sec
variant_get_decimal_int64_to_decimal256(76,-1)_scale_down_8192_safe                               1.04    530.9±1.88µs        ? ?/sec    1.00    508.2±1.98µs        ? ?/sec
variant_get_decimal_int64_to_decimal32(9,-1)_scale_before_narrowing_8192_safe                     1.02    480.5±1.82µs        ? ?/sec    1.00    472.6±1.59µs        ? ?/sec
variant_get_decimal_int64_to_decimal32(9,-20)_all_zero_8192_safe                                  1.02    465.1±1.30µs        ? ?/sec    1.00    457.3±1.58µs        ? ?/sec
variant_get_decimal_short_string_to_decimal256(40,-2)_scale_down_8192_safe                        1.02    581.6±2.28µs        ? ?/sec    1.00    570.0±1.94µs        ? ?/sec
variant_get_decimal_short_string_to_decimal256(76,2)_wide_8192_safe                               1.00    959.7±3.71µs        ? ?/sec    1.00    958.0±1.39µs        ? ?/sec
variant_get_decimal_short_string_to_decimal32(7,2)_valid_8192_safe                                1.01    548.0±2.57µs        ? ?/sec    1.00    542.1±2.33µs        ? ?/sec
variant_get_decimal_short_string_to_decimal32(7,2)_valid_8192_strict                              1.01    597.3±7.95µs        ? ?/sec    1.00    591.0±7.91µs        ? ?/sec
variant_get_decimal_string_to_decimal128(38,3)_long_8192_safe                                     1.00   1107.0±7.73µs        ? ?/sec    1.00   1102.9±9.95µs        ? ?/sec
variant_get_decimal_string_to_decimal128(38,3)_long_8192_strict                                   1.00  1178.3±13.87µs        ? ?/sec    1.00  1172.8±13.10µs        ? ?/sec
variant_get_list_index_all_oob_int64_64_rows                                                      1.00   866.6±14.78ns        ? ?/sec    1.00   865.6±12.30ns        ? ?/sec
variant_get_list_index_shredded                                                                   1.01     47.1±1.30µs        ? ?/sec    1.00     46.7±1.36µs        ? ?/sec
variant_get_list_index_unshredded                                                                 1.08  1039.7±11.30µs        ? ?/sec    1.00   960.3±16.83µs        ? ?/sec
variant_get_object_path_metadata/json_to_variant                                                  1.18      3.6±0.41ms        ? ?/sec    1.00      3.0±0.18ms        ? ?/sec
variant_get_object_path_metadata/per_row_metadata                                                 1.07      3.0±0.21ms        ? ?/sec    1.00      2.8±0.06ms        ? ?/sec
variant_get_object_path_metadata/variant_array_builder                                            1.24      3.9±0.26ms        ? ?/sec    1.00      3.1±0.16ms        ? ?/sec
variant_get_primitive                                                                             1.00    551.4±6.75ns        ? ?/sec    1.00    551.2±8.35ns        ? ?/sec
variant_get_shredded_utf8                                                                         1.00    716.9±3.30ns        ? ?/sec    1.01    722.7±3.80ns        ? ?/sec
variant_get_unshredded_object_path_262k_rows                                                      1.08     37.5±0.71ms        ? ?/sec    1.00     34.6±0.25ms        ? ?/sec
variant_get_utf8_from_date                                                                        1.01    571.2±5.10µs        ? ?/sec    1.00    566.2±5.13µs        ? ?/sec
variant_get_utf8_from_decimal                                                                     1.02    475.3±6.32µs        ? ?/sec    1.00    467.4±8.61µs        ? ?/sec
variant_get_utf8_from_float                                                                       1.02    448.8±1.09µs        ? ?/sec    1.00    438.0±1.32µs        ? ?/sec
variant_get_utf8_from_int                                                                         1.02    448.7±0.91µs        ? ?/sec    1.00    438.7±1.24µs        ? ?/sec
variant_get_utf8_from_list                                                                        1.11    659.1±8.77µs        ? ?/sec    1.00   594.9±11.98µs        ? ?/sec
variant_get_utf8_from_map_in_list                                                                 1.12   690.8±10.98µs        ? ?/sec    1.00   618.4±14.16µs        ? ?/sec
variant_get_utf8_from_time                                                                        1.01    783.0±1.63µs        ? ?/sec    1.00    772.8±2.84µs        ? ?/sec
variant_get_utf8_from_timestamp_with_timezone                                                     1.00    569.4±2.86µs        ? ?/sec    1.00    568.3±3.21µs        ? ?/sec
variant_get_utf8_from_timestamp_without_timezone                                                  1.00    569.9±2.85µs        ? ?/sec    1.00    569.0±3.59µs        ? ?/sec
variant_get_utf8_from_unshredded_string                                                           1.02    497.9±1.50µs        ? ?/sec    1.00    486.3±2.30µs        ? ?/sec
variant_to_json_dictionary_metadata                                                               1.04      2.9±0.04ms        ? ?/sec    1.00      2.8±0.04ms        ? ?/sec
variant_to_json_list                                                                              1.03     36.3±0.66ms        ? ?/sec    1.00     35.2±0.57ms        ? ?/sec
variant_to_json_object                                                                            1.03      2.9±0.05ms        ? ?/sec    1.00      2.8±0.05ms        ? ?/sec
variant_to_json_primitive                                                                         1.01    735.5±8.40µs        ? ?/sec    1.00    729.0±7.35µs        ? ?/sec
variant_to_json_ree_metadata                                                                      1.05      3.0±0.03ms        ? ?/sec    1.00      2.9±0.06ms        ? ?/sec
variant_to_json_shredded_primitive                                                                1.00    423.4±6.06µs        ? ?/sec    1.00    422.6±6.49µs        ? ?/sec
variant_to_json_wide_object                                                                       1.00    159.4±1.27ms        ? ?/sec    1.00    159.2±0.93ms        ? ?/sec

Per-runner information
variant_kernels — shard 1/1

Node: gk3-benchmark-cluster-nap-hlsk0klo-fe6a26fa-rsn5

Instance: c4a-highmem-16 (12 vCPU / 65 GiB)

uname:

Linux bench-c6999100380-5466-66f89 6.12.105+ #1 SMP Sat Sep 19 07:55:25 UTC 2026 aarch64 GNU/Linux

BENCH_COMMAND:

cargo bench --features=arrow,async,test_common,experimental,object_store --bench variant_kernels
CPU Details (lscpu)
Architecture:                            aarch64
CPU op-mode(s):                          64-bit
Byte Order:                              Little Endian
CPU(s):                                  16
On-line CPU(s) list:                     0-15
Vendor ID:                               ARM
Model name:                              Neoverse-V2
Model:                                   1
Thread(s) per core:                      1
Core(s) per cluster:                     16
Socket(s):                               -
Cluster(s):                              1
Stepping:                                r0p1
BogoMIPS:                                2000.00
Flags:                                   fp asimd evtstrm aes pmull sha1 sha2 crc32 atomics fphp asimdhp cpuid asimdrdm jscvt fcma lrcpc dcpop sha3 sm3 sm4 asimddp sha512 sve asimdfhm dit uscat ilrcpc flagm sb paca pacg dcpodp sve2 sveaes svepmull svebitperm svesha3 svesm4 flagm2 frint svei8mm svebf16 i8mm bf16 dgh rng bti
L1d cache:                               1 MiB (16 instances)
L1i cache:                               1 MiB (16 instances)
L2 cache:                                32 MiB (16 instances)
L3 cache:                                80 MiB (1 instance)
NUMA node(s):                            1
NUMA node0 CPU(s):                       0-15
Vulnerability Gather data sampling:      Not affected
Vulnerability Indirect target selection: Not affected
Vulnerability Itlb multihit:             Not affected
Vulnerability L1tf:                      Not affected
Vulnerability Mds:                       Not affected
Vulnerability Meltdown:                  Not affected
Vulnerability Mmio stale data:           Not affected
Vulnerability Reg file data sampling:    Not affected
Vulnerability Retbleed:                  Not affected
Vulnerability Spec rstack overflow:      Not affected
Vulnerability Spec store bypass:         Mitigation; Speculative Store Bypass disabled via prctl
Vulnerability Spectre v1:                Mitigation; __user pointer sanitization
Vulnerability Spectre v2:                Mitigation; CSV2, BHB
Vulnerability Srbds:                     Not affected
Vulnerability Tsa:                       Not affected
Vulnerability Tsx async abort:           Not affected
Vulnerability Vmscape:                   Not affected
Resource Usage

base (merge-base)

Metric Value
Wall time 860.2s
Peak memory 451.7 MiB
Avg memory 279.1 MiB
CPU user 853.4s
CPU sys 0.3s
Peak spill 0 B

branch

Metric Value
Wall time 875.2s
Peak memory 450.3 MiB
Avg memory 278.6 MiB
CPU user 872.8s
CPU sys 0.3s
Peak spill 0 B

File an issue against this benchmark runner

@sdf-jkl
sdf-jkl added this pull request to the merge queue Nov 19, 2026
Merged via the queue into apache:main with commit 9f87453 Nov 19, 2026
22 checks passed
@Jefffrey Jefffrey added the development-process Internal changes; PRs with this label are excluded from changelog label Nov 19, 2026
adriangb added a commit to pydantic/arrow-rs that referenced this pull request Nov 26, 2026
Subodh-17 pushed a commit to Subodh-17/arrow-rs that referenced this pull request Nov 29, 2026
…#12309)

# Which issue does this PR close?

- Part of apache#11511

This PR is the first open PR of the stack split out of apache#11511. apache#12308
has merged, so the diff is this PR's change alone.

# Rationale for this change

Every access to a variant value pays for checks that the access does not
use. The unvalidated constructors of `VariantMetadata`, `VariantObject`
and `VariantList` read the first and last offsets of each header and
trim the byte slices. A field lookup then validates the UTF-8 of every
dictionary name that its binary search probes.

For most kernel work, a value needs much less: the header of each object
or list along a path, the names that the binary search probes, and the
bytes of the value that it finds.

This PR adds the types that read that way, and builds the existing types
on them. The kernels start to use the views in the next PRs of the
stack.

# What changes are included in this PR?

- New types `VariantView`, `VariantMetadataView`, `VariantObjectView`
and `VariantListView`.
  - Building a view reads one header and nothing else.
- Each accessor reads and bounds-checks only the bytes that it needs,
and returns an error on malformed input. It never panics.
- Field names are compared as bytes, which orders them as `str` does, so
a search does not validate UTF-8.
- A view converts to the existing types:
`VariantMetadataView::try_into_metadata`,
`VariantObjectView::try_into_object`, `VariantListView::try_into_list`
and `VariantView::try_to_variant` add the first and last offset checks
that unvalidated instances guarantee.
- `VariantMetadata`, `VariantObject` and `VariantList` are now built by
promoting a view, and their accessors (`get`, `field`, `field_name`,
`iter`, `get_entry`) delegate to it. Their public behavior is unchanged.
- `ObjectBuilder::finish` sorts fields with a new
`MetadataBuilder::cmp_field_names`. `ReadOnlyMetadataBuilder` compares
the names as bytes.

The docs of `VariantView` describe the design.

# Are these changes tested?

The existing types now delegate to the views, so their tests also cover
the views. The views also have direct tests: agreement with
`VariantMetadata`, `VariantObject` and `VariantList`, and every
truncation and single-byte corruption of a sample value, which must not
panic.

Benchmark runner results, this PR against `main`: `variant_validation`
on
[x86_64](apache#12309 (comment))
and [Neoverse
V2](apache#12309 (comment)),
and `variant_kernels` on
[x86_64](apache#12309 (comment)).

| benchmark | x86_64 | Neoverse V2 |
|---|---:|---:|
| `metadata_iter/8` | −1% | −5% |
| `metadata_iter/32` | +5% | −7% |
| `metadata_iter/128` | +4% | −6% |
| `bench_validate_complex_object` | −1% | 0% |
| `bench_validate_large_object` | 0% | −7% |
| `bench_validate_large_nested_list` | −1% | 0% |
| `object_field_name/{8,32,128}` | −2% | −2% to −3% |
| `object_get_by_name/8` | −15% | +1% |
| `object_get_by_name/32` | −17% | −4% |
| `object_get_by_name/128` | −21% | −9% |
| `object_iter/8` | −7% | −10% |
| `object_iter/32` | −9% | −8% |
| `object_iter/128` | −11% | −8% |
| `variant_get_unshredded_object_path_262k_rows` | +3% | |
| `variant_get_list_index_unshredded` | +2% | |
| `variant_get_utf8_from_list` | +3% | |
| `variant_get_utf8_from_map_in_list` | +3% | |
| `variant_get_utf8_from_unshredded_string` | −6% | |
| `variant_get_decimal_short_string_to_decimal256(76,2)_wide_8192_safe`
| +11% | |
| `variant_get_decimal_string_to_decimal128(38,3)_long_8192_safe` | +9%
| |
| `variant_get_decimal_string_to_decimal128(38,3)_long_8192_strict` |
+6% | |
| `unshred_variant_object` | −5% | |
| `unshred_variant_list` | −6% | |
| `unshred_variant_partial_object` | +3% | |
| `unshred_variant_partial_object_unsorted_metadata` | −19% | |

Slower inputs:

- `metadata_iter` is 4–5% slower on x86_64 at 32 and 128 entries, and
5–7% faster on Neoverse V2. My local x86_64 machine measured it 13–16%
slower.
- Three `variant_get` benchmarks that cast long strings to decimals are
6–11% slower on x86_64. I have not found the cause. This PR does not
change the string decoding or decimal parsing code. On that path it only
changes how `VariantMetadata` is built, which does the same checks as
before.
- The four unshredded `variant_get` benchmarks that read objects or
lists are 2–3% slower on x86_64. The next PR of the stack moves
`variant_get` onto the views.

# Are there any user-facing changes?

New public API in `parquet-variant`:

- `VariantView`, `VariantMetadataView`, `VariantObjectView` and
`VariantListView`, with their methods.
- `VariantMetadata::view`, `VariantObject::view` and
`VariantList::view`.
- `MetadataBuilder::cmp_field_names`, a provided trait method.
- `VariantBasicType`, which `VariantView::basic_type` returns.

No existing public API changes. Some out-of-range errors from metadata
and list accessors have new message text.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

---------

Co-authored-by: Claude Fable 5.2 <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

development-process Internal changes; PRs with this label are excluded from changelog parquet-variant parquet-variant* crates

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants