ParkMyCar / compact_str

A memory efficient string type that can store up to 24* bytes on the stack
MIT License
638 stars 46 forks source link

Simplify `is_empty()` #330

Closed Kijewski closed 11 months ago

Kijewski commented 11 months ago

Before:

0000000000000000 <<compact_str::CompactString>::is_empty>:
   0:   48 8b 77 08             mov    0x8(%rdi),%rsi
   4:   0f b6 57 17             movzbl 0x17(%rdi),%edx
   8:   48 8d 8a 40 ff ff ff    lea    -0xc0(%rdx),%rcx
   f:   48 83 f9 18             cmp    $0x18,%rcx
  13:   b8 18 00 00 00          mov    $0x18,%eax
  18:   48 0f 42 c1             cmovb  %rcx,%rax
  1c:   48 81 fa d8 00 00 00    cmp    $0xd8,%rdx
  23:   48 0f 43 c6             cmovae %rsi,%rax
  27:   48 85 c0                test   %rax,%rax
  2a:   0f 94 c0                sete   %al
  2d:   c3                      ret

After

0000000000000000 <<compact_str::CompactString>::is_empty>:
   0:   48 8b 57 08             mov    0x8(%rdi),%rdx
   4:   0f b6 4f 17             movzbl 0x17(%rdi),%ecx
   8:   48 8d 81 40 ff ff ff    lea    -0xc0(%rcx),%rax
   f:   48 81 f9 d8 00 00 00    cmp    $0xd8,%rcx
  16:   48 0f 43 c2             cmovae %rdx,%rax
  1a:   48 85 c0                test   %rax,%rax
  1d:   0f 94 c0                sete   %al
  20:   c3                      ret
Kijewski commented 11 months ago

3 instructions less, and 1 less register used. Not as much of an optimization as I had hoped for.

ParkMyCar commented 11 months ago

Interesting! Thanks for the PR, and for abstracting out ensure_read(...)!