Skip to content

Commit

Permalink
Move code that uses fmt out of line
Browse files Browse the repository at this point in the history
Including fmt is fairly expensive. This patch moves code that uses fmt
out of line in preparation for avoiding including the fmt/* headers in
commonly used seastar headers.

Signed-off-by: Rafael Ávila de Espíndola <[email protected]>
Message-Id: <[email protected]>
  • Loading branch information
espindola authored and avikivity committed Jan 21, 2019
1 parent 7a7b80f commit 80e131e
Show file tree
Hide file tree
Showing 8 changed files with 52 additions and 37 deletions.
15 changes: 5 additions & 10 deletions include/seastar/core/aligned_buffer.hh
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@

namespace seastar {

namespace internal {
void* allocate_aligned_buffer_impl(size_t size, size_t align);
}

struct free_deleter {
void operator()(void* p) { ::free(p); }
Expand All @@ -35,16 +38,8 @@ template <typename CharType>
inline
std::unique_ptr<CharType[], free_deleter> allocate_aligned_buffer(size_t size, size_t align) {
static_assert(sizeof(CharType) == 1, "must allocate byte type");
void* ret;
auto r = posix_memalign(&ret, align, size);
if (r == ENOMEM) {
throw std::bad_alloc();
} else if (r == EINVAL) {
throw std::runtime_error(format("Invalid alignment of {:d}; allocating {:d} bytes", align, size));
} else {
assert(r == 0);
return std::unique_ptr<CharType[], free_deleter>(reinterpret_cast<CharType *>(ret));
}
void* ret = internal::allocate_aligned_buffer_impl(size, align);
return std::unique_ptr<CharType[], free_deleter>(reinterpret_cast<CharType *>(ret));
}


Expand Down
10 changes: 1 addition & 9 deletions include/seastar/core/scollectd.hh
Original file line number Diff line number Diff line change
Expand Up @@ -303,15 +303,7 @@ class type_instance_id {
static thread_local unsigned _next_truncated_idx;

/// truncate a given field to the maximum allowed length
void truncate(sstring& field, const char* field_desc) {
if (field.size() > max_collectd_field_text_len) {
auto suffix_len = std::ceil(std::log10(++_next_truncated_idx)) + 1;
sstring new_field(seastar::format("{}~{:d}", sstring(field.data(), max_collectd_field_text_len - suffix_len), _next_truncated_idx));

logger.warn("Truncating \"{}\" to {} chars: \"{}\" -> \"{}\"", field_desc, max_collectd_field_text_len, field, new_field);
field = std::move(new_field);
}
}
void truncate(sstring& field, const char* field_desc);
public:
type_instance_id() = default;
type_instance_id(plugin_id p, plugin_instance_id pi, type_id t,
Expand Down
10 changes: 1 addition & 9 deletions include/seastar/net/api.hh
Original file line number Diff line number Diff line change
Expand Up @@ -46,15 +46,7 @@ bool is_port_unspecified(ipv4_addr &addr) {
return addr.port == 0;
}

static inline
std::ostream& operator<<(std::ostream &os, ipv4_addr addr) {
fmt_print(os, "{:d}.{:d}.{:d}.{:d}",
(addr.ip >> 24) & 0xff,
(addr.ip >> 16) & 0xff,
(addr.ip >> 8) & 0xff,
(addr.ip) & 0xff);
return os << ":" << addr.port;
}
std::ostream& operator<<(std::ostream& os, ipv4_addr addr);

static inline
socket_address make_ipv4_address(ipv4_addr addr) {
Expand Down
10 changes: 1 addition & 9 deletions include/seastar/net/ip.hh
Original file line number Diff line number Diff line change
Expand Up @@ -57,15 +57,7 @@ class tcp;
struct ipv4_address {
ipv4_address() : ip(0) {}
explicit ipv4_address(uint32_t ip) : ip(ip) {}
explicit ipv4_address(const std::string& addr) {
boost::system::error_code ec;
auto ipv4 = boost::asio::ip::address_v4::from_string(addr, ec);
if (ec) {
throw std::runtime_error(format("Wrong format for IPv4 address {}. Please ensure it's in dotted-decimal format",
addr));
}
ip = static_cast<uint32_t>(std::move(ipv4).to_ulong());
}
explicit ipv4_address(const std::string& addr);
ipv4_address(ipv4_addr addr) {
ip = addr.ip;
}
Expand Down
13 changes: 13 additions & 0 deletions src/core/memory.cc
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,19 @@

namespace seastar {

void* internal::allocate_aligned_buffer_impl(size_t size, size_t align) {
void *ret;
auto r = posix_memalign(&ret, align, size);
if (r == ENOMEM) {
throw std::bad_alloc();
} else if (r == EINVAL) {
throw std::runtime_error(format("Invalid alignment of {:d}; allocating {:d} bytes", align, size));
} else {
assert(r == 0);
return ret;
}
}

namespace memory {

static thread_local int abort_on_alloc_failure_suppressed = 0;
Expand Down
12 changes: 12 additions & 0 deletions src/core/scollectd.cc
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,18 @@

namespace seastar {

void scollectd::type_instance_id::truncate(sstring& field, const char* field_desc) {
if (field.size() > max_collectd_field_text_len) {
auto suffix_len = std::ceil(std::log10(++_next_truncated_idx)) + 1;
sstring new_field(seastar::format(
"{}~{:d}", sstring(field.data(), max_collectd_field_text_len - suffix_len), _next_truncated_idx));

logger.warn("Truncating \"{}\" to {} chars: \"{}\" -> \"{}\"", field_desc, max_collectd_field_text_len, field,
new_field);
field = std::move(new_field);
}
}

bool scollectd::type_instance_id::operator<(
const scollectd::type_instance_id& id2) const {
auto& id1 = *this;
Expand Down
10 changes: 10 additions & 0 deletions src/net/ip.cc
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,16 @@ namespace seastar {

namespace net {

ipv4_address::ipv4_address(const std::string& addr) {
boost::system::error_code ec;
auto ipv4 = boost::asio::ip::address_v4::from_string(addr, ec);
if (ec) {
throw std::runtime_error(
format("Wrong format for IPv4 address {}. Please ensure it's in dotted-decimal format", addr));
}
ip = static_cast<uint32_t>(std::move(ipv4).to_ulong());
}

std::ostream& operator<<(std::ostream& os, ipv4_address a) {
auto ip = a.ip;
return fmt_print(os, "{:d}.{:d}.{:d}.{:d}",
Expand Down
9 changes: 9 additions & 0 deletions src/net/net.cc
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,15 @@

namespace seastar {

std::ostream& operator<<(std::ostream &os, ipv4_addr addr) {
fmt_print(os, "{:d}.{:d}.{:d}.{:d}",
(addr.ip >> 24) & 0xff,
(addr.ip >> 16) & 0xff,
(addr.ip >> 8) & 0xff,
(addr.ip) & 0xff);
return os << ":" << addr.port;
}

using std::move;

ipv4_addr::ipv4_addr(const std::string &addr) {
Expand Down

0 comments on commit 80e131e

Please sign in to comment.