[GH-ISSUE #1580] [Issue]: character list loading transfers ~300 MB of embedded avatar images on every fetch — slow at 180 characters, unusable at scale #818

Closed
opened 2026-10-21 05:21:29 +07:00 by bsakaguchi · 1 comment
Owner

Originally created by @Gunterlie on GitHub (May 29, 2026).
Original GitHub issue: https://github.com/Pasta-Devs/Marinara-Engine/issues/1580

Big one, should really be multiple Issues. But I think important for long time stability.

Summary

Every character list fetch — triggered by pickers, the library view, the sidebar, and lorebook editors — transfers the full base64-encoded avatar image for every character inside the JSON payload. With 182 characters, this is a 300 MB transfer that takes ~12 seconds ON LOCAL CONNECTION! before any UI can render. The field-selection optimization in CHARACTER_SUMMARY_OPTIONS has no effect because avatarPath (the embedded data URI) is included in the summary. Removing avatarPath from list queries drops the payload to 144 KB and 0.35 seconds — a 2,180× reduction.

Expected behavior

Character pickers, the library grid, and the sidebar open in well under one second regardless of collection size. Avatar images load per-card, on demand, only when visible.

Actual behavior

Measured against a live server with 182 characters:

Query Payload Time
storage_list with full records (useCharacters) 319 MB 11.9 s
storage_list with summary fields incl. avatarPath (useCharacterSummaries) 315 MB 11.9 s
storage_list without avatarPath 144 KB 0.35 s

The summary fetch is almost identical in size to the full fetch because avatarPath dominates: 300.7 MB of the 315 MB summary payload is base64-encoded avatar images (169 of 182 characters store their avatar as a data:image/png;base64,... URI directly in the record).

Per-character breakdown for a single record:

avatarPath:      ~1,168,304 bytes  (base64 data URI — the entire image)
data.*:                ~629 bytes  (name, tags, extensions)
id + comment:           ~25 bytes

Because avatarPath values that start with data: are loaded with loading="eager" (see getCharacterAvatarLoadingMode), lazy-loading provides no relief either — every avatar in the payload renders immediately.

Steps to reproduce

  1. Import or create 150+ characters with avatar images
  2. Open any character picker (ChatSetupWizard, GameSetupWizard, ChatSettingsDrawer) or navigate to the Character Library
  3. Open DevTools → Network tab → observe a single storage_list response that is hundreds of MB
  4. Note the time from navigation to the list becoming interactive
  5. On a remote/Docker deployment the wait is 10–15 seconds; on a fast local machine it is shorter but the payload is identical

Environment

  • Marinara Engine version: Refactor
  • Install type: MacOS App from Source <---> HTTP Docker Backend on Ubuntu
  • OS + version: macOS (Tauri) + Linux server (Docker/web)

Additional context

Root cause

The avatarPath field in a character record is not a filesystem path or URL — it is a precomputed data:image/png;base64,... string that encodes the full avatar image. This field is included in CHARACTER_SUMMARY_OPTIONS (the field selection used by useCharacterSummaries), which means every list fetch downloads every avatar image in full resolution as part of the JSON body.

// src/features/catalog/characters/hooks/use-characters.ts
const CHARACTER_SUMMARY_OPTIONS = {
  fields: ["id", "data", "comment", "avatarPath"],  // ← avatarPath pulls in ~1 MB per character
  fieldSelections: { data: ["name", "tags", "extensions"] }
}

The backend already stores the avatar as a file and tracks its path via a separate avatarFilePath field (verified in the full record response). An HTTP asset endpoint already exists at /api/assets/:kind/*path but does not currently support avatar as a kind.

Secondary issues (smaller but real)

These compound the main problem and should be addressed in the same pass:

1. No DOM virtualization
CharacterLibraryView.tsx (line 400) and CharactersPanel.tsx (line 115) map all filtered characters directly to DOM nodes with no windowing. At 200 characters, 200 full card elements exist in the DOM simultaneously. @tanstack/react-virtual is not installed.

2. Full records fetched for list views
CharacterLibraryView and CharactersPanel call useCharacters() (query key ["characters","list"], no field projection) instead of useCharacterSummaries(). This fetches description, personality, scenario, first_mes, and alternate_greetings that are not displayed in a list card. With the avatar fix in place, this adds ~10 KB per character unnecessarily.

3. No backend search
The Rust entity list handler (src-tauri/src/commands/storage/commands/entities.rs line 61) supports only exact-match filters via list_where. All name/tag/creator search is done client-side via Array.filter() after downloading the full dataset. This means the full payload must land before the user can type.

4. Two separate unbounded fetches can be active simultaneously
useCharacters() (key ["characters","list"]) and useCharacterSummaries() (key ["characters","summaries"]) are separate React Query cache entries. React Query correctly deduplicates within each key, but when the library view and a picker are open at the same time, both fetches fire independently — doubling the transfer.


Proposed fix

Step 1 — Remove avatarPath from all list queries (99.5% payload reduction, no architectural changes needed)

Remove avatarPath from CHARACTER_SUMMARY_OPTIONS. Replace it with avatarFilename (already present in the record, 42 bytes).

const CHARACTER_SUMMARY_OPTIONS = {
  fields: ["id", "data", "comment", "avatarFilename"],  // no avatarPath
  fieldSelections: { data: ["name", "tags", "extensions"] }
}

This alone drops the 315 MB summary payload to ~144 KB and load time from ~12 s to ~0.35 s.

Step 2 — Add avatar file serving endpoint

Add "avatar" to the managed_asset_path match in http_server.rs:

"avatar" => Ok(state.avatars_dir.join(path)),

This exposes /api/assets/avatar/{filename} as a proper HTTP endpoint, compatible with remote deployments.

Step 3 — Load avatars per card, lazily, using the filename

In CharacterAvatarImage (or the hook that feeds it), construct the avatar URL:

  • Remote / web: /api/assets/avatar/{avatarFilename}
  • Local Tauri: convertFileSrc(avatarFilePath) (Tauri's existing utility for converting filesystem paths to webview-loadable URLs — no change to the local install path)

Because the src is now a URL (not a data URI), the browser uses loading="lazy" — avatars genuinely only load when the card scrolls into view. Both install modes continue to work.

Step 4 — Add DOM virtualization (frontend only, no backend changes)

Install @tanstack/react-virtual and virtualize CharacterLibraryView and CharactersPanel. With Step 1 done, the list data lands in ~350ms; virtualization then ensures only ~30 DOM nodes exist regardless of collection size.

Step 5 — Switch library/panel from useCharacters() to useCharacterSummaries()

CharacterLibraryView.tsx:201 and CharactersPanel.tsx:115 use useCharacters() which fetches the full record. After Step 1 the summary is lean enough that the library can use it — full records are only needed in the character detail/editor view.

Step 6 — Backend substring search (enables true search-as-you-type)

Add a search parameter to the Rust entity list handler. A SQLite LIKE '%term%' on the name field is sufficient for <10k records. The React Query key includes the search term, so each distinct query has its own cache entry. This removes the requirement to download the full dataset before the user can search.


Constraint: local Tauri install must keep working

Every change above is backward-compatible and transport-agnostic:

  • Steps 1–3 go through the existing storageApi / invoke abstraction — no HTTP-only assumptions
  • convertFileSrc is the standard Tauri utility for filesystem paths in the webview; it is already used elsewhere in the codebase
  • New query params (search) are additive and optional; callers that omit them get current behavior
  • The /api/assets/avatar/ endpoint is additive; nothing is removed

Quick wins (can ship in one PR, zero backend changes)

These four changes alone make the app dramatically faster and carry no risk:

  1. Remove avatarPath from CHARACTER_SUMMARY_OPTIONS — 315 MB → 144 KB, 12 s → 0.35 s
  2. Switch CharacterLibraryView + CharactersPanel to useCharacterSummaries() — eliminates the second unbounded full-record fetch
  3. Add @tanstack/react-virtual to both list views — fixes DOM bloat
  4. ChatSidebar: replace useCharacterSummaries() with useCharacterSummariesByIds(activeIds) — the sidebar only needs avatars/names for characters in active chats, not the entire library

Items 2 and 3 in the avatar endpoint (Steps 2–3 above) are required to make the list display avatars again after removing avatarPath — they should ship in the same PR as quick win #1.


Template check

  • I DID NOT read this template and provide the requested details.
Originally created by @Gunterlie on GitHub (May 29, 2026). Original GitHub issue: https://github.com/Pasta-Devs/Marinara-Engine/issues/1580 Big one, should really be multiple Issues. But I think important for long time stability. ## Summary Every character list fetch — triggered by pickers, the library view, the sidebar, and lorebook editors — transfers the full base64-encoded avatar image for every character inside the JSON payload. With 182 characters, this is a **300 MB transfer that takes ~12 seconds** ON LOCAL CONNECTION! before any UI can render. The field-selection optimization in `CHARACTER_SUMMARY_OPTIONS` has no effect because `avatarPath` (the embedded data URI) is included in the summary. Removing `avatarPath` from list queries drops the payload to **144 KB and 0.35 seconds** — a 2,180× reduction. ## Expected behavior Character pickers, the library grid, and the sidebar open in well under one second regardless of collection size. Avatar images load per-card, on demand, only when visible. ## Actual behavior Measured against a live server with 182 characters: | Query | Payload | Time | |---|---|---| | `storage_list` with full records (`useCharacters`) | 319 MB | 11.9 s | | `storage_list` with summary fields incl. `avatarPath` (`useCharacterSummaries`) | 315 MB | 11.9 s | | `storage_list` without `avatarPath` | 144 KB | 0.35 s | The summary fetch is almost identical in size to the full fetch because `avatarPath` dominates: **300.7 MB of the 315 MB summary payload is base64-encoded avatar images** (169 of 182 characters store their avatar as a `data:image/png;base64,...` URI directly in the record). Per-character breakdown for a single record: ``` avatarPath: ~1,168,304 bytes (base64 data URI — the entire image) data.*: ~629 bytes (name, tags, extensions) id + comment: ~25 bytes ``` Because `avatarPath` values that start with `data:` are loaded with `loading="eager"` (see `getCharacterAvatarLoadingMode`), lazy-loading provides no relief either — every avatar in the payload renders immediately. ## Steps to reproduce 1. Import or create 150+ characters with avatar images 2. Open any character picker (ChatSetupWizard, GameSetupWizard, ChatSettingsDrawer) or navigate to the Character Library 3. Open DevTools → Network tab → observe a single `storage_list` response that is hundreds of MB 4. Note the time from navigation to the list becoming interactive 5. On a remote/Docker deployment the wait is 10–15 seconds; on a fast local machine it is shorter but the payload is identical ## Environment - Marinara Engine version: Refactor - Install type: MacOS App from Source <---> HTTP Docker Backend on Ubuntu - OS + version: macOS (Tauri) + Linux server (Docker/web) --- ## Additional context ### Root cause The `avatarPath` field in a character record is not a filesystem path or URL — it is a precomputed `data:image/png;base64,...` string that encodes the full avatar image. This field is included in `CHARACTER_SUMMARY_OPTIONS` (the field selection used by `useCharacterSummaries`), which means every list fetch downloads every avatar image in full resolution as part of the JSON body. ```ts // src/features/catalog/characters/hooks/use-characters.ts const CHARACTER_SUMMARY_OPTIONS = { fields: ["id", "data", "comment", "avatarPath"], // ← avatarPath pulls in ~1 MB per character fieldSelections: { data: ["name", "tags", "extensions"] } } ``` The backend already stores the avatar as a file and tracks its path via a separate `avatarFilePath` field (verified in the full record response). An HTTP asset endpoint already exists at `/api/assets/:kind/*path` but does not currently support `avatar` as a kind. ### Secondary issues (smaller but real) These compound the main problem and should be addressed in the same pass: **1. No DOM virtualization** `CharacterLibraryView.tsx` (line 400) and `CharactersPanel.tsx` (line 115) map all filtered characters directly to DOM nodes with no windowing. At 200 characters, 200 full card elements exist in the DOM simultaneously. `@tanstack/react-virtual` is not installed. **2. Full records fetched for list views** `CharacterLibraryView` and `CharactersPanel` call `useCharacters()` (query key `["characters","list"]`, no field projection) instead of `useCharacterSummaries()`. This fetches `description`, `personality`, `scenario`, `first_mes`, and `alternate_greetings` that are not displayed in a list card. With the avatar fix in place, this adds ~10 KB per character unnecessarily. **3. No backend search** The Rust entity list handler (`src-tauri/src/commands/storage/commands/entities.rs` line 61) supports only exact-match `filters` via `list_where`. All name/tag/creator search is done client-side via `Array.filter()` after downloading the full dataset. This means the full payload must land before the user can type. **4. Two separate unbounded fetches can be active simultaneously** `useCharacters()` (key `["characters","list"]`) and `useCharacterSummaries()` (key `["characters","summaries"]`) are separate React Query cache entries. React Query correctly deduplicates within each key, but when the library view and a picker are open at the same time, both fetches fire independently — doubling the transfer. --- ### Proposed fix #### Step 1 — Remove `avatarPath` from all list queries (99.5% payload reduction, no architectural changes needed) Remove `avatarPath` from `CHARACTER_SUMMARY_OPTIONS`. Replace it with `avatarFilename` (already present in the record, 42 bytes). ```ts const CHARACTER_SUMMARY_OPTIONS = { fields: ["id", "data", "comment", "avatarFilename"], // no avatarPath fieldSelections: { data: ["name", "tags", "extensions"] } } ``` This alone drops the 315 MB summary payload to ~144 KB and load time from ~12 s to ~0.35 s. #### Step 2 — Add avatar file serving endpoint Add `"avatar"` to the `managed_asset_path` match in `http_server.rs`: ```rust "avatar" => Ok(state.avatars_dir.join(path)), ``` This exposes `/api/assets/avatar/{filename}` as a proper HTTP endpoint, compatible with remote deployments. #### Step 3 — Load avatars per card, lazily, using the filename In `CharacterAvatarImage` (or the hook that feeds it), construct the avatar URL: - **Remote / web:** `/api/assets/avatar/{avatarFilename}` - **Local Tauri:** `convertFileSrc(avatarFilePath)` (Tauri's existing utility for converting filesystem paths to webview-loadable URLs — no change to the local install path) Because the `src` is now a URL (not a data URI), the browser uses `loading="lazy"` — avatars genuinely only load when the card scrolls into view. Both install modes continue to work. #### Step 4 — Add DOM virtualization (frontend only, no backend changes) Install `@tanstack/react-virtual` and virtualize `CharacterLibraryView` and `CharactersPanel`. With Step 1 done, the list data lands in ~350ms; virtualization then ensures only ~30 DOM nodes exist regardless of collection size. #### Step 5 — Switch library/panel from `useCharacters()` to `useCharacterSummaries()` `CharacterLibraryView.tsx:201` and `CharactersPanel.tsx:115` use `useCharacters()` which fetches the full record. After Step 1 the summary is lean enough that the library can use it — full records are only needed in the character detail/editor view. #### Step 6 — Backend substring search (enables true search-as-you-type) Add a `search` parameter to the Rust entity list handler. A SQLite `LIKE '%term%'` on the name field is sufficient for <10k records. The React Query key includes the search term, so each distinct query has its own cache entry. This removes the requirement to download the full dataset before the user can search. --- ### Constraint: local Tauri install must keep working Every change above is backward-compatible and transport-agnostic: - Steps 1–3 go through the existing `storageApi` / `invoke` abstraction — no HTTP-only assumptions - `convertFileSrc` is the standard Tauri utility for filesystem paths in the webview; it is already used elsewhere in the codebase - New query params (`search`) are additive and optional; callers that omit them get current behavior - The `/api/assets/avatar/` endpoint is additive; nothing is removed ### Quick wins (can ship in one PR, zero backend changes) These four changes alone make the app dramatically faster and carry no risk: 1. **Remove `avatarPath` from `CHARACTER_SUMMARY_OPTIONS`** — 315 MB → 144 KB, 12 s → 0.35 s 2. **Switch `CharacterLibraryView` + `CharactersPanel` to `useCharacterSummaries()`** — eliminates the second unbounded full-record fetch 3. **Add `@tanstack/react-virtual`** to both list views — fixes DOM bloat 4. **`ChatSidebar`**: replace `useCharacterSummaries()` with `useCharacterSummariesByIds(activeIds)` — the sidebar only needs avatars/names for characters in active chats, not the entire library Items 2 and 3 in the avatar endpoint (Steps 2–3 above) are required to make the list display avatars again after removing `avatarPath` — they should ship in the same PR as quick win #1. --- ## Template check - [ ] I DID NOT read this template and provide the requested details.
bsakaguchi 2026-10-21 05:21:29 +07:00
  • closed this issue
  • added the
    bug
    label
Author
Owner

@Gunterlie commented on GitHub (May 29, 2026):

Will split issues

<!-- gh-comment-id:4577276498 --> @Gunterlie commented on GitHub (May 29, 2026): Will split issues
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#818
No description provided.