Skip to content

Commit 363db81

Browse files
authored
Merge pull request #482 from rustcoreutils/updates
HashMap and Vec micro-opts
2 parents 0a79367 + 54ed1aa commit 363db81

File tree

10 files changed

+40
-23
lines changed

10 files changed

+40
-23
lines changed

awk/interpreter/mod.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ fn strtod(s: &str) -> f64 {
6161
}
6262

6363
fn gather_values(stack: &mut Stack, count: u16) -> Result<Vec<AwkValue>, String> {
64-
let mut values = Vec::new();
64+
let mut values = Vec::with_capacity(count as usize);
6565
for _ in 0..count {
6666
values.push(stack.pop_scalar_value()?);
6767
}
@@ -73,7 +73,7 @@ fn print_to_string(
7373
argc: u16,
7474
global_env: &GlobalEnv,
7575
) -> Result<AwkString, String> {
76-
let mut values = Vec::new();
76+
let mut values = Vec::with_capacity(argc as usize);
7777
for _ in 0..argc {
7878
values.push(
7979
stack

cc/arch/x86_64/codegen.rs

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1898,7 +1898,7 @@ impl X86_64CodeGen {
18981898
// First pass: determine which args go on stack
18991899
// For variadic calls: variadic INTEGER args go on stack, variadic FLOAT args use XMM
19001900
// For non-variadic calls: overflow args go on stack
1901-
let mut stack_arg_indices = Vec::new();
1901+
let mut stack_arg_indices = Vec::with_capacity(insn.src.len());
19021902
let mut temp_int_idx = 0;
19031903
let mut temp_fp_idx = 0;
19041904

@@ -3734,22 +3734,23 @@ impl X86_64CodeGen {
37343734
// Build operand strings for asm substitution
37353735
// For constraints requiring specific registers (a,b,c,d,S,D), we use those registers
37363736
// and emit mov instructions to/from the actual locations.
3737-
let mut operand_regs: Vec<Option<Reg>> = Vec::new();
3738-
let mut operand_mem: Vec<Option<String>> = Vec::new();
3739-
let mut operand_names: Vec<Option<String>> = Vec::new();
3737+
let operand_count = asm_data.outputs.len() + asm_data.inputs.len();
3738+
let mut operand_regs: Vec<Option<Reg>> = Vec::with_capacity(operand_count);
3739+
let mut operand_mem: Vec<Option<String>> = Vec::with_capacity(operand_count);
3740+
let mut operand_names: Vec<Option<String>> = Vec::with_capacity(operand_count);
37403741

37413742
// Track which outputs need to be moved from specific registers after asm
37423743
// (output_idx, specific_reg, actual_loc)
3743-
let mut output_moves: Vec<(usize, Reg, Loc)> = Vec::new();
3744+
let mut output_moves: Vec<(usize, Reg, Loc)> = Vec::with_capacity(asm_data.outputs.len());
37443745

37453746
// Track which inputs need to be moved to specific registers before asm
37463747
// (specific_reg, actual_loc)
3747-
let mut input_moves: Vec<(Reg, Loc)> = Vec::new();
3748+
let mut input_moves: Vec<(Reg, Loc)> = Vec::with_capacity(asm_data.inputs.len());
37483749

37493750
// Track register remaps: if an allocated reg conflicts with reserved, use temp
37503751
// (original_reg, temp_reg, actual_loc for restore)
3751-
let mut remap_setup: Vec<(Reg, Reg, Loc)> = Vec::new();
3752-
let mut remap_restore: Vec<(Reg, Reg, Loc)> = Vec::new();
3752+
let mut remap_setup: Vec<(Reg, Reg, Loc)> = Vec::with_capacity(operand_count);
3753+
let mut remap_restore: Vec<(Reg, Reg, Loc)> = Vec::with_capacity(operand_count);
37533754

37543755
// Track which pseudos have been assigned temp registers (for +r where input/output share pseudo)
37553756
let mut pseudo_to_temp: std::collections::HashMap<PseudoId, Reg> =

cc/ir/linearize.rs

Lines changed: 13 additions & 6 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,
@@ -833,9 +838,11 @@ impl<'a> Linearizer<'a> {
833838
// Add parameters
834839
// For struct/union parameters, we need to copy them to local storage
835840
// so member access works properly
836-
let mut struct_params: Vec<(String, TypeId, PseudoId)> = Vec::new();
841+
let mut struct_params: Vec<(String, TypeId, PseudoId)> =
842+
Vec::with_capacity(func.params.len());
837843
// Complex parameters also need local storage for real/imag access
838-
let mut complex_params: Vec<(String, TypeId, PseudoId)> = Vec::new();
844+
let mut complex_params: Vec<(String, TypeId, PseudoId)> =
845+
Vec::with_capacity(func.params.len());
839846

840847
for (i, param) in func.params.iter().enumerate() {
841848
let name = param

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/cut.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@ fn is_character_boundary(bytes: &[u8]) -> bool {
126126
/// A vector containing the selected bytes from the input line based on the specified ranges.
127127
///
128128
fn cut_bytes(line: &[u8], delim: Option<char>, ranges: &Vec<(i32, i32)>, n: bool) -> Vec<u8> {
129-
let mut result = Vec::new();
129+
let mut result = Vec::with_capacity(line.len());
130130

131131
for (start, end) in ranges {
132132
let mut start = *start as usize;

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)