shanielh commented on PR #12470:
URL: https://github.com/apache/arrow-rs/pull/12470#issuecomment-7183202406

   Thanks for picking up #12460's comparison. On the question of where the 
bound should go, #12460 puts it inline, in front of the out-of-line call:
   
   ```rust
   #[inline(always)]
   pub fn cmp_bytes(a: &[u8], b: &[u8]) -> Ordering {
       let (Some(x), Some(y)) = (a.first_chunk::<8>(), b.first_chunk::<8>()) 
else {
           return cmp_short(a, b); // u64 key from overlapping 4-byte loads, 
then length
       };
       if x != y {
           return u64::from_be_bytes(*x).cmp(&u64::from_be_bytes(*y));
       }
       if a.len().min(b.len()) > 64 {
           return a.cmp(b);
       }
       cmp_after_prefix(a, b) // #[inline(never)] leaf: 8-byte chunks, then one 
overlapping last word
   }
   ```
   
   The extra `memcmp` call target in the caller's loop didn't measure as a 
cost. Bounding inside the out-of-line function did, because it gave the leaf a 
stack frame. The full code is in #12460 (`is_greater_bytes`, 
`is_greater_after_prefix`, `short_key`).
   
   `cmp_bytes` as it is now matches the first variant I measured for #12460: 
the first 8 bytes inline, then `a > b`. On top of #12401, in instructions per 
write on an Apple M5 Pro:
   
   | input | first 8 bytes only | #12460 |
   |---|--:|--:|
   | `string_dictionary` | −9.7% | −11.4% |
   | `string_dictionary_1pct_cardinality` (14 bytes, first 8 shared) | +0.5% | 
−6.9% |
   | `Utf8`, 8-byte shared prefix + 4 to 11 random bytes | +0.3% | −10.2% |
   | `Utf8`, 32-byte shared prefix + 4 to 11 random bytes | −0.9% | −4.5% |
   | `Utf8`, random 1 to 7 bytes | −4.0% | −9.6% |
   
   The +0.5% on `string_dictionary_1pct_cardinality` lines up with this PR's 
0.96x to 0.98x on that benchmark on the Xeon. The bounded out-of-line loop is 
what turns values sharing their first 8 bytes from a loss into a gain. Values 
with a common length over 64 bytes still go to `memcmp`, and 
`large_string_non_null` and `small_string_*` measured within noise of main. I 
measured only on aarch64, so the amd64 runs here would show whether it holds 
there.
   
   Two smaller points:
   
   - The comments cite #12460 for "everything else goes to `memcmp`". #12460 
hands only common lengths over 64 bytes to `memcmp`; below that, the leaf scan 
measured faster. The note on `common_prefix_len` that it must stay a leaf comes 
from #12460's comparison function, not from a prefix scan.
   - In `compare_greater`, `as_bytes()` panics on a `ByteArray` with no data, 
which `a > b` ordered first. Statistics run before the encoder, which panics on 
such a value anyway, so only the panic site changes.
   


-- 
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]

Reply via email to