Skip to content

⚡ Optimize LSP struct field parsing regex compilation - #862

Closed
godofecht wants to merge 1 commit into
mainfrom
perf/optimize-lsp-regex-773527547509198172
Closed

godofecht wants to merge 1 commit into
mainfrom
perf/optimize-lsp-regex-773527547509198172

Conversation

@godofecht

Copy link
Copy Markdown
Contributor

💡 What:
The regex used to parse struct fields (field_re) in FlowLanguageServer._regex_struct_fields was being compiled dynamically every time the method was called. Similarly, a regex loop condition (re.search(r'^\s*\}', row)) was being evaluated dynamically inside the method's loop.

This PR lifts both of these regexes up to the FlowLanguageServer class scope so that they are compiled exactly once when the module loads, instead of repetitively on every method invocation or loop iteration.

🎯 Why:
The _regex_struct_fields method is used as a fallback mechanism for LSP symbol generation. Since regex string compilation carries a non-trivial baseline overhead in Python, repetitive compilation inside loops or repeatedly called methods unnecessarily wastes CPU cycles and slows down language server responsiveness.

📊 Measured Improvement:
Using timeit via bench_lsp_regex.py (parsing a small mock 2-struct file with 100,000 iterations):

  • Baseline: 1.70 seconds (for 100k iterations)
  • Optimized: 1.33 seconds (for 100k iterations)
  • Improvement: ~22% reduction in execution time for this code path.

PR created automatically by Jules for task 773527547509198172 started by @godofecht

Move `field_re` compilation out of `_regex_struct_fields` to the class level to avoid compiling it on every invocation. Additionally, optimize the inline `re.search` loop condition by hoisting it to a class level compiled regex as well.

Co-authored-by: godofecht <[email protected]>
@google-labs-jules

Copy link
Copy Markdown
Contributor

👋 Jules, reporting for duty! I'm here to lend a hand with this pull request.

When you start a review, I'll add a 👀 emoji to each comment to let you know I've read it. I'll focus on feedback directed at me and will do my best to stay out of conversations between you and other bots or reviewers to keep the noise down.

I'll push a commit with your requested changes shortly after. Please note there might be a delay between these steps, but rest assured I'm on the job!

For more direct control, you can switch me to Reactive Mode. When this mode is on, I will only act on comments where you specifically mention me with @jules. You can find this option in the Pull Request section of your global Jules UI settings. You can always switch back!

New to Jules? Learn more at jules.google/docs.


For security, I will only act on instructions from the user who triggered this task.

@godofecht
godofecht force-pushed the perf/optimize-lsp-regex-773527547509198172 branch from 625f7d0 to b49015a Compare September 7, 2026 14:52
@godofecht
godofecht marked this pull request as draft September 8, 2026 07:58

Copy link
Copy Markdown
Contributor Author

Moved to draft because this overlaps #859 on _regex_struct_fields. Do not merge both independently. After the 1.0.2 candidate is frozen/released, consolidate the struct-field/end-regex hoist here with the declaration-regex hoist from #859 into one locally qualified LSP patch.

@google-labs-jules

Copy link
Copy Markdown
Contributor

Moved to draft because this overlaps #859 on _regex_struct_fields. Do not merge both independently. After the 1.0.2 candidate is frozen/released, consolidate the struct-field/end-regex hoist here with the declaration-regex hoist from #859 into one locally qualified LSP patch.

Understood. Acknowledging that this work is now obsolete and stopping work on this task.

@godofecht godofecht closed this Sep 9, 2026
@godofecht
godofecht deleted the perf/optimize-lsp-regex-773527547509198172 branch September 9, 2026 13:07
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant