Skip to content

Commit

Permalink
Migrate from Folly Format to fmt
Browse files Browse the repository at this point in the history
Summary: Migrate from Folly Format to fmt which provides smaller compile times and per-call binary code size.

Reviewed By: alandau

Differential Revision: D14954926

fbshipit-source-id: 9d2c39e74a5d11e0f90c8ad0d71b79424c56747f
  • Loading branch information
vitaut authored and facebook-github-bot committed Apr 25, 2019
1 parent bf3bb89 commit 7e53bf0
Show file tree
Hide file tree
Showing 26 changed files with 75 additions and 72 deletions.
1 change: 1 addition & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@ if(lib_only OR build_all)
find_package(yarpl CONFIG REQUIRED)
find_package(rsocket CONFIG)
find_package(fizz CONFIG REQUIRED)
find_package(fmt CONFIG REQUIRED)
find_package(wangle CONFIG REQUIRED)
find_package(Zlib REQUIRED)
find_package(Zstd REQUIRED)
Expand Down
3 changes: 2 additions & 1 deletion build/fbcode_builder/specs/fbthrift.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

import specs.folly as folly
import specs.fizz as fizz
import specs.fmt as fmt
import specs.rsocket as rsocket
import specs.sodium as sodium
import specs.wangle as wangle
Expand All @@ -22,7 +23,7 @@ def fbcode_builder_spec(builder):
ShellQuoted('$(git describe --abbrev=0 --tags)')
)
return {
'depends_on': [folly, fizz, sodium, rsocket, wangle, zstd],
'depends_on': [folly, fizz, fmt, sodium, rsocket, wangle, zstd],
'steps': [
# This isn't a separete spec, since only fbthrift uses mstch.
builder.github_project_workdir('no1msd/mstch', 'build'),
Expand Down
1 change: 1 addition & 0 deletions thrift/lib/cpp/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ target_include_directories(
target_link_libraries(
thrift-core
PUBLIC
fmt::fmt
Folly::folly
)

Expand Down
16 changes: 9 additions & 7 deletions thrift/lib/cpp/protocol/TProtocolException.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

#include <thrift/lib/cpp/protocol/TProtocolException.h>

#include <folly/Format.h>
#include <fmt/core.h>

namespace apache {
namespace thrift {
Expand Down Expand Up @@ -45,25 +45,27 @@ namespace protocol {
[[noreturn]] void TProtocolException::throwMissingRequiredField(
folly::StringPiece field,
folly::StringPiece type) {
constexpr auto fmt =
"Required field '{}' was not found in serialized data! Struct: {}";
throw TProtocolException(
TProtocolException::MISSING_REQUIRED_FIELD,
folly::sformat(fmt, field, type));
fmt::format(
"Required field '{}' was not found in serialized data! Struct: {}",
field,
type));
}

[[noreturn]] void TProtocolException::throwBoolValueOutOfRange(uint8_t value) {
throw TProtocolException(
TProtocolException::INVALID_DATA,
folly::sformat(
"Attempt to interpret value {} as bool, probably the data is corrupted",
fmt::format(
"Attempt to interpret value {} as bool, probably the data is "
"corrupted",
value));
}

[[noreturn]] void TProtocolException::throwInvalidSkipType(TType type) {
throw TProtocolException(
TProtocolException::INVALID_DATA,
folly::sformat(
fmt::format(
"Encountered invalid field/element type ({}) during skipping",
static_cast<uint8_t>(type)));
}
Expand Down
6 changes: 3 additions & 3 deletions thrift/lib/cpp/transport/THeader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@

#include <thrift/lib/cpp/transport/THeader.h>

#include <fmt/core.h>
#include <folly/Conv.h>
#include <folly/ExceptionString.h>
#include <folly/Format.h>
#include <folly/String.h>
#include <folly/compression/Compression.h>
#include <folly/io/Cursor.h>
Expand Down Expand Up @@ -555,7 +555,7 @@ unique_ptr<IOBuf> THeader::untransform(
default:
throw TApplicationException(
TApplicationException::MISSING_RESULT,
folly::sformat("Unknown transform: {}", transId));
fmt::format("Unknown transform: {}", transId));
}
}

Expand Down Expand Up @@ -617,7 +617,7 @@ unique_ptr<IOBuf> THeader::transform(
default:
throw TTransportException(
TTransportException::CORRUPTED_DATA,
folly::sformat("Unknown transform: {}", transId));
fmt::format("Unknown transform: {}", transId));
}
++it;
}
Expand Down
4 changes: 2 additions & 2 deletions thrift/lib/cpp/util/THttpParser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@

#include <thrift/lib/cpp/transport/TTransportException.h>

#include <folly/Format.h>
#include <fmt/core.h>
#include <folly/String.h>

#include <cstdlib>
Expand Down Expand Up @@ -336,7 +336,7 @@ void THttpClientParser::parseHeaderLine(folly::StringPiece header) {

bool THttpClientParser::parseStatusLine(folly::StringPiece status) {
auto const badStatus = [&] {
return TTransportException(folly::sformat("Bad Status: {}", status));
return TTransportException(fmt::format("Bad Status: {}", status));
};

// Skip over the "HTTP/<version>" string.
Expand Down
9 changes: 5 additions & 4 deletions thrift/lib/cpp/util/test/THttpParserTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,9 @@
*/

#include <thrift/lib/cpp/util/THttpParser.h>
#include <folly/Format.h>
#include <fmt/core.h>

#include <folly/String.h>
#include <folly/portability/GMock.h>
#include <folly/portability/GTest.h>

Expand Down Expand Up @@ -45,7 +46,7 @@ TEST_F(THttpClientParserTest, too_many_headers) {
apache::thrift::util::THttpClientParser parser;
std::map<std::string, std::string> header;
for (int i = 0; i < 100; i++) {
header[folly::sformat("testing_header{}", i)] = "test_header";
header[fmt::format("testing_header{}", i)] = "test_header";
}
std::map<std::string, std::string> header_persistent;
auto answer = std::string("{'testing': 'this is a test'}");
Expand All @@ -55,13 +56,13 @@ TEST_F(THttpClientParserTest, too_many_headers) {
auto fbs = buf->moveToFbString();
std::string output = std::string(fbs.c_str(), fbs.size());
for (int i = 0; i < 100; i++) {
std::string s = folly::sformat("testing_header{}: test_header\r\n", i);
std::string s = fmt::format("testing_header{}: test_header\r\n", i);
EXPECT_THAT(output, ::testing::HasSubstr(s));
}
std::vector<std::string> o;
folly::split("\r\n", output, o);
if (o.at(o.size() - 1) != answer) {
FAIL() << folly::sformat(
FAIL() << fmt::format(
"Final line should be {} not {}", answer, o.at(o.size() - 1));
}
}
Expand Down
2 changes: 1 addition & 1 deletion thrift/lib/cpp2/GeneratedCodeHelper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -274,7 +274,7 @@ namespace detail {
namespace si {
[[noreturn]] void throw_app_exn_unimplemented(char const* const name) {
throw TApplicationException(
folly::sformat("Function {} is unimplemented", name));
fmt::format("Function {} is unimplemented", name));
}
} // namespace si
} // namespace detail
Expand Down
4 changes: 2 additions & 2 deletions thrift/lib/cpp2/GeneratedCodeHelper.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
#include <type_traits>
#include <utility>

#include <folly/Format.h>
#include <fmt/core.h>
#include <folly/Traits.h>
#include <folly/futures/Future.h>
#include <folly/io/Cursor.h>
Expand Down Expand Up @@ -644,7 +644,7 @@ process_missing(
using h = helper_r<ProtocolReader>;
const char* fn = "process";
auto type = TApplicationException::TApplicationExceptionType::UNKNOWN_METHOD;
const auto msg = folly::sformat("Method name {} not found", fname);
const auto msg = fmt::format("Method name {} not found", fname);
return h::process_exn(fn, type, msg, std::move(req), ctx, eb, protoSeqId);
}

Expand Down
4 changes: 2 additions & 2 deletions thrift/lib/cpp2/async/HTTPClientChannel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@

#include <utility>

#include <folly/Format.h>
#include <fmt/core.h>
#include <proxygen/lib/http/HTTPCommonHeaders.h>
#include <proxygen/lib/http/HTTPMethod.h>
#include <proxygen/lib/http/codec/HTTP1xCodec.h>
Expand Down Expand Up @@ -446,7 +446,7 @@ void HTTPClientChannel::HTTPTransactionCallback::onEOM() noexcept {
if (!oneway_ && cb_) {
if (!body_) {
requestError(folly::make_exception_wrapper<
transport::TTransportException>(folly::sformat(
transport::TTransportException>(fmt::format(
"Empty HTTP response, {}",
(msg_ ? folly::to<std::string>(
msg_->getStatusCode(), ", ", msg_->getStatusMessage())
Expand Down
2 changes: 1 addition & 1 deletion thrift/lib/cpp2/protocol/DebugProtocol.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ std::string fieldTypeName(TType type) {
case TType::T_STREAM:
return "stream";
default:
return folly::format("unknown({})", int(type)).str();
return fmt::format("unknown({})", int(type));
}
}

Expand Down
8 changes: 3 additions & 5 deletions thrift/lib/cpp2/protocol/DebugProtocol.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
#ifndef CPP2_PROTOCOL_DEBUGPROTOCOL_H_
#define CPP2_PROTOCOL_DEBUGPROTOCOL_H_

#include <folly/Format.h>
#include <fmt/core.h>
#include <folly/io/Cursor.h>
#include <folly/io/IOBuf.h>
#include <thrift/lib/cpp2/Thrift.h>
Expand Down Expand Up @@ -128,10 +128,8 @@ class DebugProtocolWriter {
}

template <class... Args>
void writePlain(Args&&... args) {
const auto& fmt = folly::format(std::forward<Args>(args)...);
auto cb = [this](folly::StringPiece sp) { this->writeRaw(sp); };
fmt(cb);
void writePlain(const Args&... args) {
writeRaw(fmt::format(args...));
}

void writeIndent() {
Expand Down
11 changes: 6 additions & 5 deletions thrift/lib/cpp2/protocol/JSONProtocolCommon.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@

#include <type_traits>

#include <folly/Format.h>
#include <fmt/core.h>

namespace {

Expand Down Expand Up @@ -158,10 +158,11 @@ static inline folly::StringPiece sp(char const& ch) {
[[noreturn]] void JSONProtocolReaderCommon::throwUnexpectedChar(
char const ch,
char const expected) {
constexpr auto fmt =
"expected '{}' (hex 0x{:02x}), read '{:c}' (hex 0x{:02x})";
auto const msg = folly::sformat(fmt, expected, expected, ch, ch);
throw TProtocolException(TProtocolException::INVALID_DATA, msg);
auto const msg = fmt::format(
"expected '{0}' (hex {0:#02x}), read '{1}' (hex {1:#02x})",
expected,
ch);
throw TProtocolException(TProtocolException::INVALID_DATA, msg);
}

[[noreturn]] void JSONProtocolReaderCommon::throwInvalidEscapeChar(
Expand Down
1 change: 0 additions & 1 deletion thrift/lib/cpp2/reflection/legacy_reflection.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@

#include <thrift/lib/cpp2/reflection/legacy_reflection.h>

#include <folly/Format.h>
#include <folly/lang/Bits.h>
#include <folly/ssl/OpenSSLHash.h>

Expand Down
9 changes: 5 additions & 4 deletions thrift/lib/cpp2/test/CompactProtocolBench.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,12 @@

#include <iostream>
#include <vector>
#include <folly/portability/GFlags.h>
#include <glog/logging.h>

#include <fmt/core.h>
#include <folly/Benchmark.h>
#include <folly/Format.h>
#include <folly/Optional.h>
#include <folly/portability/GFlags.h>
#include <glog/logging.h>
#include <thrift/lib/cpp2/protocol/Serializer.h>

using namespace std;
Expand All @@ -48,7 +49,7 @@ Deep makeDeep(size_t triplesz) {
for (size_t j = 0; j < triplesz; ++j) {
Deep2 data2;
for (size_t k = 0; k < triplesz; ++k) {
data2.datas.push_back(sformat("omg[{}, {}, {}]", i, j, k));
data2.datas.push_back(fmt::format("omg[{}, {}, {}]", i, j, k));
}
data1.deeps.push_back(move(data2));
}
Expand Down
4 changes: 1 addition & 3 deletions thrift/lib/cpp2/test/ProtoBufBench.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2014 Facebook, Inc.
* Copyright 2014-present Facebook, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -13,13 +13,11 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/

#include <thrift/lib/cpp2/test/ProtoBufBenchData.pb.h>

#include <folly/portability/GFlags.h>
#include <glog/logging.h>
#include <folly/Benchmark.h>
#include <folly/Format.h>
#include <folly/Optional.h>

using namespace std;
Expand Down
4 changes: 1 addition & 3 deletions thrift/lib/cpp2/test/ProtocolBench.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2014 Facebook, Inc.
* Copyright 2014-present Facebook, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -13,14 +13,12 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/

#include <thrift/lib/cpp2/protocol/Serializer.h>
#include <thrift/lib/cpp2/test/gen-cpp2/ProtocolBenchData_types.h>

#include <folly/portability/GFlags.h>
#include <glog/logging.h>
#include <folly/Benchmark.h>
#include <folly/Format.h>
#include <folly/Optional.h>

#include <vector>
Expand Down
7 changes: 4 additions & 3 deletions thrift/lib/cpp2/test/SerializationTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,10 @@
#include <folly/portability/GTest.h>

#include <memory>
#include <folly/Format.h>
#include <thrift/lib/cpp2/test/gen-cpp2/TestService.h>

#include <fmt/core.h>
#include <thrift/lib/cpp2/protocol/Serializer.h>
#include <thrift/lib/cpp2/test/gen-cpp2/TestService.h>

using namespace std;
using namespace folly;
Expand Down Expand Up @@ -216,7 +217,7 @@ TestStructRecursive makeTestStructRecursive(size_t levels) {
unique_ptr<TestStructRecursive> s;
for (size_t i = levels; i > 0; --i) {
auto t = make_unique<TestStructRecursive>();
t->tag = sformat("level-{}", i);
t->tag = fmt::format("level-{}", i);
t->cdr = std::move(s);
s = std::move(t);
}
Expand Down
Loading

0 comments on commit 7e53bf0

Please sign in to comment.