Visitar URL original
GH-113373: Speed up pathlib parsing by avoiding some attribute lookup misses by barneygale · Pull Request #113375 · python/cpython · GitHub
The Wayback Machine - https://web.archive.org/web/20260407170817/https://github.com/python/cpython/pull/113375
Skip to content
/ cpython Public

GH-113373: Speed up pathlib parsing by avoiding some attribute lookup misses#113375

Closed
barneygale wants to merge 7 commits intopython:mainfrom
barneygale:gh-113373
Closed

GH-113373: Speed up pathlib parsing by avoiding some attribute lookup misses#113375
barneygale wants to merge 7 commits intopython:mainfrom
barneygale:gh-113373

Conversation

@barneygale
Copy link
Copy Markdown
Contributor

@barneygale barneygale commented Dec 21, 2023 •

Improvements:

  • Compute string representation in _load_parts(), which results in fewer attribute lookup misses.
  • Store and re-use result of os.path.join()
  • Check for the common case (len(paths) == 1) first, and speed up the subsequent elif by avoiding a length check

Small improvements:

- `Path('foo', '')` and `Path('foo') / ''` are improved by omitting an
  `os.path.join()` call.
- The result of `os.path.join()` is stored and re-used for subsequent
  joins.

Smaller improvements:

- Check for the common case (`len(paths) == 0`) first, and speed up the
  subsequent `elif` by avoiding a length check
- Don't bother assigning the `_parse_path()` result to local variables
@barneygale
Copy link
Copy Markdown
Contributor Author

barneygale commented Dec 21, 2023 •

(edit: out of date, see later comment)

Some timings:

$ ./python -m timeit -n 500000 -s "from pathlib import PurePath;" "str(PurePath())"
500000 loops, best of 5: 1.97 usec per loop  # before
500000 loops, best of 5: 1.85 usec per loop  # after
# --> 6.5% faster

$ ./python -m timeit -n 500000 -s "from pathlib import PurePath;" "str(PurePath('foo'))"
500000 loops, best of 5: 3.08 usec per loop  # before
500000 loops, best of 5: 2.96 usec per loop  # after
# --> 4.1% faster

$ ./python -m timeit -n 500000 -s "from pathlib import PurePath;" "str(PurePath('foo', ''))"
500000 loops, best of 5: 4.08 usec per loop  # before
500000 loops, best of 5: 3.09 usec per loop  # after
# --> 32% faster

$ ./python -m timeit -n 500000 -s "from pathlib import PurePath;" "str(PurePath('foo', 'bar'))"
500000 loops, best of 5: 4.45 usec per loop  # before
500000 loops, best of 5: 4.39 usec per loop  # after
# --> 1.4% faster

$ ./python -m timeit -n 100000 -s "from pathlib import PurePath" "p = PurePath('a', 'b', 'c', 'd'); str(p); str(p / 'e')"
100000 loops, best of 5: 11.5 usec per loop  # before
100000 loops, best of 5: 10.5 usec per loop  # after
# --> 9.5% faster

@barneygale barneygale marked this pull request as ready for review December 21, 2023 20:01
@barneygale barneygale changed the title GH-113373: Slightly speed up path parsing in pathlib GH-113373: Speed up path parsing in pathlib Dec 22, 2023
@barneygale
Copy link
Copy Markdown
Contributor Author

barneygale commented Dec 22, 2023 •

Timings after 28049ce:

$ ./python -m timeit -n 500000 -s "from pathlib import PurePath;" "str(PurePath())"
500000 loops, best of 5: 1.97 usec per loop  # before
500000 loops, best of 5: 1.09 usec per loop  # after
# --> 1.81x faster

$ ./python -m timeit -n 500000 -s "from pathlib import PurePath;" "str(PurePath('foo'))"
500000 loops, best of 5: 3.08 usec per loop  # before
500000 loops, best of 5: 2.37 usec per loop  # after
# --> 1.3x faster

$ ./python -m timeit -n 500000 -s "from pathlib import PurePath;" "str(PurePath('foo', ''))"
500000 loops, best of 5: 4.08 usec per loop  # before
500000 loops, best of 5: 2.44 usec per loop  # after
# --> 1.67x faster

$ ./python -m timeit -n 500000 -s "from pathlib import PurePath;" "str(PurePath('foo', 'bar'))"
500000 loops, best of 5: 4.45 usec per loop  # before
500000 loops, best of 5: 3.73 usec per loop  # after
# --> 1.19x faster

$ ./python -m timeit -n 100000 -s "from pathlib import PurePath" "p = PurePath('a', 'b', 'c', 'd'); str(p); str(p / 'e')"
100000 loops, best of 5: 11.5 usec per loop  # before
100000 loops, best of 5: 9.16 usec per loop  # after
# --> 1.26x faster

The downside is that it's slower if you need the drive/root/tail but not the string representation, e.g.:

$ ./python -m timeit -n 100000 -s "from pathlib import PurePath as P" "P('foo').drive"
100000 loops, best of 5: 2.06 usec per loop  # before
100000 loops, best of 5: 2.29 usec per loop  # after
# --> 1.11x slower

$ ./python -m timeit -n 100000 -s "from pathlib import PurePath as P" "P('foo').name"
100000 loops, best of 5: 2.13 usec per loop  # before
100000 loops, best of 5: 2.36 usec per loop  # after
# --> 1.11x slower

I reckon the string representation is more often needed than the pure parts, so the trade-off makes sense.

@serhiy-storchaka serhiy-storchaka self-requested a review December 22, 2023 10:23
@barneygale barneygale changed the title GH-113373: Speed up path parsing in pathlib GH-113373: Speed up pathlib parsing by avoiding some attribute lookup misses Dec 22, 2023
def __init__(self, *paths):
self._raw_paths = paths
def __init__(self, *args):
self._raw_paths = list(args)
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Why should it be a list?

Copy link
Copy Markdown
Contributor Author

@barneygale barneygale Dec 22, 2023 •

Choose a reason for hiding this comment

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

PurePath._raw_paths is a list. PurePathBase has only just been split off from PurePath, and in doing so, I mistakenly changed the type of _raw_paths to a tuple, but only in PurePathBase and not PurePath. So the answer is: for consistency with its subclass, and to correct a mistake.

You could reasonably ask "why is PurePath._raw_paths a list?". The answer: PurePath.__init__() builds up the path list incrementally, and performs some special handling of other PurePath objects passed in as arguments. It's cheaper to use the list produced there as-is than convert it to a tuple/similar.

You could also ask "why fix it here?". The answer: this patch manipulates the list (see the "Store the joined path" bit of the patch), and so the difference between the list and tuple matters for the first time.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Perhaps I should add a comment here to explain why we convert it to a list?

@serhiy-storchaka
Copy link
Copy Markdown
Member

I slightly concerned about increasing coupling and possible performance regressions in some scenarios, but the current design of pathlib is very different from the one that I know, so I have no confidence to either accept or reject these changes.

@barneygale
Copy link
Copy Markdown
Contributor Author

Thanks for the review @serhiy-storchaka, very much appreciated. Given your concerns I'll withdraw this PR and have a re-think.

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

3 participants