Skip to content

Conversation

@zhutao100
Copy link

This commit significantly improves the performance and memory efficiency of integer-to-byte conversion utilities.

Key optimizations include:

  • Replacing slow, iterative struct packing/unpacking with highly efficient memoryview.cast() and int.to_bytes() operations.
  • Using array.array and memory views to eliminate intermediate memory copies, especially in dump_bar0 and when creating byte arrays from integer lists.
  • Added a new unit test suite (tests/test_ints_helpers.py) to verify correctness and prevent future regressions.
  • Adding a performance test suite (tests/test_ints_performance.py) that validates and benchmarks the gains over the previous implementation.

BREAKING CHANGE:
This commit introduces backward-incompatible API changes to improve performance and clarity.

  • NvSwitch.dump_bar0() now returns a read-only memoryview instead of a bytearray.
  • The utils.bytearray_from_ints() function has been removed and replaced by utils.bytearray_view_from_ints(), which returns a memoryview.
  • The NiceStruct.to_int_array() method has been renamed to to_int_list(), indicating it returns a int list.
  • The NiceStruct.from_int_array() method has been renamed to from_ints(), indicating it accepts any iterables of int.

This commit significantly improves the performance and memory efficiency of integer-to-byte conversion utilities.

Key optimizations include:
- Replacing slow, iterative struct packing/unpacking with highly efficient `memoryview.cast()` and `int.to_bytes()` operations.
- Using `array.array` and memory views to eliminate intermediate memory copies, especially in `dump_bar0` and when creating byte arrays from integer lists.
- Added a new unit test suite (`tests/test_ints_helpers.py`) to verify correctness and prevent future regressions.
- Adding a performance test suite (`tests/test_ints_performance.py`) that validates and benchmarks the gains over the previous implementation.

BREAKING CHANGE:
This commit introduces backward-incompatible API changes to improve performance and clarity.

- `NvSwitch.dump_bar0()` now returns a read-only `memoryview` instead of a `bytearray`.
- The `utils.bytearray_from_ints()` function has been removed and replaced by `utils.bytearray_view_from_ints()`, which returns a `memoryview`.
- The `NiceStruct.to_int_array()` method has been renamed to `to_int_list()`, indicating it returns a int list.
- The `NiceStruct.from_int_array()` method has been renamed to `from_ints()`, indicating it accepts any iterables of int.
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