From 59ac348b103fc91c7c39fe126b84bb45dbaec7b4 Mon Sep 17 00:00:00 2001 From: Philip Pronin Date: Sat, 27 Jun 2015 18:09:24 -0700 Subject: [PATCH] cleanup includes in folly/Format.h MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit Summary: `folly/Format.h` pulls in `folly/small_vector.h` and `folly/FBVector.h`, which (according to @​oleksandr's analysis) are the most expensive includes for multiple fbcode projects. Reviewed By: @ot Differential Revision: D2198904 --- folly/FBVector.h | 11 ++++++ folly/Format-inl.h | 58 ++++-------------------------- folly/Format.h | 8 ----- folly/FormatTraits.h | 66 ++++++++++++++++++++++++++++++++++ folly/Makefile.am | 1 + folly/small_vector.h | 16 +++++++-- folly/test/FormatOtherTest.cpp | 26 +++++++++++++- 7 files changed, 123 insertions(+), 63 deletions(-) create mode 100644 folly/FormatTraits.h diff --git a/folly/FBVector.h b/folly/FBVector.h index 754ebfef..b4e253ed 100644 --- a/folly/FBVector.h +++ b/folly/FBVector.h @@ -36,6 +36,7 @@ #include #include +#include #include #include #include @@ -1586,6 +1587,16 @@ void swap(fbvector& lhs, fbvector& rhs) noexcept { //----------------------------------------------------------------------------- // other +namespace detail { + +// Format support. +template +struct IndexableTraits> + : public IndexableTraitsSeq> { +}; + +} // namespace detail + template void compactResize(fbvector* v, size_t sz) { v->resize(sz); diff --git a/folly/Format-inl.h b/folly/Format-inl.h index 6205a568..1239188c 100644 --- a/folly/Format-inl.h +++ b/folly/Format-inl.h @@ -18,7 +18,14 @@ #error This file may only be included from Format.h. #endif +#include +#include +#include +#include +#include + #include +#include #include // Ignore -Wformat-nonliteral warnings within this file @@ -770,45 +777,6 @@ class FormatValue< namespace detail { -// Shortcut, so we don't have to use enable_if everywhere -struct FormatTraitsBase { - typedef void enabled; -}; - -// Traits that define enabled, value_type, and at() for anything -// indexable with integral keys: pointers, arrays, vectors, and maps -// with integral keys -template struct IndexableTraits; - -// Base class for sequences (vectors, deques) -template -struct IndexableTraitsSeq : public FormatTraitsBase { - typedef C container_type; - typedef typename C::value_type value_type; - static const value_type& at(const C& c, int idx) { - return c.at(idx); - } - - static const value_type& at(const C& c, int idx, - const value_type& dflt) { - return (idx >= 0 && size_t(idx) < c.size()) ? c.at(idx) : dflt; - } -}; - -// Base class for associative types (maps) -template -struct IndexableTraitsAssoc : public FormatTraitsBase { - typedef typename C::value_type::second_type value_type; - static const value_type& at(const C& c, int idx) { - return c.at(static_cast(idx)); - } - static const value_type& at(const C& c, int idx, - const value_type& dflt) { - auto pos = c.find(static_cast(idx)); - return pos != c.end() ? pos->second : dflt; - } -}; - // std::array template struct IndexableTraits> @@ -827,18 +795,6 @@ struct IndexableTraits> : public IndexableTraitsSeq> { }; -// fbvector -template -struct IndexableTraits> - : public IndexableTraitsSeq> { -}; - -// small_vector -template -struct IndexableTraits> - : public IndexableTraitsSeq> { -}; - // std::map with integral keys template struct IndexableTraits< diff --git a/folly/Format.h b/folly/Format.h index 4afe7e40..02e5d1a8 100644 --- a/folly/Format.h +++ b/folly/Format.h @@ -17,22 +17,14 @@ #ifndef FOLLY_FORMAT_H_ #define FOLLY_FORMAT_H_ -#include #include #include #include -#include -#include -#include -#include -#include #include #include #include -#include #include -#include #include // Ignore shadowing warnings within this file, so includers can use -Wshadow. diff --git a/folly/FormatTraits.h b/folly/FormatTraits.h new file mode 100644 index 00000000..7ca2ae5e --- /dev/null +++ b/folly/FormatTraits.h @@ -0,0 +1,66 @@ +/* + * Copyright 2015 Facebook, Inc. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef FOLLY_FORMAT_TRAITS_H_ +#define FOLLY_FORMAT_TRAITS_H_ + +#include + +namespace folly { namespace detail { + +// Shortcut, so we don't have to use enable_if everywhere +struct FormatTraitsBase { + typedef void enabled; +}; + +// Traits that define enabled, value_type, and at() for anything +// indexable with integral keys: pointers, arrays, vectors, and maps +// with integral keys +template struct IndexableTraits; + +// Base class for sequences (vectors, deques) +template +struct IndexableTraitsSeq : public FormatTraitsBase { + typedef C container_type; + typedef typename C::value_type value_type; + + static const value_type& at(const C& c, int idx) { + return c.at(idx); + } + + static const value_type& at(const C& c, int idx, const value_type& dflt) { + return (idx >= 0 && size_t(idx) < c.size()) ? c.at(idx) : dflt; + } +}; + +// Base class for associative types (maps) +template +struct IndexableTraitsAssoc : public FormatTraitsBase { + typedef typename C::value_type::second_type value_type; + + static const value_type& at(const C& c, int idx) { + return c.at(static_cast(idx)); + } + + static const value_type& at(const C& c, int idx, const value_type& dflt) { + auto pos = c.find(static_cast(idx)); + return pos != c.end() ? pos->second : dflt; + } +}; + +}} // namespaces + +#endif /* FOLLY_FORMAT_TRAITS_H_ */ diff --git a/folly/Makefile.am b/folly/Makefile.am index e615a733..c8e14e61 100644 --- a/folly/Makefile.am +++ b/folly/Makefile.am @@ -123,6 +123,7 @@ nobase_follyinclude_HEADERS = \ folly-config.h \ Foreach.h \ FormatArg.h \ + FormatTraits.h \ Format.h \ Format-inl.h \ futures/Deprecated.h \ diff --git a/folly/small_vector.h b/folly/small_vector.h index 318fcbfd..ca62f147 100644 --- a/folly/small_vector.h +++ b/folly/small_vector.h @@ -23,8 +23,6 @@ #ifndef FOLLY_SMALL_VECTOR_H_ #define FOLLY_SMALL_VECTOR_H_ -#include - #include #include #include @@ -46,7 +44,9 @@ #include #include +#include #include +#include #if defined(__GNUC__) && FOLLY_X64 # include @@ -1141,7 +1141,17 @@ void swap(small_vector& a, ////////////////////////////////////////////////////////////////////// -} +namespace detail { + +// Format support. +template +struct IndexableTraits> + : public IndexableTraitsSeq> { +}; + +} // namespace detail + +} // namespace folly #pragma GCC diagnostic pop diff --git a/folly/test/FormatOtherTest.cpp b/folly/test/FormatOtherTest.cpp index c41f1f06..97d2db10 100644 --- a/folly/test/FormatOtherTest.cpp +++ b/folly/test/FormatOtherTest.cpp @@ -16,9 +16,11 @@ #include +#include #include -#include #include +#include +#include #include #include @@ -76,6 +78,28 @@ TEST(FormatOther, dynamic) { EXPECT_EQ("(null)", sformat("{}", dynamic(nullptr))); } +namespace { + +template +void testFormatSeq() { + T v{10, 20, 30}; + EXPECT_EQ("30 10", sformat("{0[2]} {0[0]}", v)); + EXPECT_EQ("0020", sformat("{0[1]:04}", v)); + EXPECT_EQ("0020", svformat("{1:04}", v)); + EXPECT_EQ("10 20", svformat("{} {}", v)); + EXPECT_EQ("10 20 0030", svformat("{} {} {:04}", v)); +} + +} // namespace + +TEST(FormatOther, fbvector) { + testFormatSeq>(); +} + +TEST(FormatOther, small_vector) { + testFormatSeq>(); +} + int main(int argc, char *argv[]) { testing::InitGoogleTest(&argc, argv); gflags::ParseCommandLineFlags(&argc, &argv, true); -- 2.34.1