tisonkun opened a new pull request, #397:
URL: https://github.com/apache/datasketches-rust/pull/397

   `XxHash64::write` previously copied trailing bytes shorter than 32 bytes 
into a `[u8; 32]` buffer before `finish64` reloaded them in 8-byte, 4-byte, and 
1-byte steps. Even when `write` and `finish64` are inlined at `T: Hash` call 
sites, LLVM cannot forward loads across a variable-length `copy_from_slice` 
into stack memory, causing stack frame allocation, `_memcpy` calls, slice 
bounds checks, and store-to-load forwarding stalls on variable-length inputs 
such as `raw_bytes` slices and strings. Callers that hash each item multiple 
times (`BloomFilter` hashes twice, `CountMinSketch` hashes once per row) paid 
that cost repeatedly per operation.
   
   This change packs buffered tail bytes into four scalar `u64` fields 
(`b0..b3`) in little-endian order, loads partial 8-byte words via overlapping 
fixed-width loads, finishes sub-8-byte remainders with bit shifts rather than 
slice indexing, and outlines the multi-write buffering path while keeping 
single-write slices, `write_u8`, and fixed-width integer `Hasher` methods 
inline in registers.
   
   Companion to #396 for `XxHash64`-backed structures (`BloomFilter`, 
`CountMinSketch` v2, and `XorFilter`).
   
   ### Benchmark results
   
   Apple M4 Max, macOS 26.6.2, `rustc 1.101.0-nightly (8006091eb 2026-10-17)`. 
Times are medians in milliseconds with full observed `min..max` ranges over 7 
alternating pairs comparing `main` at `28d4f35` (with the new `variable_bytes` 
and `xor::query_items` benchmark cases copied over so both revisions run 
identical inputs and round counts) against `4266b5b`.
   
   | Scenario                                         |     Before, ms |      
After, ms | Ratio |
   | :----------------------------------------------- | -------------: | 
-------------: | ----: |
   | `bloom::update::u64`                             | 231 (223..243) | 226 
(218..236) | 0.98x |
   | `bloom::update::bytes_32`                        | 218 (213..221) | 214 
(206..219) | 0.98x |
   | `bloom::update::variable_bytes`                  | 316 (310..323) | 272 
(265..283) | 0.86x |
   | `bloom::update::contains_and_insert_present_u64` | 192 (190..206) | 195 
(189..214) | 1.02x |
   | `bloom::update::contains_and_insert_absent_u64`  | 118 (111..137) | 117 
(113..127) | 0.99x |
   | `bloom::update::contains_and_insert_mixed_u64`   | 129 (126..151) | 128 
(126..202) | 0.99x |
   | `bloom::update::contains_and_insert_build_u64`   | 129 (122..135) | 125 
(122..147) | 0.97x |
   | `bloom::query::present_u64`                      | 211 (205..216) | 208 
(206..213) | 0.99x |
   | `bloom::query::absent_u64`                       |  88 (84..103)  |  90 
(84..100)  | 1.02x |
   | `bloom::query::present_bytes_32`                 | 208 (204..213) | 209 
(196..243) | 1.00x |
   | `bloom::query::absent_bytes_32`                  | 176 (171..187) | 154 
(150..171) | 0.88x |
   | `bloom::query::present_variable_bytes`           | 301 (295..311) | 255 
(249..262) | 0.85x |
   | `bloom::query::absent_variable_bytes`            | 364 (357..386) | 230 
(222..239) | 0.63x |
   | `countmin::update::u64`                          | 182 (177..204) | 180 
(177..185) | 0.99x |
   | `countmin::update::bytes_32`                     | 244 (241..249) | 235 
(230..243) | 0.96x |
   | `countmin::update::variable_bytes`               | 308 (303..317) | 231 
(224..251) | 0.75x |
   | `countmin::estimate::u64`                        | 165 (161..167) | 164 
(161..169) | 0.99x |
   | `countmin::estimate::bytes_32`                   | 233 (228..245) | 227 
(219..231) | 0.97x |
   | `countmin::estimate::variable_bytes`             | 301 (293..316) | 228 
(218..234) | 0.76x |
   | `xor::query_items::u64`                          | 292 (286..300) | 243 
(240..253) | 0.83x |
   | `xor::query_items::bytes_32`                     | 321 (316..322) | 268 
(264..273) | 0.83x |
   | `xor::query_items::variable_bytes`               | 341 (334..349) | 256 
(251..271) | 0.75x |
   
   ### Performance summary
   
   * **Variable-length byte slices (`1..=32` bytes)**: Eliminating the `[u8; 
32]` stack copy and `_memcpy` calls speeds up `BloomFilter::contains` by 37% 
(`0.63x`) on absent keys and 15% (`0.85x`) on present keys, 
`BloomFilter::insert` by 14% (`0.86x`), `CountMinSketch::update` and 
`CountMinSketch::estimate` (`num_hashes = 4`) by 24% to 25% (`0.75x` to 
`0.76x`), and `XorFilter::contains` by 25% (`0.75x`). Negative `BloomFilter` 
queries show the largest gain because bit probing stops at the first unset bit 
while `hash_pair` still computes both `XxHash64` hashes up front.
   * **Fixed 32-byte slices**: Compile-time 32-byte inputs have no tail 
remainder (`rem == 0`), so skipping stack buffer initialization primarily 
benefits short-circuiting `bloom::query::absent_bytes_32` (`0.88x`) and 
`xor::query_items::bytes_32` (`0.83x`), while probe-heavy `BloomFilter` and 
`CountMinSketch` operations are roughly unchanged (`0.96x` to `1.00x`).
   * **64-bit integers**: `BloomFilter` and `CountMinSketch` `u64` paths 
already folded to register arithmetic and remain unchanged within noise 
(`0.97x` to `1.02x`). `XorFilter::contains` on `u64` improves by 17% (`0.83x`) 
as the smaller register-backed `finish64` lowers hashing overhead in 
`xor::filter::hash_item`.
   
   ### Reproduction
   
   ```sh
   cargo x bench -- bloom::update --repeat 7 --raw
   cargo x bench -- bloom::query --repeat 7 --raw
   cargo x bench -- countmin::update --repeat 7 --raw
   cargo x bench -- countmin::estimate --repeat 7 --raw
   cargo x bench -- xor::query_items --repeat 7 --raw
   ```
   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to