Commit 66f614d7 authored by Victor Zverovich's avatar Victor Zverovich Committed by Facebook GitHub Bot

Reduce binary size via format string compilation

Summary: Switch to format string compilation to prevent pulling in too many symbols from the {fmt} library into `folly/detail:ip_address`. This change saves ~64k (fixed cost) in opt mode compared to the current master or ~36k compared to the old code using Folly Format. This doesn't matter for server projects especially since {fmt} will likely be linked anyway but matters for some mobile ones.

Reviewed By: Orvid

Differential Revision: D25904078

fbshipit-source-id: 56473cf9bf4456a3cb3c5aa375c16393e144b6e1
parent 7ba43ce3
/*
* Copyright (c) Facebook, Inc. and its affiliates.
*
* 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.
*/
#pragma once
#include <fmt/compile.h>
// Fallback to runtime format string processing for compatibility with fmt 6.x.
#ifndef FMT_COMPILE
#define FMT_COMPILE(format_str) format_str
#endif
......@@ -16,8 +16,7 @@
#include <folly/File.h>
#include <fmt/core.h>
#include <folly/CompiledFormat.h>
#include <folly/Exception.h>
#include <folly/FileUtil.h>
#include <folly/ScopeGuard.h>
......@@ -41,8 +40,8 @@ File::File(int fd, bool ownsFd) noexcept : fd_(fd), ownsFd_(ownsFd) {
File::File(const char* name, int flags, mode_t mode)
: fd_(::open(name, flags, mode)), ownsFd_(false) {
if (fd_ == -1) {
throwSystemError(
fmt::format("open(\"{}\", {:#o}, 0{:#o}) failed", name, flags, mode));
throwSystemError(fmt::format(
FMT_COMPILE("open(\"{}\", {:#o}, 0{:#o}) failed"), name, flags, mode));
}
ownsFd_ = true;
}
......
......@@ -28,8 +28,7 @@
#include <iostream>
#include <string>
#include <fmt/core.h>
#include <folly/CompiledFormat.h>
#include <folly/Demangle.h>
#include <folly/ScopeGuard.h>
#include <folly/detail/SingletonStackTrace.h>
......@@ -159,8 +158,8 @@ void singletonPrintDestructionStackTrace(const TypeDescriptor& type) {
}
[[noreturn]] void singletonThrowNullCreator(const std::type_info& type) {
auto const msg = fmt::format(
"nullptr_t should be passed if you want {} to be default constructed",
auto const msg = fmt::format(FMT_COMPILE(
"nullptr_t should be passed if you want {} to be default constructed"),
folly::StringPiece(demangle(type)));
throw std::logic_error(msg);
}
......
......@@ -16,19 +16,20 @@
#include <folly/detail/IPAddress.h>
#include <fmt/core.h>
#include <stdexcept>
#include <folly/CompiledFormat.h>
namespace folly {
namespace detail {
std::string familyNameStrDefault(sa_family_t family) {
return fmt::format("sa_family_t({})", family);
return fmt::format(FMT_COMPILE("sa_family_t({})"), family);
}
[[noreturn]] void getNthMSBitImplThrow(size_t bitCount, sa_family_t family) {
throw std::invalid_argument(fmt::format(
"Bit index must be < {} for addresses of type: {}",
FMT_COMPILE("Bit index must be < {} for addresses of type: {}"),
bitCount,
familyNameStr(family)));
}
......
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