Commit 3eb1687
authored
⚡️ Speed up method
The optimization achieves a **52% speedup** by eliminating repeated attribute lookups through a simple but effective change: storing `self._cache` in a local variable `cache` at the beginning of the method.
**Key optimization:**
- **Reduced attribute access overhead**: Instead of accessing `self._cache` multiple times (3-4 times in the original), the optimized version accesses it once and stores it in a local variable. In Python, local variable access is significantly faster than attribute access since it avoids the overhead of attribute resolution through the object's `__dict__`.
**Performance impact by operation:**
- The `cache.get("file_to_path")` call becomes ~3x faster (from 14,423ns to 1,079ns per hit)
- Dictionary assignments and returns also benefit from faster local variable access
- Total runtime drops from 22.7μs to 14.8μs
**Best suited for:**
Based on the test results, this optimization is particularly effective for scenarios with frequent cache lookups, showing **48-58% improvements** in basic usage patterns. The optimization scales well regardless of the `code_strings` content size since the bottleneck was in the cache access pattern, not the dictionary comprehension itself.
This is a classic Python micro-optimization that leverages the performance difference between local variables (stored in a fast array) versus instance attributes (requiring dictionary lookups).CodeStringsMarkdown.file_to_path by 53% in PR #553 (feat/markdown-read-writable-context)1 parent f187456 commit 3eb1687
1 file changed
+7
-7
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
3 | 3 | | |
4 | 4 | | |
5 | 5 | | |
| 6 | + | |
6 | 7 | | |
7 | 8 | | |
8 | 9 | | |
| |||
19 | 20 | | |
20 | 21 | | |
21 | 22 | | |
22 | | - | |
| 23 | + | |
23 | 24 | | |
24 | 25 | | |
25 | 26 | | |
| |||
192 | 193 | | |
193 | 194 | | |
194 | 195 | | |
195 | | - | |
196 | | - | |
197 | | - | |
198 | | - | |
199 | | - | |
200 | | - | |
| 196 | + | |
| 197 | + | |
| 198 | + | |
| 199 | + | |
| 200 | + | |
201 | 201 | | |
202 | 202 | | |
203 | 203 | | |
| |||
0 commit comments