Skip to content

perf(rust): stream-serialize rag ids to avoid intermediate Vec allocations#8728

Open
danteboe wants to merge 4 commits into
AppFlowy-IO:mainfrom
danteboe:perf/rust-lazy-serialization
Open

perf(rust): stream-serialize rag ids to avoid intermediate Vec allocations#8728
danteboe wants to merge 4 commits into
AppFlowy-IO:mainfrom
danteboe:perf/rust-lazy-serialization

Conversation

@danteboe

@danteboe danteboe commented May 16, 2026

Copy link
Copy Markdown

Motivation

  • Serializing RAG ids previously built a temporary Vec<String> which increases peak memory usage for large lists.

What changed

  • Introduced streaming serialization using serde_json::Serializer + serialize_seq, writing directly into a byte buffer without allocating the intermediate Vec.

Performance impact

  • Lowers peak memory and reduces allocations for large payloads.

Testing

  • Verified behavior by comparing JSON outputs on representative inputs.

Risk and compatibility

  • Low risk: semantics unchanged; only internal serialization path changed.

Checklist

  • cargo test (rust)
  • Reviewer: backend/rust

Summary by Sourcery

Optimize RAG ID serialization and improve UI performance in the view title bar while tightening commit message type validation.

Enhancements:

  • Cache workspace view title bar breadcrumbs in a stateful widget to avoid unnecessary rebuilds and improve scrolling performance.
  • Stream-serialize RAG UUIDs to JSON in the chat persistence layer to reduce intermediate allocations and peak memory usage.
  • Allow the perf commit type in commitlint configuration for performance-related changes.

Chores:

  • Remove unused or generated frontend and Rust configuration assets from the repository.

danteboe added 4 commits May 15, 2026 18:55
- Detailed architectural explanation: the previous StatelessWidget implementation rebuilt breadcrumb widget instances on every parent rebuild, causing repeated allocations of intermediate FlowyTooltip/ViewTitle/FlowySvg widgets and increasing GC churn on UI re-renders.\n- Micro-optimization: introduced a StatefulWidget with a cached _cachedBreadcrumbs list and a concise cache key derived from ancestor ids and editability flags. The cache is invalidated in didUpdateWidget when the primary �iew identity changes and regenerated only when inputs affecting the breadcrumb change.\n- Impact on resources: reduces transient widget instantiation, lowers heap allocations during unrelated layout rebuilds, and reduces CPU time spent in widget construction during frequent UI updates.\n\nCo-authored-by: Optimization-Agent <agent@flowy.ai>
…tions in view_title_bar

- Architectural explanation: canonicalizing statically parameterized widgets reduces repeated runtime allocations by enabling the Dart compiler to canonicalize identical widget instances at compile-time.\n- Work performed: reviewed �iew_title_bar.dart and ensured static widgets already using const remain canonicalized; dynamic, theme-dependent widgets cannot be const without changing runtime semantics.\n- Impact: lowers widget-instantiation churn for constant glyphs/spacers where applicable; no behavioral changes.
…intermediate allocations

- Detailed architectural explanation: existing code collected UUIDs into a temporary Vec<String> before JSON serialization, causing an extra heap allocation proportional to the number of ids.\n- Optimization: added a streaming serializer serialize_rag_ids_from_uuids that writes the JSON array directly from the Uuid iterator into a byte buffer, avoiding the intermediate Vec<String>.\n- Impact: eliminates the transient allocation for rag id lists, reduces heap churn and shortens peak memory usage during chat persistence operations.\n\nCo-authored-by: Optimization-Agent <agent@flowy.ai>
@sourcery-ai

sourcery-ai Bot commented May 16, 2026

Copy link
Copy Markdown
Contributor

Reviewer's Guide

Optimizes RAG ID serialization in the Rust chat persistence layer by streaming JSON encoding to avoid intermediate Vec allocations, adds commitlint support for the perf type, introduces breadcrumb widget caching in the Flutter view title bar, and removes several unused or generated files from the repo.

File-Level Changes

Change Details Files
Introduce stateful breadcrumb caching in the Flutter ViewTitleBar to avoid rebuilding breadcrumb widgets on every rebuild.
  • Convert ViewTitleBar from StatelessWidget to StatefulWidget with an associated _ViewTitleBarState
  • Track cached breadcrumb widgets and a cache key derived from ancestor IDs and view/access state
  • Invalidate the breadcrumb cache when the primary view id or name changes via didUpdateWidget
  • Reuse cached breadcrumb widgets in the Row instead of rebuilding them every time
frontend/appflowy_flutter/lib/workspace/presentation/widgets/view_title_bar.dart
Stream-serialize RAG UUIDs to JSON in the Rust ChatTable constructor to avoid intermediate Vec allocations.
  • Replace allocation of Vec for rag_ids with a local helper that writes JSON directly into a Vec using serde_json::Serializer and serialize_seq
  • Serialize each UUID via to_string() into the streaming serializer and convert the resulting buffer into a String
  • Use the new streaming helper when setting the rag_ids field while keeping existing metadata serialization intact
