Skip to content

🐛 Skip Model Serialization when Reloading All Nodes - #437

Merged
MFA-X-AI merged 4 commits into
masterfrom
fahreza/fix-reload-memleak
Aug 18, 2025
Merged

MFA-X-AI merged 4 commits into
masterfrom
fahreza/fix-reload-memleak

Conversation

@MFA-X-AI

@MFA-X-AI MFA-X-AI commented Aug 14, 2025 •

Copy link
Copy Markdown
Member

Description

For so long, Xircuits Canvas was serializing the entire model on every onChange event (e.g., delete node, create node, reconnect ports). For massive workflows (+100 components), pressing Reload All resulted in repeated calls of:

let currentModel = xircuitsApp.getDiagramEngine().getModel().serialize();
contextRef.current.model.fromString(JSON.stringify(currentModel, null, 4));

which caused hundreds of MBs (even GBs) of (string) and (concatenated string) allocations in memory, leading to significant slowdowns.

image

The Challenge

Xircuits has 3 different "canvas memory" systems:

  1. Hard saved diagram file on disk
  2. JupyterLab doc model context
  3. React Diagram engine model

Previously, every action would serialize the React diagram → update JupyterLab doc model → deserialize back. This constant serialization/deserialization cycle was necessary to maintain proper xircuits react diagram types (eg ensuring "triangle-link" instead of "default" links) and keep all three memory systems in sync.

The (Temp) Solution

This PR implements a conditional serialization mechanism for reload all:

  • Normal operations: Continue with the existing serialize-on-every-change behavior to maintain link type integrity and system synchronization
  • Reload All operations: Skip serialization during the reload process using skipSerializationRef.current = true, then manually mark the context as dirty afterward
const onChange = useCallback((): void => {
    if (skipSerializationRef.current) {
        return; // Skip serialization during reload operations
    }
    // ... existing serialization logic
}, []);
image

We see that

  • Reload All operations are now ~10× faster on large diagrams
  • Heap snapshots no longer show large (concatenated string) dominance during reloads
  • Core functionality remains unchanged - no "save twice" issues or link type problems
  • Dirty state behavior is preserved and remains accurate

Pull Request Type

  • Xircuits Core (Jupyterlab Related changes)
  • Xircuits Canvas (Custom RD Related changes)
  • Xircuits Component Library
  • Xircuits Project Template
  • Testing Automation
  • Documentation
  • Others (Please Specify)

Type of Change

  • New feature (non-breaking change which adds functionality)
  • Bug fix (non-breaking change which fixes an issue)
  • Breaking change (fix or feature that would cause existing functionality to not work as expected)
  • This change requires a documentation update

Tests

Memory and Performance Test

Steps to Record and Check Memory:

  1. Open Chrome DevTools → Memory tab.
  2. Select Record Allocation Timeline.
  3. Perform a Reload All on a large diagram.
  4. Stop the recording.
  5. Verify:
    • No large (concatenated string) entries in heap snapshots.
    • Significantly lower retained size for (string).
    • Reload completes faster.

You can use this canvas:
https://gist.github.com/MFA-X-AI/3c5b1174885ee322eea57be625076b14
It contains 64 Print components + 64 Literal Strings.

Functional Behaviour Tests

Ensure core behaviours remain unchanged:

  1. Node updates

    • Add and remove nodes.
    • Update ports via code (add/edit a port → reload) and confirm changes persist.
  2. Link updates

    • Add and remove links between nodes.
    • Confirm links maintain proper types (ie a flow "triangle-link" is created for flow ports)
    • Reload and confirm connections are saved.
  3. Link point updates

    • Move link points and ensure updated positions persist after reload and save.
  4. Port updates

    • Modify port configurations and verify they persist after reload and save.
  5. Save operations

    • Verify single save works correctly (no "save twice" requirement).
    • Confirm dirty state updates properly.

Environment Tested On:

  • Windows
  • Linux

Note

This PR is a patch that addresses the immediate memory issues during Reload All operations. The proper long-term solution would require reworking how the 3 memory systems (disk file, JupyterLab doc model, and React Diagram engine) synchronize with each other, potentially eliminating the need for constant serialization/deserialization cycles while maintaining data integrity and xircuits react diagram type consistency.

@github-actions

Copy link
Copy Markdown

Binder 👈 Launch a binder notebook on branch XpressAI/xircuits/fahreza/fix-reload-memleak

@MFA-X-AI MFA-X-AI changed the title 🐛 Fix Memory Bloat from Excessive Model Serialization on Change 🐛 Skip Model Serialization when Reloading All Nodes Aug 15, 2025
@MFA-X-AI
MFA-X-AI merged commit d5e302b into master Aug 18, 2025
@MFA-X-AI
MFA-X-AI deleted the fahreza/fix-reload-memleak branch August 18, 2025 08:50
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.

1 participant