首页›Raph Levien›文章

Xilem: an architecture for UI in Rust

2022年5月7日

AI 摘要 AI 生成,可能有误

本文介绍了 Xilem,这是一种用 Rust 构建用户界面的新架构,旨在克服大多数现有 UI 架构依赖共享可变状态的问题,而共享可变状态并不符合 Rust 的惯用写法。Xilem 基于声明式、静态类型的视图树,每个周期都会对其进行差异比较,以增量更新持久存在的部件树;其关键创新包括通过 id 路径进行事件分发,以及借助 Adapt 节点实现干净利落的组件组合,同时保持各自独立的可变状态访问。该架构借鉴了 SwiftUI、Flutter 和 React 等工具包中的现代响应式模式,旨在通过精确的变更传播,实现编写直观且高性能的 UI 代码。

Rust is an appealing language for building user interfaces for a variety of reasons, especially the promise of delivering both performance and safety. However, finding a good architecture is challenging. Architectures that work well in other languages generally don’t adapt well to Rust, mostly because they rely on shared mutable state and that is not idiomatic Rust, to put it mildly. It is sometimes asserted for this reason that Rust is a poor fit for UI. I have long believed that it is possible to find an architecture for UI well suited to implementation in Rust, but my previous attempts (including the current Druid architecture) have all been flawed. I have studied a range of other Rust UI projects and don’t feel that any of those have suitable architecture either.

This post presents a new architecture, which is a synthesis of existing work and a few new ideas. The goals include expression of modern reactive, declarative UI, in components which easily compose, and a high performance implementation. UI code written in this architecture will look very intuitive to those familiar with state of the art toolkits such as SwiftUI, Flutter, and React, while at the same time being idiomatic Rust.

The name “Xilem” is derived from xylem, a type of transport tissue in vascular plants, including trees. The word is spelled with an “i” in several languages including Romanian and Malay, and is a reference to xi-editor, a starting place for explorations into UI in Rust (now on hold).

继续阅读全文

为了尊重作者,这里只展示开头几段。你可以去原文阅读,或者在 HorizonReader 里读:划线做笔记、让 AI 帮忙。

更多来自 Raph Levien

I want a good parallel computer

AI 摘要The article argues that GPUs, despite being 10-100x more powerful than CPUs, are prevented from becoming general-purpose parallel computers by an impoverished execution model and inadequate languages and tools, illustrated by the author's struggles with bounded memory in the Vello 2D renderer. Reviewing historical designs like the Connection Machine, Cell, and Larrabee, the author shows that promising parallel architectures existed but failed to catch on, leaving today's overly complex GPUs and limited AI accelerators. With workloads increasingly shifting toward compute, the author believes a simpler, more powerful parallel computer is possible and points to paths forward such as chips with large grids of CPU cores.

A note on Metal shader converter

AI 摘要The author critiques Apple's new Metal shader converter, which eases porting DirectX 12 games by translating DXIL/HLSL shaders to Metal, arguing it adds underspecified complexity rather than advancing robust GPU infrastructure. A key complaint is that Metal remains the only major GPU API lacking device-scope atomic barriers (present in HLSL since Direct3D 11 in 2009), which prevents single-pass prefix sum implementations and reliable cross-threadgroup coordination, and in turn holds back WebGPU's performance. The author recommends Apple document which HLSL features are unsupported, and points to WebGPU and Asahi Linux's work as more promising paths toward well-specified, portable GPU compute.

Simplifying Bézier paths

AI 摘要The article describes refinements to curve-fitting techniques for approximating arbitrary curves with near-optimal Bézier paths, implemented in the Rust library kurbo, built around a new ParamCurveFit trait and efficient error evaluation using Green's theorem area/moment prefix sums. It covers error metrics, favoring Fréchet distance approximated via Tiller-Hanson ray casting, with a slower but more robust arc-length approach used for 'spicy' high-curvature regions, and goes beyond simple adaptive subdivision by optimizing subdivision point placement to minimize segment count. Finally, it addresses an unexpected problem where aggressively Fréchet-optimized curves develop bumps, solved by penalizing extreme control-point configurations to keep the output smooth at a cost of only one additional segment.

Moving from Rust to C++

AI 摘要This is a satirical April Fools' post in which the author, a longtime Rust developer, mock-announces that the Linebender GUI projects will migrate from Rust to C++. In a deliberately persuasive style, the piece humorously dismisses memory safety concerns, praises C++'s abundance of build systems and rapid language evolution, and claims the C++ community is friendlier. The post is intended to satirize bad arguments commonly made against Rust, with the author noting afterward that its serious points and tone confused some readers.

Requiem for piet-gpu-hal

AI 摘要The article explains why the Vello 2D GPU renderer (formerly piet-gpu) abandoned piet-gpu-hal, its hand-rolled GPU abstraction layer, in favor of wgpu and the WebGPU/WGSL ecosystem. Although the custom layer achieved goals like precompiled shaders (saving binary size and startup time) and access to advanced GPU features such as subgroups and descriptor indexing, wgpu offers a better developer experience, community tooling, and web deployment. The author reflects on lessons learned, notes WebGPU pain points, and says the roadmap may eventually revisit precompiled shaders and native back-ends to regain those lost capabilities.