Skip to content

Commit 6f4a9d1

Browse files
committed
Preallocate to win a few miliseconds
1 parent 8d62f40 commit 6f4a9d1

File tree

1 file changed

+6
-6
lines changed
  • crates/lox_core/src/ephemeris/daf_spk

1 file changed

+6
-6
lines changed

crates/lox_core/src/ephemeris/daf_spk/parser.rs

+6-6
Original file line numberDiff line numberDiff line change
@@ -221,16 +221,16 @@ pub fn parse_daf_comment_area(
221221
input: &[u8],
222222
comment_areas_count: u32,
223223
) -> nom::IResult<&[u8], String> {
224-
// This function is definetely not optimal as there are several allocations at
225-
// different points but this is not a hot data path.
226-
let mut comment_area = String::new();
224+
let record_size = 1000;
225+
226+
let mut comment_area = String::with_capacity((comment_areas_count * record_size) as usize);
227227

228228
let mut input_cursor = input;
229229
for _ in 0..comment_areas_count {
230230
let comment_areas;
231231
(input_cursor, comment_areas) = nb::take(RECORD_SIZE)(input_cursor)?;
232232

233-
let (_, comment_record_content) = nb::take(1000u32)(comment_areas)?;
233+
let (_, comment_record_content) = nb::take(record_size)(comment_areas)?;
234234

235235
let comment_record_content = match nb::take_until("\x04")(comment_record_content) {
236236
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(
267267

268268
let nc = 8 * (nd + (ni + 1) / 2);
269269

270-
let mut summaries = Vec::new();
271-
272270
// 1. The record number of the next summary record in the file. (Zero if this is
273271
// the final summary record.)
274272
let (summary_record_input, next) = nn::f64(endianness)(summary_record_input)?;
@@ -282,6 +280,8 @@ pub fn parse_daf_summary_and_name_record_pair(
282280
let (mut summary_record_input, nsum) = nn::f64(endianness)(summary_record_input)?;
283281
let nsum = nsum as u32;
284282

283+
let mut summaries = Vec::with_capacity(nsum as usize);
284+
285285
for _ in 0..nsum {
286286
let double_precision_components;
287287
(summary_record_input, double_precision_components) =

0 commit comments

Comments
 (0)