Skip to content

Commit e34206d

Browse files
committed
refactor: use with_capacity to improve allocate performance
1 parent 9fe1545 commit e34206d

5 files changed

Lines changed: 6 additions & 6 deletions

File tree

engine-base/src/executor.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ impl Executor {
5151
info!("Searching {song} with engines {engines:?}");
5252
self.validate_engines(engines)?;
5353

54-
let mut futures = Vec::new();
54+
let mut futures = Vec::with_capacity(engines.len());
5555

5656
for engine_id in engines {
5757
let engine = self.resolve_engine(engine_id)?;
@@ -107,7 +107,7 @@ impl Executor {
107107
/// Validate engines to check if the engines specified are all registered.
108108
fn validate_engines(&self, engines: &[EngineId]) -> ExecutorResult<()> {
109109
debug!("Validating if all the engines ({engines:?}) are registered…");
110-
let mut missing_engines = Vec::new();
110+
let mut missing_engines = Vec::with_capacity(engines.len());
111111

112112
for engine_id in engines {
113113
trace!("Validating: {engine_id}");

engines/kugou/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ impl From<KugouSongContext> for HashMap<String, String> {
6464
fn from(ctx: KugouSongContext) -> Self {
6565
debug!("Constructing the context HashMap from KugouSongContext…");
6666

67-
let mut map = HashMap::new();
67+
let mut map = HashMap::with_capacity(3);
6868

6969
[
7070
(KugouFormat::Hash, ctx.id),

engines/migu/src/types.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -239,7 +239,7 @@ mod tests {
239239
.into_iter();
240240

241241
let expected = {
242-
let mut hm = HashMap::new();
242+
let mut hm = HashMap::with_capacity(3);
243243

244244
hm.insert(
245245
"LQ".to_string(),

engines/ytdl/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -167,7 +167,7 @@ mod tests {
167167
let config = ConfigManager::new(config);
168168
assert_eq!(decide_ytdl_exe(&Some(config)), DEFAULT_EXECUTABLE);
169169

170-
let mut config = HashMap::new();
170+
let mut config = HashMap::with_capacity(1);
171171
config.insert(Cow::Borrowed("ytdl:exe"), "youtube-dl".to_string());
172172
let config = ConfigManager::new(config);
173173
assert_eq!(decide_ytdl_exe(&Some(config)), "youtube-dl");

request/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,7 @@ mod tests {
170170
use std::collections::HashMap;
171171

172172
static TRANSLATE_MAP: Lazy<HashMap<&str, &str>> = Lazy::new(|| {
173-
let mut map = HashMap::new();
173+
let mut map = HashMap::with_capacity(2);
174174
map.insert("www.cloudflare.com", "1.1.1.1");
175175
map.insert("www.google.com", "www.bing.com");
176176

0 commit comments

Comments
 (0)