Commit d28571e
authored
⚡️ Speed up method
Here’s how you can optimize your code for **speed** and **memory** while maintaining return values, behavior, and comments. I focused on making `pytest_collection_modifyitems` faster by.
- **Avoiding function calls/attribute checks inside tight loops:**
Since ideally `get_closest_marker` existence is consistent across items, fetch it once per item and cache the lookup.
- **Reducing repeated work:**
Move more work (e.g. creation of the skip marker) outside the inner loop.
### Changes and Optimizations.
- **Cache Attribute**:
`getattr(item, "get_closest_marker", None)` is used to avoid repeated `hasattr` checks or repeated attribute lookups.
- **Reuse Marker Instance**:
`skip_marker` is created once, not in every loop iteration.
- **Skip on missing attribute** instead of raising.
**This will speed up the loop by:**
- Reducing per-item attribute lookup
- Reducing decorator construction
- Reducing function calls on items which don’t have `get_closest_marker` (if any)
---
Let me know if you want further or different optimizations!CodeFlashBenchmarkPlugin.pytest_collection_modifyitems by 670% in PR #574 (easier-benchmark)1 parent 32d9919 commit d28571e
1 file changed
+8
-4
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
233 | 233 | | |
234 | 234 | | |
235 | 235 | | |
| 236 | + | |
236 | 237 | | |
237 | 238 | | |
238 | | - | |
239 | | - | |
240 | | - | |
241 | | - | |
| 239 | + | |
| 240 | + | |
| 241 | + | |
| 242 | + | |
| 243 | + | |
| 244 | + | |
| 245 | + | |
242 | 246 | | |
243 | 247 | | |
244 | 248 | | |
| |||
0 commit comments