Commit f82f2b0d authored by Andrew Krieger's avatar Andrew Krieger Committed by Facebook Github Bot

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
parent 1d4de585
...@@ -440,12 +440,18 @@ typename std::enable_if<std::is_convertible<Src, const char*>::value, size_t>:: ...@@ -440,12 +440,18 @@ typename std::enable_if<std::is_convertible<Src, const char*>::value, size_t>::
return 0; return 0;
} }
template <class Src>
typename std::enable_if<IsSomeString<Src>::value, size_t>::type
estimateSpaceNeeded(Src const& value) {
return value.size();
}
template <class Src> template <class Src>
typename std::enable_if< typename std::enable_if<
(std::is_convertible<Src, folly::StringPiece>::value || std::is_convertible<Src, folly::StringPiece>::value &&
IsSomeString<Src>::value) && !IsSomeString<Src>::value &&
!std::is_convertible<Src, const char*>::value, !std::is_convertible<Src, const char*>::value,
size_t>::type size_t>::type
estimateSpaceNeeded(Src value) { estimateSpaceNeeded(Src value) {
return folly::StringPiece(value).size(); return folly::StringPiece(value).size();
} }
...@@ -1480,6 +1486,14 @@ tryTo(StringPiece src) { ...@@ -1480,6 +1486,14 @@ tryTo(StringPiece src) {
}); });
} }
template <class Tgt, class Src>
inline typename std::enable_if<
IsSomeString<Src>::value && !std::is_same<StringPiece, Tgt>::value,
Tgt>::type
to(Src const& src) {
return to<Tgt>(StringPiece(src.data(), src.size()));
}
template <class Tgt> template <class Tgt>
inline inline
typename std::enable_if<!std::is_same<StringPiece, Tgt>::value, Tgt>::type typename std::enable_if<!std::is_same<StringPiece, Tgt>::value, Tgt>::type
...@@ -1557,8 +1571,10 @@ to(const Src& value) { ...@@ -1557,8 +1571,10 @@ to(const Src& value) {
template <class Tgt, class Src> template <class Tgt, class Src>
typename std::enable_if< typename std::enable_if<
std::is_enum<Tgt>::value && !std::is_same<Src, Tgt>::value, Tgt>::type !IsSomeString<Src>::value && std::is_enum<Tgt>::value &&
to(const Src & value) { !std::is_same<Src, Tgt>::value,
Tgt>::type
to(const Src& value) {
return static_cast<Tgt>(to<typename std::underlying_type<Tgt>::type>(value)); return static_cast<Tgt>(to<typename std::underlying_type<Tgt>::type>(value));
} }
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment