Skip to content

Comments

Add append_non_nulls to StructBuilder#9430

Open
Fokko wants to merge 3 commits intoapache:mainfrom
Fokko:fd-append-n-non-nulls
Open

Add append_non_nulls to StructBuilder#9430
Fokko wants to merge 3 commits intoapache:mainfrom
Fokko:fd-append-n-non-nulls

Conversation

@Fokko
Copy link
Contributor

@Fokko Fokko commented Feb 18, 2026

Which issue does this PR close?

I'm doing some performance optimization, and noticed that we have a loop adding one value to the null mask at a time. Instead, I'd suggest adding append_non_nulls to do this at once.

append_non_nulls(n) vs append(true) in a loop (with bitmap allocated)

┌───────────┬───────────────────┬─────────────────────┬─────────┐
│     n     │ append(true) loop │ append_non_nulls(n) │ speedup │
├───────────┼───────────────────┼─────────────────────┼─────────┤
│ 100       │ 251 ns            │ 73 ns               │ ~3x     │
├───────────┼───────────────────┼─────────────────────┼─────────┤
│ 1,000     │ 2.0 µs            │ 94 ns               │ ~21x    │
├───────────┼───────────────────┼─────────────────────┼─────────┤
│ 10,000    │ 19.3 µs           │ 119 ns              │ ~162x   │
├───────────┼───────────────────┼─────────────────────┼─────────┤
│ 100,000   │ 191 µs            │ 348 ns              │ ~549x   │
├───────────┼───────────────────┼─────────────────────┼─────────┤
│ 1,000,000 │ 1.90 ms           │ 3.5 µs              │ ~543x   │
└───────────┴───────────────────┴─────────────────────┴─────────┘

Rationale for this change

It adds a new public API in favor of performance improvements.

What changes are included in this PR?

A new public API

Are these changes tested?

Yes, with new unit-tests.

Are there any user-facing changes?

Just a new convient API.

Closes apache#9429

I'm doing some performance optimization, and noticed that we have a loop adding one value to the null mask at a time. Instead, I'd suggest adding `append_non_nulls` to do this at once.

```
append_non_nulls(n) vs append(true) in a loop (with bitmap allocated)

┌───────────┬───────────────────┬─────────────────────┬─────────┐
│     n     │ append(true) loop │ append_non_nulls(n) │ speedup │
├───────────┼───────────────────┼─────────────────────┼─────────┤
│ 100       │ 251 ns            │ 73 ns               │ ~3x     │
├───────────┼───────────────────┼─────────────────────┼─────────┤
│ 1,000     │ 2.0 µs            │ 94 ns               │ ~21x    │
├───────────┼───────────────────┼─────────────────────┼─────────┤
│ 10,000    │ 19.3 µs           │ 119 ns              │ ~162x   │
├───────────┼───────────────────┼─────────────────────┼─────────┤
│ 100,000   │ 191 µs            │ 348 ns              │ ~549x   │
├───────────┼───────────────────┼─────────────────────┼─────────┤
│ 1,000,000 │ 1.90 ms           │ 3.5 µs              │ ~543x   │
└───────────┴───────────────────┴─────────────────────┴─────────┘
```
Copy link
Contributor

@scovich scovich left a comment

Choose a reason for hiding this comment

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

Do we have in-repo benchmarks that show the improvements, by chance?

/// Appends `n` non-null entries into the builder.
#[inline]
pub fn append_non_nulls(&mut self, n: usize) {
self.null_buffer_builder.append_n_non_nulls(n);
Copy link
Contributor

Choose a reason for hiding this comment

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

append_nulls below currently appends an n-element vec of false instead of calling its append_n_nulls. It seems like both these methods should follow the same approach -- whichever way is faster?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Hey @scovich The n-element vec has been removed here: #9428 I thought splitting it up into smaller PRs would make the discussion easier. Allocating the vec is, as expected, way slower. The null_buffer is implemented using a Bitmap in the form of a MutableBuffer, which is way faster than the vec.

Copy link
Contributor

Choose a reason for hiding this comment

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

Ah, I somehow missed that PR. We have our answer then!

@Fokko
Copy link
Contributor Author

Fokko commented Feb 20, 2026

Do we have in-repo benchmarks that show the improvements, by chance?

I've posted the result of a micro-benchmark created by Claude. Let me know if you want to add the benchmark itself as well. Since it is so specific, I left it out, and creating a new one is fairly trivial.

Copy link
Contributor

@scovich scovich left a comment

Choose a reason for hiding this comment

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

LGTM!

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

Labels

arrow Changes to the arrow crate

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Add append_non_nulls to StructBuilder

2 participants