Skip to content

Conversation

@Thom1729
Copy link
Member

Previously, we handled the mapping between file and resource paths using get_resource_roots() and get_installed_resource_roots(). This PR consolidates the mapping logic into ResourceRoot objects, each representing a directory containing either zipped or unzipped packages. file_path() and from_file_path() merely iterate over the ResourceRoot objects and return the first result.

Other tweaks:

  • Cache the list of roots instead of recomputing it each time.
  • Instead of pathlib.Path.relative_to, use a custom implementation that doesn't spend most of its CPU time formatting an error message that we don't care about.
  • Use a wrap_path function to avoid calling the (relatively expensive) Path constructor on Path objects.
  • Avoid unnecessary reparsing when calling the ResourcePath constructor with a ResourcePath object as the first argument.

These changes improved the performance of a function that calls from_file_path() on all of the paths in sys.modules by more than a factor of three.

Currently, the ResourceRoot list isn't exposed or documented. This may be a useful primitive for packages that care where resources are located.

@FichteFoll FichteFoll merged commit 09c82a1 into master Feb 19, 2019
@FichteFoll FichteFoll deleted the resource-path-improvements branch February 19, 2019 01:25
@Thom1729 Thom1729 mentioned this pull request Jun 4, 2019
@FichteFoll FichteFoll added this to the 1.4.0 milestone Jun 5, 2019
@Thom1729 Thom1729 mentioned this pull request Jun 5, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants