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

[WIP] GH-102783: Speed up pathlib.PurePath.__fspath__() by returning raw path #112397

Closed

Conversation

barneygale
Copy link
Contributor

@barneygale barneygale commented Nov 25, 2023

Return an unnormalized path from pathlib.PurePath.__fspath__(). This is equivalent to a normalized path (because pathlib's normalization doesn't change the meaning of paths), but considerably cheaper to generate.

Draft PR because it depends on a fix for #65238.

…g raw path

Return an unnormalized path from `pathlib.PurePath.__fspath__()`. This is
equivalent to a normalized path (because pathlib's normalization doesn't
change the meaning of paths), but considerably cheaper to generate.
@barneygale
Copy link
Contributor Author

~3.5x faster for 0 or 1 initialiser arguments, ~2x faster for more:

$ ./python -m timeit -s "from pathlib import Path" "Path().__fspath__()"
200000 loops, best of 5:  1.9 usec per loop  # before
500000 loops, best of 5:  547 nsec per loop  # after
# --> 3.47x faster

$ ./python -m timeit -s "from pathlib import Path" "Path('foo').__fspath__()"
100000 loops, best of 5: 2.73 usec per loop  # before
500000 loops, best of 5:  719 nsec per loop  # after
# --> 3.80x faster

$ ./python -m timeit -s "from pathlib import Path" "Path('foo', 'bar').__fspath__()"
100000 loops, best of 5: 3.76 usec per loop  # before
200000 loops, best of 5: 1.82 usec per loop  # after
# --> 2.07x faster

$ ./python -m timeit -s "from pathlib import Path" "Path('foo', 'bar', 'baz').__fspath__()"
 50000 loops, best of 5: 4.12 usec per loop  # before
100000 loops, best of 5: 2.15 usec per loop  # after
# --> 1.92x faster

@barneygale barneygale changed the title GH-102783: Speed up pathlib.PurePath.__fspath__() by returning raw path [WIP] GH-102783: Speed up pathlib.PurePath.__fspath__() by returning raw path Nov 25, 2023
@barneygale
Copy link
Contributor Author

I'm going to implement this slightly differently, and it's reliant on a fix for #65238, so I'll close this PR.

@barneygale barneygale closed this Dec 11, 2023
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.

1 participant