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

Fix performance regression in utils._generate_installed_modules #2703

Merged
merged 2 commits into from Feb 5, 2024
Merged

Fix performance regression in utils._generate_installed_modules #2703

merged 2 commits into from Feb 5, 2024

Conversation

GlenWalker
Copy link
Contributor

Commit 8c24d33 causes a performance regression when PYTHONPATH is long, because it traverses PYTHONPATH for every distribution found (importlib.metadata.version traverses PYTHONPATH searching for a matching distribution for every call)

In our case we have an environment containing ~500 paths, and containing ~100 distributions, and where the first call to sentry_sdk.utils.package_version causes ~150k filesystems operations taking 10-20 seconds.

This commit uses the version from the distribution found when iterating all distributions, instead of calling importlib.metadata.version for each, which fixes the performance issue for us.

Note that if multiple copies of a distribution with different versions exist in PYTHONPATH the existing _generate_installed_modules will return the name and version of the first matching distribution found multiple times, which will then be discarded by creation of a dict in _get_installed_modules. I have preserved the same behaviour by returning the name and version of a distribution only the first time a distribution name is seen.

The existing test for sentry_sdk.utils._get_installed_modules checks that the correct result is still returned.

…dules

Commit 8c24d33 causes a performance regression when PYTHONPATH is long, because it traverses PYTHONPATH for every distribution found (importlib.metadata.version traverses PYTHONPATH searching for a matching distribution for every call)

In our case we have an environment containing ~500 paths, and containing ~100 distributions, and where the first call to sentry_sdk.utils.package_version causes ~150k filesystems operations taking 10-20 seconds.

This commit uses the version from the distribution found when iterating all distributions, instead of calling importlib.metadata.version for each, which fixes the performance issue for us.

Note that if multiple copies of a distribution with different versions exist in PYTHONPATH the existing _generate_installed_modules will return the name and version of the first matching distribution found multiple times, which will then be discarded by creation of a dict in _get_installed_modules. I have preserved the same behaviour by returning the name and version of a distribution only the first time a distribution name is seen.
Copy link
Contributor

@sentrivana sentrivana left a comment

Choose a reason for hiding this comment

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

Thanks for the fix @GlenWalker! Looking good.

@sentrivana sentrivana added the Trigger: tests using secrets PR code is safe; run CI label Feb 5, 2024
@github-actions github-actions bot removed the Trigger: tests using secrets PR code is safe; run CI label Feb 5, 2024
@sentrivana sentrivana added the Trigger: tests using secrets PR code is safe; run CI label Feb 5, 2024
@sentrivana sentrivana enabled auto-merge (squash) February 5, 2024 15:22
@sentrivana sentrivana merged commit 738506c into getsentry:master Feb 5, 2024
124 of 126 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Trigger: tests using secrets PR code is safe; run CI
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants