Skip to content

perf(python): rewrite hot Seq combinators as native Python generators#4728

Open
MangelMaxime wants to merge 1 commit into
mainfrom
perf/python-seq
Open

perf(python): rewrite hot Seq combinators as native Python generators#4728
MangelMaxime wants to merge 1 commit into
mainfrom
perf/python-seq

Conversation

@MangelMaxime

@MangelMaxime MangelMaxime commented Jul 6, 2026

Copy link
Copy Markdown
Member

Python's Seq combinators built on generate/Enumerator.generateWhileSome: an Option allocated per element, plus a MoveNext/Current call per step - a faithful port of F#'s IEnumerable/IEnumerator model, but on CPython the per-element object churn dominates.

Benchmark

Compiled a pipeline exercising several of the converted combinators together (map, choose,
map2, takeWhile, tryItem, fold - not just map/filter/collect), before vs after,
5,000 calls:

before after speedup
4.60s 2.91s 1.58x

Results identical both runs: 69360.

A simpler map/filter/collect/fold pipeline measured separately shows ~1.7x.

@MangelMaxime MangelMaxime requested a review from dbrattli July 6, 2026 18:41
@github-actions

github-actions Bot commented Jul 6, 2026

Copy link
Copy Markdown
Contributor

Python Type Checking Results (Pyright)

Metric Value
Total errors 34
Files with errors 4
Excluded files 4
New errors ✅ No
Excluded files with errors (4 files)

These files have known type errors and are excluded from CI. Remove from pyrightconfig.ci.json as errors are fixed.

File Errors Status
temp/tests/Python/test_hash_set.py 18 Excluded
temp/tests/Python/test_applicative.py 12 Excluded
temp/tests/Python/test_nested_and_recursive_pattern.py 2 Excluded
temp/tests/Python/fable_modules/thoth_json_python/encode.py 2 Excluded

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.

1 participant