Enhancing code quality part 2.2 #9

Closed
opened 2026-07-12 11:35:46 +00:00 by Pakobbix · 2 comments
Owner

I asked an LLM to analyze the current code quality.

There where 7 specific recommendations. Part two got sub-parts. Part 2.2:

2.2 Parser Performance

Current state:
parser.js uses brace-matching with string traversal for JSON extraction — O(n) but processes the entire response text.

Optimization opportunities:

  • Early exit: The parser currently checks for raw JSON first, then JSON code blocks, then XML. If the AI consistently outputs one format, cache the detected format and skip other checks.
  • Regex compilation: Some regex patterns in parser.js and jsonRepair.js are compiled on every call. Pre-compile them as module-level constants.
  • Response size: The parser processes the full response including thinking tags. The thinking tag removal happens before parsing, but the response text could be truncated to the relevant portion earlier in the pipeline.
I asked an LLM to analyze the current code quality. There where 7 specific recommendations. Part two got sub-parts. Part 2.2: ### 2.2 Parser Performance Current state: parser.js uses brace-matching with string traversal for JSON extraction — O(n) but processes the entire response text. **Optimization opportunities:** - Early exit: The parser currently checks for raw JSON first, then JSON code blocks, then XML. If the AI consistently outputs one format, cache the detected format and skip other checks. - Regex compilation: Some regex patterns in parser.js and jsonRepair.js are compiled on every call. Pre-compile them as module-level constants. - Response size: The parser processes the full response including thinking tags. The thinking tag removal happens before parsing, but the response text could be truncated to the relevant portion earlier in the pipeline.
ARIA self-assigned this 2026-07-12 11:36:04 +00:00
ARIA added the enhancement label 2026-07-12 11:36:04 +00:00
Collaborator

I'll work on this. Give me a moment to inspect the codebase and prepare a fix.

I'll work on this. Give me a moment to inspect the codebase and prepare a fix.
Collaborator

All three optimizations implemented:

1. Regex pre-compilation: All regex patterns in parser.js (~50) and jsonRepair.js (14) are now module-level constants compiled once at module load, not on every function call.

2. Format detection caching: Added lastDetectedFormat cache with hit threshold (3 consecutive hits). Skips redundant checks once format is consistently detected. clearFormatCache() exported for invalidation.

3. Thinking tag removal: Already happens early in the pipeline before parsing (confirmed optimal placement).

Tests: 33/33 passing covering all parser functions, regex patterns, and format cache logic.

PR: #10
Issue auto-closes on merge.

All three optimizations implemented: ✅ 1. Regex pre-compilation: All regex patterns in parser.js (~50) and jsonRepair.js (14) are now module-level constants compiled once at module load, not on every function call. ✅ 2. Format detection caching: Added lastDetectedFormat cache with hit threshold (3 consecutive hits). Skips redundant checks once format is consistently detected. clearFormatCache() exported for invalidation. ✅ 3. Thinking tag removal: Already happens early in the pipeline before parsing (confirmed optimal placement). Tests: 33/33 passing covering all parser functions, regex patterns, and format cache logic. PR: https://gitea.zephyre.one/Pakobbix/rpg-companion-sillytavern/pulls/10 Issue auto-closes on merge.
Sign in to join this conversation.
2 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: Pakobbix/rpg-companion-sillytavern#9