From b3d6de4d89acf513d06854288b0ba83d1a0c724f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Emilio=20Cobos=20=C3=81lvarez?= Date: Fri, 11 Dec 2020 03:03:52 +0100 Subject: [PATCH 1/2] Don't check dtoa output for utf-8 validity in release builds. I've seen this bit of code profiling microbenchmarks, and I believe it's a safe an easy optimization to make. --- src/lib.rs | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/src/lib.rs b/src/lib.rs index 5270377..c5c23ee 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -62,7 +62,12 @@ fn write_with_prec(dest: &mut W, value: V, prec: usize) let mut buf = [b'\0'; BUFFER_SIZE + 8]; let len = dtoa::write(&mut buf[1..], value).map_err(|_| fmt::Error)?; let (result, notation) = restrict_prec(&mut buf[0..len + 1], prec); - dest.write_str(str::from_utf8(result).unwrap())?; + dest.write_str(if cfg!(debug_assertions) { + str::from_utf8(result).unwrap() + } else { + // safety: dtoa only generates ascii. + unsafe { str::from_utf8_unchecked(result) } + })?; Ok(notation) } From 5539ea1044d7015018a37af79f6f8c7129654aa4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Emilio=20Cobos=20=C3=81lvarez?= Date: Fri, 11 Dec 2020 03:05:03 +0100 Subject: [PATCH 2/2] Version bump. --- Cargo.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Cargo.toml b/Cargo.toml index 52ce205..068e872 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "dtoa-short" -version = "0.3.2" +version = "0.3.3" authors = ["Xidorn Quan "] description = "Serialize float number and truncate to certain precision"