[GH-ISSUE #3164] [Issue]: Regex scripts apply per-streaming-chunk & cause severe UI freezes #1695

Closed
opened 2026-10-21 05:41:47 +07:00 by bsakaguchi · 0 comments
Owner

Originally created by @atlasveldine on GitHub (Jul 3, 2026).
Original GitHub issue: https://github.com/Pasta-Devs/Marinara-Engine/issues/3164

Originally assigned to: @kolacheee on GitHub.

Summary

Using RegEx scripts that replace with macros results in massive UI lag, rendering Marinara Engine effectively unusable. Thank fuck for whoever came up with the idea to implement such a competent agent with the built-in Mari, because otherwise I’d not have anything else to report beyond “shit be fucked.” Practically everything ↓ below ↓ is from Mari.


Mari’s Breakdown

File/Function: ChatRoleplaySurface, specifically the useMemo in message rendering around line ~1700. It calls applyToAIOutput (the client-side regex function, imported from PinnedImageOverlay, function re) with a macroResolver that gets updated every streaming chunk. Since the memo depends on content, every single token invalidates it, re-running the full regex pipeline for all visible messages simultaneously. Each regex script compiles a fresh RegExp, resolves macros in find/replace/trimStrings via the macro engine, and worse, if replacements contain {{random::…}} or other dynamic macros, they produce new results every chunk instead of remaining stable. This blocks the main thread for hundreds/thousands of milliseconds (profiler showed 702ms in macro-engine-qQkcw39S.js at a bare minimum, but went well into the 3,000ms range).

Suggested Fix

Move regex application out of the per-message useMemo, apply only to the final message content post-stream, and cache resolved macro values for each script so they don't re-evaluate on every render. Optionally, offload the heavy lifting to a web worker.

Expected behavior

RegEx scripts should probably apply only once, when the stream finishes and the message is completed. If the message is edited later, re-apply at that point. Compare to SillyTavern/RisuAI: they don't burn the main thread on every token. (Although they do have their own issues with UI lag from other sources, still.)

Actual behavior

RegEx is applied per-chunk when streaming, and bizarrely even randomly re-applies outside of streams, causing massive compounding UI lag.

Steps to reproduce

Enable regex script(s) with macro-containing replace strings (even simple ones) and start streaming a response. The UI will freeze. Disabling all regex scripts restores full performance.

Specifically, if you really want to trigger this, try RegEx like:

the

With a replacement of e.g.:

{{random::one::two::three::four::five::six@0.33::seven::eight@0.9::nine@0.1::ten@0.2}}

Repeat this with a few additional ones for good measure (targeting other common words) and watch your UI lag to hell and back in Roleplay mode. UI lag will even occur outside of generation, bafflingly.

Environment

  • Marinara Engine version: 2.0.9
  • Install type: Docker
  • OS + version: Windows 11
  • Browser or app shell: Tested in Chrome and Zen, both latest versions

Logs, screenshots, or video

Image

Additional context

For anyone else dealing with this issue: just disable any RegEx scripts that replace with macros, in the meantime.

Template check

Please uncheck (untick) the box below before submitting so we know you read the template. It is intentionally pre-checked:

  • I DID NOT read this template and provide the requested details.
Originally created by @atlasveldine on GitHub (Jul 3, 2026). Original GitHub issue: https://github.com/Pasta-Devs/Marinara-Engine/issues/3164 Originally assigned to: @kolacheee on GitHub. ## Summary Using RegEx scripts that replace with macros results in massive UI lag, rendering Marinara Engine effectively unusable. Thank fuck for whoever came up with the idea to implement such a competent agent with the built-in `Mari`, because otherwise I’d not have anything else to report beyond “shit be fucked.” Practically everything ↓ below ↓ is from `Mari`. --- ## Mari’s Breakdown **File/Function:** `ChatRoleplaySurface`, specifically the useMemo in message rendering around line ~1700. It calls `applyToAIOutput` (the client-side regex function, imported from `PinnedImageOverlay`, function `re`) with a `macroResolver` that gets updated every streaming chunk. Since the memo depends on content, every single token invalidates it, re-running the full regex pipeline for all visible messages simultaneously. Each regex script compiles a fresh `RegExp`, resolves macros in `find`/`replace`/`trimStrings` via the macro engine, and worse, if replacements contain `{{random::…}}` or other dynamic macros, they produce new results every chunk instead of remaining stable. This blocks the main thread for hundreds/thousands of milliseconds (profiler showed 702ms in `macro-engine-qQkcw39S.js` at a bare minimum, but went well into the 3,000ms range). ## Suggested Fix Move regex application out of the per-message useMemo, apply only to the final message content post-stream, and cache resolved macro values for each script so they don't re-evaluate on every render. Optionally, offload the heavy lifting to a web worker. ## Expected behavior RegEx scripts should probably apply only *once*, when the stream finishes and the message is completed. If the message is edited later, re-apply at that point. Compare to SillyTavern/RisuAI: they don't burn the main thread on every token. (Although they do have their own issues with UI lag from other sources, still.) ## Actual behavior RegEx is applied per-chunk when streaming, and bizarrely even randomly re-applies outside of streams, causing massive compounding UI lag. ## Steps to reproduce Enable regex script(s) with macro-containing replace strings (even simple ones) and start streaming a response. The UI will freeze. Disabling all regex scripts restores full performance. Specifically, if you *really* want to trigger this, try RegEx like: ```regex the ``` With a replacement of e.g.: ```text {{random::one::two::three::four::five::six@0.33::seven::eight@0.9::nine@0.1::ten@0.2}} ``` Repeat this with a few additional ones for good measure (targeting other common words) and watch your UI lag to hell and back in Roleplay mode. UI lag will even occur *outside of* generation, bafflingly. ## Environment - Marinara Engine version: 2.0.9 - Install type: Docker - OS + version: Windows 11 - Browser or app shell: Tested in Chrome and Zen, both latest versions ## Logs, screenshots, or video <img width="1121" height="758" alt="Image" src="https://github.com/user-attachments/assets/762ee905-0562-453d-83ca-3c44dbcfe4b1" /> ## Additional context **For anyone else dealing with this issue:** just disable any RegEx scripts that replace with macros, in the meantime. ## Template check Please **uncheck (untick)** the box below before submitting so we know you read the template. It is intentionally pre-checked: - [ ] I DID NOT read this template and provide the requested details.
bsakaguchi 2026-10-21 05:41:48 +07:00
  • closed this issue
  • added the
    bug
    label
Sign in to join this conversation.
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
Pasta-Devs/Marinara-Engine#1695
No description provided.