From e6594a4e02f9e1611ab347cb221e438c7558bfbb Mon Sep 17 00:00:00 2001 From: Andrei Zisu Date: Sat, 11 Nov 2023 13:59:40 +0100 Subject: [PATCH] Preallocate to win a few miliseconds --- crates/lox_core/src/ephemeris/daf_spk/parser.rs | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/crates/lox_core/src/ephemeris/daf_spk/parser.rs b/crates/lox_core/src/ephemeris/daf_spk/parser.rs index 1667280d..93d65137 100644 --- a/crates/lox_core/src/ephemeris/daf_spk/parser.rs +++ b/crates/lox_core/src/ephemeris/daf_spk/parser.rs @@ -221,16 +221,16 @@ pub fn parse_daf_comment_area( input: &[u8], comment_areas_count: u32, ) -> nom::IResult<&[u8], String> { - // This function is definetely not optimal as there are several allocations at - // different points but this is not a hot data path. - let mut comment_area = String::new(); + let record_size = 1000; + + let mut comment_area = String::with_capacity((comment_areas_count * record_size) as usize); let mut input_cursor = input; for _ in 0..comment_areas_count { let comment_areas; (input_cursor, comment_areas) = nb::take(RECORD_SIZE)(input_cursor)?; - let (_, comment_record_content) = nb::take(1000u32)(comment_areas)?; + let (_, comment_record_content) = nb::take(record_size)(comment_areas)?; let comment_record_content = match nb::take_until("\x04")(comment_record_content) { Ok((_, content_to_end_of_transmission_char)) => content_to_end_of_transmission_char, @@ -267,8 +267,6 @@ pub fn parse_daf_summary_and_name_record_pair( let nc = 8 * (nd + (ni + 1) / 2); - let mut summaries = Vec::new(); - // 1. The record number of the next summary record in the file. (Zero if this is // the final summary record.) let (summary_record_input, next) = nn::f64(endianness)(summary_record_input)?; @@ -282,6 +280,8 @@ pub fn parse_daf_summary_and_name_record_pair( let (mut summary_record_input, nsum) = nn::f64(endianness)(summary_record_input)?; let nsum = nsum as u32; + let mut summaries = Vec::with_capacity(nsum as usize); + for _ in 0..nsum { let double_precision_components; (summary_record_input, double_precision_components) =