clock: Cleanup ReplicaId, Lamport and Global (#40600)

- Notable change is the use of a newtype for `ReplicaId`
- Fixes `WorktreeStore::create_remote_worktree` creating a remote
worktree with the local replica id, though this is not currently used
- Fixes observing the `Agent` (that is following the agent) causing
global clocks to allocate 65535 elements
- Shrinks the size of `Global` a bit. In a local or non-collab remote
session it won't ever allocate still.

Release Notes:

- N/A *or* Added/Fixed/Improved ...
This commit is contained in:
Lukas Wirth
2025-10-20 13:26:20 +02:00
committed by GitHub
parent 37e264ab99
commit 43a9368dff
33 changed files with 427 additions and 284 deletions
+13 -13
View File
@@ -12,7 +12,7 @@ mod undo_map;
pub use anchor::*;
use anyhow::{Context as _, Result};
use clock::LOCAL_BRANCH_REPLICA_ID;
use clock::Lamport;
pub use clock::ReplicaId;
use collections::{HashMap, HashSet};
use locator::Locator;
@@ -573,7 +573,7 @@ struct InsertionFragment {
fragment_id: Locator,
}
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, PartialOrd, Ord)]
#[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord)]
struct InsertionFragmentKey {
timestamp: clock::Lamport,
split_offset: usize,
@@ -709,7 +709,7 @@ impl FromIterator<char> for LineIndent {
}
impl Buffer {
pub fn new(replica_id: u16, remote_id: BufferId, base_text: impl Into<String>) -> Buffer {
pub fn new(replica_id: ReplicaId, remote_id: BufferId, base_text: impl Into<String>) -> Buffer {
let mut base_text = base_text.into();
let line_ending = LineEnding::detect(&base_text);
LineEnding::normalize(&mut base_text);
@@ -717,7 +717,7 @@ impl Buffer {
}
pub fn new_normalized(
replica_id: u16,
replica_id: ReplicaId,
remote_id: BufferId,
line_ending: LineEnding,
normalized: Rope,
@@ -731,10 +731,7 @@ impl Buffer {
let visible_text = history.base_text.clone();
if !visible_text.is_empty() {
let insertion_timestamp = clock::Lamport {
replica_id: 0,
value: 1,
};
let insertion_timestamp = clock::Lamport::new(ReplicaId::LOCAL);
lamport_clock.observe(insertion_timestamp);
version.observe(insertion_timestamp);
let fragment_id = Locator::between(&Locator::min(), &Locator::max());
@@ -788,7 +785,7 @@ impl Buffer {
history: History::new(self.base_text().clone()),
deferred_ops: OperationQueue::new(),
deferred_replicas: HashSet::default(),
lamport_clock: clock::Lamport::new(LOCAL_BRANCH_REPLICA_ID),
lamport_clock: clock::Lamport::new(ReplicaId::LOCAL_BRANCH),
subscriptions: Default::default(),
edit_id_resolvers: Default::default(),
wait_for_version_txs: Default::default(),
@@ -1254,7 +1251,7 @@ impl Buffer {
for edit_id in edit_ids {
let insertion_slice = InsertionSlice {
edit_id: *edit_id,
insertion_id: clock::Lamport::default(),
insertion_id: clock::Lamport::MIN,
range: 0..0,
};
let slices = self
@@ -1858,7 +1855,7 @@ impl Buffer {
T: rand::Rng,
{
let mut edits = self.get_random_edits(rng, edit_count);
log::info!("mutating buffer {} with {:?}", self.replica_id, edits);
log::info!("mutating buffer {:?} with {:?}", self.replica_id, edits);
let op = self.edit(edits.iter().cloned());
if let Operation::Edit(edit) = &op {
@@ -1881,7 +1878,7 @@ impl Buffer {
if let Some(entry) = self.history.undo_stack.choose(rng) {
let transaction = entry.transaction.clone();
log::info!(
"undoing buffer {} transaction {:?}",
"undoing buffer {:?} transaction {:?}",
self.replica_id,
transaction
);
@@ -2918,7 +2915,10 @@ impl InsertionFragment {
impl sum_tree::ContextLessSummary for InsertionFragmentKey {
fn zero() -> Self {
Default::default()
InsertionFragmentKey {
timestamp: Lamport::MIN,
split_offset: 0,
}
}
fn add_summary(&mut self, summary: &Self) {