perf: optimize BNLP — 2-7.4x speedup with Rust-accelerated tokenizer#55
Open
Fatin-Ishraq wants to merge 1 commit into
Open
perf: optimize BNLP — 2-7.4x speedup with Rust-accelerated tokenizer#55Fatin-Ishraq wants to merge 1 commit into
Fatin-Ishraq wants to merge 1 commit into
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
🚀 Performance Optimization PR
This PR delivers systematic performance improvements across the BNLP library, achieving 2–7.4× speedup depending on the operation, with an optional Rust extension for maximum performance.
📊 Benchmark Results
🔧 What Changed
Rust Extension (Optional —
bnlp_rust/)unicodedata.category()bridge overheadclean_text()function that does punctuation + emoji + digit removal in one Rust passbnlp_rustis not installed, the optimized pure Python fallback is used automaticallyPure Python Optimizations
frozensetof Unicode punctuation codepoints (eliminated 5.2Municodedata.category()calls), fast-path skip for tokens without punctuationhas_emoji()— skips expensiveemojilibrary for text without emojistopwords_setfor O(1) membership testing instead of O(n) list scan — 7.1× faster lookupsBengaliSpellCheckerandLanguageDetectornow load on-demand via__getattr__— avoids requiringsymspellpy/fasttextat package installCI / Publishing
build_and_test.yml: Builds Rust extension before testing, adds pure Python fallback test job, usespytestbnlp-publish-auto.yml: Usesmaturin-actionto build Rust wheels for Linux/macOS/Windows across Python 3.9–3.13, publishes both pure Python and Rust wheels to PyPI on release🔒 Backward Compatibility
pip install bnlp, no Rust toolchain needed📁 Files Changed (22 files, +1,382 / -210 lines)
bnlp_rust/(new)bnlp/_rust.py(new)bnlp/tokenizer/basic.pybnlp/cleantext/clean.pybnlp/corpus/corpus.pybnlp/core/batch.pybnlp/core/pipeline.pybnlp/embedding/bnlp/spellcheck/spellchecker.pybnlp/utils/registry.py(new)bnlp/__init__.pysetup.py.github/workflows/🧪 How to Test