From 10ed79f7b20aeb03e720cd9a01f43f5a5f15f021 Mon Sep 17 00:00:00 2001 From: Bi Xue Date: Sat, 20 Aug 2016 21:10:36 -0700 Subject: [PATCH] Suggestion of fixing folly::to(double/float) performance issue Summary: When calling folly::to(double), generic implementation will firstly reserve 24 (or 25 when negative value) bytes. This will introduce a malloc call for most of mainstream string implementation. But for most cases, a floating point doesn't need 24 (or 25) bytes to be converted as a string. This diff try to introduce a special version which does not do string reserve. Reviewed By: ericniebler Differential Revision: D3728171 fbshipit-source-id: d70ead396ad6c8d0df1f542c5516f7534e82cb97 --- folly/Conv.h | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/folly/Conv.h b/folly/Conv.h index 9fc19696..09f762bc 100644 --- a/folly/Conv.h +++ b/folly/Conv.h @@ -928,6 +928,27 @@ to(const Ts&... vs) { return result; } +/** + * Special version of to for floating point. When calling + * folly::to(double), generic implementation above will + * firstly reserve 24 (or 25 when negative value) bytes. This will + * introduce a malloc call for most mainstream string implementations. + * + * But for most cases, a floating point doesn't need 24 (or 25) bytes to + * be converted as a string. + * + * This special version will not do string reserve. + */ +template +typename std::enable_if< + IsSomeString::value && std::is_floating_point::value, + Tgt>::type +to(Src value) { + Tgt result; + toAppend(value, &result); + return result; +} + /** * toDelim(SomeString str) returns itself. */ -- 2.34.1