Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

gh-111259: Optimize recursive wildcards in pathlib #111303

Merged

Conversation

serhiy-storchaka
Copy link
Member

@serhiy-storchaka serhiy-storchaka commented Oct 25, 2023

Regular expression pattern (?s:.) is much faster than [\s\S].

Regular expression pattern `(?s:.)` is much faster than `[\s\S]`.
@AlexWaygood
Copy link
Member

Is there a benchmark where this shows a clear improvement? I'm struggling to find one :/

I tried various simple benchmarks locally, such as

python -m timeit -n5 -r10 -s "from pathlib import Path" "list(Path('.').rglob('Lib/**/test_*.py'))"

I've no doubt that (?s:.) is much faster than [\s\S] in general, but here, I wonder if it makes a noticeable difference

@AlexWaygood AlexWaygood added performance Performance or resource usage topic-pathlib labels Oct 25, 2023
@serhiy-storchaka
Copy link
Member Author

You can ask the OP. I guess that you can see some difference in match() for very long path.

Simple example (in debug build):

$ ./python -m timeit -s "from pathlib import Path; p = Path('a/'*1000)" "p.match('**/a')"
5000 loops, best of 5: 42.5 usec per loop

With this PR:

10000 loops, best of 5: 25.5 usec per loop

@AlexWaygood
Copy link
Member

You can ask the OP. I guess that you can see some difference in match() for very long path.

Using a PGO-optimised build, I had to create an extremely long path for there to be a difference that wasn't just noise. But yeah, I can see a difference with this benchmark:

main:

>python -m timeit -n10 -r20 -s "from pathlib import Path; p = Path('a/'*1000000)" "p.match('**/a')"
Running PGUpdate|x64 interpreter...
10 loops, best of 20: 22.2 msec per loop

This PR:

10 loops, best of 20: 13 msec per loop

Copy link
Member

@AlexWaygood AlexWaygood left a comment

Choose a reason for hiding this comment

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

LGTM, though you may want to wait for @barneygale's feedback, since he knows the code here much better than me :)

Copy link
Contributor

@barneygale barneygale left a comment

Choose a reason for hiding this comment

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

Looks fine to me, but perhaps worth noting that I'm intending to replace this code shortly - see #106703

That PR will only land in main, but this PR could be backported to 3.12 I think?

@serhiy-storchaka serhiy-storchaka merged commit 309efb3 into python:main Oct 26, 2023
29 checks passed
@serhiy-storchaka serhiy-storchaka deleted the pathlib-match-starstar-optimize branch October 26, 2023 15:07
aisk pushed a commit to aisk/cpython that referenced this pull request Feb 11, 2024
…1303)

Regular expression pattern `(?s:.)` is much faster than `[\s\S]`.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
performance Performance or resource usage topic-pathlib
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants