Skip to content

Commit 65620f0

Browse files
perf: Optimize IndexMap instantiation by pre-allocating capacity
Co-authored-by: Tcode-Motion <188012755+Tcode-Motion@users.noreply.github.com>
1 parent a46a643 commit 65620f0

3 files changed

Lines changed: 12 additions & 6 deletions

File tree

‎stdlib/src/async_mod.rs‎

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,8 @@ impl StdlibRegistry {
1717
arity: 1,
1818
callback: |_ctx, args| {
1919
let callback = args[0].clone();
20-
let mut fut_map = IndexMap::new();
20+
// ⚡ Bolt Performance Optimization: Pre-allocated IndexMap capacity to avoid re-hashing during insertion.
21+
let mut fut_map = IndexMap::with_capacity(2);
2122
fut_map.insert(
2223
"state".to_string(),
2324
RuntimeValue::Str("pending".to_string()),
@@ -68,7 +69,8 @@ impl StdlibRegistry {
6869
name: "make_future".to_string(),
6970
arity: 0,
7071
callback: |_ctx, _args| {
71-
let mut fut_map = IndexMap::new();
72+
// ⚡ Bolt Performance Optimization: Pre-allocated IndexMap capacity to avoid re-hashing during insertion.
73+
let mut fut_map = IndexMap::with_capacity(2);
7274
fut_map.insert(
7375
"state".to_string(),
7476
RuntimeValue::Str("pending".to_string()),
@@ -106,7 +108,8 @@ impl StdlibRegistry {
106108
let (tx, rx) = std::sync::mpsc::channel::<RuntimeValue>();
107109
let tx_id = ctx.resources.borrow_mut().insert(tx);
108110
let rx_id = ctx.resources.borrow_mut().insert(rx);
109-
let mut map = IndexMap::new();
111+
// ⚡ Bolt Performance Optimization: Pre-allocated IndexMap capacity to avoid re-hashing during insertion.
112+
let mut map = IndexMap::with_capacity(2);
110113
map.insert("_tx_handle".to_string(), RuntimeValue::Int(tx_id as i64));
111114
map.insert("_rx_handle".to_string(), RuntimeValue::Int(rx_id as i64));
112115
Ok(RuntimeValue::Map {

‎stdlib/src/net_handler.rs‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,8 @@ pub fn tcp_listen_fn(
2020
)
2121
})?;
2222
let handle_id = ctx.resources.borrow_mut().insert(listener);
23-
let mut listener_map = IndexMap::new();
23+
// ⚡ Bolt Performance Optimization: Pre-allocated IndexMap capacity to avoid re-hashing during insertion.
24+
let mut listener_map = IndexMap::with_capacity(2);
2425
listener_map.insert("port".to_string(), RuntimeValue::Int(port));
2526
listener_map.insert("_handle".to_string(), RuntimeValue::Int(handle_id as i64));
2627
Ok(RuntimeValue::Map {
@@ -43,7 +44,8 @@ pub fn tcp_connect_fn(
4344
)
4445
})?;
4546
let handle_id = ctx.resources.borrow_mut().insert(stream);
46-
let mut stream_map = IndexMap::new();
47+
// ⚡ Bolt Performance Optimization: Pre-allocated IndexMap capacity to avoid re-hashing during insertion.
48+
let mut stream_map = IndexMap::with_capacity(3);
4749
stream_map.insert("ip".to_string(), RuntimeValue::Str(ip));
4850
stream_map.insert("port".to_string(), RuntimeValue::Int(port));
4951
stream_map.insert("_handle".to_string(), RuntimeValue::Int(handle_id as i64));

‎stdlib/src/process.rs‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,8 @@ fn process_run(
6262
)
6363
})?;
6464

65-
let mut res_map = IndexMap::new();
65+
// ⚡ Bolt Performance Optimization: Pre-allocated IndexMap capacity to avoid re-hashing during insertion.
66+
let mut res_map = IndexMap::with_capacity(3);
6667
res_map.insert(
6768
"stdout".to_string(),
6869
RuntimeValue::Str(String::from_utf8_lossy(&output.stdout).to_string()),

0 commit comments

Comments
 (0)