Skip to content

fix!: Verify sizes against the allocator and harden the derive macro - #68

Merged
bircni merged 13 commits into
mainfrom
test/pre-release-hardening
Nov 15, 2026
Merged

bircni merged 13 commits into
mainfrom
test/pre-release-hardening

Conversation

@bircni

@bircni bircni commented Nov 14, 2026 •

Copy link
Copy Markdown
Owner

Pre-release hardening for 0.12.0. Sizes are now checked against a counting allocator instead of against our own formulas, on eight platforms, and everything that surfaced was fixed. Six independent review passes went over the result.

Size fixes

  • IndexMap/OrderMap created with a capacity of 4 to 6 or 8 to 13 reported a hash table with half its buckets.
  • Hash tables on LoongArch: the standard library scans 16 control bytes at once there, the hashbrown behind indexmap and ordermap 8. Both are told apart now.
  • RoaringBitmap reported eight times the size of its bitset containers and twice the capacity of its array containers, because the byte totals of statistics() do not describe the allocations. Bitset and array containers are exact now, run containers a lower bound.
  • ThinVec of zero sized elements reported a header it does not allocate, and the header padding for over-aligned elements was missing. Requires thin-vec 0.2.20.

Tracker

  • StandardTracker hashes addresses with a folded 128 bit multiply. The previous finalizer lost its spread beyond 2^26 buckets and on 32 bit targets.

Derive macro

  • Field attributes are parsed by hand; attribute-derive is no longer a dependency. Every error message is our own and pinned by a unit test.
  • The local variables of the generated code have the hygiene of a macro_rules! local and keep their __get_size_ prefix, so neither a helper function, a field nor an item of the user can collide with them. The prefix is documented as reserved.
  • The generated code only names paths under ::get_size2, including usize, so a consumer may shadow usize or depend on a crate called core.
  • A size literal is handed on as written, so the compiler rejects one which does not fit the usize of the target instead of truncating it.

portable-atomic

  • The feature builds for targets without atomic compare-and-swap such as thumbv6m.
  • New feature portable-atomic-critical-section for the 64 and 128 bit atomics on such targets; the portable_atomic_unsafe_assume_single_core cfg is honoured, and AVR and MSP430 are covered.

Tests

  • tests/allocation_ground_truth.rs: a counting global allocator compares reported and real sizes, exactly where the layout is known and as a lower bound otherwise.
  • tests/public_api.rs: implements both traits like a downstream crate, and derives next to items, helpers and fields named like the generated locals.
  • Unit tests expand the derive macro and pin its messages and output.

CI

  • test on LoongArch and on x86 without SSE2, run the integration tests on WASI and under Miri, build for thumbv6m
  • coverage for the whole workspace (96%, was 91%)
  • the breaking change check runs in a workflow of its own, so editing a title no longer reruns the test suite
  • the publish check packages both crates in one go, which also works on the commit bumping the version

Dependencies

  • declared minimums which the crates were built and tested with: chrono 0.4.34 (first to export TimeDelta), thin-vec 0.2.20, syn 3.0.4, quote 1.0.40, proc-macro2 1.0.95

Breaking changes

  • RoaringBitmap, RoaringTreemap and ThinVec of zero sized elements report smaller sizes; small IndexMap/OrderMap report larger ones.
  • A size attribute with a suffix other than usize is rejected (0XFF used to be read as 0), and so is a value which does not fit the usize of the target.
  • Clippy's literal lints now apply to the literal of a size attribute, since it is passed through as written.
  • A constant, static, unit or tuple struct, imported enum variant or const generic parameter whose name starts with __get_size_ clashes with the generated code.
  • The derive macro needs a get-size2 of the same release; both crates have to be published together as 0.12.0.
  • On targets without compare-and-swap the wide portable-atomic types only implement GetSize through the new feature or the cfg.

Known limits

  • hash tables report too little after removals, down to zero once emptied, since the capacity is the only input
  • BTreeMap/BTreeSet nodes, DashMap shards and the container list of a RoaringBitmap are not counted
  • a shared or 'static Bytes buffer cannot be told apart

…uild `portable-atomic` without compare-and-swap

Found by comparing the reported sizes with a counting allocator:

- an `IndexMap`/`OrderMap` created with a capacity of 4 to 6, or 8 to 13,
  reported a hash table with half its buckets, since the reconstruction
  only handled capacities a table reports itself
- a `RoaringBitmap` reported eight times the size of its bitset containers
  and twice the capacity of its array containers, because the byte totals of
  its statistics do not describe the allocations; the size is now derived
  from the container and value counts

The `portable-atomic` feature did not build for targets without atomic
compare-and-swap, like `thumbv6m`, where the 64 and 128 bit atomics do not
exist.
…rive errors

- an integration test with a counting global allocator compares the reported
  heap sizes with the real allocations, exactly where the layout is known and
  as a lower bound otherwise
