Skip to content

Commit 54ed1aa

Browse files
committed
Vec micro-opts
Summary 4 files modified with Vec::with_capacity() optimizations: | File | Change | Capacity | |-------------------------------------|-------------------------------|--------------------------------------------| | awk/interpreter/mod.rs:64 | gather_values | count as usize | | awk/interpreter/mod.rs:76 | print_to_string | argc as usize | | text/cut.rs:129 | cut_bytes | line.len() | | cc/arch/x86_64/codegen.rs:1901 | stack_arg_indices | insn.src.len() | | cc/ir/linearize.rs:841-845 | struct_params, complex_params | func.params.len() | | cc/arch/x86_64/codegen.rs:3737-3753 | 7 inline asm arrays | operand_count, outputs.len(), inputs.len() |
1 parent c5fbccf commit 54ed1aa

File tree

4 files changed

+16
-13
lines changed

4 files changed

+16
-13
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: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -838,9 +838,11 @@ impl<'a> Linearizer<'a> {
838838
// Add parameters
839839
// For struct/union parameters, we need to copy them to local storage
840840
// so member access works properly
841-
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());
842843
// Complex parameters also need local storage for real/imag access
843-
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());
844846

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

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;

0 commit comments

Comments
 (0)