Skip to content

PERF Faster text processing with parallelism - #34747

Draft
itamarst wants to merge 9 commits into
scikit-learn:mainfrom
itamarst:faster-text-processing
Draft

itamarst wants to merge 9 commits into
scikit-learn:mainfrom
itamarst:faster-text-processing

Conversation

@itamarst

@itamarst itamarst commented Aug 13, 2026 •

Copy link
Copy Markdown
Contributor

This PR does two things:

  • Introduces a new API for parallelism for threads. The motivation is speed: joblib.Parallel has very significant per-operation overhead, which is fine for the usual use case of a few large chunks of work, but becomes a problem if you have lots of small fast functions to call. See the benchmark results for numbers.
  • Uses this new API to demonstrate how with free-threaded Python, new code paths can be parallelized.

Note that much of the code in both CountVectorizer and HashingVectorizer is still single-threaded, so in theory more could be done to speed these up.

All benchmarks were run with 12 cores, on Python 3.14 free-threaded.

Questions for reviewer:

  • This code now assumes that the analyzer is thread-safe; this was not previously a constraint. How should this be approached? Documentation? Disallowing parallelism if anything is changed from scikit-learn's base classes? Just assuming that it will be thread-safe?

Benchmarks

Baseline on main branch

Loading 20 newsgroups training data
3803 documents - 6.245MB
CountVectorizer done in 0.433 s at 14.4 MB/s
Found 47885 unique terms
HashingVectorizer done in 0.333 s at 18.7 MB/s

parallel_thread_map() in this PR

concurrent.futures.ThreadPoolExecutor is faster:

CountVectorizer done in 0.355 s at 17.6 MB/s
HashingVectorizer done in 0.229 s at 27.3 MB/s

Other alternative thread pool benchmarks

Way too slow: sklearn.utils.parallel.Parallel

This actually makes things worse, because the overhead is so high:

CountVectorizer done in 0.484 s at 12.9 MB/s
HashingVectorizer done in 0.374 s at 16.7 MB/s

Faster, but not yet ready for real-world usage: spinningjenny

spinningjenny is a faster alternative to stdlib ThreadPoolExecutor. Still just a prototype, so missing features and not quite ready to use, but just to give an example of what is possible:

CountVectorizer done in 0.309 s at 20.2 MB/s
HashingVectorizer done in 0.190 s at 32.9 MB/s

@itamarst
itamarst marked this pull request as ready for review August 13, 2026 17:50
@itamarst

itamarst commented Aug 15, 2026 •

Copy link
Copy Markdown
Contributor Author

I considered reducing joblib overhead in joblib itself, but it's very complex, with a lot of backwards compatibility requirements; this is much simpler and much less constrained by the need to support every edge case. And scikit-learn only uses a subset of joblib's functionality, which also makes it easier.

@itamarst

itamarst commented Sep 9, 2026

Copy link
Copy Markdown
Contributor Author

Since this PR includes two distinct questions—a new API and a new parallelism site—I'm going to put it on hold, and instead have a PR that just converts existing parallelism into a faster API. And then can revisit this if/when new API is merged.

@itamarst
itamarst marked this pull request as draft September 9, 2026 17:46

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant