Skip to content

Commit

Permalink
Merge 'treewide: reduce the usage of iostream in headers' from Kefu Chai
Browse files Browse the repository at this point in the history
for better compilation speed, as `<iostream>` is the one of the heaviest C++ headers.

in this series:

- stop using iostream in `seastar/net/packet-util.hh`
- extract `seastar::format()` into `format.hh`
- use `format.hh` directly

tested on my laptop with 32 G memory, 8c16t.

- ccache and distcc are not enabled
- all interaction with the computer are stopped
- most of the active foreground tasks showed by `top` are stopped

before this change:
---

```console
$ git clean -dfx
$ ./configure.py --mode debug --disable-dpdk --compiler clang++
$ time cmake --build build/debug
...
real    2m43.926s
user    35m58.795s
sys     1m50.180s
```

after this change:
---

```console
$ git clean -dfx
$ ./configure.py --mode debug --disable-dpdk --compiler clang++
$ time cmake --build build/debug
...
real    2m42.688s
user    35m55.328s
sys     1m50.926s
```

i repeated the test for 3 times. the change shortened the build time 3 out of 3 times.

Closes scylladb#2526

* https://github.com/scylladb/seastar:
  treewide: include core/format.hh when appropriate
  print: remove unused fmt/ostream.h
  print: extract format() into format.hh
  net: route error messages to logger instead of to stderr
  net: stop printing when reaching unreachable branch
  • Loading branch information
xemul committed Nov 6, 2024
2 parents 7ae238f + 4b1f3d1 commit f2c0668
Show file tree
Hide file tree
Showing 12 changed files with 65 additions and 31 deletions.
1 change: 1 addition & 0 deletions apps/io_tester/ioinfo.cc
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
/*
* Copyright (C) 2021 ScyllaDB
*/
#include <iostream>
#include <seastar/core/app-template.hh>
#include <seastar/core/thread.hh>
#include <seastar/core/reactor.hh>
Expand Down
1 change: 1 addition & 0 deletions apps/rpc_tester/rpc_tester.cc
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
* Copyright (C) 2022 ScyllaDB
*/

#include <iostream>
#include <vector>
#include <chrono>
#include <random>
Expand Down
1 change: 1 addition & 0 deletions demos/rpc_demo.cc
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
* Copyright 2015 Cloudius Systems
*/
#include <cmath>
#include <iostream>
#include <ranges>
#include <seastar/core/reactor.hh>
#include <seastar/core/app-template.hh>
Expand Down
48 changes: 48 additions & 0 deletions include/seastar/core/format.hh
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/*
* This file is open source software, licensed to you under the terms
* of the Apache License, Version 2.0 (the "License"). See the NOTICE file
* distributed with this work for additional information regarding copyright
* ownership. 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.
*/
/*
* Copyright (C) 2014 Cloudius Systems, Ltd.
*/

#pragma once

#include <seastar/core/sstring.hh>
#ifndef SEASTAR_MODULE
#include <fmt/format.h>
#endif

namespace seastar {

/**
* Evaluate the formatted string in a native fmt library format
*
* @param fmt format string with the native fmt library syntax
* @param a positional parameters
*
* @return sstring object with the result of applying the given positional
* parameters on a given format string.
*/
template <typename... A>
sstring
format(fmt::format_string<A...> fmt, A&&... a) {
fmt::memory_buffer out;
fmt::format_to(fmt::appender(out), fmt, std::forward<A>(a)...);
return sstring{out.data(), out.size()};
}

}
2 changes: 1 addition & 1 deletion include/seastar/core/internal/estimated_histogram.hh
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@
#include <algorithm>
#include <vector>
#include <chrono>
#include <string>
#include <seastar/core/metrics_types.hh>
#include <seastar/core/print.hh>
#include <seastar/core/bitops.hh>
#include <limits>
#include <array>
Expand Down
18 changes: 1 addition & 17 deletions include/seastar/core/print.hh
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,10 @@

#pragma once

#include <seastar/core/format.hh>
#include <seastar/core/sstring.hh>
#include <seastar/util/modules.hh>
#ifndef SEASTAR_MODULE
#include <fmt/ostream.h>
#include <fmt/printf.h>
#include <iostream>
#include <iomanip>
Expand Down Expand Up @@ -118,22 +118,6 @@ log(A&&... a) {
print(std::forward<A>(a)...);
}

