Fine-Grained Context Caching
Relevant source files
The following files were used as context for generating this wiki page:
- website/content/blog/2022-05-12-distributed-computing-in-the-graphene-runtime.md
- website/content/volunteer/guide/student-projects/completed-projects.md
- tools/node-docs/src/page_node.rs
- website/content/blog/2024-07-31-graphite-progress-report-q2-2024.md
- website/content/volunteer/guide/student-projects/_index.md
- website/content/volunteer/guide/codebase-overview/debugging-tips.md
Introduction
Fine-grained context caching is a critical architectural component of the Graphene node graph engine, designed to optimize the execution of nondestructive editing operations. Within the Graphite editor, every user action is encoded as an operation within a Directed Acyclic Graph (DAG). Caching is utilized heavily to minimize the need for recomputation by storing intermediate results of node evaluations. This ensures that when a user modifies a specific part of the graph, only the downstream nodes affected by the change are recomputed, while upstream cached data is reused.
The system is managed by the Graphene Cache Manager, which works in coordination with the scheduler to handle data locality and memoization. By intelligently evicting results when space is limited and tracking data dependencies, the system maintains a responsive user experience even as document complexity grows.
Sources: website/content/blog/2022-05-12-distributed-computing-in-the-graphene-runtime.md, website/content/volunteer/guide/codebase-overview/_index.md
Architecture and Components
The Cache Manager
The Cache Manager is responsible for storing intermediate node evaluation results. It enables memoization, a technique where a node's output is retrieved from the cache if its inputs remain identical to a previous execution. This is particularly vital for computationally expensive nodes like "Blur" or machine learning-powered operations.
The following diagram illustrates the relationship between the user interaction, the node graph, and the caching layer:
The Graphene Cache Manager intelligently handles eviction and data locality to minimize latency.
Sources: website/content/blog/2022-05-12-distributed-computing-in-the-graphene-runtime.md
Data Locality and Batched Execution
To further reduce overhead, Graphene attempts to group related nodes on the same physical hardware (CPU or GPU) to maintain memory and cache locality. This prevents expensive data transfers between RAM, VRAM, or over a network. When frequent execution of specific node paths is observed, the system may batch multiple nodes by recompiling them into a single execution unit, conceptually similar to Just-In-Time (JIT) optimization.
| Feature | Description |
|---|---|
| Memoization | Avoiding recomputation when input values are unchanged. |
| Intelligent Eviction | Removing older or less relevant cached results when memory is limited. |
| Data Locality | Prioritizing execution on hardware where intermediate data already resides. |
| Batched Execution | Inlining sequential nodes into a single unit to reduce inter-node overhead. |
Sources: website/content/blog/2022-05-12-distributed-computing-in-the-graphene-runtime.md
Node Interface and Context Dependencies
The caching system is integrated into how nodes define their inputs and outputs. Nodes in Graphite have metadata that specifies "Context Features," which include read/write dependencies that the caching and scheduling systems must respect.
Context Read/Write Logic
Nodes can declare specific types they need to extract from or inject into the execution context. This metadata is used to generate documentation and manage the flow of data within the graph.
// Example of context feature extraction for documentation
// tools/node-docs/src/page_node.rs:55-73
fn write_context(page: &mut std::fs::File, context_dependencies: ContextDependencies) {
let extract = context_dependencies.extract;
let inject = context_dependencies.inject;
if !extract.is_empty() || !inject.is_empty() {
let mut context_features = "| | |\n|:-|:-|".to_string();
if !extract.is_empty() {
let names = extract.iter().map(|ty| format!("`{}`", ty.name())).collect::<Vec<_>>().join("<br />");
context_features.push_str(&format!("\n| **Reads** | {names} |"));
}
if !inject.is_empty() {
let names = inject.iter().map(|ty| format!("`{}`", ty.name())).collect::<Vec<_>>().join("<br />");
context_features.push_str(&format!("\n| **Sets** | {names} |"));
}
// ...
};
}
Sources: tools/node-docs/src/page_node.rs:55-73
Dependency Tracking
The compiler tracks node equivalency and data transformations. For instance, two "Transform" nodes can sometimes be combined into one. A proposed system for "Node equivalence rewriting" aims to use E-graphs to symbolically track and simplify these transformations, potentially reducing the number of cache entries required by collapsing redundant nodes.
Sources: website/content/volunteer/guide/student-projects/_index.md
Execution Flow and Performance
The performance of the caching system is monitored through various debugging and profiling tools. Developers can view message system logs to track how data flows through the graph and identify where cache misses occur.
Performance Monitoring Tools
- Message System Logs: Accessible via Help > Debug: Print Messages, these logs reveal the activity of the message flow and the data being passed between nodes.
- Node/Layer IDs: Hovering over nodes in debug mode reveals unique IDs, which are essential for tracking specific cached results in logs.
- Browser Profiler: The Firefox and Chromium performance tabs allow for analyzing bottlenecks in the execution pipeline.
Sources: website/content/volunteer/guide/codebase-overview/debugging-tips.md
Sequence of a Cached Render
When a render is triggered, the scheduler interacts with the cache manager as follows:
Summary
Fine-grained context caching is the foundation of Graphite's "nondestructive" promise. By leveraging memoization, data locality, and intelligent cache management, the Graphene engine ensures that the editor remains interactive and responsive. The system continues to evolve, with recent advancements focusing on GPU-accelerated raster operations and automated node layout management to further optimize the rendering pipeline.
Sources: website/content/volunteer/guide/student-projects/completed-projects.md, website/content/blog/2024-07-31-graphite-progress-report-q2-2024.md