Skip to content

fix: remove incorrect debug assertion in BatchCoalescer #9508

Merged
alamb merged 2 commits intoapache:mainfrom
Tim-53:fix/coalesce-reserve-exact
Mar 9, 2026
Merged

fix: remove incorrect debug assertion in BatchCoalescer #9508
alamb merged 2 commits intoapache:mainfrom
Tim-53:fix/coalesce-reserve-exact

Conversation

@Tim-53
Copy link
Contributor

@Tim-53 Tim-53 commented Mar 5, 2026

Which issue does this PR close?

Rationale for this change

Vec::reserve(n) does not guarantee exact capacity, Rust's MIN_NON_ZERO_CAP optimization means reserve(2) gives capacity = 4 for most numeric types, causing debug_assert_eq!(capacity, batch_size) to panic in debug mode when batch_size < 4.

What changes are included in this PR?

Replace reserve with reserve_exact in ensure_capacity in both InProgressPrimitiveArray and InProgressByteViewArray. reserve_exact skips the amortized growth optimization and allocates exactly the requested capacity, making the assertion correct.

Are these changes tested?

No. This only fixes an incorrect debug assertion.

Are there any user-facing changes?

No

@github-actions github-actions bot added the arrow Changes to the arrow crate label Mar 5, 2026
@cetra3
Copy link
Contributor

cetra3 commented Mar 5, 2026

This should be a change to the debug_assert to be >= rather than here I think? Reserving exact will increase memory fragmentation

@alamb
Copy link
Contributor

alamb commented Mar 5, 2026

This should be a change to the debug_assert to be >= rather than here I think? Reserving exact will increase memory fragmentation

I agree fixing the assert might be best. The idea with the assert as I recall was to ensure that the memory growth is as expected. Maybe we can find a way to write a test instead that does the check and remove the debug assert entirely 🤔

@Dandandan
Copy link
Contributor

I agree, updating the debug (or even removing with the test) should be the fix.

@mbutrovich
Copy link
Contributor

Thanks for working on this, I'm hitting it as well :(

@Tim-53
Copy link
Contributor Author

Tim-53 commented Mar 7, 2026

Just updated the PR to remove the debug_assert_eq! instead of weakening it to >=.
The assertion was originally added in #9132 as part of an anti-over-allocation optimization. Changing to >= silently lose that invariant. But i agree removing the assertion here is the right call.

I'm not sure what a meaningful replacement test would look like here, since the assertion was guarding internal memory behavior rather than observable output. Happy to add a test if someone has a good idea what to check here.

Copy link
Contributor

@Dandandan Dandandan left a comment

Choose a reason for hiding this comment

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

LGTM

@Dandandan
Copy link
Contributor

Dandandan commented Mar 7, 2026

This should be a change to the debug_assert to be >= rather than here I think? Reserving exact will increase memory fragmentation

Btw - I didn't really understand this part? In this code using reserve_exact shouldn't lead to memory fragmentation?
Note that we always target max self.batch_size in this code (it will never exceed this).

@cetra3
Copy link
Contributor

cetra3 commented Mar 7, 2026

The standard reservation strategy for Vec is powers of two, which plays nicer with allocators when you are allocating a lot of memory. If you start allocating exact sizes, then it is has to "slot" in an allocation with the rest of the system, and so you end up allocating more than that anyway.

@Dandandan
Copy link
Contributor

The standard reservation strategy for Vec is powers of two, which plays nicer with allocators when you are allocating a lot of memory. If you start allocating exact sizes, then it is has to "slot" in an allocation with the rest of the system, and so you end up allocating more than that anyway.

Ah good one, yeah that is true. Although batch_size is in practice always set to a power of 2 as well...

@alamb alamb changed the title fix: use reserve_exact in BatchCoalescer to fix debug assertion fix: remove incorrect debug assertion in BatchCoalescer Mar 9, 2026
Copy link
Contributor

@alamb alamb left a comment

Choose a reason for hiding this comment

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

I agree -- this looks good to me too now. Removing the over zealous assert 👍

@alamb alamb merged commit fec3c02 into apache:main Mar 9, 2026
27 checks passed
@Tim-53
Copy link
Contributor Author

Tim-53 commented Mar 9, 2026

This should be a change to the debug_assert to be >= rather than here I think? Reserving exact will increase memory fragmentation

Btw - I didn't really understand this part? In this code using reserve_exact shouldn't lead to memory fragmentation? Note that we always target max self.batch_size in this code (it will never exceed this).

I think when I wrote this, I was thinking of cases where the allocation size could change over time and trigger multiple reallocations.

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.

debug_assert_eq! in BatchCoalescer panics in debug mode when batch_size < 4

5 participants