/**
* Evaluate the formatted string in a native fmt library format
*
* @param fmt format string with the native fmt library syntax
* @param a positional parameters
*
* @return sstring object with the result of applying the given positional
* parameters on a given format string.
*/
template <typename... A>
sstring
format(fmt::format_string<A...> fmt, A&&... a) {
fmt::memory_buffer out;
fmt::format_to(fmt::appender(out), fmt, std::forward<A>(a)...);
return sstring{out.data(), out.size()};
}

// temporary, use fmt::print() instead
template <typename... A>
Expand Down
4 changes: 1 addition & 3 deletions include/seastar/net/packet-util.hh
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@

#include <seastar/net/packet.hh>
#include <map>
#include <iostream>

namespace seastar {

Expand Down Expand Up @@ -145,8 +144,7 @@ public:
continue;
} else {
// If we reach here, we have a bug with merge.
std::cerr << "packet_merger: merge error\n";
abort();
std::abort();
break;
}
}
Expand Down
2 changes: 1 addition & 1 deletion include/seastar/rpc/rpc_impl.hh
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,14 @@
*/
#pragma once

#include <seastar/core/format.hh>
#include <seastar/core/function_traits.hh>
#include <seastar/core/shared_ptr.hh>
#include <seastar/core/sstring.hh>
#include <seastar/core/when_all.hh>
#include <seastar/util/is_smart_ptr.hh>
#include <seastar/core/simple-stream.hh>
#include <seastar/net/packet-data-source.hh>
#include <seastar/core/print.hh>

#include <boost/type.hpp> // for compatibility

Expand Down
3 changes: 2 additions & 1 deletion include/seastar/util/backtrace.hh
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@

#pragma once

#include <seastar/core/format.hh>
#include <seastar/core/sstring.hh>
#include <seastar/core/print.hh>
#include <seastar/core/scheduling.hh>
#include <seastar/core/shared_ptr.hh>
#include <seastar/util/modules.hh>
Expand All @@ -36,6 +36,7 @@
#include <memory>
#include <variant>
#include <boost/container/static_vector.hpp>
#include <fmt/ostream.h>
#endif

namespace seastar {
Expand Down
2 changes: 1 addition & 1 deletion include/seastar/util/program-options.hh
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@

#pragma once

#include <seastar/core/format.hh>
#include <seastar/core/sstring.hh>
#include <seastar/core/print.hh>
#include <seastar/util/modules.hh>

#ifndef SEASTAR_MODULE
Expand Down
1 change: 1 addition & 0 deletions src/net/dpdk.cc
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ module;

#include <cinttypes>
#include <atomic>
#include <iostream>
#include <vector>
#include <queue>
#include <getopt.h>
Expand Down
13 changes: 6 additions & 7 deletions src/net/native-stack-impl.hh
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,14 @@

#pragma once

#ifndef SEASTAR_MODULE
#include <iostream>
#endif

#include <seastar/net/stack.hh>
#include <seastar/net/inet_address.hh>
#include <seastar/util/log.hh>

namespace seastar {

extern logger seastar_logger;

namespace net {

using namespace seastar;
Expand Down Expand Up @@ -135,7 +134,7 @@ public:

virtual void set_reuseaddr(bool reuseaddr) override {
// FIXME: implement
std::cerr << "Reuseaddr is not supported by native stack" << std::endl;
seastar_logger.error("Reuseaddr is not supported by native stack");
}

virtual bool get_reuseaddr() const override {
Expand Down Expand Up @@ -244,7 +243,7 @@ native_connected_socket_impl<Protocol>::get_nodelay() const {
template <typename Protocol>
void native_connected_socket_impl<Protocol>::set_keepalive(bool keepalive) {
// FIXME: implement
std::cerr << "Keepalive is not supported by native stack" << std::endl;
seastar_logger.error("Keepalive is not supported by native stack");
}
template <typename Protocol>
bool native_connected_socket_impl<Protocol>::get_keepalive() const {
Expand All @@ -255,7 +254,7 @@ bool native_connected_socket_impl<Protocol>::get_keepalive() const {
template <typename Protocol>
void native_connected_socket_impl<Protocol>::set_keepalive_parameters(const keepalive_params&) {
// FIXME: implement
std::cerr << "Keepalive parameters are not supported by native stack" << std::endl;
seastar_logger.error("Keepalive parameters are not supported by native stack");
}

template <typename Protocol>
Expand Down

0 comments on commit f2c0668

Please sign in to comment.