Skip to content

Commit c2d1555

Browse files
committed
Auto merge of rust-lang#17842 - mo8it:crossbeam-channel, r=Veykril
internal: Optimize the usage of channel senders Used `Sender` directly instead of a boxed closure. There is no need to use the boxed closure. This also allows the caller to decide to do something other than `unwrap` (not a fan of it BTW).
2 parents 757b0a5 + f7c4716 commit c2d1555

File tree

13 files changed

+41
-34
lines changed

13 files changed

+41
-34
lines changed

src/tools/rust-analyzer/Cargo.lock

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2317,6 +2317,7 @@ checksum = "49874b5167b65d7193b8aba1567f5c7d93d001cafc34600cee003eda787e483f"
23172317
name = "vfs"
23182318
version = "0.0.0"
23192319
dependencies = [
2320+
"crossbeam-channel",
23202321
"fst",
23212322
"indexmap",
23222323
"nohash-hasher",

src/tools/rust-analyzer/crates/ide-db/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ doctest = false
1414

1515
[dependencies]
1616
cov-mark = "2.0.0-pre.1"
17-
crossbeam-channel = "0.5.5"
17+
crossbeam-channel.workspace = true
1818
tracing.workspace = true
1919
rayon.workspace = true
2020
fst = { version = "0.4.7", default-features = false }

src/tools/rust-analyzer/crates/load-cargo/src/lib.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,8 +64,7 @@ pub fn load_workspace(
6464
let (sender, receiver) = unbounded();
6565
let mut vfs = vfs::Vfs::default();
6666
let mut loader = {
67-
let loader =
68-
vfs_notify::NotifyHandle::spawn(Box::new(move |msg| sender.send(msg).unwrap()));
67+
let loader = vfs_notify::NotifyHandle::spawn(sender);
6968
Box::new(loader)
7069
};
7170

src/tools/rust-analyzer/crates/rust-analyzer/Cargo.toml

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ path = "src/bin/main.rs"
2121

2222
[dependencies]
2323
anyhow.workspace = true
24-
crossbeam-channel = "0.5.5"
24+
crossbeam-channel.workspace = true
2525
dirs = "5.0.1"
2626
dissimilar.workspace = true
2727
itertools.workspace = true
@@ -90,13 +90,13 @@ jemalloc = ["jemallocator", "profile/jemalloc"]
9090
force-always-assert = ["always-assert/force"]
9191
sysroot-abi = []
9292
in-rust-tree = [
93-
"sysroot-abi",
94-
"syntax/in-rust-tree",
95-
"parser/in-rust-tree",
96-
"hir/in-rust-tree",
97-
"hir-def/in-rust-tree",
98-
"hir-ty/in-rust-tree",
99-
"load-cargo/in-rust-tree",
93+
"sysroot-abi",
94+
"syntax/in-rust-tree",
95+
"parser/in-rust-tree",
96+
"hir/in-rust-tree",
97+
"hir-def/in-rust-tree",
98+
"hir-ty/in-rust-tree",
99+
"load-cargo/in-rust-tree",
100100
]
101101

102102
[lints]

src/tools/rust-analyzer/crates/rust-analyzer/src/flycheck.rs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ pub(crate) struct FlycheckHandle {
109109
impl FlycheckHandle {
110110
pub(crate) fn spawn(
111111
id: usize,
112-
sender: Box<dyn Fn(FlycheckMessage) + Send>,
112+
sender: Sender<FlycheckMessage>,
113113
config: FlycheckConfig,
114114
sysroot_root: Option<AbsPathBuf>,
115115
workspace_root: AbsPathBuf,
@@ -199,7 +199,7 @@ enum StateChange {
199199
struct FlycheckActor {
200200
/// The workspace id of this flycheck instance.
201201
id: usize,
202-
sender: Box<dyn Fn(FlycheckMessage) + Send>,
202+
sender: Sender<FlycheckMessage>,
203203
config: FlycheckConfig,
204204
manifest_path: Option<AbsPathBuf>,
205205
/// Either the workspace root of the workspace we are flychecking,
@@ -235,7 +235,7 @@ pub(crate) const SAVED_FILE_PLACEHOLDER: &str = "$saved_file";
235235
impl FlycheckActor {
236236
fn new(
237237
id: usize,
238-
sender: Box<dyn Fn(FlycheckMessage) + Send>,
238+
sender: Sender<FlycheckMessage>,
239239
config: FlycheckConfig,
240240
sysroot_root: Option<AbsPathBuf>,
241241
workspace_root: AbsPathBuf,
@@ -478,8 +478,9 @@ impl FlycheckActor {
478478
Some(cmd)
479479
}
480480

481+
#[track_caller]
481482
fn send(&self, check_task: FlycheckMessage) {
482-
(self.sender)(check_task);
483+
self.sender.send(check_task).unwrap();
483484
}
484485
}
485486

src/tools/rust-analyzer/crates/rust-analyzer/src/global_state.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -185,8 +185,7 @@ impl GlobalState {
185185
pub(crate) fn new(sender: Sender<lsp_server::Message>, config: Config) -> GlobalState {
186186
let loader = {
187187
let (sender, receiver) = unbounded::<vfs::loader::Message>();
188-
let handle: vfs_notify::NotifyHandle =
189-
vfs::loader::Handle::spawn(Box::new(move |msg| sender.send(msg).unwrap()));
188+
let handle: vfs_notify::NotifyHandle = vfs::loader::Handle::spawn(sender);
190189
let handle = Box::new(handle) as Box<dyn vfs::loader::Handle>;
191190
Handle { handle, receiver }
192191
};
@@ -564,8 +563,9 @@ impl GlobalState {
564563
self.req_queue.incoming.is_completed(&request.id)
565564
}
566565

566+
#[track_caller]
567567
fn send(&self, message: lsp_server::Message) {
568-
self.sender.send(message).unwrap()
568+
self.sender.send(message).unwrap();
569569
}
570570

571571
pub(crate) fn publish_diagnostics(

src/tools/rust-analyzer/crates/rust-analyzer/src/reload.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -758,7 +758,7 @@ impl GlobalState {
758758
self.flycheck = match invocation_strategy {
759759
crate::flycheck::InvocationStrategy::Once => vec![FlycheckHandle::spawn(
760760
0,
761-
Box::new(move |msg| sender.send(msg).unwrap()),
761+
sender,
762762
config,
763763
None,
764764
self.config.root_path().clone(),
@@ -793,10 +793,9 @@ impl GlobalState {
793793
))
794794
})
795795
.map(|(id, (root, manifest_path), sysroot_root)| {
796-
let sender = sender.clone();
797796
FlycheckHandle::spawn(
798797
id,
799-
Box::new(move |msg| sender.send(msg).unwrap()),
798+
sender.clone(),
800799
config.clone(),
801800
sysroot_root,
802801
root.to_path_buf(),

src/tools/rust-analyzer/crates/stdx/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ backtrace = { version = "0.3.67", optional = true }
1717
always-assert = { version = "0.2.0", features = ["tracing"] }
1818
jod-thread = "0.1.2"
1919
libc.workspace = true
20-
crossbeam-channel = "0.5.5"
20+
crossbeam-channel.workspace = true
2121
itertools.workspace = true
2222
# Think twice before adding anything here
2323

src/tools/rust-analyzer/crates/vfs-notify/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ doctest = false
1515
[dependencies]
1616
tracing.workspace = true
1717
walkdir = "2.3.2"
18-
crossbeam-channel = "0.5.5"
18+
crossbeam-channel.workspace = true
1919
notify = "6.1.1"
2020
rayon = "1.10.0"
2121

src/tools/rust-analyzer/crates/vfs-notify/src/lib.rs

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -119,8 +119,7 @@ impl NotifyActor {
119119
self.watched_dir_entries.clear();
120120
self.watched_file_entries.clear();
121121

122-
let send = |msg| (self.sender)(msg);
123-
send(loader::Message::Progress {
122+
self.send(loader::Message::Progress {
124123
n_total,
125124
n_done: LoadingProgress::Started,
126125
config_version,
@@ -130,7 +129,8 @@ impl NotifyActor {
130129
let (entry_tx, entry_rx) = unbounded();
131130
let (watch_tx, watch_rx) = unbounded();
132131
let processed = AtomicUsize::new(0);
133-
config.load.into_par_iter().enumerate().for_each(move |(i, entry)| {
132+
133+
config.load.into_par_iter().enumerate().for_each(|(i, entry)| {
134134
let do_watch = config.watch.contains(&i);
135135
if do_watch {
136136
_ = entry_tx.send(entry.clone());
@@ -140,18 +140,18 @@ impl NotifyActor {
140140
entry,
141141
do_watch,
142142
|file| {
143-
send(loader::Message::Progress {
143+
self.send(loader::Message::Progress {
144144
n_total,
145145
n_done: LoadingProgress::Progress(
146146
processed.load(std::sync::atomic::Ordering::Relaxed),
147147
),
148148
dir: Some(file),
149149
config_version,
150-
})
150+
});
151151
},
152152
);
153-
send(loader::Message::Loaded { files });
154-
send(loader::Message::Progress {
153+
self.send(loader::Message::Loaded { files });
154+
self.send(loader::Message::Progress {
155155
n_total,
156156
n_done: LoadingProgress::Progress(
157157
processed.fetch_add(1, std::sync::atomic::Ordering::AcqRel) + 1,
@@ -160,9 +160,13 @@ impl NotifyActor {
160160
dir: None,
161161
});
162162
});
163+
164+
drop(watch_tx);
163165
for path in watch_rx {
164166
self.watch(&path);
165167
}
168+
169+
drop(entry_tx);
166170
for entry in entry_rx {
167171
match entry {
168172
loader::Entry::Files(files) => {
@@ -173,6 +177,7 @@ impl NotifyActor {
173177
}
174178
}
175179
}
180+
176181
self.send(loader::Message::Progress {
177182
n_total,
178183
n_done: LoadingProgress::Finished,
@@ -316,8 +321,9 @@ impl NotifyActor {
316321
}
317322
}
318323

324+
#[track_caller]
319325
fn send(&self, msg: loader::Message) {
320-
(self.sender)(msg);
326+
self.sender.send(msg).unwrap();
321327
}
322328
}
323329

src/tools/rust-analyzer/crates/vfs/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ tracing.workspace = true
1818
fst = "0.4.7"
1919
indexmap.workspace = true
2020
nohash-hasher.workspace = true
21+
crossbeam-channel.workspace = true
2122

2223
paths.workspace = true
2324
stdx.workspace = true

src/tools/rust-analyzer/crates/vfs/src/loader.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ pub enum Message {
7272
}
7373

7474
/// Type that will receive [`Messages`](Message) from a [`Handle`].
75-
pub type Sender = Box<dyn Fn(Message) + Send + Sync>;
75+
pub type Sender = crossbeam_channel::Sender<Message>;
7676

7777
/// Interface for reading and watching files.
7878
pub trait Handle: fmt::Debug {

src/tools/rust-analyzer/lib/lsp-server/Cargo.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,11 @@ edition = "2021"
1010
log = "0.4.17"
1111
serde_json = "1.0.108"
1212
serde = { version = "1.0.192", features = ["derive"] }
13-
crossbeam-channel = "0.5.8"
13+
crossbeam-channel.workspace = true
1414

1515
[dev-dependencies]
1616
lsp-types = "=0.95"
1717
ctrlc = "3.4.1"
1818

1919
[lints]
20-
workspace = true
20+
workspace = true

0 commit comments

Comments
 (0)