Skip to content

Borrow the input of native functions - #146

Merged
yunzheng merged 2 commits into
fox-it:mainfrom
falklindner:feature/native-borrow-input
Oct 28, 2026
Merged

yunzheng merged 2 commits into
fox-it:mainfrom
falklindner:feature/native-borrow-input

Conversation

@falklindner

@falklindner falklindner commented Oct 25, 2026 •

Copy link
Copy Markdown
Contributor

The native lz4.decompress, lzo.decompress, crc32c and crc64c now borrow a bytes input and copy a bytearray in one go, through a shared Buffer argument type. Any other sequence of ints is still accepted as before. LZO also no longer copies the body after a header.

Linux, Python 3.13, native:

main this PR
crc32c, 4 KiB 34.3 µs 8.2 µs
crc32c, 1 MiB 8.1 ms 2.1 ms
lzo, 64 KiB block 266 µs 78 µs
lz4, 256 KiB block 747 µs 162 µs

crc64c (from #138) gains as much as crc32c: on a second machine, a 4 KiB input takes 7.7 µs instead of 39.6 µs, and 1 MiB 1.9 ms instead of 10.2 ms.

Test with tox -e py3,native. New tests cover bytes, bytearray and memoryview input; a benchmark covers crc32c on a 4 KiB page.

Closes #145

🤖 Generated with Claude Code

https://claude.ai/code/session_01HJvjcGbjsG74UunzfT5zNE

Schamper
Schamper previously approved these changes Oct 27, 2026
@Schamper
Schamper requested a review from yunzheng October 27, 2026 11:51
@codecov

codecov Bot commented Oct 27, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 0.00%. Comparing base (2e15883) to head (9235a51).

Additional details and impacted files
@@          Coverage Diff          @@
##            main    #146   +/-   ##
=====================================
  Coverage   0.00%   0.00%           
=====================================
  Files         31      31           
  Lines       2179    2179           
=====================================
  Misses      2179    2179           
Flag Coverage Δ
unittests 0.00% <ø> (ø)

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@codspeed

codspeed Bot commented Oct 27, 2026

Copy link
Copy Markdown

Merging this PR will not alter performance

✅ 38 untouched benchmarks
🆕 1 new benchmark

Performance Changes

Benchmark BASE HEAD Efficiency
🆕 test_crc32c_benchmark_page[python] N/A 3.6 ms N/A

Comparing falklindner:feature/native-borrow-input (b45da01) with main (c1768d0)

Open in CodSpeed

@yunzheng
yunzheng force-pushed the feature/native-borrow-input branch from b45da01 to 94b241d Compare October 27, 2026 12:50

@yunzheng yunzheng left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

@falklindner can you rebase and resolve conflicts and also check+fix for the same issues in crc64c.rs that was merged in #138 ?

falklindner and others added 2 commits October 28, 2026 06:06
The native LZ4 and LZO decompress and the CRC32C functions take their
input as a Vec<u8>. pyo3 fills one from a bytes object as from any
sequence, one Python int per byte, which costs about 10 ns a byte and
is most of the time of a call: the CRC32C of a 4 KiB page took 34 us,
of which 8 us is computing it.

A bytes is now borrowed and a bytearray copied in one go, through a
Buffer type the three share. Other sequences of ints, a memoryview
among them, still take the old path, so every input that worked before
still does. LZO also no longer copies the body after a header.

Measured on Linux with Python 3.13, before and after:

  crc32c, 13 bytes                  0.22 us   0.10 us
  crc32c, 4 KiB                     34.3 us    8.2 us
  crc32c, 1 MiB                      8.1 ms    2.1 ms
  lzo, 64 KiB block (20 KB in)       266 us     78 us
  lz4, 256 KiB block (95 KB in)      747 us    162 us

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01LNZJRJFni88V8JGtP2tj29
The native crc64c from fox-it#138 takes its input as a Vec<u8>, which pyo3
fills from a bytes object one Python int at a time. It now takes the
same Buffer argument as crc32c. On Linux with Python 3.13, a 4 KiB
input takes 7.7 us instead of 39.6 us.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01HJvjcGbjsG74UunzfT5zNE
@falklindner

Copy link
Copy Markdown
Contributor Author

Done: rebased onto main, and crc64c now borrows its input too (9235a51). The commit that moved the LZO test is dropped, as #143 is merged.

🤖 Generated with Claude Code

@yunzheng
yunzheng merged commit 47679e8 into fox-it:main Oct 28, 2026
30 of 31 checks passed
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.

Native functions copy their input one int at a time

3 participants