Skip to main content

Module versioned

Module versioned 

Source
Expand description

Versioned<T> — a tiny newtype wrapper that bumps a monotonic u64 version on every &mut access via DerefMut.

§Why this exists

Perf plan B.4: identity-preserving Arc publish for unchanged RenderState sub-states. Every Editor::build_render_state tick today freshly Arc::news every sub-state struct even when the backing data hasn’t moved. Most keystrokes don’t touch pane_tree / active_modes / buffer_locals / pane_highlights / lsp_progress, so reusing the prior Arc when nothing changed drops both the outer allocation and the inner map / tree clones they would otherwise produce.

The cache lookup needs a cheap-to-compare key. The naive option (“content-hash on every publish”) makes the hash cost the rebuild cost — wash. The alternative (“dirty-flag set by every mutator”) is bulletproof in spec but easy to forget at one of the ~30 mutation sites and fails silently when missed.

Versioned<T> takes the third path: any code that obtains a &mut to the inner data goes through DerefMut, which increments the counter atomically with the access. There is no way to mutate through a Versioned<T> without bumping. Reads (Deref) don’t bump. The cost is one u64 add per &mut borrow — sub-nanosecond, dwarfed by whatever mutation follows.

§Trade-offs

  • Over-bumps on read-then-no-op-mutate (e.g. .iter_mut() that the caller never actually writes through) cause a spurious cache miss the next publish. Safe; just costs one extra rebuild of that sub-state. Bounded.
  • Field-assignment (self.field = ...) is not a DerefMut — it replaces the wrapper entirely. The From<T> / Versioned::new constructors zero the version, so the next build_render_state will rebuild correctly. Use [Self::replace] when you want to bump rather than reset.
  • Single-threaded only by design. The wrapper isn’t atomic; it relies on Editor being mutated from one thread (the actor). If you need cross-thread mutation, you have other problems — talk to the actor instead of mutating Editor directly.

Structs§

Versioned
Newtype wrapper that bumps a u64 version counter on every DerefMut access. See module docs for the rationale.