From a5019a5745e5a4cd99b52b23f41aca85b3e5364f Mon Sep 17 00:00:00 2001 From: Nik Revenco Date: Tue, 21 Oct 2025 00:52:21 +0100 Subject: [PATCH] perf: pre-allocate arrays and maps --- src/file/format/ini.rs | 2 +- src/file/format/json.rs | 4 ++-- src/file/format/toml.rs | 4 ++-- src/file/format/yaml.rs | 4 ++-- 4 files changed, 7 insertions(+), 7 deletions(-) diff --git a/src/file/format/ini.rs b/src/file/format/ini.rs index 7394d6dd..7b3233ce 100644 --- a/src/file/format/ini.rs +++ b/src/file/format/ini.rs @@ -14,7 +14,7 @@ pub(crate) fn parse( for (sec, prop) in i.iter() { match sec { Some(sec) => { - let mut sec_map: Map = Map::new(); + let mut sec_map: Map = Map::with_capacity(prop.len()); for (k, v) in prop.iter() { sec_map.insert( k.to_owned(), diff --git a/src/file/format/json.rs b/src/file/format/json.rs index 9100b662..1ab0f3b3 100644 --- a/src/file/format/json.rs +++ b/src/file/format/json.rs @@ -30,7 +30,7 @@ fn from_json_value(uri: Option<&String>, value: &serde_json::Value) -> Value { serde_json::Value::Bool(value) => Value::new(uri, ValueKind::Boolean(value)), serde_json::Value::Object(ref table) => { - let mut m = Map::new(); + let mut m = Map::with_capacity(table.len()); for (key, value) in table { m.insert(key.clone(), from_json_value(uri, value)); @@ -40,7 +40,7 @@ fn from_json_value(uri: Option<&String>, value: &serde_json::Value) -> Value { } serde_json::Value::Array(ref array) => { - let mut l = Vec::new(); + let mut l = Vec::with_capacity(array.len()); for value in array { l.push(from_json_value(uri, value)); diff --git a/src/file/format/toml.rs b/src/file/format/toml.rs index 454a057c..e6ceacdf 100644 --- a/src/file/format/toml.rs +++ b/src/file/format/toml.rs @@ -13,7 +13,7 @@ pub(crate) fn parse( } fn from_toml_table(uri: Option<&String>, table: toml::Table) -> Map { - let mut m = Map::new(); + let mut m = Map::with_capacity(table.len()); for (key, value) in table { m.insert(key, from_toml_value(uri, value)); @@ -35,7 +35,7 @@ fn from_toml_value(uri: Option<&String>, value: toml::Value) -> Value { } toml::Value::Array(array) => { - let mut l = Vec::new(); + let mut l = Vec::with_capacity(array.len()); for value in array { l.push(from_toml_value(uri, value)); diff --git a/src/file/format/yaml.rs b/src/file/format/yaml.rs index f74c2d66..e827ab5d 100644 --- a/src/file/format/yaml.rs +++ b/src/file/format/yaml.rs @@ -45,7 +45,7 @@ fn from_yaml_value( yaml::Yaml::Integer(value) => Ok(Value::new(uri, ValueKind::I64(value))), yaml::Yaml::Boolean(value) => Ok(Value::new(uri, ValueKind::Boolean(value))), yaml::Yaml::Hash(ref table) => { - let mut m = Map::new(); + let mut m = Map::with_capacity(table.len()); for (key, value) in table { match key { yaml::Yaml::String(k) => m.insert(k.to_owned(), from_yaml_value(uri, value)?), @@ -58,7 +58,7 @@ fn from_yaml_value( Ok(Value::new(uri, ValueKind::Table(m))) } yaml::Yaml::Array(ref array) => { - let mut l = Vec::new(); + let mut l = Vec::with_capacity(array.len()); for value in array { l.push(from_yaml_value(uri, value)?);