From d2f593570db4ddc4bfc7c442dde2fcc8d93ded8a Mon Sep 17 00:00:00 2001 From: Ritchie Vink Date: Fri, 10 Jun 2022 21:49:50 +0200 Subject: [PATCH] Improved performance in lexical write (~5%) (#1067) --- src/util/lexical.rs | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/src/util/lexical.rs b/src/util/lexical.rs index 970c972e0af..047986cbbed 100644 --- a/src/util/lexical.rs +++ b/src/util/lexical.rs @@ -19,8 +19,19 @@ pub fn lexical_to_bytes_mut(n: N, buf: &mut Vec) // Length of buf is set as written length afterwards. lexical_core // creates a valid string, so doesn't need to be checked. let slice = std::slice::from_raw_parts_mut(buf.as_mut_ptr(), buf.capacity()); - let len = lexical_core::write(n, slice).len(); - buf.set_len(len); + + // Safety: + // Omits an unneeded bound check as we just ensured that we reserved `N::FORMATTED_SIZE_DECIMAL` + #[cfg(debug_assertions)] + { + let len = lexical_core::write(n, slice).len(); + buf.set_len(len); + } + #[cfg(not(debug_assertions))] + { + let len = lexical_core::write_unchecked(n, slice).len(); + buf.set_len(len); + } } }