Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove duplicate lookup in get_or_insert_inner #273

Merged
merged 1 commit into from
Feb 23, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 24 additions & 26 deletions src/manager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -158,32 +158,30 @@ impl Songbird {
}

fn get_or_insert_inner(&self, guild_id: GuildId) -> Arc<Mutex<Call>> {
self.get(guild_id).unwrap_or_else(|| {
self.calls
.entry(guild_id)
.or_insert_with(|| {
let info = self
.client_data
.get()
.expect("Manager has not been initialised");

let shard = shard_id(guild_id.0.get(), info.shard_count);
let shard_handle = self
.sharder
.get_shard(shard)
.expect("Failed to get shard handle: shard_count incorrect?");

let call = Call::from_config(
guild_id,
shard_handle,
info.user_id,
self.config.read().clone(),
);

Arc::new(Mutex::new(call))
})
.clone()
})
self.calls
.entry(guild_id)
.or_insert_with(|| {
let info = self
.client_data
.get()
.expect("Manager has not been initialised");

let shard = shard_id(guild_id.0.get(), info.shard_count);
let shard_handle = self
.sharder
.get_shard(shard)
.expect("Failed to get shard handle: shard_count incorrect?");

let call = Call::from_config(
guild_id,
shard_handle,
info.user_id,
self.config.read().clone(),
);

Arc::new(Mutex::new(call))
})
.clone()
}

/// Creates an iterator for all [`Call`]s currently managed.
Expand Down