- an integration test implements the traits like a downstream crate does, so
  a changed signature fails to compile
- the derive macro is expanded in unit tests, which pin its error messages
  and the shape of the generated implementation
- run the test suite on LoongArch and on x86 without SSE2, whose hash tables
  use other group widths, and the integration tests on WASI and under Miri
- build for a target without atomic compare-and-swap
- measure the coverage of the whole workspace, now that the derive macro has
  unit tests, and leave the examples out
- do not name a Rust version for the integrations in the READMEs, which went
  stale with the last dependency update
…part

The standard library builds `hashbrown` with its `nightly` feature, which
scans 16 control bytes at once with LSX, while the `hashbrown` behind
`indexmap` and `ordermap` is built without it. Both were given the same
group width, which was wrong for one of them on LoongArch.

The unit test comparing the standard library with a `hashbrown` built by
`cargo` cannot hold on such a target and is superseded by the comparison with
the allocator. The test jobs now run every test binary, so one failure no
longer hides the others.
- fold the full 128 bit product in the address hasher: rotating left the
  always zero low bits of an address into the bucket index of tables beyond
  2^26 buckets, and into the tag bits used on 32 bit targets
- keep the 64 and 128 bit `portable-atomic` types on targets without
  compare-and-swap when the new `portable-atomic-critical-section` feature
  or the `portable_atomic_unsafe_assume_single_core` cfg provides them
- report the real capacity of roaring array containers, which is half of
  what the statistics state
- measure a `ThinVec` of zero sized or over-aligned elements correctly
- parse the field attributes by hand instead of with `attribute-derive`, so
  every error message is our own and independent of the features other
  crates enable for `syn`; accept an empty `ignore()` and a bare attribute
- run the breaking change check in a workflow of its own, so editing a title
  no longer reruns the test suite, and keep tag runs apart from branch runs
- correct comments and documentation about emptied hash tables, zero sized
  entries and variant level `ignore(...)`, and give the tests about removals
  a floor
Version 0.2.20 stopped allocating a header for them, so the size of such a
vector depended on the resolved version.
Found by building and testing against the minimal versions of all direct
dependencies: `chrono` only exports `TimeDelta` since 0.4.34, and `syn`
needs newer `quote` and `proc-macro2` than the bare major version. The
allocator test no longer needs a recent `bytes` either.
- accept the parenthesised attribute spellings `size(4)`, `size_fn(helper)`
  and `ignore(true)` again, which compiled before the field attributes were
  parsed by hand
- reject a `size` with a suffix other than `usize`, which was dropped
  silently and turned `0XFF` into 0, and report every invalid `size` with
  the same message
- compute the number of buckets without an intermediate overflow, and
  saturate where the table size is added up
- build the single core cfg of `portable-atomic` in CI, and document which
  ways of enabling its wide atomics this crate can see
- mention the new feature where the features are listed, and drop a stale
  dependency name from a comment
- give the local variables of the generated code the hygiene of a
  `macro_rules!` local, so a helper function named like one of them is no
  longer shadowed
- hand the `size` literal on as written, so the compiler rejects one which
  does not fit the `usize` of the target instead of truncating it
- tell an invalid value for `ignore` apart from a list of generics, and
  describe precisely what `size` accepts
- implement `GetSize` for the wide `portable-atomic` types on AVR and
  MSP430, where they always exist
- check the publication of both crates in one go, which also works on the
  commit bumping the version
- saturate where the size of an index map is added up, like for the tables
  of the standard library
- correct comments and documentation, and a test which no longer pinned
  what it claimed
Hygiene only keeps local bindings apart. An item of the same name, like a
lower case constant or a static brought in by a glob import of generated
bindings, turned the binding into a pattern or was rejected as shadowed.
The locals carry their `__get_size_` prefix again, on top of the hygiene.

The generated code also names `usize` by its full path, so a type of that
name in scope no longer breaks the derive macro.
`::core::primitive::usize` does not resolve in a crate with a dependency
named `core`. The generated code already relies on `::get_size2`, which now
re-exports the type for it.

Also from the fifth review:

- sum up tuples and ranges by rebinding instead of assigning, which needs
  no lint expectation that newer clippy versions leave unfulfilled
- do not count the two bytes of the serialized run count of a roaring run
  container
- document the `__get_size_` prefix as reserved, and test that the hygiene
  of the generated locals holds for helpers and fields carrying it
Neither the hidden re-export of `usize` nor the corrected size of a run
container was guarded by a test. The documentation now lists every kind of
item the reserved prefix clashes with, and says how the generated code
refers to the trait crate.
@bircni bircni changed the title fix: Correct index map and roaring sizes, and verify sizes against the allocator fix!: Verify sizes against the allocator and harden the derive macro Nov 15, 2026
@bircni
bircni merged commit 7d06db3 into main Nov 15, 2026
19 checks passed
@bircni
bircni deleted the test/pre-release-hardening branch November 15, 2026 10:44
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.

1 participant