Skip to content

perf: optimize SQL editor for large files and fix tab switching bugs#35

Merged
datlechin merged 3 commits intomainfrom
feat/dock-menu
Feb 7, 2026
Merged

perf: optimize SQL editor for large files and fix tab switching bugs#35
datlechin merged 3 commits intomainfrom
feat/dock-menu

Conversation

@datlechin
Copy link
Copy Markdown
Member

Summary

  • SQL editor performance: Eliminates 100% CPU and multi-second freezes when editing/executing queries in large SQL files (40MB+, 170k lines)
  • Tab switching bugs: Fixes wrong query content appearing when opening table tabs or SQL files while another query tab is open
  • Line highlight ghost: Fixes stale current-line highlight persisting on persisted tab load

Performance Optimizations

  • Rewrite SQLContextAnalyzer to use NSString UTF-16 character-at-index ops instead of O(n²) Swift String index operations
  • Add local window extraction (~10KB around cursor) in CompletionEngine for documents >500K UTF-16 units
  • Early-exit in extractQueryAtCursor — stop scanning once the cursor's statement is found instead of parsing the entire file
  • Batch @Published tab array mutations (isExecuting, executionTime, errorMessage) into single writes to reduce SwiftUI update cycles
  • Replace O(n) Swift String ops with O(1) NSString in auto-pairing, import truncation, and export capacity reservation
  • Skip persistence for queries >500KB to prevent UserDefaults/JSON encoding freeze
  • Add SyntaxHighlighter reentrancy guard to prevent didProcessEditing loop
  • LineNumberView draw() enumerates layout manager line fragments (O(visible lines)) instead of iterating full line cache

Bug Fixes

  • Fix queryTextBinding writing to wrong tab during tab switch by using tab ID lookup instead of selectedTabIndex
  • Fix SQL file open showing old tab content by desyncing version counter in handleTabSwitch to force text view sync
  • Fix current-line highlight ghost on persisted tabs: clip highlight drawing to dirty rect and initialize cursor tracking after setup
  • Support opening SQL files from Finder while connected

Test plan

  • Open a 40MB+ SQL file — typing should be responsive with no CPU spikes
  • Execute a single statement (Cmd+Enter) in a large file — no freeze before execution
  • Autocomplete should work in large files (extracts local window around cursor)
  • Open a table tab while a query tab is open — table data loads correctly
  • Open a SQL file from Finder while a query tab is open — new tab shows file content
  • Persisted tabs should show correct single-line highlight on load
  • Tab switching should preserve correct query content in each tab
  • Line numbers should render correctly and update on scroll in large files

Performance:
- Rewrite SQLContextAnalyzer to use NSString UTF-16 ops (fixes 100% CPU on 40MB files)
- Add local window extraction in CompletionEngine for large docs (>500K chars)
- Early-exit in extractQueryAtCursor — stop scanning once cursor's statement found
- Batch @published tab mutations to reduce SwiftUI update cycles
- Replace O(n) Swift String ops with O(1) NSString in auto-pairing, import, export
- Skip persistence for queries >500KB to prevent UserDefaults freeze
- Add SyntaxHighlighter reentrancy guard to prevent didProcessEditing loop
- LineNumberView draw() uses layout manager fragments instead of line cache

Bug fixes:
- Fix queryTextBinding writing to wrong tab during tab switch (use tab ID lookup)
- Fix SQL file open showing old tab content (desync version counter on tab switch)
- Fix current line highlight ghost on persisted tabs (clip to dirty rect, init tracking)
- Open SQL files from Finder while connected
Copy link
Copy Markdown

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 2c81008e69

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread TablePro/Core/Autocomplete/SQLContextAnalyzer.swift
… string/comment overlap

- EditorCoordinator: capture self.textView (weak) in debounced Task
  instead of strongly capturing the local textView from the notification,
  avoiding stale reference and stale selectedRange after 100ms delay
- SyntaxHighlighter: check full range intersection against
  stringOrCommentIndices instead of only the start position, correctly
  detecting keywords that partially overlap string/comment regions
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