Rope Optimizations, Part 1
Zed's Rope, a SumTree of 128-byte chunks, sped up offset-to-point translation by storing u128 bitmasks per chunk marking newlines, char boundaries and UTF-16 widths, replacing a per-character loop. With the bitmask, row and column come from masking up to the offset and calling count_ones and leading_zeros, which compile to single CPU instructions and avoid branches. A micro-benchmark shows 57ns versus 1ns, and point_to_offset got up to 70% faster in production. The same technique adds a tabs bitmask, since tab rendering depends on column position and showed up as costly in profiles; wiring it into higher layers comes in a later post.
Related posts
- Rope & SumTree
Zed mediumAdeptZed represents buffer text with a rope rather than a string, because contiguous strings make mid-file inserts and deletes, line navigation and very large files expensive. Its rope is not a classic binary-tree rope but a SumTree of fixed-capacity inline Chunks of at most 128 characters in release builds. The leaves are reference-counted via Arc, so edits mostly rewire pointers and snapshots are cheap. Concurrent access was the deciding requirement: every edit can hand a snapshot to a background thread for Tree-sitter re-parsing without copying the text, which made the rope win over gap buffers and piece tables.
- This Week at Zed Industries: #1
Zed mediumAdeptZed grew out of Xray, the experimental editor Nathan Sobo and Antonio worked on at GitHub in 2018, and this weekly update recaps the path from there to a company and a private alpha. It also describes the team's first "quality week" of bug fixing, which they plan to repeat every six weeks. Multibuffer performance in project-wide search got much faster after two fixes. Rope fingerprints, computed with the monoid-homomorphic bromberg_sl2 hash and aggregated through a B-tree, are now stored as HashMatrix values instead of allocated Strings. Excerpts are also built on a background thread and streamed into the multibuffer so the main thread no longer blocks.
- Don’t stop early: Case-folding source code at memory speed
GitHub Engineering hardMasterGitHub's Blackbird code search case-folds every byte of 480TB of source, and the fastest ASCII path came from removing the early exit at the first non-ASCII byte. A branchless sweep that ORs bytes into an accumulator and lowercases arithmetically lets LLVM vectorize to over 45 GiB/s on an Apple M4, versus 3.1 GiB/s for the naive break loop. A two-pass chunked scan reaches about 23 GiB/s, and branchless code without vectorization is actually slower. The open-source Rust crate casefold also avoids heap allocation by reusing the input buffer. For non-ASCII text, it rejects non-folding characters with a 64-code-point page bitmap read straight from UTF-8 lead bytes, and run-length/stride-encoded fold ranges shrink Unicode's 1484 simple folds to 1776 bytes.
- We Have to Start Over: From Atom to Zed
Zed easyNoviceZed's co-founders explain why they abandoned Atom's Electron/JavaScript stack and rebuilt a code editor in Rust: JavaScript's pointer-chasing arrays, uncontrollable GC pauses, and the costly JS/C++ boundary for multithreading made performance work feel like fighting the platform. Rust's ownership model pushed them toward a copy-on-write B-tree (SumTree) rope built on Arc, letting snapshots move to background threads in O(1), and high-level Rust code cut a find-all-matches operation from 1s to 4ms. GPU rendering with custom shaders and signed distance fields came out of necessity after Pathfinder proved too slow. Owning the stack (Tree-sitter, GPUI) lets them change anything, such as adding WASM language extensions, at the cost of building more themselves.
- Bun v1.0.20
Bun mediumAdept
Bun v1.0.20 cuts memory usage in node:fs async operations by avoiding redundant cross-thread string clones. Benchmarks show fs.readlink and fs.stat dropping from 107 MB to about 45–71 MB RSS, and a fs.writeFile leak with large non-ASCII strings falling from 1,696 MB to 91 MB. HTMLRewriter.transform now accepts strings and ArrayBuffers and has a destructor-timing leak fixed, and fs.readFile/writeFile gain hex and base64 encodings. A Linux setTimeout high-CPU regression is fixed, Bun.spawn gains a resourceUsage report, and debug builds use macOS malloc zones to track native allocations.