Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Lift alloc requirement for bits feature#614

Merged
wcampbell0x2a merged 5 commits into
sharksforarms:masterfrom
amboar:bit-precise-no-alloc
Nov 11, 2025
Merged

Lift alloc requirement for bits feature#614
wcampbell0x2a merged 5 commits into
sharksforarms:masterfrom
amboar:bit-precise-no-alloc

Conversation

@amboar
Copy link
Copy Markdown
Contributor

@amboar amboar commented Oct 16, 2025

This has been of interest to me for a while. In parallel others had similar thoughts, e.g. in #607. I've opened the PR on the back of discussion there.

@amboar amboar force-pushed the bit-precise-no-alloc branch from 94c1da8 to 7b65a18 Compare October 16, 2025 04:14
@amboar amboar changed the title Lift alloc requirement for bits feature [WIP] Lift alloc requirement for bits feature Oct 20, 2025
@amboar amboar force-pushed the bit-precise-no-alloc branch from 7b65a18 to e89f3c5 Compare October 20, 2025 04:03
@amboar
Copy link
Copy Markdown
Contributor Author

amboar commented Oct 20, 2025

I hit a bug in the leftover bit-order swap path for the writer. I've fixed it in a fairly terrible way (exploded the code-paths so they weren't dependent, then fixed them independently). I think it should probably be tidied up for maintenance purposes, but I've pushed what I've got so the underlying bug is fixed. I've marked the PR as [WIP] as a result.

I don't believe I've done anything to trigger the MSRV failure, which seems to relate to indexmap. It looks like it had a release a couple of days ago, but yet to look into the problem in detail.

@nullstalgia
Copy link
Copy Markdown

Indeed indexmap has put out a new release requiring an MSRV of 1.82, above deku's current 1.81.

indexmap-rs/indexmap#422

@wcampbell0x2a
Copy link
Copy Markdown
Collaborator

I'm fine with updating the MSRV.

@amboar amboar force-pushed the bit-precise-no-alloc branch from e89f3c5 to e354aa1 Compare October 23, 2025 00:53
@amboar
Copy link
Copy Markdown
Contributor Author

amboar commented Oct 23, 2025

I've added a patch bumping the MSRV to 1.82.

I'm looking for feedback on the Writer changes particularly, but also Reader. Once we've got consensus there and done any desired reworks I'll drop the [WIP] label.

@nullstalgia
Copy link
Copy Markdown

Just to have the idea floating around, would it make sense for targets with allocators to use a heap-allocated BitVec for potentially "huge" types, and default to the stack-based BoundedBitVec for most types sized under whatever "huge" threshold?

https://github.com/sharksforarms/deku/pull/614/files#diff-ab13062c7a9940b962bf1fd3d9e8288d3effec084bc391b6c3945f893171df12R191

Or if there's no real benefit, that's fine.

Comment thread src/attributes.rs Outdated
Comment thread src/attributes.rs Outdated
Comment thread src/attributes.rs Outdated
Comment thread src/attributes.rs Outdated
Comment thread src/writer.rs Outdated
@amboar
Copy link
Copy Markdown
Contributor Author

amboar commented Oct 26, 2025

Just to have the idea floating around, would it make sense for targets with allocators to use a heap-allocated BitVec for potentially "huge" types, and default to the stack-based BoundedBitVec for most types sized under whatever "huge" threshold?

https://github.com/sharksforarms/deku/pull/614/files#diff-ab13062c7a9940b962bf1fd3d9e8288d3effec084bc391b6c3945f893171df12R191

Or if there's no real benefit, that's fine.

Not that there's no real benefit, but maybe just some missing context: The specific case of the linked code implements the traits for Rust's primitive types. You can see the tree of macros invoking the implementations here:

https://github.com/amboar/deku/blob/bit-precise-no-alloc/src/impls/primitive.rs#L1209-L1260

As such there are no types that I'd consider "huge" there, in the sense of being inappropriate for the stack.

That said, the strategy of offering different implementations based on cfg(feature = "alloc") is reasonable but not something that's implemented yet.

@amboar amboar force-pushed the bit-precise-no-alloc branch from e354aa1 to 036c221 Compare October 27, 2025 00:00
@amboar
Copy link
Copy Markdown
Contributor Author

amboar commented Oct 27, 2025

@wcampbell0x2a I've addressed your comments so far, and have fixed the missing API docs as well.

@nullstalgia
Copy link
Copy Markdown

The specific case of the linked code implements the traits for Rust's primitive types.

Ah, thank you for the correction. For the record, I was thinking about a potential case where a user's struct's size with all the members added up equaled an insane size, but that wouldn't be caught at this stage like you said. I had originally misinterpreted the type going into the size_of, assuming it was the user-generated type. If it's limited to the stack-friendly primitives, then I'm fine retracting my suggestion. :)

Don't prevent the bits feature from being enabled in no-alloc
environments.

Signed-off-by: Andrew Jeffery <[email protected]>
Allow selection of bits feature in no-alloc environments.

Signed-off-by: Andrew Jeffery <[email protected]>
@amboar amboar force-pushed the bit-precise-no-alloc branch from 036c221 to 54ab016 Compare October 27, 2025 04:09
@amboar
Copy link
Copy Markdown
Contributor Author

amboar commented Oct 27, 2025

Found a bug where we were failing to not require alloc for read padding while testing nvme-mi-dev against usbnvme. I've fixed it and pushed the result.

@amboar amboar changed the title [WIP] Lift alloc requirement for bits feature Lift alloc requirement for bits feature Oct 27, 2025
@amboar
Copy link
Copy Markdown
Contributor Author

amboar commented Nov 6, 2025

@sharksforarms any thoughts on this?

Comment thread src/reader.rs
// WA: required to apply endianness for cases where the field is not aligned with the byte boundary
// (see https://github.com/sharksforarms/deku/issues/603)
front_bits = &front_bits_le;
// TODO: Profile and optimise
Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

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

what were the considerations you were thinking about at the time of writing this TODO?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

When iterating dst below we read into a slice backed by a buffer of a single byte.

I left the comment not so much as something to fix immediately before the PR is merged, but to acknowledge the existing performance concern if someone did profile and find the current implementation problematic. The current implementation is what it is because it's easy to reason about, inside a bunch of code that's (IMO) tricky to comprehend.

The follow-up would be to figure out chunking strategies that can reduce issued reads to 1/N.

Comment thread tests/test_alloc.rs
Copy link
Copy Markdown
Owner

@sharksforarms sharksforarms left a comment

Choose a reason for hiding this comment

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

Thank you - this looks great. I like the liberal use of debug_assert! and the extraction of the funky bit-manipulation logic into functions.

@wcampbell0x2a wcampbell0x2a merged commit 4fb3527 into sharksforarms:master Nov 11, 2025
9 of 10 checks passed
@github-actions github-actions Bot mentioned this pull request Nov 10, 2025
@github-actions github-actions Bot mentioned this pull request Nov 24, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants