From f82f2b0dafc1bff1f36154c513c39f05e10db268 Mon Sep 17 00:00:00 2001 From: Andrew Krieger Date: Tue, 17 Oct 2017 09:17:30 -0700 Subject: [PATCH] Don't make copies of std::string or fbstring when converting. Summary: This overload of estimateSpaceNeeded was taking a Src by value, but Src is constrained by IsSomeString which only returns true for std::string or fbstring, so this was inducing a copy in any situation where folly::to<> is used with varargs which contain fb/string arguments. Reviewed By: yfeldblum Differential Revision: D6059517 fbshipit-source-id: adc239f9049e161fc4b750bae0e3de5dbdcd1bfc --- folly/Conv.h | 28 ++++++++++++++++++++++------ 1 file changed, 22 insertions(+), 6 deletions(-) diff --git a/folly/Conv.h b/folly/Conv.h index ce30765c..ff3bb81d 100644 --- a/folly/Conv.h +++ b/folly/Conv.h @@ -440,12 +440,18 @@ typename std::enable_if::value, size_t>:: return 0; } +template +typename std::enable_if::value, size_t>::type +estimateSpaceNeeded(Src const& value) { + return value.size(); +} + template typename std::enable_if< - (std::is_convertible::value || - IsSomeString::value) && - !std::is_convertible::value, - size_t>::type + std::is_convertible::value && + !IsSomeString::value && + !std::is_convertible::value, + size_t>::type estimateSpaceNeeded(Src value) { return folly::StringPiece(value).size(); } @@ -1480,6 +1486,14 @@ tryTo(StringPiece src) { }); } +template +inline typename std::enable_if< + IsSomeString::value && !std::is_same::value, + Tgt>::type +to(Src const& src) { + return to(StringPiece(src.data(), src.size())); +} + template inline typename std::enable_if::value, Tgt>::type @@ -1557,8 +1571,10 @@ to(const Src& value) { template typename std::enable_if< - std::is_enum::value && !std::is_same::value, Tgt>::type -to(const Src & value) { + !IsSomeString::value && std::is_enum::value && + !std::is_same::value, + Tgt>::type +to(const Src& value) { return static_cast(to::type>(value)); } -- 2.34.1