Skip to content

Commit

Permalink
luau: perf tweaks
Browse files Browse the repository at this point in the history
- amortize allocation of computed_value in hot loop
- directly pass flag_remap bool to map_computedvalue fn instead of passing the whole arg struct
  • Loading branch information
jqnatividad committed Oct 1, 2023
1 parent 2a35be5 commit c71cd16
Showing 1 changed file with 20 additions and 6 deletions.
26 changes: 20 additions & 6 deletions src/cmd/luau.rs
Original file line number Diff line number Diff line change
Expand Up @@ -688,6 +688,8 @@ fn sequential_mode(
LUAU_STAGE.store(Stage::Main as i8, Ordering::Relaxed);
info!("Executing MAIN script.");

let mut computed_value;

// main loop
// without an index, we stream the CSV in sequential order
'main: while rdr.read_record(&mut record)? {
Expand Down Expand Up @@ -721,7 +723,7 @@ fn sequential_mode(
}
}

let computed_value: Value = match luau
computed_value = match luau
.load(&main_bytecode)
.set_mode(mlua::ChunkMode::Binary)
.eval()
Expand Down Expand Up @@ -749,7 +751,12 @@ fn sequential_mode(
}

if args.cmd_map {
map_computedvalue(computed_value, &mut record, args, new_column_count)?;
map_computedvalue(
computed_value,
&mut record,
args.flag_remap,
new_column_count,
)?;

// check if the script is trying to insert a record with
// qsv_insertrecord(). We do this by checking if the global
Expand Down Expand Up @@ -1027,6 +1034,8 @@ fn random_access_mode(
row_count - 1
);

let mut computed_value;

// main loop - here we use an indexed file reader to implement random access mode,
// seeking to the next record to read by looking at _INDEX special var
'main: while idx_file.read_record(&mut record)? {
Expand Down Expand Up @@ -1058,7 +1067,7 @@ fn random_access_mode(
}
}

let computed_value: Value = match luau
computed_value = match luau
.load(&main_bytecode)
.set_mode(mlua::ChunkMode::Binary)
.eval()
Expand Down Expand Up @@ -1086,7 +1095,12 @@ fn random_access_mode(
}

if args.cmd_map {
map_computedvalue(computed_value, &mut record, args, new_column_count)?;
map_computedvalue(
computed_value,
&mut record,
args.flag_remap,
new_column_count,
)?;

// check if the MAIN script is trying to insert a record
insertrecord_table = luau
Expand Down Expand Up @@ -1213,7 +1227,7 @@ fn random_access_mode(
fn map_computedvalue(
computed_value: Value,
record: &mut csv::StringRecord,
args: &Args,
flag_remap: bool,
new_column_count: u8,
) -> Result<(), CliError> {
match computed_value {
Expand All @@ -1235,7 +1249,7 @@ fn map_computedvalue(
record.push_field("");
},
Value::Table(table) => {
if args.flag_remap {
if flag_remap {
// we're in remap mode, so we clear the record
// and only write the new columns to output
record.clear();
Expand Down

0 comments on commit c71cd16

Please sign in to comment.