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
+8 -6
View File
@@ -25,7 +25,7 @@ use rpc::{
};
use smol::channel::Receiver;
use std::{io, pin::pin, sync::Arc, time::Instant};
use text::BufferId;
use text::{BufferId, ReplicaId};
use util::{ResultExt as _, TryFutureExt, debug_panic, maybe, rel_path::RelPath};
use worktree::{File, PathChange, ProjectEntryId, Worktree, WorktreeId};
@@ -158,7 +158,7 @@ impl RemoteBufferStore {
pub fn handle_create_buffer_for_peer(
&mut self,
envelope: TypedEnvelope<proto::CreateBufferForPeer>,
replica_id: u16,
replica_id: ReplicaId,
capability: Capability,
cx: &mut Context<BufferStore>,
) -> Result<Option<Entity<Buffer>>> {
@@ -626,7 +626,9 @@ impl LocalBufferStore {
cx.spawn(async move |_, cx| {
let loaded = load_file.await?;
let text_buffer = cx
.background_spawn(async move { text::Buffer::new(0, buffer_id, loaded.text) })
.background_spawn(async move {
text::Buffer::new(ReplicaId::LOCAL, buffer_id, loaded.text)
})
.await;
cx.insert_entity(reservation, |_| {
Buffer::build(text_buffer, Some(loaded.file), Capability::ReadWrite)
@@ -639,7 +641,7 @@ impl LocalBufferStore {
Ok(buffer) => Ok(buffer),
Err(error) if is_not_found_error(&error) => cx.new(|cx| {
let buffer_id = BufferId::from(cx.entity_id().as_non_zero_u64());
let text_buffer = text::Buffer::new(0, buffer_id, "");
let text_buffer = text::Buffer::new(ReplicaId::LOCAL, buffer_id, "");
Buffer::build(
text_buffer,
Some(Arc::new(File {
@@ -917,7 +919,7 @@ impl BufferStore {
path: file.path.clone(),
worktree_id: file.worktree_id(cx),
});
let is_remote = buffer.replica_id() != 0;
let is_remote = buffer.replica_id().is_remote();
let open_buffer = OpenBuffer::Complete {
buffer: buffer_entity.downgrade(),
};
@@ -1317,7 +1319,7 @@ impl BufferStore {
pub fn handle_create_buffer_for_peer(
&mut self,
envelope: TypedEnvelope<proto::CreateBufferForPeer>,
replica_id: u16,
replica_id: ReplicaId,
capability: Capability,
cx: &mut Context<Self>,
) -> Result<()> {
+5 -5
View File
@@ -271,7 +271,7 @@ mod tests {
use language::language_settings::AllLanguageSettings;
use serde_json::json;
use settings::Settings as _;
use text::{Buffer, BufferId, Point, ToOffset as _};
use text::{Buffer, BufferId, Point, ReplicaId, ToOffset as _};
use unindent::Unindent as _;
use util::{path, rel_path::rel_path};
use worktree::WorktreeSettings;
@@ -299,7 +299,7 @@ mod tests {
.unindent();
let buffer_id = BufferId::new(1).unwrap();
let buffer = Buffer::new(0, buffer_id, test_content);
let buffer = Buffer::new(ReplicaId::LOCAL, buffer_id, test_content);
let snapshot = buffer.snapshot();
let conflict_snapshot = ConflictSet::parse(&snapshot);
@@ -374,7 +374,7 @@ mod tests {
.unindent();
let buffer_id = BufferId::new(1).unwrap();
let buffer = Buffer::new(0, buffer_id, test_content);
let buffer = Buffer::new(ReplicaId::LOCAL, buffer_id, test_content);
let snapshot = buffer.snapshot();
let conflict_snapshot = ConflictSet::parse(&snapshot);
@@ -405,7 +405,7 @@ mod tests {
>>>>>>> "#
.unindent();
let buffer_id = BufferId::new(1).unwrap();
let buffer = Buffer::new(0, buffer_id, test_content);
let buffer = Buffer::new(ReplicaId::LOCAL, buffer_id, test_content);
let snapshot = buffer.snapshot();
let conflict_snapshot = ConflictSet::parse(&snapshot);
@@ -447,7 +447,7 @@ mod tests {
.unindent();
let buffer_id = BufferId::new(1).unwrap();
let buffer = Buffer::new(0, buffer_id, test_content.clone());
let buffer = Buffer::new(ReplicaId::LOCAL, buffer_id, test_content.clone());
let snapshot = buffer.snapshot();
let conflict_snapshot = ConflictSet::parse(&snapshot);
+2 -3
View File
@@ -3025,9 +3025,8 @@ impl LocalLspStore {
Some(buffer_to_edit.read(cx).saved_version().clone())
};
let most_recent_edit = version.and_then(|version| {
version.iter().max_by_key(|timestamp| timestamp.value)
});
let most_recent_edit =
version.and_then(|version| version.most_recent());
// Check if the edit that triggered that edit has been made by this participant.
if let Some(most_recent_edit) = most_recent_edit {
+3 -3
View File
@@ -1560,7 +1560,7 @@ impl Project {
})?;
let agent_server_store = cx.new(|cx| AgentServerStore::collab(cx))?;
let replica_id = response.payload.replica_id as ReplicaId;
let replica_id = ReplicaId::new(response.payload.replica_id as u16);
let project = cx.new(|cx| {
let snippets = SnippetProvider::new(fs.clone(), BTreeSet::from_iter([]), cx);
@@ -1975,9 +1975,9 @@ impl Project {
ProjectClientState::Remote { replica_id, .. } => replica_id,
_ => {
if self.remote_client.is_some() {
1
ReplicaId::REMOTE_SERVER
} else {
0
ReplicaId::LOCAL
}
}
}
+1 -1
View File
@@ -4307,7 +4307,7 @@ async fn test_rescan_and_remote_updates(cx: &mut gpui::TestAppContext) {
let remote = cx.update(|cx| {
Worktree::remote(
0,
1,
ReplicaId::REMOTE_SERVER,
metadata,
project.read(cx).client().into(),
project.read(cx).path_style(cx),
+1 -1
View File
@@ -551,7 +551,7 @@ impl WorktreeStore {
let worktree = cx.update(|cx| {
Worktree::remote(
REMOTE_SERVER_PROJECT_ID,
0,
ReplicaId::REMOTE_SERVER,
proto::WorktreeMetadata {
id: response.worktree_id,
root_name,