Skip to content

In development. Expect rough edges and changes. The source code is not public yet.

Zed 9 min read Deep dive mediumAdept

Ownership and data flow in GPUI

GPUI, Zed's Rust UI framework, sidesteps Rust's single-ownership constraints by having a top-level App own every model and view, while code holds reference-counted Entity<T> handles that can only read or update state when given an App or Context. Entities communicate via notify/observe and typed emit/subscribe, but calling emit or notify only enqueues an effect; App::update flushes the effect queue after the outermost update, giving run-to-completion semantics without the reentrancy bugs of naive event emitters. To hand out both &mut T and &mut Context<T>, update_entity temporarily leases the entity's state out of the App onto the stack and returns it afterward, at the cost of forbidding reentrant updates to the same entity.

Assumes: Rust ownership and borrowingRc reference countingObserver/event emitter pattern

Related posts

  • Zed Weekly: #25

    Zed mediumAdept

    Zed is rewriting its UI framework as GPUI 2 and porting the whole editor onto it before open-sourcing, trading feature work for better ergonomics while keeping GPUI 1's performance. All state lives in an AppContext that acts like an application-specific heap: Model<T> handles are dereferenced through a context, and models emit and subscribe to events. View<T> wraps models whose types implement a Render trait that returns element trees. Components, Tailwind-style styling helpers, a color-scale-based theme system and a new layout engine and executor round out the design. Bottleneck crates (project, workspace, editor) made the port largely serial, so the team leaned on Rust's type system and Zed's collaborative editing to pair on it, with roughly a third to half of the app ported after two weeks.

  • Why the big rewrite?

    Zed easyNovice

    Zed rewrote its Rust UI framework from GPUI 1 to GPUI 2 because the original design blocked fast UI work: a Flutter-style constraints-down/sizes-up layout model, styles split between Rust element code and TypeScript-generated JSON "themes" loaded at runtime, model code entangled with presentation (a legacy of an early Electron/JSON architecture), and weakly typed geometry (raw f32s and usizes). GPUI 2 adopts a Tailwind/Flexbox-inspired Rust DSL with stronger types and one-directional ownership. The migration ran incrementally: GPUI 2 first ran as a prototype on top of GPUI 1, then crates were duplicated (e.g. project_panel2) with the old copies frozen except for bug fixes, which kept the option of sharing code until the final switch.

  • Zed Weekly: #22

    Zed easyNovice

    Zed team members report weekly progress: GPUI is being rewritten with a ported Metal renderer that uses plane-split for primitive draw ordering, multi-threaded entity access, and a MainThread<T: Context> wrapper so native UI APIs are only called from the main thread. Channels are being reworked to separate chat-style communication from DAG-structured project notes, with unread indicators, notifications, public channels and deep links on the way. Julia split markdown parsing from rendering for completion documentation, avoiding flicker when grammars load late and speeding up rendering. Other work covers Vue LSP and tree-sitter support, CPU and memory telemetry, and a fix for a follow-loop bug in collaboration.

  • Zed Weekly: #29

    Zed mediumAdept

    Zed's port to GPUI 2 is nearing daily usability, and the team is focused on frame-rendering performance measured by holding tab-switch at 60Hz key repeat. Because every element is a boxed trait object, they replaced malloc with a custom thread-local bump arena that enforces safety dynamically instead of via lifetimes, and changed `Element::paint` to take `&mut self` to avoid heap-to-stack copies. The target is full-window redraws under 4ms on an M1, with view caching planned next. Other updates cover a better VS Code theme importer, focus handlers moved to run after each frame draw to fix a split-pane focus bug, and a port of the feedback UI.

  • This Week at Zed Industries: #16

    Zed mediumAdept

    Zed has started using its new channels feature internally, where members can message, talk, share screens and co-edit projects, and plans to use it to stream development and host office hours once the editor is open-sourced. Alongside this, GPUI layout and styling are being reworked with the taffy layout engine and Tailwind-like style methods. Antonio proposes `auto` and `prettier` options for the formatter setting. Mikayla walks through a new composable Component primitive for GPUI in Rust: a builder-style trait consumed into an Element, a StylableComponent trait with associated Style/Output types, and the Typestate pattern using `()` as an unbound style, so wrappers like Hover and Toggle compose type-safely and rendering an unstyled component fails at compile time.

The quest for software engineering knowledge.

Look:
Layout: