`base64` module: Link against SIMD library for 10x performance.
まだ誰も着手していません。
- 主要言語
- Python
- スター
- 77.2k
- フォーク
- 35.9k
- PR マージ指標
- PR 指標を取得中
説明
Performance enhancement
Proposal:
https://pypi.org/project/pybase64/ aka https://github.com/mayeut/pybase64 (BSD licensed) exists. On top of some of its own SIMD code for base64 module extra features (character translation)^, it links against https://github.com/aklomp/base64, a BSD licensed C99 library with SIMD acceleration giving 5-20x performance on base64 encoding and decoding operations vs our existing generic byte based base64 C code.
We could adopt a bunch of the pybase64 code to make the default base64 module experience better - it is relatively straight forward extension module code (as one would expect). On the other hand, I expect pybase64 to still be where new development and further improvements in this space continue to happen as people who care strongly about performance need the latest and greatest from PyPI regardless of their current CPython version. (looping in @mayeut for thoughts on that)
Practicalities: Library availability? we'd vendor a libbase64 build for use on our binary distributions. I don't think it is currently widely available (? I only did a quick search on Ubuntu) as a package on Linux distributions though so we'd currently need to vendor our own copy in tree to be fair and match the good performance there (yuck, but ideally only temporary until distros pick it up as a package of its own, consider it similar to a Modules/_decimal/libmpdec/ situation - our configure.ac finds an installed one & distros link against that)
Risks: It is a new C library dependency. Security concerns within it thus become our own. As base64 is frequently used to process untrusted input. But its surface of possible problems is limited (very simple data format). We should ensure the library gets proper oss-fuzz test coverage before adoption (@aklomp for visibility).
^ bytes.translate, bytearray.translate, or str.translate might benefit from similar SIMD treatment - which would be better from a CPython perspective than only doing that within this module? If so, lets file a new issue just for that bit.
❯ python -m pybase64 benchmark `which python`
pybase64 1.4.0 (C extension active - NEON) # running on my Apple M3
bench: altchars=None, validate=False
pybase64._pybase64.encodebytes: 4776.815 MB/s (5,936,128 bytes -> 8,018,983 bytes)
pybase64._pybase64.b64encode: 11989.872 MB/s (5,936,128 bytes -> 7,914,840 bytes)
pybase64._pybase64.b64decode: 3039.329 MB/s (7,914,840 bytes -> 5,936,128 bytes)
base64.encodebytes: 292.876 MB/s (5,936,128 bytes -> 8,018,983 bytes)
base64.b64encode: 601.307 MB/s (5,936,128 bytes -> 7,914,840 bytes)
base64.b64decode: 492.088 MB/s (7,914,840 bytes -> 5,936,128 bytes)
bench: altchars=None, validate=True
pybase64._pybase64.b64encode: 12327.286 MB/s (5,936,128 bytes -> 7,914,840 bytes)
pybase64._pybase64.b64decode: 8611.733 MB/s (7,914,840 bytes -> 5,936,128 bytes)
base64.b64encode: 597.389 MB/s (5,936,128 bytes -> 7,914,840 bytes)
base64.b64decode: 472.430 MB/s (7,914,840 bytes -> 5,936,128 bytes)
bench: altchars=b'-_', validate=False
pybase64._pybase64.b64encode: 1287.615 MB/s (5,936,128 bytes -> 7,914,840 bytes)
pybase64._pybase64.b64decode: 2524.966 MB/s (7,914,840 bytes -> 5,936,128 bytes)
base64.b64encode: 473.320 MB/s (5,936,128 bytes -> 7,914,840 bytes)
base64.b64decode: 406.411 MB/s (7,914,840 bytes -> 5,936,128 bytes)
bench: altchars=b'-_', validate=True
pybase64._pybase64.b64encode: 1283.111 MB/s (5,936,128 bytes -> 7,914,840 bytes)
pybase64._pybase64.b64decode: 6745.809 MB/s (7,914,840 bytes -> 5,936,128 bytes)
base64.b64encode: 464.526 MB/s (5,936,128 bytes -> 7,914,840 bytes)
base64.b64decode: 391.959 MB/s (7,914,840 bytes -> 5,936,128 bytes)
Has this already been discussed elsewhere?
No response given
Links to previous discussion of this feature:
If we spawn Discuss threads around this, lets edit and drop links here.
Linked PRs
- gh-143262
コントリビューションガイド
はじめの一歩
- issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
- 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
- リポジトリをフォークし、ブランチを切って変更します。
- issue 番号を参照したプルリクエストを送ります。
調査の方向性
configure.ac と base64 モジュールの基盤となっている既存の C 実装から始め、提案されている pybase64 と aklomp/base64 のライブラリを比較します。完了には、依存関係と vendoring の方針を解決し、SIMD ビルドを統合し、採用前にセキュリティと oss-fuzz のカバレッジに対処することが必要です。
索引モデルが issue の本文から書いたものです。
評価
- 技術スタック
- c, python
- 領域
- backend, performance
- issue の種類
- 機能追加
- 難易度
- 5/5
- 見積もり時間
- 1週間以上
- 活発さ
- 停滞
- 明瞭さ
- 説明が足りない
- 初心者へのやさしさ
- 25/100