From e7a6c956efda5fcc442faa3a91fd4d2d3760e549 Mon Sep 17 00:00:00 2001 From: BuildTools Date: Thu, 22 Aug 2024 15:44:09 -0700 Subject: [PATCH] Optimize IntoLinear table generation as detailed by okaneco --- palette/build/lut.rs | 50 ++++++++++++++++++++++++++------------------ 1 file changed, 30 insertions(+), 20 deletions(-) diff --git a/palette/build/lut.rs b/palette/build/lut.rs index 7a93475e..9b6e4074 100644 --- a/palette/build/lut.rs +++ b/palette/build/lut.rs @@ -144,7 +144,8 @@ pub fn build_transfer_fn(writer: &mut File) { } fn gen_into_linear_lut_u8(writer: &mut File, entries: &[LutEntryU8]) { - use std::io::Write; + use std::fmt::Write as _; + use std::io::Write as _; for LutEntryU8 { fn_type, @@ -155,20 +156,24 @@ fn gen_into_linear_lut_u8(writer: &mut File, entries: &[LutEntryU8]) { { let table_size = 1 << 8; let mut table = Vec::new(); - for i in 0..table_size { + // Handle integer floats printing without decimal + table.extend_from_slice(b"\t0.0,\n"); + + let mut float_string = String::new(); + + for i in 1..(table_size - 1) { let encoded = (i as f64) / ((table_size - 1) as f64); let linear = into_linear(encoded); - // Handle integer floats printing without decimal - let float_string = if linear <= 0.0 { - "\t0.0,\n".to_owned() - } else if linear >= 1.0 { - "\t1.0,\n".to_owned() - } else { - format!("\t{linear},\n") - }; + + writeln!(&mut float_string, "\t{linear},").unwrap(); table.extend_from_slice(float_string.as_bytes()); + + float_string.clear(); } + // Handle integer floats printing without decimal + table.extend_from_slice(b"\t1.0,\n"); + let table_name = format!("{fn_type_uppercase}_U8_TO_F64"); writeln!(writer, "const {table_name}: [f64; {table_size}] = [").unwrap(); writer.write_all(&table).unwrap(); @@ -200,7 +205,8 @@ fn gen_into_linear_lut_u8(writer: &mut File, entries: &[LutEntryU8]) { #[cfg(feature = "prophoto_lut")] fn gen_into_linear_lut_u16(writer: &mut File, entries: &[LutEntryU16]) { - use std::io::Write; + use std::fmt::Write as _; + use std::io::Write as _; for LutEntryU16 { fn_type, @@ -211,20 +217,24 @@ fn gen_into_linear_lut_u16(writer: &mut File, entries: &[LutEntryU16]) { { let table_size = 1 << 16; let mut table = Vec::new(); - for i in 0..table_size { + // Handle integer floats printing without decimal + table.extend_from_slice(b"\t0.0,\n"); + + let mut float_string = String::new(); + + for i in 1..(table_size - 1) { let encoded = (i as f64) / ((table_size - 1) as f64); let linear = into_linear(encoded); - // Handle integer floats printing without decimal - let float_string = if linear <= 0.0 { - "\t0.0,\n".to_owned() - } else if linear >= 1.0 { - "\t1.0,\n".to_owned() - } else { - format!("\t{linear},\n") - }; + + writeln!(&mut float_string, "\t{linear},").unwrap(); table.extend_from_slice(float_string.as_bytes()); + + float_string.clear(); } + // Handle integer floats printing without decimal + table.extend_from_slice(b"\t1.0,\n"); + let table_name = format!("{fn_type_uppercase}_U16_TO_F64"); writeln!(writer, "static {table_name}: [f64; {table_size}] = [").unwrap(); writer.write_all(&table).unwrap();