tree-wide: shuffle headers around for about 30s compile time

This didn't really feel so worth it afterwards, but I did untangle a
bunch of stuff that should not have been tangled.

The general gist of this change is that variant bullshit was causing a
bunch of compile time, and it seems like the only way to deal with
variant induced compile time is to keep variant types out of headers.
Explicit template instantiation seems to do nothing for them.

I also seem to have gotten some back-end time improvement from
explicitly instantiating regex, but I don't know why. There is no
corresponding front-end time improvement from it: regex is still at the
top of the sinners list.

**** Templates that took longest to instantiate:
 15231 ms: std::basic_regex<char>::_M_compile (28 times, avg 543 ms)
 15066 ms: std::__detail::_Compiler<std::regex_traits<char>>::_Compiler (28 times, avg 538 ms)
 12571 ms: std::__detail::_Compiler<std::regex_traits<char>>::_M_disjunction (28 times, avg 448 ms)
 12454 ms: std::__detail::_Compiler<std::regex_traits<char>>::_M_alternative (28 times, avg 444 ms)
 12225 ms: std::__detail::_Compiler<std::regex_traits<char>>::_M_term (28 times, avg 436 ms)
 11363 ms: nlohmann::basic_json<>::parse<const char *> (21 times, avg 541 ms)
 10628 ms: nlohmann::basic_json<>::basic_json (109 times, avg 97 ms)
 10134 ms: std::__detail::_Compiler<std::regex_traits<char>>::_M_atom (28 times, avg 361 ms)

