-
Notifications
You must be signed in to change notification settings - Fork 197
feat: add dense union encoding #9367
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,33 @@ | ||
| [package] | ||
| name = "vortex-dense-union" | ||
| authors = { workspace = true } | ||
| categories = { workspace = true } | ||
| description = "Dense union encoding for Vortex arrays" | ||
| edition = { workspace = true } | ||
| homepage = { workspace = true } | ||
| include = { workspace = true } | ||
| keywords = { workspace = true } | ||
| license = { workspace = true } | ||
| readme = "README.md" | ||
| repository = { workspace = true } | ||
| rust-version = { workspace = true } | ||
| version = { workspace = true } | ||
|
|
||
| [dependencies] | ||
| prost = { workspace = true } | ||
| vortex-array = { workspace = true } | ||
| vortex-error = { workspace = true } | ||
| vortex-mask = { workspace = true } | ||
| vortex-session = { workspace = true } | ||
|
|
||
| [dev-dependencies] | ||
| divan = { workspace = true } | ||
| vortex-array = { workspace = true, features = ["_test-harness"] } | ||
| vortex-buffer = { workspace = true } | ||
|
|
||
| [lints] | ||
| workspace = true | ||
|
|
||
| [[bench]] | ||
| name = "take" | ||
| harness = false |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| # Vortex Dense Union | ||
|
|
||
| An external dense physical encoding for Vortex's logical `DType::Union`. |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,118 @@ | ||
| // SPDX-License-Identifier: Apache-2.0 | ||
| // SPDX-FileCopyrightText: Copyright the Vortex contributors | ||
|
|
||
| #![expect(clippy::unwrap_used)] | ||
| #![expect(clippy::cast_possible_truncation)] | ||
|
|
||
| use std::sync::LazyLock; | ||
|
|
||
| use divan::Bencher; | ||
| use vortex_array::ArrayRef; | ||
| use vortex_array::IntoArray; | ||
| use vortex_array::RecursiveCanonical; | ||
| use vortex_array::VortexSessionExecute; | ||
| use vortex_array::array_session; | ||
| use vortex_array::arrays::PrimitiveArray; | ||
| use vortex_array::arrays::UnionArray; | ||
| use vortex_array::dtype::DType; | ||
| use vortex_array::dtype::FieldNames; | ||
| use vortex_array::dtype::Nullability; | ||
| use vortex_array::dtype::PType; | ||
| use vortex_array::dtype::UnionVariants; | ||
| use vortex_dense_union::DenseUnion; | ||
| use vortex_dense_union::initialize; | ||
| use vortex_session::VortexSession; | ||
|
|
||
| const LEN: usize = 65_536; | ||
| const N_VARIANTS: usize = 28; | ||
| const TAKE_LEN: usize = 4_096; | ||
|
|
||
| fn main() { | ||
| LazyLock::force(&SESSION); | ||
| divan::main(); | ||
| } | ||
|
|
||
| static SESSION: LazyLock<VortexSession> = LazyLock::new(|| { | ||
| let session = array_session(); | ||
| initialize(&session); | ||
| session | ||
| }); | ||
|
|
||
| fn variants() -> UnionVariants { | ||
| let names = FieldNames::from_iter((0..N_VARIANTS).map(|index| format!("variant_{index}"))); | ||
| let dtypes = vec![DType::Primitive(PType::I32, Nullability::NonNullable); N_VARIANTS]; | ||
| let type_ids = (1..=N_VARIANTS).map(|type_id| type_id as u8).collect(); | ||
| UnionVariants::try_new(names, dtypes, type_ids).unwrap() | ||
| } | ||
|
|
||
| fn selectors() -> (ArrayRef, ArrayRef, Vec<usize>) { | ||
| let mut child_lengths = vec![0usize; N_VARIANTS]; | ||
| let mut type_ids = Vec::with_capacity(LEN); | ||
| let mut offsets = Vec::with_capacity(LEN); | ||
| for row in 0..LEN { | ||
| let child_index = row % N_VARIANTS; | ||
| type_ids.push((child_index + 1) as u8); | ||
| offsets.push(child_lengths[child_index] as i32); | ||
| child_lengths[child_index] += 1; | ||
| } | ||
| ( | ||
| PrimitiveArray::from_iter(type_ids).into_array(), | ||
| PrimitiveArray::from_iter(offsets).into_array(), | ||
| child_lengths, | ||
| ) | ||
| } | ||
|
|
||
| fn dense_union() -> ArrayRef { | ||
| let (type_ids, offsets, child_lengths) = selectors(); | ||
| let children = child_lengths | ||
| .into_iter() | ||
| .map(|len| PrimitiveArray::from_iter(0..len as i32).into_array()) | ||
| .collect::<Vec<_>>(); | ||
| DenseUnion::try_new(type_ids, offsets, variants(), children) | ||
| .unwrap() | ||
| .into_array() | ||
| } | ||
|
|
||
| fn sparse_union() -> ArrayRef { | ||
| let (type_ids, ..) = selectors(); | ||
| let children = (0..N_VARIANTS) | ||
| .map(|child_index| { | ||
| PrimitiveArray::from_iter((0..LEN).map(move |row| { | ||
| if row % N_VARIANTS == child_index { | ||
| (row / N_VARIANTS) as i32 | ||
| } else { | ||
| 0 | ||
| } | ||
| })) | ||
| .into_array() | ||
| }) | ||
| .collect::<Vec<_>>(); | ||
| UnionArray::try_new(type_ids, variants(), children) | ||
| .unwrap() | ||
| .into_array() | ||
| } | ||
|
|
||
| fn indices() -> ArrayRef { | ||
| PrimitiveArray::from_iter((0..TAKE_LEN).rev().map(|index| index as u32)).into_array() | ||
| } | ||
|
|
||
| fn bench_take(bencher: Bencher, array: ArrayRef, indices: ArrayRef) { | ||
| bencher | ||
| .with_inputs(|| (&array, &indices, SESSION.create_execution_ctx())) | ||
| .bench_refs(|(array, indices, ctx)| { | ||
| array | ||
| .take((*indices).clone()) | ||
| .unwrap() | ||
| .execute::<RecursiveCanonical>(ctx) | ||
| }); | ||
| } | ||
|
|
||
| #[divan::bench] | ||
| fn dense_take(bencher: Bencher) { | ||
| bench_take(bencher, dense_union(), indices()); | ||
| } | ||
|
|
||
| #[divan::bench] | ||
| fn sparse_take(bencher: Bencher) { | ||
| bench_take(bencher, sparse_union(), indices()); | ||
| } | ||
|
Comment on lines
+111
to
+118
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You need to execute the array here otherwise there is no work that is done
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ah, that's correct, for now it is lazy-take. Fixed.
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. im interested in what the new benchmark results are?
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Already showed in the PR content. DenseUnion is 2.10-2.12x slower. |
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This benchmark never runs in CI.
.github/workflows/codspeed.ymldrives benchmarks off an explicit package matrix (9 shards, four of them "Encodings 1" through "Encodings 4"), andvortex-dense-unionis in none of them — so the numbers in the PR description cannot regress a build, and the 2.1x gap has nothing watching it.Adding the package to one of the encodings shards is a one-line change (shard 4 and shard 7 are the shortest lists). No
featuresentry is needed: the_test-harnessfeature is already pulled in through this crate'sdev-dependencies.Given the PR ships a known 2.1x regression versus sparse and the canonicalize hot loop has obvious headroom, this is the check that would tell you whether the follow-up optimizations actually landed.
Generated by Claude Code