Skip to content

Commit

Permalink
perf(ws on rt_worker): SessionMap: use Vec::swap_remove in `remov…
Browse files Browse the repository at this point in the history
…e` & omit redundant `None` check in `get`, `get_mut`
  • Loading branch information
kanarus committed Nov 17, 2024
1 parent b9267df commit 718f61d
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 18 deletions.
26 changes: 13 additions & 13 deletions ohkami/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ mews = { version = "0.1", optional = true }


[features]
default = ["testing"]
#default = ["testing"]

rt_tokio = ["__rt__", "__rt_native__", "dep:tokio", "tokio/io-util", "tokio/macros", "ohkami_lib/signal", "mews?/tokio"]
rt_async-std = ["__rt__", "__rt_native__", "dep:async-std", "dep:futures-util", "ohkami_lib/signal", "mews?/async-std"]
Expand All @@ -65,15 +65,15 @@ DEBUG = [
"tokio?/rt-multi-thread",
"async-std?/attributes",
]
#default = [
# "nightly",
# "testing",
# "sse",
# "ws",
# #"rt_tokio",
# #"rt_async-std",
# #"rt_smol",
# #"rt_glommio",
# "rt_worker",
# "DEBUG",
#]
default = [
"nightly",
"testing",
"sse",
"ws",
#"rt_tokio",
#"rt_async-std",
#"rt_smol",
#"rt_glommio",
"rt_worker",
"DEBUG",
]
8 changes: 3 additions & 5 deletions ohkami/src/ws/worker.rs
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,7 @@ impl<S> SessionMap<S> {
pub fn remove(&mut self, ws: &worker::WebSocket) -> Option<S> {
ws.close::<&str>(None, None).ok();
if let Some(index) = self.index_of(ws) {
Some(self.0.remove(index).1)
Some(self.0.swap_remove(index).1)
} else {
None
}
Expand All @@ -201,13 +201,11 @@ impl<S> SessionMap<S> {

pub fn get(&self, ws: &worker::WebSocket) -> Option<&S> {
let index = self.index_of(&ws)?;
let (_, session) = self.0.get(index)?;
Some(session)
Some(&self.0[index].1)
}
pub fn get_mut(&mut self, ws: &worker::WebSocket) -> Option<&mut S> {
let index = self.index_of(&ws)?;
let (_, session) = self.0.get_mut(index)?;
Some(session)
Some(&mut self.0[index].1)
}

pub fn iter(&self) -> impl Iterator<Item = &(worker::WebSocket, S)> {
Expand Down

0 comments on commit 718f61d

Please sign in to comment.