Back-end time before messing with the regex:
**** Function sets that took longest to compile / optimize:
  8076 ms: void boost::io::detail::put<$>(boost::io::detail::put_holder<$> cons... (177 times, avg 45 ms)
  4382 ms: std::_Rb_tree<$>::_M_erase(std::_Rb_tree_node<$>*) (1247 times, avg 3 ms)
  3137 ms: boost::stacktrace::detail::to_string_impl_base<boost::stacktrace::de... (137 times, avg 22 ms)
  2896 ms: void boost::io::detail::mk_str<$>(std::__cxx11::basic_string<$>&, ch... (177 times, avg 16 ms)
  2304 ms: std::_Rb_tree<$>::_M_get_insert_hint_unique_pos(std::_Rb_tree_const_... (210 times, avg 10 ms)
  2116 ms: bool std::__detail::_Compiler<$>::_M_expression_term<$>(std::__detai... (112 times, avg 18 ms)
  2051 ms: std::_Rb_tree_iterator<$> std::_Rb_tree<$>::_M_emplace_hint_unique<$... (244 times, avg 8 ms)
  2037 ms: toml::result<$> toml::detail::sequence<$>::invoke<$>(toml::detail::l... (93 times, avg 21 ms)
  1928 ms: std::__detail::_Compiler<$>::_M_quantifier() (28 times, avg 68 ms)
  1859 ms: nlohmann::json_abi_v3_11_3::detail::serializer<$>::dump(nlohmann::js... (41 times, avg 45 ms)
  1824 ms: std::_Function_handler<$>::_M_manager(std::_Any_data&, std::_Any_dat... (973 times, avg 1 ms)
  1810 ms: std::__detail::_BracketMatcher<$>::_BracketMatcher(std::__detail::_B... (112 times, avg 16 ms)
  1793 ms: nix::fetchers::GitInputScheme::fetch(nix::ref<$>, nix::fetchers::Inp... (1 times, avg 1793 ms)
  1759 ms: std::_Rb_tree<$>::_M_get_insert_unique_pos(std::__cxx11::basic_strin... (281 times, avg 6 ms)
  1722 ms: bool nlohmann::json_abi_v3_11_3::detail::parser<$>::sax_parse_intern... (19 times, avg 90 ms)
  1677 ms: boost::io::basic_altstringbuf<$>::overflow(int) (194 times, avg 8 ms)
  1674 ms: std::__cxx11::basic_string<$>::_M_mutate(unsigned long, unsigned lon... (249 times, avg 6 ms)
  1660 ms: std::_Rb_tree_node<$>* std::_Rb_tree<$>::_M_copy<$>(std::_Rb_tree_no... (304 times, avg 5 ms)
  1599 ms: bool nlohmann::json_abi_v3_11_3::detail::parser<$>::sax_parse_intern... (19 times, avg 84 ms)
  1568 ms: void std::__detail::_Compiler<$>::_M_insert_bracket_matcher<$>(bool) (112 times, avg 14 ms)
  1541 ms: std::__shared_ptr<$>::~__shared_ptr() (531 times, avg 2 ms)
  1539 ms: nlohmann::json_abi_v3_11_3::detail::serializer<$>::dump_escaped(std:... (41 times, avg 37 ms)
  1471 ms: void std::__detail::_Compiler<$>::_M_insert_character_class_matcher<... (112 times, avg 13 ms)

After messing with the regex (notice std::__detail::_Compiler vanishes
here, but I don't know why):

**** Function sets that took longest to compile / optimize:
  8054 ms: void boost::io::detail::put<$>(boost::io::detail::put_holder<$> cons... (177 times, avg 45 ms)
  4313 ms: std::_Rb_tree<$>::_M_erase(std::_Rb_tree_node<$>*) (1217 times, avg 3 ms)
  3259 ms: boost::stacktrace::detail::to_string_impl_base<boost::stacktrace::de... (137 times, avg 23 ms)
  3045 ms: void boost::io::detail::mk_str<$>(std::__cxx11::basic_string<$>&, ch... (177 times, avg 17 ms)
  2314 ms: std::_Rb_tree<$>::_M_get_insert_hint_unique_pos(std::_Rb_tree_const_... (207 times, avg 11 ms)
  1923 ms: std::_Rb_tree_iterator<$> std::_Rb_tree<$>::_M_emplace_hint_unique<$... (216 times, avg 8 ms)
  1817 ms: bool nlohmann::json_abi_v3_11_3::detail::parser<$>::sax_parse_intern... (18 times, avg 100 ms)
  1816 ms: toml::result<$> toml::detail::sequence<$>::invoke<$>(toml::detail::l... (93 times, avg 19 ms)
  1788 ms: nlohmann::json_abi_v3_11_3::detail::serializer<$>::dump(nlohmann::js... (40 times, avg 44 ms)
  1749 ms: std::_Rb_tree<$>::_M_get_insert_unique_pos(std::__cxx11::basic_strin... (278 times, avg 6 ms)
  1724 ms: std::__cxx11::basic_string<$>::_M_mutate(unsigned long, unsigned lon... (248 times, avg 6 ms)
  1697 ms: boost::io::basic_altstringbuf<$>::overflow(int) (194 times, avg 8 ms)
  1684 ms: nix::fetchers::GitInputScheme::fetch(nix::ref<$>, nix::fetchers::Inp... (1 times, avg 1684 ms)
  1680 ms: std::_Rb_tree_node<$>* std::_Rb_tree<$>::_M_copy<$>(std::_Rb_tree_no... (303 times, avg 5 ms)
  1589 ms: bool nlohmann::json_abi_v3_11_3::detail::parser<$>::sax_parse_intern... (18 times, avg 88 ms)
  1483 ms: non-virtual thunk to boost::wrapexcept<$>::~wrapexcept() (181 times, avg 8 ms)
  1447 ms: nlohmann::json_abi_v3_11_3::detail::serializer<$>::dump_escaped(std:... (40 times, avg 36 ms)
  1441 ms: std::__shared_ptr<$>::~__shared_ptr() (496 times, avg 2 ms)
  1420 ms: boost::stacktrace::basic_stacktrace<$>::init(unsigned long, unsigned... (137 times, avg 10 ms)
  1396 ms: boost::basic_format<$>::~basic_format() (194 times, avg 7 ms)
  1290 ms: std::__cxx11::basic_string<$>::_M_replace_cold(char*, unsigned long,... (231 times, avg 5 ms)
  1258 ms: std::vector<$>::~vector() (354 times, avg 3 ms)
  1222 ms: std::__cxx11::basic_string<$>::_M_replace(unsigned long, unsigned lo... (231 times, avg 5 ms)
  1194 ms: std::_Rb_tree<$>::_M_get_insert_hint_unique_pos(std::_Rb_tree_const_... (49 times, avg 24 ms)
  1186 ms: bool tao::pegtl::internal::sor<$>::match<$>(std::integer_sequence<$>... (1 times, avg 1186 ms)
  1149 ms: std::__detail::_Executor<$>::_M_dfs(std::__detail::_Executor<$>::_Ma... (70 times, avg 16 ms)
  1123 ms: toml::detail::sequence<$>::invoke(toml::detail::location&) (69 times, avg 16 ms)
  1110 ms: nlohmann::json_abi_v3_11_3::basic_json<$>::json_value::destroy(nlohm... (55 times, avg 20 ms)
  1079 ms: std::_Function_handler<$>::_M_manager(std::_Any_data&, std::_Any_dat... (541 times, avg 1 ms)
  1033 ms: nlohmann::json_abi_v3_11_3::detail::lexer<$>::scan_number() (20 times, avg 51 ms)

Change-Id: I10af282bcd4fc39c2d3caae3453e599e4639c70b
This commit is contained in:
jade 2024-08-23 16:33:48 -07:00 committed by Rebecca Turner
parent e6f2af06e6
commit 04f8a14833
Signed by: rbt
SSH key fingerprint: SHA256:SiNaEWabvotTldoNb5jIKqjJ3RnpS4aRXA4KLAdW5vs
64 changed files with 190 additions and 125 deletions

View file

@ -1,11 +1,7 @@
#include <cstdlib>
#include <cstring>
#include <algorithm>
#include <set>
#include <memory>
#include <string_view>
#include <tuple>
#include <iomanip>
#if __APPLE__
#include <sys/time.h>
#endif
@ -18,6 +14,7 @@
#include "build-result.hh"
#include "store-api.hh"
#include "derivations.hh"
#include "strings.hh"
#include "local-store.hh"
#include "legacy.hh"
#include "experimental-features.hh"

View file

@ -1,6 +1,7 @@
#include "editor-for.hh"
#include "environment-variables.hh"
#include "source-path.hh"
#include "strings.hh"
namespace nix {

View file

@ -19,6 +19,7 @@
#include "gc-small-vector.hh"
#include "fetch-to-store.hh"
#include "flake/flakeref.hh"
#include "exit.hh"
#include <algorithm>
#include <iostream>

View file

@ -12,6 +12,7 @@
#include "experimental-features.hh"
#include "search-path.hh"
#include "repl-exit-status.hh"
#include "backed-string-view.hh"
#include <map>
#include <optional>
@ -791,4 +792,4 @@ static constexpr std::string_view corepkgsPrefix{"/__corepkgs__/"};
}
#include "eval-inline.hh"
#include "eval-inline.hh" // IWYU pragma: keep

View file

@ -11,6 +11,7 @@
#include "eval-error.hh"
#include "pos-idx.hh"
#include "pos-table.hh"
#include "strings.hh"
namespace nix {

View file

@ -7,6 +7,8 @@
#include "path.hh"
#include "attrs.hh"
#include "url.hh"
#include "ref.hh"
#include "strings.hh"
#include <memory>

View file

@ -1,8 +1,9 @@
#include "progress-bar.hh"
#include "file-system.hh"
#include "sync.hh"
#include "store-api.hh"
#include "names.hh"
#include "terminal.hh"
#include "strings.hh"
#include <map>
#include <thread>

View file

@ -6,6 +6,8 @@
#include "loggers.hh"
#include "current-process.hh"
#include "terminal.hh"
#include "strings.hh"
#include "exit.hh"
#include <algorithm>
#include <exception>

View file

@ -7,12 +7,10 @@
#include "path.hh"
#include "derived-path.hh"
#include "processes.hh"
#include "exit.hh"
#include "strings.hh"
#include <signal.h>
#include <locale>
namespace nix {

View file

@ -10,6 +10,7 @@
#include "nar-accessor.hh"
#include "thread-pool.hh"
#include "signals.hh"
#include "strings.hh"
#include <chrono>
#include <regex>

View file

@ -9,6 +9,7 @@
#include "logging-json.hh"
#include "substitution-goal.hh"
#include "drv-output-substitution-goal.hh"
#include "strings.hh"
#include <fstream>
#include <sys/types.h>

View file

@ -2,6 +2,7 @@
#include "substitution-goal.hh"
#include "derivation-goal.hh"
#include "local-store.hh"
#include "strings.hh"
namespace nix {

View file

@ -2,6 +2,7 @@
#include "file-system.hh"
#include "globals.hh"
#include "hook-instance.hh"
#include "strings.hh"
namespace nix {

View file

@ -1,15 +1,12 @@
#include "local-derivation-goal.hh"
#include "indirect-root-store.hh"
#include "hook-instance.hh"
#include "machines.hh"
#include "store-api.hh"
#include "worker.hh"
#include "builtins.hh"
#include "builtins/buildenv.hh"
#include "path-references.hh"
#include "finally.hh"
#include "archive.hh"
#include "compression.hh"
#include "daemon.hh"
#include "topo-sort.hh"
#include "json-utils.hh"
@ -19,6 +16,7 @@
#include "child.hh"
#include "unix-domain-socket.hh"
#include "mount.hh"
#include "strings.hh"
#include <regex>
#include <queue>

View file

@ -1,4 +1,5 @@
#include "buildenv.hh"
#include "strings.hh"
#include <sys/stat.h>
#include <sys/types.h>

View file

@ -3,6 +3,7 @@
#include "store-api.hh"
#include "archive.hh"
#include "compression.hh"
#include "strings.hh"
namespace nix {

View file

@ -1,6 +1,7 @@
#include "args.hh"
#include "content-address.hh"
#include "split.hh"
#include "strings.hh"
namespace nix {

View file

@ -2,7 +2,7 @@
#include "monitor-fd.hh"
#include "worker-protocol.hh"
#include "worker-protocol-impl.hh"
#include "build-result.hh"
#include "build-result.hh" // IWYU pragma: keep
#include "store-api.hh"
#include "store-cast.hh"
#include "gc-store.hh"
@ -12,6 +12,7 @@
#include "finally.hh"
#include "archive.hh"
#include "derivations.hh"
#include "strings.hh"
#include "args.hh"
#include <sstream>

View file

@ -3,11 +3,12 @@
#include "store-api.hh"
#include "globals.hh"
#include "types.hh"
#include "split.hh"
#include "common-protocol.hh"
#include "common-protocol-impl.hh"
#include "fs-accessor.hh"
#include "json-utils.hh"
#include "strings.hh"
#include "backed-string-view.hh"
#include <boost/container/small_vector.hpp>
#include <nlohmann/json.hpp>

View file

@ -5,6 +5,7 @@
#include "path.hh"
#include "outputs-spec.hh"
#include "comparator.hh"
#include "ref.hh"
#include <variant>

View file

@ -5,6 +5,7 @@
#include "s3.hh"
#include "signals.hh"
#include "compression.hh"
#include "strings.hh"
#if ENABLE_S3
#include <aws/core/client/ClientConfiguration.h>

View file

@ -2,6 +2,7 @@
///@file
#include "box_ptr.hh"
#include "ref.hh"
#include "logging.hh"
#include "serialise.hh"
#include "types.hh"

View file

@ -5,6 +5,7 @@
#include "signals.hh"
#include "finally.hh"
#include "unix-domain-socket.hh"
#include "strings.hh"
#include <queue>
#include <regex>

View file

@ -9,6 +9,7 @@
#include "path-with-outputs.hh"
#include "ssh.hh"
#include "ssh-store.hh"
#include "strings.hh"
#include "derivations.hh"
namespace nix {

View file

@ -10,6 +10,7 @@
#include "signals.hh"
#include "finally.hh"
#include "compression.hh"
#include "strings.hh"
#include <algorithm>
#include <cstring>

View file

@ -1,11 +1,10 @@
#pragma once
///@file
#include "types.hh"
#include <optional>
#include <memory>
#include <sys/types.h>
#include <vector>
namespace nix {

View file

@ -1,7 +1,9 @@
#pragma once
///@file
#include "types.hh"
#include "ref.hh"
#include <set>
#include <vector>
namespace nix {

View file

@ -1,5 +1,6 @@
#include "make-content-addressed.hh"
#include "references.hh"
#include "strings.hh"
namespace nix {

View file

@ -1,12 +1,12 @@
#include "derivations.hh"
#include "parsed-derivations.hh"
#include "globals.hh"
#include "local-store.hh"
#include "store-api.hh"
#include "thread-pool.hh"
#include "topo-sort.hh"
#include "closure.hh"
#include "filetransfer.hh"
#include "strings.hh"
namespace nix {

View file

@ -1,10 +1,12 @@
#pragma once
///@file
#include "fs-accessor.hh"
#include "ref.hh"
#include <functional>
#include <nlohmann/json_fwd.hpp>
#include "fs-accessor.hh"
namespace nix {

View file

@ -4,6 +4,7 @@
#include "sqlite.hh"
#include "globals.hh"
#include "users.hh"
#include "strings.hh"
#include <sqlite3.h>
#include <nlohmann/json.hpp>

View file

@ -1,4 +1,4 @@
#include "globals.hh"
#include "strings.hh"
#include "nar-info.hh"
#include "store-api.hh"

View file

@ -1,6 +1,7 @@
#include "local-store.hh"
#include "globals.hh"
#include "signals.hh"
#include "strings.hh"
#include <cstring>
#include <sys/types.h>

View file

@ -1,4 +1,5 @@
#include "parsed-derivations.hh"
#include "strings.hh"
#include <nlohmann/json.hpp>
#include <regex>

View file

@ -1,5 +1,6 @@
#include "path-info.hh"
#include "store-api.hh"
#include "strings.hh"
namespace nix {

View file

@ -1,7 +1,6 @@
#include "path-with-outputs.hh"
#include "store-api.hh"
#include <regex>
#include "strings.hh"
namespace nix {

View file

@ -2,6 +2,7 @@
#include "signals.hh"
#include "platform/darwin.hh"
#include "regex.hh"
#include "strings.hh"
#include <sys/proc_info.h>
#include <sys/sysctl.h>

View file

@ -5,6 +5,7 @@
#include "signals.hh"
#include "platform/linux.hh"
#include "regex.hh"
#include "strings.hh"
#include <grp.h>
#include <regex>

View file

@ -1,12 +1,11 @@
#include "profiles.hh"
#include "store-api.hh"
#include "local-fs-store.hh"
#include "users.hh"
#include "strings.hh"
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <errno.h>
#include <stdio.h>

View file

@ -15,6 +15,8 @@
#include "finally.hh"
#include "logging.hh"
#include "filetransfer.hh"
#include "strings.hh"
#include <nlohmann/json.hpp>
namespace nix {
@ -64,7 +66,7 @@ void RemoteStore::initConnection(Connection & conn)
{
/* Send the magic greeting, check for the reply. */
try {
conn.from.endOfFileError = "Nix daemon disconnected unexpectedly (maybe it crashed?)";
conn.from.specialEndOfFileError = "Nix daemon disconnected unexpectedly (maybe it crashed?)";
conn.to << WORKER_MAGIC_1;
conn.to.flush();

View file

@ -7,6 +7,7 @@
#include "globals.hh"
#include "compression.hh"
#include "filetransfer.hh"
#include "strings.hh"
#include <aws/core/Aws.h>
#include <aws/core/VersionConfig.h>

View file

@ -1,13 +1,11 @@
#include "ssh-store.hh"
#include "store-api.hh"
#include "local-fs-store.hh"
#include "remote-store.hh"
#include "remote-store-connection.hh"
#include "remote-fs-accessor.hh"
#include "archive.hh"
#include "worker-protocol.hh"
#include "pool.hh"
#include "ssh.hh"
#include "strings.hh"
namespace nix {

View file

@ -5,10 +5,10 @@
#include "nar-info-disk-cache.hh"
#include "thread-pool.hh"
#include "url.hh"
#include "references.hh"
#include "archive.hh"
#include "remote-store.hh"
#include "uds-remote-store.hh"
#include "signals.hh"
#include "strings.hh"
// FIXME this should not be here, see TODO below on
// `addMultipleToStore`.
#include "worker-protocol.hh"

View file

@ -1,10 +1,11 @@
#include "args.hh"
#include "args/root.hh"
#include "hash.hh"
#include "json-utils.hh"
#include "strings.hh"
#include "json-utils.hh" // IWYU pragma: keep (instances)
#include "environment-variables.hh"
#include "experimental-features-json.hh"
#include "experimental-features-json.hh" // IWYU pragma: keep (instances)
#include "logging.hh"
#include <glob.h>

View file

@ -3,6 +3,8 @@
#include "experimental-features.hh"
#include "types.hh"
#include "ref.hh"
#include <functional>
#include <map>
#include <memory>

View file

@ -0,0 +1,69 @@
#pragma once
/// @file String view that can be either owned or borrowed.
#include <variant>
#include <string>
#include <string_view>
/**
* This wants to be a little bit like rust's Cow type.
* Some parts of the evaluator benefit greatly from being able to reuse
* existing allocations for strings, but have to be able to also use
* newly allocated storage for values.
*
* We do not define implicit conversions, even with ref qualifiers,
* since those can easily become ambiguous to the reader and can degrade
* into copying behaviour we want to avoid.
*/
class BackedStringView {
private:
std::variant<std::string, std::string_view> data;
/**
* Needed to introduce a temporary since operator-> must return
* a pointer. Without this we'd need to store the view object
* even when we already own a string.
*/
class Ptr {
private:
std::string_view view;
public:
Ptr(std::string_view view): view(view) {}
const std::string_view * operator->() const { return &view; }
};
public:
BackedStringView(std::string && s): data(std::move(s)) {}
BackedStringView(std::string_view sv): data(sv) {}
template<size_t N>
BackedStringView(const char (& lit)[N]): data(std::string_view(lit)) {}
BackedStringView(const BackedStringView &) = delete;
BackedStringView & operator=(const BackedStringView &) = delete;
/**
* We only want move operations defined since the sole purpose of
* this type is to avoid copies.
*/
BackedStringView(BackedStringView && other) = default;
BackedStringView & operator=(BackedStringView && other) = default;
bool isOwned() const
{
return std::holds_alternative<std::string>(data);
}
std::string toOwned() &&
{
return isOwned()
? std::move(std::get<std::string>(data))
: std::string(std::get<std::string_view>(data));
}
std::string_view operator*() const
{
return isOwned()
? std::get<std::string>(data)
: std::get<std::string_view>(data);
}
Ptr operator->() const { return Ptr(**this); }
};

View file

@ -3,6 +3,7 @@
#include "logging.hh"
#include "position.hh"
#include "terminal.hh"
#include "strings.hh"
#include <iostream>
#include <optional>

View file

@ -10,6 +10,7 @@
#include "logging.hh"
#include "serialise.hh"
#include "signals.hh"
#include "strings.hh"
#include "types.hh"
#include "users.hh"

View file

@ -10,6 +10,7 @@
#include "charptr-cast.hh"
#include "logging.hh"
#include "split.hh"
#include "strings.hh"
#include <sys/types.h>
#include <sys/stat.h>

View file

@ -53,6 +53,7 @@ libutil_headers = files(
'archive.hh',
'args/root.hh',
'args.hh',
'backed-string-view.hh',
'box_ptr.hh',
'canon-path.hh',
'cgroup.hh',

View file

@ -3,6 +3,7 @@
#include "finally.hh"
#include "logging.hh"
#include "processes.hh"
#include "strings.hh"
#include "serialise.hh"
#include "signals.hh"

View file

@ -1,6 +1,9 @@
#include <string>
#include <regex>
// Declared as extern in precompiled-headers.hh
template class std::basic_regex<char>;
namespace nix::regex {
std::string quoteRegexChars(const std::string & raw)
{

View file

@ -9,6 +9,21 @@
namespace nix {
namespace {
/**
* Convert a little-endian integer to host order.
*/
template<typename T>
T readLittleEndian(unsigned char * p)
{
T x = 0;
for (size_t i = 0; i < sizeof(x); ++i, ++p) {
x |= ((T) *p) << (i * 8);
}
return x;
}
}
template<typename T>
T readNum(Source & source)
{
@ -152,7 +167,7 @@ size_t FdSource::readUnbuffered(char * data, size_t len)
n = ::read(fd, data, len);
} while (n == -1 && errno == EINTR);
if (n == -1) { _good = false; throw SysError("reading from file"); }
if (n == 0) { _good = false; throw EndOfFile(std::string(*endOfFileError)); }
if (n == 0) { _good = false; throw EndOfFile(endOfFileError()); }
read += n;
return n;
}
@ -163,6 +178,11 @@ bool FdSource::good()
return _good;
}
std::string FdSource::endOfFileError() const
{
return specialEndOfFileError.has_value() ? *specialEndOfFileError : "unexpected end-of-file";
}
size_t StringSource::read(char * data, size_t len)
{

View file

@ -1,12 +1,10 @@
#pragma once
///@file
#include <concepts>
#include <memory>
#include "charptr-cast.hh"
#include "generator.hh"
#include "strings.hh"
#include "types.hh"
#include "file-descriptor.hh"
@ -153,7 +151,10 @@ struct FdSource : BufferedSource
{
int fd;
size_t read = 0;
BackedStringView endOfFileError{"unexpected end-of-file"};
/** Defaults to "unexpected end-of-file" */
std::optional<std::string> specialEndOfFileError;
std::string endOfFileError() const;
FdSource() : fd(-1) { }
FdSource(int fd) : fd(fd) { }

View file

@ -1,4 +1,5 @@
#include "source-path.hh"
#include "strings.hh"
namespace nix {

View file

@ -164,19 +164,6 @@ template<class N>
std::optional<N> string2Float(const std::string_view s);
/**
* Convert a little-endian integer to host order.
*/
template<typename T>
T readLittleEndian(unsigned char * p)
{
T x = 0;
for (size_t i = 0; i < sizeof(x); ++i, ++p) {
x |= ((T) *p) << (i * 8);
}
return x;
}
/**
* Convert a string to lower case.
*/

View file

@ -1,17 +1,15 @@
#pragma once
///@file
#include "ref.hh"
#include <list>
#include <optional>
#include <set>
#include <string>
#include <limits>
#include <string_view>
#include <map>
#include <variant>
#include <vector>
#include <span>
#include <stdint.h> // IWYU pragma: keep (this is used literally everywhere)
namespace nix {
@ -166,70 +164,4 @@ constexpr auto enumerate(T && iterable)
template<class... Ts> struct overloaded : Ts... { using Ts::operator()...; };
template<class... Ts> overloaded(Ts...) -> overloaded<Ts...>;
/**
* This wants to be a little bit like rust's Cow type.
* Some parts of the evaluator benefit greatly from being able to reuse
* existing allocations for strings, but have to be able to also use
* newly allocated storage for values.
*
* We do not define implicit conversions, even with ref qualifiers,
* since those can easily become ambiguous to the reader and can degrade
* into copying behaviour we want to avoid.
*/
class BackedStringView {
private:
std::variant<std::string, std::string_view> data;
/**
* Needed to introduce a temporary since operator-> must return
* a pointer. Without this we'd need to store the view object
* even when we already own a string.
*/
class Ptr {
private:
std::string_view view;
public:
Ptr(std::string_view view): view(view) {}
const std::string_view * operator->() const { return &view; }
};
public:
BackedStringView(std::string && s): data(std::move(s)) {}
BackedStringView(std::string_view sv): data(sv) {}
template<size_t N>
BackedStringView(const char (& lit)[N]): data(std::string_view(lit)) {}
BackedStringView(const BackedStringView &) = delete;
BackedStringView & operator=(const BackedStringView &) = delete;
/**
* We only want move operations defined since the sole purpose of
* this type is to avoid copies.
*/
BackedStringView(BackedStringView && other) = default;
BackedStringView & operator=(BackedStringView && other) = default;
bool isOwned() const
{
return std::holds_alternative<std::string>(data);
}
std::string toOwned() &&
{
return isOwned()
? std::move(std::get<std::string>(data))
: std::string(std::get<std::string_view>(data));
}
std::string_view operator*() const
{
return isOwned()
? std::get<std::string>(data)
: std::get<std::string_view>(data);
}
Ptr operator->() const { return Ptr(**this); }
};
}

View file

@ -1,6 +1,7 @@
#include "archive.hh"
#include "derivations.hh"
#include "dotgraph.hh"
#include "exit.hh"
#include "globals.hh"
#include "build-result.hh"
#include "store-cast.hh"
@ -17,7 +18,6 @@
#include <iostream>
#include <algorithm>
#include <cstdio>
#include <sys/types.h>
#include <sys/stat.h>

View file

@ -7,6 +7,7 @@
#include "store-api.hh"
#include "local-fs-store.hh"
#include "worker-protocol.hh"
#include "exit.hh"
using namespace nix;

View file

@ -1,10 +1,9 @@
#include "command.hh"
#include "shared.hh"
#include "store-api.hh"
#include "sync.hh"
#include "thread-pool.hh"
#include "signals.hh"
#include "references.hh"
#include "exit.hh"
#include <atomic>

View file

@ -58,3 +58,19 @@
#include <unistd.h>
#include <nlohmann/json.hpp>
// This stuff is here to force the compiler to actually apply the extern
// template directives in all compilation units. To borrow a term, under
// complex microarchitectural conditions, clang ignores the extern template
// declaration, as revealed in the profile.
//
// In most cases, extern template works fine in the header itself. We don't
// have any idea why this happens.
// Here because of all the regexes everywhere (it is infeasible to block instantiation everywhere)
// For some reason this does not actually prevent the instantiation of
// regex::_M_compile, and the regex compiler (my interpretation of what this is
// supposed to do is make the template bits out-of-line), but it *does* prevent
// a bunch of codegen of regex stuff, which seems to save about 30s on-cpu.
// Instantiated in libutil/regex.cc.
extern template class std::basic_regex<char>;

View file

@ -1,8 +1,8 @@
#include <nlohmann/json.hpp>
#include <gtest/gtest.h>
#include "experimental-features.hh"
#include "derivations.hh"
#include "strings.hh"
#include "tests/libstore.hh"
#include "tests/characterization.hh"

View file

@ -1,6 +1,7 @@
#include <nlohmann/json.hpp>
#include <gtest/gtest.h>
#include "strings.hh"
#include "tests/libstore.hh"
#include "tests/characterization.hh"

View file

@ -1,4 +1,5 @@
#include "references.hh"
#include "strings.hh"
#include <gtest/gtest.h>
namespace nix {