Skip to content

Commit

Permalink
apply & applydp: use smallvec for operations_vec which is normall…
Browse files Browse the repository at this point in the history
…y very small

so we can store it inline rather than allocating on the heap
  • Loading branch information
jqnatividad committed Oct 19, 2024
1 parent c9baadc commit 0450193
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 8 deletions.
9 changes: 5 additions & 4 deletions src/cmd/apply.rs
Original file line number Diff line number Diff line change
Expand Up @@ -321,6 +321,7 @@ use rayon::{
};
use regex::Regex;
use serde::Deserialize;
use smallvec::SmallVec;
use strsim::{
damerau_levenshtein, hamming, jaro_winkler, normalized_damerau_levenshtein, osa_distance,
sorensen_dice,
Expand Down Expand Up @@ -508,7 +509,7 @@ pub fn run(argv: &[&str]) -> CliResult<()> {
String::new()
};

let mut ops_vec: Vec<Operations> = Vec::new();
let mut ops_vec = SmallVec::<[Operations; 4]>::new();

let apply_cmd = if args.cmd_operations {
match validate_operations(
Expand Down Expand Up @@ -727,7 +728,7 @@ fn validate_operations(
flag_replacement: &str,
flag_new_column: Option<&String>,
flag_formatstr: &str,
) -> Result<Vec<Operations>, CliError> {
) -> Result<SmallVec<[Operations; 4]>, CliError> {
let mut censor_invokes = 0_u8;
let mut copy_invokes = 0_u8;
let mut eudex_invokes = 0_u8;
Expand All @@ -738,7 +739,7 @@ fn validate_operations(
let mut strip_invokes = 0_u8;
let mut whatlang_invokes = 0_u8;

let mut ops_vec: Vec<Operations> = Vec::with_capacity(operations.len());
let mut ops_vec = SmallVec::with_capacity(operations.len());

for op in operations {
let Ok(operation) = Operations::from_str(op) else {
Expand Down Expand Up @@ -960,7 +961,7 @@ fn validate_operations(

#[inline]
fn apply_operations(
ops_vec: &Vec<Operations>,
ops_vec: &SmallVec<[Operations; 4]>,
cell: &mut String,
comparand: &str,
replacement: &str,
Expand Down
9 changes: 5 additions & 4 deletions src/cmd/applydp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,7 @@ use rayon::{
};
use regex::Regex;
use serde::Deserialize;
use smallvec::SmallVec;
use strum_macros::EnumString;

use crate::{
Expand Down Expand Up @@ -331,7 +332,7 @@ pub fn run(argv: &[&str]) -> CliResult<()> {
EmptyReplace,
}

let mut ops_vec: Vec<Operations> = Vec::new();
let mut ops_vec = SmallVec::<[Operations; 4]>::new();

let applydp_cmd = if args.cmd_operations {
match validate_operations(
Expand Down Expand Up @@ -496,13 +497,13 @@ fn validate_operations(
flag_replacement: &str,
flag_new_column: &Option<String>,
flag_formatstr: &str,
) -> Result<Vec<Operations>, CliError> {
) -> Result<SmallVec<[Operations; 4]>, CliError> {
let mut copy_invokes = 0_u8;
let mut regex_replace_invokes = 0_u8;
let mut replace_invokes = 0_u8;
let mut strip_invokes = 0_u8;

let mut ops_vec: Vec<Operations> = Vec::with_capacity(operations.len());
let mut ops_vec = SmallVec::with_capacity(operations.len());

for op in operations {
let Ok(operation) = Operations::from_str(op) else {
Expand Down Expand Up @@ -587,7 +588,7 @@ fn validate_operations(

#[inline]
fn applydp_operations(
ops_vec: &Vec<Operations>,
ops_vec: &SmallVec<[Operations; 4]>,
cell: &mut String,
comparand: &str,
replacement: &str,
Expand Down

0 comments on commit 0450193

Please sign in to comment.