Skip to content

Commit c5fbccf

Browse files
committed
HashMap micro-opts
1 parent 0a79367 commit c5fbccf

File tree

7 files changed

+24
-10
lines changed

7 files changed

+24
-10
lines changed

cc/ir/linearize.rs

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,11 @@ use crate::target::Target;
2626
use crate::types::{MemberInfo, TypeId, TypeKind, TypeModifiers, TypeTable};
2727
use std::collections::HashMap;
2828

29+
/// Default capacity for variable mapping HashMaps during IR linearization
30+
const DEFAULT_VAR_MAP_CAPACITY: usize = 64;
31+
/// Default capacity for label/static HashMaps (typically smaller)
32+
const DEFAULT_LABEL_MAP_CAPACITY: usize = 16;
33+
2934
/// Information about a local variable
3035
#[derive(Clone)]
3136
struct LocalVarInfo {
@@ -127,9 +132,9 @@ impl<'a> Linearizer<'a> {
127132
current_bb: None,
128133
next_pseudo: 0,
129134
next_bb: 0,
130-
var_map: HashMap::new(),
131-
locals: HashMap::new(),
132-
label_map: HashMap::new(),
135+
var_map: HashMap::with_capacity(DEFAULT_VAR_MAP_CAPACITY),
136+
locals: HashMap::with_capacity(DEFAULT_VAR_MAP_CAPACITY),
137+
label_map: HashMap::with_capacity(DEFAULT_LABEL_MAP_CAPACITY),
133138
break_targets: Vec::new(),
134139
continue_targets: Vec::new(),
135140
run_ssa: true, // Enable SSA conversion by default
@@ -142,7 +147,7 @@ impl<'a> Linearizer<'a> {
142147
current_func_name: String::new(),
143148
static_local_counter: 0,
144149
compound_literal_counter: 0,
145-
static_locals: HashMap::new(),
150+
static_locals: HashMap::with_capacity(DEFAULT_LABEL_MAP_CAPACITY),
146151
current_pos: None,
147152
target,
148153
current_func_is_non_static_inline: false,

cc/symbol.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,9 @@ use crate::strings::StringId;
1414
use crate::types::TypeId;
1515
use std::collections::HashMap;
1616

17+
/// Default capacity for symbol table name lookup HashMap
18+
const DEFAULT_SYMBOL_MAP_CAPACITY: usize = 256;
19+
1720
// ============================================================================
1821
// Symbol ID
1922
// ============================================================================
@@ -234,7 +237,7 @@ impl SymbolTable {
234237
scopes: Vec::new(),
235238
current_scope: 0,
236239
scope_depth: 0,
237-
name_map: HashMap::new(),
240+
name_map: HashMap::with_capacity(DEFAULT_SYMBOL_MAP_CAPACITY),
238241
};
239242
// Create the global scope
240243
table.scopes.push(Scope::new(None));

cc/token/preprocess.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,9 @@ use crate::diag;
2323
use crate::os;
2424
use crate::target::Target;
2525

26+
/// Default capacity for macro HashMap (covers predefined macros + typical program macros)
27+
const DEFAULT_MACRO_CAPACITY: usize = 32;
28+
2629
// ============================================================================
2730
// Macro Definition
2831
// ============================================================================
@@ -390,7 +393,7 @@ impl<'a> Preprocessor<'a> {
390393

391394
let mut pp = Self {
392395
target,
393-
macros: HashMap::new(),
396+
macros: HashMap::with_capacity(DEFAULT_MACRO_CAPACITY),
394397
cond_stack: Vec::new(),
395398
system_include_paths: Vec::new(),
396399
quote_include_paths: Vec::new(),

sh/shell/environment.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,7 @@ impl Environment {
174174
}
175175

176176
pub fn exported(&self) -> impl Iterator<Item = (&String, &String)> {
177-
let mut exported = HashMap::new();
177+
let mut exported = HashMap::with_capacity(self.global_scope.len());
178178
for (name, var) in &self.global_scope {
179179
if var.export {
180180
if let Some(value) = &var.value {

sh/shell/mod.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,9 @@ use std::rc::Rc;
4444
use std::time::Duration;
4545
use std::{env, io};
4646

47+
/// Default capacity for cached command locations HashMap
48+
const DEFAULT_COMMAND_CACHE_CAPACITY: usize = 64;
49+
4750
pub mod environment;
4851
pub mod history;
4952
pub mod opened_files;
@@ -1058,7 +1061,7 @@ impl Default for Shell {
10581061
background_jobs: JobManager::default(),
10591062
history: History::new(32767),
10601063
umask: !0o022 & 0o777,
1061-
saved_command_locations: HashMap::new(),
1064+
saved_command_locations: HashMap::with_capacity(DEFAULT_COMMAND_CACHE_CAPACITY),
10621065
is_subshell: false,
10631066
last_pipeline_command: String::new(),
10641067
terminal: Terminal::default(),

text/sed.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1333,7 +1333,7 @@ fn print_multiline_binary(line: &str) {
13331333

13341334
/// Find first label in [`Script`] that has duplicates
13351335
fn find_first_repeated_label(vec: Vec<String>) -> Option<String> {
1336-
let mut counts = HashMap::new();
1336+
let mut counts = HashMap::with_capacity(vec.len());
13371337
for item in &vec {
13381338
*counts.entry(item).or_insert(0) += 1;
13391339
}

tree/mv.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -302,7 +302,7 @@ fn move_files(cfg: &MvConfig, sources: &[PathBuf], target: &Path) -> Option<()>
302302
let mut created_files = HashSet::new();
303303

304304
// inode of source -> target path
305-
let mut inode_map = HashMap::new();
305+
let mut inode_map = HashMap::with_capacity(sources.len());
306306

307307
// Postpone deletion when moving across filesystems because it would
308308
// otherwise error when copying dangling hard links

0 commit comments

Comments
 (0)