Skip to content

Optimize raw backend PCM conversion path in audioread/rawread.py#154

Merged
semohr merged 1 commit into
beetbox:mainfrom
AsymptotaX:perf/rawread-cpu-optimizations
Apr 9, 2026
Merged

Optimize raw backend PCM conversion path in audioread/rawread.py#154
semohr merged 1 commit into
beetbox:mainfrom
AsymptotaX:perf/rawread-cpu-optimizations

Conversation

@AsymptotaX

@AsymptotaX AsymptotaX commented Feb 20, 2026

Copy link
Copy Markdown
Contributor

What changed

  • byteswap(s):

    • replaced per-chunk Python struct.pack/unpack loop with array('h').byteswap()
    • this reduces Python-level overhead and allocations
  • read_data(self, block_samples=1024):

    • precompute needs_conversion and needs_byteswap outside the loop
    • skip audioop.lin2lin(...) when old_width == TARGET_WIDTH
    • keep byteswap only when required (big-endian / non-sowt)

Environment

  • Machine: Mac mini M4, 16GB RAM
  • Backend used: rawread.RawAudioFile
  • Inputs: 4 WAV + 4 AIFF files
  • Repeats: 5 runs per file

Results

file size samples Old mean(ms) New mean(ms) Speedup
300s.wav 50.47 MiB 13230000 31.95 11.78 2.71x
60s.wav 10.09 MiB 2646000 14.83 2.64 5.62x
20s.wav 3.36 MiB 882000 2.83 0.99 2.86x
5s.wav 861.4 KiB 220500 0.97 0.22 4.41x
300s.aiff 50.47 MiB 13230000 3355.95 25.01 134.18x
60s.aiff 10.09 MiB 2646000 626.81 4.50 139.29x
20s.aiff 3.36 MiB 882000 209.07 1.37 152.61x
5s.aiff 861.4 KiB 220500 52.30 0.36 145.28x

@semohr

semohr commented Apr 9, 2026

Copy link
Copy Markdown
Contributor

LGTM. Sorry for the long delay. Was just investigating beetbox/pyacoustid#88 and found we already handle byteswaps correctly in here.

Also thought that we should no do the swap in a python loop 😄

@semohr
semohr merged commit d5d8e25 into beetbox:main Apr 9, 2026
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