Commit c5728b2
authored
fix(perf): avoid repeated scan of entire venv via packages_distributions() at import time (#16579)
packages_distributions() scans every installed package in the
environment to build a complete module-to-distribution mapping. In large
venvs (500+ packages, common with many google-cloud-* libs), this causes
multi-second import delays for google.api_core and every library that
depends on it.
This PR contains 2 changes:
- Wrap `packages_distributions()` with functools.cache so the expensive
O(n) scan happens at most once per process.
- Defer the package label resolution in `check_python_version()` so it
only runs when a warning is actually emitted, not on the common happy
path of a supported Python version.
Thank you for opening a Pull Request! Before submitting your PR, there
are a few things you can do to make sure it goes smoothly:
- [x] Make sure to open an issue as a
[bug/issue](https://github.com/googleapis/google-cloud-python/issues)
before writing your code! That way we can discuss the change, evaluate
designs, and agree on the general idea
- [ ] Ensure the tests and linter pass
- [x] Code coverage does not decrease (if any source code was changed)
- [x] Appropriate docs were updated (if necessary)
Fixes #15015 and #16552.1 parent f86bdfd commit c5728b2Copy full SHA for c5728b2
1 file changed
+15-5Lines changed: 15 additions & 5 deletions
File tree
Expand file treeCollapse file tree
Open diff view settings
Filter options
- packages/google-api-core/google/api_core
Expand file treeCollapse file tree
Open diff view settings
Collapse file
packages/google-api-core/google/api_core/_python_version_support.py
Copy file name to clipboardExpand all lines: packages/google-api-core/google/api_core/_python_version_support.py+15-5Lines changed: 15 additions & 5 deletions
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| ||
16 | 16 | |
17 | 17 | |
18 | 18 | |
| 19 | + |
19 | 20 | |
20 | 21 | |
21 | 22 | |
| ||
152 | 153 | |
153 | 154 | |
154 | 155 | |
| 156 | + |
| 157 | + |
| 158 | + |
| 159 | + |
155 | 160 | |
156 | 161 | |
157 | 162 | |
158 | | - |
159 | | - |
| 163 | + |
160 | 164 | |
161 | | - |
162 | 165 | |
163 | | - |
164 | 166 | |
165 | 167 | |
166 | 168 | |
| ||
195 | 197 | |
196 | 198 | |
197 | 199 | |
198 | | - |
199 | 200 | |
200 | 201 | |
201 | 202 | |
| ||
221 | 222 | |
222 | 223 | |
223 | 224 | |
| 225 | + |
| 226 | + |
| 227 | + |
| 228 | + |
| 229 | + |
| 230 | + |
224 | 231 | |
| 232 | + |
225 | 233 | |
226 | 234 | |
227 | 235 | |
| ||
236 | 244 | |
237 | 245 | |
238 | 246 | |
| 247 | + |
239 | 248 | |
240 | 249 | |
241 | 250 | |
| ||
250 | 259 | |
251 | 260 | |
252 | 261 | |
| 262 | + |
253 | 263 | |
254 | 264 | |
255 | 265 | |
|
0 commit comments