Skip to content

Optimize slow code paths in AI service, database, and React components - #2

Draft
WilliamsMiao with Copilot wants to merge 3 commits into
mainfrom
copilot/improve-code-efficiency
Draft

Optimize slow code paths in AI service, database, and React components#2
WilliamsMiao with Copilot wants to merge 3 commits into
mainfrom
copilot/improve-code-efficiency

Conversation

Copilot AI commented Dec 1, 2025

Copy link
Copy Markdown

Performance audit identified several inefficient patterns: O(n) priority queue insertion, repeated toLowerCase() calls in hot paths, sequential awaits where parallelism is possible, and missing React memoization.

Backend

  • AIService.js: Cache lowercase keywords in WeakMap instead of mutating trigger objects. Pre-filter events by chapter range before iteration.

    // Before: O(n) toLowerCase on every call
    trigger.keywords?.some(kw => lowerInput.includes(kw.toLowerCase()))
    
    // After: Cache in WeakMap, compute once
    let lowerKeywords = keywordCache.get(trigger);
    if (!lowerKeywords) {
      lowerKeywords = trigger.keywords.map(kw => kw.toLowerCase());
      keywordCache.set(trigger, lowerKeywords);
    }
  • RequestQueue.js: Replace O(n) linear search with O(log n) binary search for priority queue insertion.

  • MemoryManager.js: Single-pass sentence processing with Set-based keyword lookup. Remove unused emotion pattern.

  • database.js: Wrap deleteStory in transaction, parallelize independent deletions with Promise.all.

Frontend

  • StoryPanel.jsx:
    • Memoize displayMessages filtering with useMemo
    • Memoize formatTimestamp with useCallback
    • Hoist static NPC regex outside render path, reset lastIndex before iteration
Original prompt

Identify and suggest improvements to slow or inefficient code


💬 We'd love your input! Share your thoughts on Copilot coding agent in our 2 minute survey.

Copilot AI and others added 2 commits December 1, 2025 06:27
Backend optimizations:
- AIService: Optimize checkDynamicEventTrigger with cached lowercase keywords and early filtering
- RequestQueue: Replace O(n) linear search with O(log n) binary search for priority queue insertion
- MemoryManager: Single-pass sentence processing with Set-based keyword lookup
- database: Batch deletions with transactions and Promise.all for parallel operations

Frontend optimizations:
- StoryPanel: Memoize displayMessages filtering with useMemo
- StoryPanel: Memoize formatTimestamp with useCallback
- StoryPanel: Pre-compile static NPC regex pattern

Co-authored-by: WilliamsMiao <181537281+WilliamsMiao@users.noreply.github.com>
- Remove unnecessary .slice() calls in StoryPanel.jsx displayMessages memoization
- Remove unused 'emotion' pattern from MemoryManager patterns
- Use WeakMap for keyword caching in AIService to avoid mutating external objects

Co-authored-by: WilliamsMiao <181537281+WilliamsMiao@users.noreply.github.com>
Copilot AI changed the title [WIP] Identify and suggest improvements for slow code Optimize slow code paths in AI service, database, and React components Dec 1, 2025
Copilot AI requested a review from WilliamsMiao December 1, 2025 06:33
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.

2 participants