Skip to content

Conversation

@bbaldino
Copy link
Owner

@bbaldino bbaldino commented May 2, 2025

This change optimizes reading and writing byte-aligned values (u8, u16, u24, u32) when the buffer is byte-aligned as well.

bench before:

Timer precision: 36 ns
put_get_ux              fastest       │ slowest       │ median        │ mean          │ samples │ iters
├─ get_ux_byte_aligned  276 ns        │ 382.6 ns      │ 307.7 ns      │ 311.8 ns      │ 100     │ 1000000
╰─ put_ux_byte_aligned  174.9 ns      │ 224.7 ns      │ 185.5 ns      │ 188.2 ns      │ 100     │ 1000000

after:

Timer precision: 43 ns
put_get_ux              fastest       │ slowest       │ median        │ mean          │ samples │ iters
├─ get_ux_byte_aligned  67.45 ns      │ 96.33 ns      │ 76.11 ns      │ 76.61 ns      │ 100     │ 1000000
╰─ put_ux_byte_aligned  69.12 ns      │ 98.73 ns      │ 76.47 ns      │ 77.76 ns      │ 100     │ 1000000

Note that even though we check for byte alignment, due to chain we can't assume the bytes we're writing to are contiguous, so even with this change we have copy the data into bytes and then write them via try_copy_to_slice_bytes/try_put_slice_bytes so the reads/writes will play nicely if the underlying buffer is chained.

@bbaldino bbaldino requested a review from Copilot May 2, 2025 18:13
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull Request Overview

This PR optimizes reading and writing of byte-aligned types by adding specialized load and store functions for u16, u24, and u32 as well as optimized branches for byte-aligned buffers in get/put operations. Key changes include:

  • Adding new ByteOrder trait methods for u16, u24, and u32 in both BigEndian and LittleEndian implementations.
  • Introducing optimized byte-aligned branches in BitBufExts and BitBufMutExts to bypass bit-level operations.
  • Updating benchmarks and Cargo configuration to reflect the performance improvements.

Reviewed Changes

Copilot reviewed 5 out of 5 changed files in this pull request and generated no comments.

Show a summary per file
File Description
src/buf/byte_order.rs Adds optimized load/store functions for u16, u24, and u32.
src/buf/bit_buf_mut_exts.rs Implements byte-aligned write optimizations with new error checks.
src/buf/bit_buf_exts.rs Implements byte-aligned read optimizations with new error checks.
benches/put_get_ux.rs Provides benchmarks to compare performance before and after.
Cargo.toml Updates dev-dependencies and bench configuration.
Comments suppressed due to low confidence (4)

src/buf/bit_buf_mut_exts.rs:63

  • [nitpick] The error message in the byte-aligned branch for put_u8 has an extra space in 'Remaining bytes'. Consider changing it to 'Remaining bytes ({}) are less than the size of the source (1)' for clarity.
                        "Remaining  bytes ({}) are less than the size of the source (1)",

src/buf/bit_buf_mut_exts.rs:102

  • [nitpick] In the put_u16 byte-aligned branch, the error message contains an extra space ('Remaining bytes'). Consider revising it to 'Remaining bytes ({}) are less than the size of the source (2)' for consistency.
                        "Remaining  bytes ({}) are less than the size of the source (2)",

src/buf/bit_buf_mut_exts.rs:140

  • [nitpick] The error message in the byte-aligned branch for put_u24 has a double space in 'Remaining bytes'. It is recommended to update it to 'Remaining bytes ({}) are less than the size of the source (3)'.
                        "Remaining  bytes ({}) are less than the size of the source (3)",

src/buf/bit_buf_mut_exts.rs:179

  • [nitpick] In the put_u32 byte-aligned branch, the error message includes an extra space in 'Remaining bytes'. Update it to 'Remaining bytes ({}) are less than the size of the source (4)' for improved readability.
                        "Remaining  bytes ({}) are less than the size of the source (4)",

@bbaldino bbaldino merged commit 0883270 into master May 2, 2025
1 check passed
@bbaldino bbaldino deleted the optimizations branch May 2, 2025 18:21
@github-actions github-actions bot mentioned this pull request May 2, 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.

2 participants