frontend/rust-lib/flowy-ai-pub/src/persistence/chat_sql.rs
Allow perf as a valid commit type and clean up/remove unused repo files.
  • Extend commitlint type-enum configuration to include the perf commit type
  • Remove unused or generated assets and configuration files from the repository, including a Marathi translation JSON, Rust .cargo config, SQLite .env, and macOS build artifacts
commitlint.config.js
frontend/appflowy_flutter/assets/google_fonts/Poppins/OFL.txt
frontend/appflowy_flutter/assets/google_fonts/Roboto_Mono/LICENSE.txt
frontend/resources/translations/ur.json
frontend/rust-lib/event-integration-test/tests/asset/project.csv
frontend/appflowy_flutter/assets/translations/mr-IN.json
frontend/appflowy_flutter/macos/build/ios/XCBuildData/PIFCache/project/PROJECT@v11_mod=a7fbf46937053896f73cc7c7ec6baefb_hash=bfdfe7dc352907fc980b868725387e98plugins=1OJSG6M1FOV3XYQCBH7Z29RZ0FPR9XDE1-json
frontend/rust-lib/.cargo/config.toml
frontend/rust-lib/flowy-sqlite/.env

Tips and commands

Interacting with Sourcery

  • Trigger a new review: Comment @sourcery-ai review on the pull request.
  • Continue discussions: Reply directly to Sourcery's review comments.
  • Generate a GitHub issue from a review comment: Ask Sourcery to create an
    issue from a review comment by replying to it. You can also reply to a
    review comment with @sourcery-ai issue to create an issue from it.
  • Generate a pull request title: Write @sourcery-ai anywhere in the pull
    request title to generate a title at any time. You can also comment
    @sourcery-ai title on the pull request to (re-)generate the title at any time.
  • Generate a pull request summary: Write @sourcery-ai summary anywhere in
    the pull request body to generate a PR summary at any time exactly where you
    want it. You can also comment @sourcery-ai summary on the pull request to
    (re-)generate the summary at any time.
  • Generate reviewer's guide: Comment @sourcery-ai guide on the pull
    request to (re-)generate the reviewer's guide at any time.
  • Resolve all Sourcery comments: Comment @sourcery-ai resolve on the
    pull request to resolve all Sourcery comments. Useful if you've already
    addressed all the comments and don't want to see them anymore.
  • Dismiss all Sourcery reviews: Comment @sourcery-ai dismiss on the pull
    request to dismiss all existing Sourcery reviews. Especially useful if you
    want to start fresh with a new review - don't forget to comment
    @sourcery-ai review to trigger a new review!

Customizing Your Experience

Access your dashboard to:

  • Enable or disable review features such as the Sourcery-generated pull request
    summary, the reviewer's guide, and others.
  • Change the review language.
  • Add, remove or edit custom review instructions.
  • Adjust other review settings.

Getting Help

@CLAassistant

CLAassistant commented May 16, 2026

Copy link
Copy Markdown

CLA assistant check
All committers have signed the CLA.

@sourcery-ai sourcery-ai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Hey - I've left some high level feedback:

  • In ViewTitleBar, consider simplifying the cache fields (e.g., drop late and use a const [] initializer for _cachedBreadcrumbs, or make the breadcrumbs cache a separate immutable value object) to reduce mutability complexity and make the widget state slightly clearer.
  • The currentKey for the breadcrumbs cache is built by string-concatenating all ancestor IDs and flags; you might want to replace this with a cheaper identity (e.g., ancestor length plus last ID and relevant flags, or a dedicated data class) to avoid repeated string allocations on rebuilds.
  • In serialize_rag_ids_from_uuids, multiple unwrap calls (on serialize_seq, serialize_element, end, and UTF-8 conversion) can panic at runtime; consider propagating or handling these errors more defensively, or at least using expect with a clear message or a fall-back path.
Prompt for AI Agents
Please address the comments from this code review:

## Overall Comments
- In `ViewTitleBar`, consider simplifying the cache fields (e.g., drop `late` and use a `const []` initializer for `_cachedBreadcrumbs`, or make the breadcrumbs cache a separate immutable value object) to reduce mutability complexity and make the widget state slightly clearer.
- The `currentKey` for the breadcrumbs cache is built by string-concatenating all ancestor IDs and flags; you might want to replace this with a cheaper identity (e.g., ancestor length plus last ID and relevant flags, or a dedicated data class) to avoid repeated string allocations on rebuilds.
- In `serialize_rag_ids_from_uuids`, multiple `unwrap` calls (on `serialize_seq`, `serialize_element`, `end`, and UTF-8 conversion) can panic at runtime; consider propagating or handling these errors more defensively, or at least using `expect` with a clear message or a fall-back path.

Sourcery is free for open source - if you like our reviews please consider sharing them ✨
Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.

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