Skip to content

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

Closed
barneygale wants to merge 3 commits into
python:mainfrom
barneygale:gh-102783-optimize-pathlib-fspath
Closed

[WIP] GH-102783: Speed up pathlib.PurePath.__fspath__() by returning raw path#112397
barneygale wants to merge 3 commits into
python:mainfrom
barneygale:gh-102783-optimize-pathlib-fspath

Conversation

@barneygale

@barneygalebarneygale commented Nov 25, 2023

Copy link
Copy Markdown
Contributor

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.
@barneygalebarneygale added performance Performance or resource usage topic-pathlib labels Nov 25, 2023
@barneygale

Copy link
Copy Markdown
ContributorAuthor

~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

@barneygalebarneygale 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 pathNov 25, 2023
@barneygale

Copy link
Copy Markdown
ContributorAuthor

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

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

Labels

performancePerformance or resource usagetopic-pathlib

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant

@barneygale