Skip to content

Commit

Permalink
Merge pull request #10812 from Mic92/build-perf
Browse files Browse the repository at this point in the history
Remove 100s of CPU time (10%) from build times (1465s -> 1302s)
  • Loading branch information
roberth authored May 31, 2024
2 parents 138aa2b + 473d2d5 commit 84e1163
Show file tree
Hide file tree
Showing 17 changed files with 59 additions and 31 deletions.
2 changes: 1 addition & 1 deletion src/libexpr/eval.cc
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,13 @@
#include <algorithm>
#include <chrono>
#include <iostream>
#include <sstream>
#include <cstring>
#include <optional>
#include <unistd.h>
#include <sys/time.h>
#include <fstream>
#include <functional>
#include <iostream>

#include <nlohmann/json.hpp>
#include <boost/container/small_vector.hpp>
Expand Down
9 changes: 4 additions & 5 deletions src/libexpr/lexer.l
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,6 @@
#pragma clang diagnostic ignored "-Wunneeded-internal-declaration"
#endif

#include <boost/lexical_cast.hpp>

#include "nixexpr.hh"
#include "parser-tab.hh"

Expand Down Expand Up @@ -129,9 +127,10 @@ or { return OR_KW; }

{ID} { yylval->id = {yytext, (size_t) yyleng}; return ID; }
{INT} { errno = 0;
try {
yylval->n = boost::lexical_cast<int64_t>(yytext);
} catch (const boost::bad_lexical_cast &) {
std::optional<int64_t> numMay = string2Int<int64_t>(yytext);
if (numMay.has_value()) {
yylval->n = *numMay;
} else {
throw ParseError(ErrorInfo{
.msg = HintFmt("invalid integer '%1%'", yytext),
.pos = state->positions[CUR_POS],
Expand Down
1 change: 1 addition & 0 deletions src/libexpr/nixexpr.cc
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
#include "print.hh"

#include <cstdlib>
#include <sstream>

namespace nix {

Expand Down
1 change: 1 addition & 0 deletions src/libexpr/primops.cc
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@

#include <algorithm>
#include <cstring>
#include <sstream>
#include <regex>

#ifndef _WIN32
Expand Down
1 change: 1 addition & 0 deletions src/libexpr/print.cc
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#include <limits>
#include <unordered_set>
#include <sstream>

#include "print.hh"
#include "ansicolor.hh"
Expand Down
1 change: 1 addition & 0 deletions src/libmain/progress-bar.cc
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#include <atomic>
#include <map>
#include <thread>
#include <sstream>
#include <iostream>
#include <chrono>

Expand Down
1 change: 1 addition & 0 deletions src/libstore/binary-cache-store.cc
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
#include <future>
#include <regex>
#include <fstream>
#include <sstream>

#include <nlohmann/json.hpp>

Expand Down
2 changes: 2 additions & 0 deletions src/libstore/daemon.cc
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
# include "monitor-fd.hh"
#endif

#include <sstream>

namespace nix::daemon {

Sink & operator << (Sink & sink, const Logger::Fields & fields)
Expand Down
2 changes: 1 addition & 1 deletion src/libstore/machines.cc
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ static Machine parseBuilderLine(const std::set<std::string> & defaultSystems, co
};

auto parseFloatField = [&](size_t fieldIndex) {
const auto result = string2Int<float>(tokens[fieldIndex]);
const auto result = string2Float<float>(tokens[fieldIndex]);
if (!result) {
throw FormatError("bad machine specification: failed to convert column #%lu in a row: '%s' to 'float'", fieldIndex, line);
}
Expand Down
1 change: 1 addition & 0 deletions src/libutil/current-process.cc
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#include "file-system.hh"
#include "processes.hh"
#include "signals.hh"
#include <math.h>

#ifdef __APPLE__
# include <mach-o/dyld.h>
Expand Down
2 changes: 0 additions & 2 deletions src/libutil/file-system.hh
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,6 @@
#endif
#include <signal.h>

#include <boost/lexical_cast.hpp>

#include <atomic>
#include <functional>
#include <map>
Expand Down
1 change: 1 addition & 0 deletions src/libutil/logging.cc
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#include "position.hh"

#include <atomic>
#include <sstream>
#include <nlohmann/json.hpp>
#include <iostream>

Expand Down
2 changes: 0 additions & 2 deletions src/libutil/processes.hh
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,6 @@
#include <unistd.h>
#include <signal.h>

#include <boost/lexical_cast.hpp>

#include <atomic>
#include <functional>
#include <map>
Expand Down
39 changes: 39 additions & 0 deletions src/libutil/util.cc
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
#include <regex>

#include <sodium.h>
#include <boost/lexical_cast.hpp>
#include <stdint.h>

#ifdef NDEBUG
#error "Nix may not be built with assertions disabled (i.e. with -DNDEBUG)."
Expand Down Expand Up @@ -111,6 +113,43 @@ std::string rewriteStrings(std::string s, const StringMap & rewrites)
return s;
}

template<class N>
std::optional<N> string2Int(const std::string_view s)
{
if (s.substr(0, 1) == "-" && !std::numeric_limits<N>::is_signed)
return std::nullopt;
try {
return boost::lexical_cast<N>(s.data(), s.size());
} catch (const boost::bad_lexical_cast &) {
return std::nullopt;
}
}

// Explicitly instantiated in one place for faster compilation
template std::optional<unsigned char> string2Int<unsigned char>(const std::string_view s);
template std::optional<unsigned short> string2Int<unsigned short>(const std::string_view s);
template std::optional<unsigned int> string2Int<unsigned int>(const std::string_view s);
template std::optional<unsigned long> string2Int<unsigned long>(const std::string_view s);
template std::optional<unsigned long long> string2Int<unsigned long long>(const std::string_view s);
template std::optional<signed char> string2Int<signed char>(const std::string_view s);
template std::optional<signed short> string2Int<signed short>(const std::string_view s);
template std::optional<signed int> string2Int<signed int>(const std::string_view s);
template std::optional<signed long> string2Int<signed long>(const std::string_view s);
template std::optional<signed long long> string2Int<signed long long>(const std::string_view s);

template<class N>
std::optional<N> string2Float(const std::string_view s)
{
try {
return boost::lexical_cast<N>(s.data(), s.size());
} catch (const boost::bad_lexical_cast &) {
return std::nullopt;
}
}

template std::optional<double> string2Float<double>(const std::string_view s);
template std::optional<float> string2Float<float>(const std::string_view s);


bool hasPrefix(std::string_view s, std::string_view prefix)
{
Expand Down
21 changes: 2 additions & 19 deletions src/libutil/util.hh
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
#include "error.hh"
#include "logging.hh"

#include <boost/lexical_cast.hpp>

#include <functional>
#include <map>
Expand Down Expand Up @@ -102,16 +101,7 @@ std::string rewriteStrings(std::string s, const StringMap & rewrites);
* Parse a string into an integer.
*/
template<class N>
std::optional<N> string2Int(const std::string_view s)
{
if (s.substr(0, 1) == "-" && !std::numeric_limits<N>::is_signed)
return std::nullopt;
try {
return boost::lexical_cast<N>(s.data(), s.size());
} catch (const boost::bad_lexical_cast &) {
return std::nullopt;
}
}
std::optional<N> string2Int(const std::string_view s);

/**
* Like string2Int(), but support an optional suffix 'K', 'M', 'G' or
Expand Down Expand Up @@ -141,14 +131,7 @@ N string2IntWithUnitPrefix(std::string_view s)
* Parse a string into a float.
*/
template<class N>
std::optional<N> string2Float(const std::string_view s)
{
try {
return boost::lexical_cast<N>(s.data(), s.size());
} catch (const boost::bad_lexical_cast &) {
return std::nullopt;
}
}
std::optional<N> string2Float(const std::string_view s);


/**
Expand Down
3 changes: 2 additions & 1 deletion src/nix-env/user-env.cc
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,9 @@
#include "eval-inline.hh"
#include "profiles.hh"
#include "print-ambiguous.hh"
#include <limits>

#include <limits>
#include <sstream>

namespace nix {

Expand Down
1 change: 1 addition & 0 deletions src/nix/develop.cc
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

#include <iterator>
#include <memory>
#include <sstream>
#include <nlohmann/json.hpp>
#include <algorithm>

Expand Down

0 comments on commit 84e1163

Please sign in to comment.