FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

gh-158451: Optimize PyUnicode_Join() by vstinner · Pull Request #158693 · python/cpython · GitHub

Repository navigation

gh-158451: Optimize PyUnicode_Join() - #158693

Open
vstinner wants to merge 2 commits into
python:mainfrom
vstinner:unicode_join
Open

vstinner wants to merge 2 commits into
python:mainfrom
vstinner:unicode_join

Conversation

vstinner commented Oct 4, 2026 •
edited by bedevere-app Bot
Loading

Copy link
Copy Markdown
Member

Move tests on the separator outside the loop. Add a loop version for empty separator.

Move tests on the separator outside the loop. Add a loop version for
empty separator.

vstinner commented Oct 4, 2026

Copy link
Copy Markdown
Member Author

Benchmark:

import pyperf
runner = pyperf.Runner()
for sep in ('', '.'):
    for list_size in ('10', '10**3', '10**6'):
        runner.timeit(
            f'sep {sep!r}, {list_size} short items',
            setup=f"data=['x']*{list_size}; sep={sep!r}",
            stmt="sep.join(data)")

    for list_size in ('10', '10**2', '10**4'):
        runner.timeit(
            f'sep {sep!r}, {list_size} long items',
            setup=f"data=['x' * 100]*{list_size}; sep={sep!r}",
            stmt="sep.join(data)")

Results on Fedora 44 with CPU isolation:

Benchmark ref change
sep '', 10 short items 155 ns 143 ns: 1.08x faster
sep '', 10**3 short items 8.22 us 8.03 us: 1.02x faster
sep '', 10**6 short items 8.58 ms 8.46 ms: 1.01x faster
sep '', 10 long items 240 ns 235 ns: 1.02x faster
sep '', 10**2 long items 1.16 us 1.04 us: 1.12x faster
sep '.', 10 short items 197 ns 181 ns: 1.09x faster
sep '.', 10 long items 282 ns 274 ns: 1.03x faster
sep '.', 10**2 long items 1.38 us 1.39 us: 1.01x slower
Geometric mean (ref) 1.02x faster

Benchmark hidden because not significant (4): sep '', 10**4 long items, sep '.', 10**3 short items, sep '.', 10**6 short items, sep '.', 10**4 long items

vstinner added the performance Performance or resource usage label Oct 4, 2026

vstinner commented Oct 4, 2026

Copy link
Copy Markdown
Member Author

See also #158689 which optimizes bytes.join().

eendebakpt left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

The gain performance gain is only a bit and involves some code duplication. Have you benchmarked the two changes individually (e.g. unrolling the loop and making a fast path for the empty sep)?

Comment thread Objects/unicodeobject.c
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

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants


Back | FazBrowse Home | New Git URL