Commit 3dfacd9
authored
Optimize LspMarkdownMessage.serialize
The key optimization is in `LspMarkdownMessage.serialize()` where **conditional preprocessing** was added to avoid expensive regex operations when they're not needed:
**What was optimized:**
- Added `if "worktrees/" in self.markdown and "/" in self.markdown:` before calling `simplify_worktree_paths()`
- Added `if '"' in self.markdown or "'" in self.markdown:` before calling `replace_quotes_with_backticks()`
**Why this leads to speedup:**
The original code always executed both regex-heavy functions regardless of content. The optimized version uses fast string containment checks (`in` operator) to skip expensive regex operations when the target patterns don't exist. From the profiler data:
- `simplify_worktree_paths` went from 41 calls to only 6 calls (85% reduction)
- `replace_quotes_with_backticks` went from 35 calls to only 10 calls (71% reduction)
**Performance characteristics:**
- **Best case**: Messages without worktree paths or quotes see 25-35% speedup (most test cases)
- **Neutral case**: Messages with quotes/paths have similar performance with slight overhead from the checks
- **Large scale**: The optimization scales well - the `test_large_scale_path_conversion` shows a dramatic 9264% improvement, indicating the conditional checks prevent unnecessary processing of large strings
The minor change in `simplify_worktree_paths` (storing `found_path` variable and using `rpartition`) provides a small additional optimization by avoiding redundant regex group calls, but the conditional execution is the primary performance driver.1 parent 231352f commit 3dfacd9
1 file changed
+5
-3
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
58 | 58 | | |
59 | 59 | | |
60 | 60 | | |
61 | | - | |
| 61 | + | |
62 | 62 | | |
63 | 63 | | |
64 | 64 | | |
| |||
92 | 92 | | |
93 | 93 | | |
94 | 94 | | |
95 | | - | |
96 | | - | |
| 95 | + | |
| 96 | + | |
| 97 | + | |
| 98 | + | |
97 | 99 | | |
0 commit comments