From d8c97d8073cbeb5f62face1d905dd9b3b05e43ca Mon Sep 17 00:00:00 2001 From: Sergei Zimmerman Date: Fri, 2 May 2025 17:40:29 +0000 Subject: [PATCH 1/4] treewide: Use StringSet alias consistently instead of std::set The intention is to switch to transparent comparators from N3657 for ordered set containers for strings and using the alias consistently would simplify things. --- src/build-remote/build-remote.cc | 4 ++-- src/libcmd/command.cc | 2 +- src/libcmd/include/nix/cmd/command.hh | 2 +- src/libcmd/installable-attr-path.cc | 2 +- src/libcmd/installable-flake.cc | 2 +- src/libexpr/attr-path.cc | 2 +- src/libexpr/eval-cache.cc | 2 +- src/libexpr/eval.cc | 4 ++-- src/libfetchers/mercurial.cc | 2 +- src/libfetchers/tarball.cc | 4 ++-- src/libflake/config.cc | 2 +- src/libflake/flake.cc | 2 +- src/libmain/plugin.cc | 2 +- src/libstore-tests/common-protocol.cc | 2 +- src/libstore-tests/outputs-spec.cc | 2 +- src/libstore-tests/serve-protocol.cc | 2 +- src/libstore-tests/worker-protocol.cc | 2 +- src/libstore/derived-path-map.cc | 12 ++++++------ src/libstore/dummy-store.cc | 2 +- .../include/nix/store/derived-path-map.hh | 14 +++++++------- src/libstore/include/nix/store/globals.hh | 2 +- .../include/nix/store/http-binary-cache-store.hh | 4 ++-- .../include/nix/store/legacy-ssh-store.hh | 2 +- .../nix/store/local-binary-cache-store.hh | 2 +- .../include/nix/store/local-overlay-store.hh | 2 +- src/libstore/include/nix/store/local-store.hh | 2 +- src/libstore/include/nix/store/machines.hh | 12 ++++++------ .../include/nix/store/path-with-outputs.hh | 2 +- .../include/nix/store/s3-binary-cache-store.hh | 2 +- src/libstore/include/nix/store/ssh-store.hh | 4 ++-- src/libstore/include/nix/store/store-api.hh | 2 +- .../include/nix/store/uds-remote-store.hh | 2 +- src/libstore/local-binary-cache-store.cc | 2 +- src/libstore/local-store.cc | 2 +- src/libstore/machines.cc | 16 ++++++++-------- src/libstore/path-with-outputs.cc | 4 ++-- src/libutil/args.cc | 2 +- src/libutil/configuration.cc | 6 +++--- src/libutil/experimental-features.cc | 2 +- src/libutil/hash.cc | 4 ++-- src/libutil/include/nix/util/args.hh | 4 ++-- src/libutil/include/nix/util/configuration.hh | 12 ++++++------ .../include/nix/util/experimental-features.hh | 2 +- src/libutil/include/nix/util/hash.hh | 4 ++-- src/libutil/include/nix/util/strings.hh | 10 ++++++---- src/libutil/include/nix/util/suggestions.hh | 2 +- src/libutil/strings.cc | 8 ++++---- src/libutil/suggestions.cc | 2 +- src/nix-build/nix-build.cc | 2 +- src/nix-env/nix-env.cc | 4 ++-- src/nix/develop.cc | 6 +++--- src/nix/diff-closures.cc | 10 +++++----- src/nix/flake.cc | 2 +- src/nix/profile.cc | 6 +++--- src/nix/why-depends.cc | 2 +- 55 files changed, 111 insertions(+), 109 deletions(-) diff --git a/src/build-remote/build-remote.cc b/src/build-remote/build-remote.cc index 60247b735..d967548f5 100644 --- a/src/build-remote/build-remote.cc +++ b/src/build-remote/build-remote.cc @@ -42,7 +42,7 @@ static AutoCloseFD openSlotLock(const Machine & m, uint64_t slot) return openLockFile(fmt("%s/%s-%d", currentLoad, escapeUri(m.storeUri.render()), slot), true); } -static bool allSupportedLocally(Store & store, const std::set& requiredFeatures) { +static bool allSupportedLocally(Store & store, const StringSet& requiredFeatures) { for (auto & feature : requiredFeatures) if (!store.systemFeatures.get().count(feature)) return false; return true; @@ -113,7 +113,7 @@ static int main_build_remote(int argc, char * * argv) auto amWilling = readInt(source); auto neededSystem = readString(source); drvPath = store->parseStorePath(readString(source)); - auto requiredFeatures = readStrings>(source); + auto requiredFeatures = readStrings(source); /* It would be possible to build locally after some builds clear out, so don't show the warning now: */ diff --git a/src/libcmd/command.cc b/src/libcmd/command.cc index 047906786..ffb745742 100644 --- a/src/libcmd/command.cc +++ b/src/libcmd/command.cc @@ -40,7 +40,7 @@ nlohmann::json NixMultiCommand::toJSON() void NixMultiCommand::run() { if (!command) { - std::set subCommandTextLines; + StringSet subCommandTextLines; for (auto & [name, _] : commands) subCommandTextLines.insert(fmt("- `%s`", name)); std::string markdownError = diff --git a/src/libcmd/include/nix/cmd/command.hh b/src/libcmd/include/nix/cmd/command.hh index 92377be6b..45739fdb2 100644 --- a/src/libcmd/include/nix/cmd/command.hh +++ b/src/libcmd/include/nix/cmd/command.hh @@ -363,7 +363,7 @@ void completeFlakeRefWithFragment( const Strings & defaultFlakeAttrPaths, std::string_view prefix); -std::string showVersions(const std::set & versions); +std::string showVersions(const StringSet & versions); void printClosureDiff( ref store, const StorePath & beforePath, const StorePath & afterPath, std::string_view indent); diff --git a/src/libcmd/installable-attr-path.cc b/src/libcmd/installable-attr-path.cc index fcbfe1482..7783b4f40 100644 --- a/src/libcmd/installable-attr-path.cc +++ b/src/libcmd/installable-attr-path.cc @@ -72,7 +72,7 @@ DerivedPathsWithInfo InstallableAttrPath::toDerivedPaths() auto newOutputs = std::visit(overloaded { [&](const ExtendedOutputsSpec::Default & d) -> OutputsSpec { - std::set outputsToInstall; + StringSet outputsToInstall; for (auto & output : packageInfo.queryOutputs(false, true)) outputsToInstall.insert(output.first); if (outputsToInstall.empty()) diff --git a/src/libcmd/installable-flake.cc b/src/libcmd/installable-flake.cc index 83285b739..85a4188a7 100644 --- a/src/libcmd/installable-flake.cc +++ b/src/libcmd/installable-flake.cc @@ -117,7 +117,7 @@ DerivedPathsWithInfo InstallableFlake::toDerivedPaths() .drvPath = makeConstantStorePathRef(std::move(drvPath)), .outputs = std::visit(overloaded { [&](const ExtendedOutputsSpec::Default & d) -> OutputsSpec { - std::set outputsToInstall; + StringSet outputsToInstall; if (auto aOutputSpecified = attr->maybeGetAttr(state->sOutputSpecified)) { if (aOutputSpecified->getBool()) { if (auto aOutputName = attr->maybeGetAttr("outputName")) diff --git a/src/libexpr/attr-path.cc b/src/libexpr/attr-path.cc index cee805d14..722b57bbf 100644 --- a/src/libexpr/attr-path.cc +++ b/src/libexpr/attr-path.cc @@ -74,7 +74,7 @@ std::pair findAlongAttrPath(EvalState & state, const std::strin auto a = v->attrs()->get(state.symbols.create(attr)); if (!a) { - std::set attrNames; + StringSet attrNames; for (auto & attr : *v->attrs()) attrNames.insert(std::string(state.symbols[attr.name])); diff --git a/src/libexpr/eval-cache.cc b/src/libexpr/eval-cache.cc index 30aa6076a..0d732f59c 100644 --- a/src/libexpr/eval-cache.cc +++ b/src/libexpr/eval-cache.cc @@ -492,7 +492,7 @@ Value & AttrCursor::forceValue() Suggestions AttrCursor::getSuggestionsForAttr(Symbol name) { auto attrNames = getAttrs(); - std::set strAttrNames; + StringSet strAttrNames; for (auto & name : attrNames) strAttrNames.insert(std::string(root->state.symbols[name])); diff --git a/src/libexpr/eval.cc b/src/libexpr/eval.cc index 10a33c042..ab7f33d5f 100644 --- a/src/libexpr/eval.cc +++ b/src/libexpr/eval.cc @@ -1446,7 +1446,7 @@ void ExprSelect::eval(EvalState & state, Env & env, Value & v) } else { state.forceAttrs(*vAttrs, pos, "while selecting an attribute"); if (!(j = vAttrs->attrs()->get(name))) { - std::set allAttrNames; + StringSet allAttrNames; for (auto & attr : *vAttrs->attrs()) allAttrNames.insert(std::string(state.symbols[attr.name])); auto suggestions = Suggestions::bestMatches(allAttrNames, state.symbols[name]); @@ -1603,7 +1603,7 @@ void EvalState::callFunction(Value & fun, std::span args, Value & vRes, user. */ for (auto & i : *args[0]->attrs()) if (!lambda.formals->has(i.name)) { - std::set formalNames; + StringSet formalNames; for (auto & formal : lambda.formals->formals) formalNames.insert(std::string(symbols[formal.name])); auto suggestions = Suggestions::bestMatches(formalNames, symbols[i.name]); diff --git a/src/libfetchers/mercurial.cc b/src/libfetchers/mercurial.cc index eb6bdd1eb..74e9fd089 100644 --- a/src/libfetchers/mercurial.cc +++ b/src/libfetchers/mercurial.cc @@ -194,7 +194,7 @@ struct MercurialInputScheme : InputScheme input.attrs.insert_or_assign("ref", chomp(runHg({ "branch", "-R", actualUrl }))); - auto files = tokenizeString>( + auto files = tokenizeString( runHg({ "status", "-R", actualUrl, "--clean", "--modified", "--added", "--no-status", "--print0" }), "\0"s); Path actualPath(absPath(actualUrl)); diff --git a/src/libfetchers/tarball.cc b/src/libfetchers/tarball.cc index ef91d6b25..1bd7e3e59 100644 --- a/src/libfetchers/tarball.cc +++ b/src/libfetchers/tarball.cc @@ -224,7 +224,7 @@ ref downloadTarball( // An input scheme corresponding to a curl-downloadable resource. struct CurlInputScheme : InputScheme { - const std::set transportUrlSchemes = {"file", "http", "https"}; + const StringSet transportUrlSchemes = {"file", "http", "https"}; bool hasTarballExtension(std::string_view path) const { @@ -236,7 +236,7 @@ struct CurlInputScheme : InputScheme virtual bool isValidURL(const ParsedURL & url, bool requireTree) const = 0; - static const std::set specialParams; + static const StringSet specialParams; std::optional inputFromURL( const Settings & settings, diff --git a/src/libflake/config.cc b/src/libflake/config.cc index a67f7884c..030104e7f 100644 --- a/src/libflake/config.cc +++ b/src/libflake/config.cc @@ -32,7 +32,7 @@ static void writeTrustedList(const TrustedList & trustedList) void ConfigFile::apply(const Settings & flakeSettings) { - std::set whitelist{"bash-prompt", "bash-prompt-prefix", "bash-prompt-suffix", "flake-registry", "commit-lock-file-summary", "commit-lockfile-summary"}; + StringSet whitelist{"bash-prompt", "bash-prompt-prefix", "bash-prompt-suffix", "flake-registry", "commit-lock-file-summary", "commit-lockfile-summary"}; for (auto & [name, value] : settings) { diff --git a/src/libflake/flake.cc b/src/libflake/flake.cc index a086251b9..516b38131 100644 --- a/src/libflake/flake.cc +++ b/src/libflake/flake.cc @@ -323,7 +323,7 @@ static FlakeRef applySelfAttrs( { auto newRef(ref); - std::set allowedAttrs{"submodules", "lfs"}; + StringSet allowedAttrs{"submodules", "lfs"}; for (auto & attr : flake.selfAttrs) { if (!allowedAttrs.contains(attr.first)) diff --git a/src/libmain/plugin.cc b/src/libmain/plugin.cc index 812506a86..db686a251 100644 --- a/src/libmain/plugin.cc +++ b/src/libmain/plugin.cc @@ -19,7 +19,7 @@ struct PluginFilesSetting : public BaseSetting const Paths & def, const std::string & name, const std::string & description, - const std::set & aliases = {}) + const StringSet & aliases = {}) : BaseSetting(def, true, name, description, aliases) { options->addSetting(this); diff --git a/src/libstore-tests/common-protocol.cc b/src/libstore-tests/common-protocol.cc index 6bfb8bd80..5164f154a 100644 --- a/src/libstore-tests/common-protocol.cc +++ b/src/libstore-tests/common-protocol.cc @@ -154,7 +154,7 @@ CHARACTERIZATION_TEST( CHARACTERIZATION_TEST( set, "set", - (std::tuple, std::set, std::set, std::set>> { + (std::tuple> { { }, { "" }, { "", "foo", "bar" }, diff --git a/src/libstore-tests/outputs-spec.cc b/src/libstore-tests/outputs-spec.cc index a17922c46..a1c13d2f8 100644 --- a/src/libstore-tests/outputs-spec.cc +++ b/src/libstore-tests/outputs-spec.cc @@ -7,7 +7,7 @@ namespace nix { TEST(OutputsSpec, no_empty_names) { - ASSERT_DEATH(OutputsSpec::Names { std::set { } }, ""); + ASSERT_DEATH(OutputsSpec::Names { StringSet { } }, ""); } #define TEST_DONT_PARSE(NAME, STR) \ diff --git a/src/libstore-tests/serve-protocol.cc b/src/libstore-tests/serve-protocol.cc index 9297d46ea..69dab5488 100644 --- a/src/libstore-tests/serve-protocol.cc +++ b/src/libstore-tests/serve-protocol.cc @@ -374,7 +374,7 @@ VERSIONED_CHARACTERIZATION_TEST( set, "set", defaultVersion, - (std::tuple, std::set, std::set, std::set>> { + (std::tuple> { { }, { "" }, { "", "foo", "bar" }, diff --git a/src/libstore-tests/worker-protocol.cc b/src/libstore-tests/worker-protocol.cc index acf83cb60..80d314b1c 100644 --- a/src/libstore-tests/worker-protocol.cc +++ b/src/libstore-tests/worker-protocol.cc @@ -574,7 +574,7 @@ VERSIONED_CHARACTERIZATION_TEST( set, "set", defaultVersion, - (std::tuple, std::set, std::set, std::set>> { + (std::tuple> { { }, { "" }, { "", "foo", "bar" }, diff --git a/src/libstore/derived-path-map.cc b/src/libstore/derived-path-map.cc index d4234d92c..b785dddd9 100644 --- a/src/libstore/derived-path-map.cc +++ b/src/libstore/derived-path-map.cc @@ -55,17 +55,17 @@ typename DerivedPathMap::ChildNode * DerivedPathMap::findSlot(const Single namespace nix { template<> -bool DerivedPathMap>::ChildNode::operator == ( - const DerivedPathMap>::ChildNode &) const noexcept = default; +bool DerivedPathMap::ChildNode::operator == ( + const DerivedPathMap::ChildNode &) const noexcept = default; // TODO libc++ 16 (used by darwin) missing `std::map::operator <=>`, can't do yet. #if 0 template<> -std::strong_ordering DerivedPathMap>::ChildNode::operator <=> ( - const DerivedPathMap>::ChildNode &) const noexcept = default; +std::strong_ordering DerivedPathMap::ChildNode::operator <=> ( + const DerivedPathMap::ChildNode &) const noexcept = default; #endif -template struct DerivedPathMap>::ChildNode; -template struct DerivedPathMap>; +template struct DerivedPathMap::ChildNode; +template struct DerivedPathMap; }; diff --git a/src/libstore/dummy-store.cc b/src/libstore/dummy-store.cc index 80367d597..3e1d168a1 100644 --- a/src/libstore/dummy-store.cc +++ b/src/libstore/dummy-store.cc @@ -22,7 +22,7 @@ struct DummyStoreConfig : virtual StoreConfig { ; } - static std::set uriSchemes() { + static StringSet uriSchemes() { return {"dummy"}; } }; diff --git a/src/libstore/include/nix/store/derived-path-map.hh b/src/libstore/include/nix/store/derived-path-map.hh index 956f8bb0b..cad86d1b4 100644 --- a/src/libstore/include/nix/store/derived-path-map.hh +++ b/src/libstore/include/nix/store/derived-path-map.hh @@ -91,20 +91,20 @@ struct DerivedPathMap { }; template<> -bool DerivedPathMap>::ChildNode::operator == ( - const DerivedPathMap>::ChildNode &) const noexcept; +bool DerivedPathMap::ChildNode::operator == ( + const DerivedPathMap::ChildNode &) const noexcept; // TODO libc++ 16 (used by darwin) missing `std::map::operator <=>`, can't do yet. #if 0 template<> -std::strong_ordering DerivedPathMap>::ChildNode::operator <=> ( - const DerivedPathMap>::ChildNode &) const noexcept; +std::strong_ordering DerivedPathMap::ChildNode::operator <=> ( + const DerivedPathMap::ChildNode &) const noexcept; template<> -inline auto DerivedPathMap>::operator <=> (const DerivedPathMap> &) const noexcept = default; +inline auto DerivedPathMap::operator <=> (const DerivedPathMap &) const noexcept = default; #endif -extern template struct DerivedPathMap>::ChildNode; -extern template struct DerivedPathMap>; +extern template struct DerivedPathMap::ChildNode; +extern template struct DerivedPathMap; } diff --git a/src/libstore/include/nix/store/globals.hh b/src/libstore/include/nix/store/globals.hh index 82211d8dc..3a677216a 100644 --- a/src/libstore/include/nix/store/globals.hh +++ b/src/libstore/include/nix/store/globals.hh @@ -24,7 +24,7 @@ struct MaxBuildJobsSetting : public BaseSetting unsigned int def, const std::string & name, const std::string & description, - const std::set & aliases = {}) + const StringSet & aliases = {}) : BaseSetting(def, true, name, description, aliases) { options->addSetting(this); diff --git a/src/libstore/include/nix/store/http-binary-cache-store.hh b/src/libstore/include/nix/store/http-binary-cache-store.hh index aaec3116d..592def4f8 100644 --- a/src/libstore/include/nix/store/http-binary-cache-store.hh +++ b/src/libstore/include/nix/store/http-binary-cache-store.hh @@ -15,10 +15,10 @@ struct HttpBinaryCacheStoreConfig : virtual BinaryCacheStoreConfig return "HTTP Binary Cache Store"; } - static std::set uriSchemes() + static StringSet uriSchemes() { static bool forceHttp = getEnv("_NIX_FORCE_HTTP") == "1"; - auto ret = std::set({"http", "https"}); + auto ret = StringSet({"http", "https"}); if (forceHttp) ret.insert("file"); return ret; diff --git a/src/libstore/include/nix/store/legacy-ssh-store.hh b/src/libstore/include/nix/store/legacy-ssh-store.hh index a1fbf3f1e..7859abbc9 100644 --- a/src/libstore/include/nix/store/legacy-ssh-store.hh +++ b/src/libstore/include/nix/store/legacy-ssh-store.hh @@ -37,7 +37,7 @@ struct LegacySSHStoreConfig : virtual CommonSSHStoreConfig const std::string name() override { return "SSH Store"; } - static std::set uriSchemes() { return {"ssh"}; } + static StringSet uriSchemes() { return {"ssh"}; } std::string doc() override; }; diff --git a/src/libstore/include/nix/store/local-binary-cache-store.hh b/src/libstore/include/nix/store/local-binary-cache-store.hh index dde4701da..a46ce07d2 100644 --- a/src/libstore/include/nix/store/local-binary-cache-store.hh +++ b/src/libstore/include/nix/store/local-binary-cache-store.hh @@ -15,7 +15,7 @@ struct LocalBinaryCacheStoreConfig : virtual BinaryCacheStoreConfig return "Local Binary Cache Store"; } - static std::set uriSchemes(); + static StringSet uriSchemes(); std::string doc() override; }; diff --git a/src/libstore/include/nix/store/local-overlay-store.hh b/src/libstore/include/nix/store/local-overlay-store.hh index 825214cb6..f3658d603 100644 --- a/src/libstore/include/nix/store/local-overlay-store.hh +++ b/src/libstore/include/nix/store/local-overlay-store.hh @@ -63,7 +63,7 @@ struct LocalOverlayStoreConfig : virtual LocalStoreConfig return ExperimentalFeature::LocalOverlayStore; } - static std::set uriSchemes() + static StringSet uriSchemes() { return { "local-overlay" }; } diff --git a/src/libstore/include/nix/store/local-store.hh b/src/libstore/include/nix/store/local-store.hh index 204720c34..baa82e7f6 100644 --- a/src/libstore/include/nix/store/local-store.hh +++ b/src/libstore/include/nix/store/local-store.hh @@ -67,7 +67,7 @@ struct LocalStoreConfig : virtual LocalFSStoreConfig const std::string name() override { return "Local Store"; } - static std::set uriSchemes() + static StringSet uriSchemes() { return {"local"}; } std::string doc() override; diff --git a/src/libstore/include/nix/store/machines.hh b/src/libstore/include/nix/store/machines.hh index f07d6b63b..2bf7408f6 100644 --- a/src/libstore/include/nix/store/machines.hh +++ b/src/libstore/include/nix/store/machines.hh @@ -15,12 +15,12 @@ typedef std::vector Machines; struct Machine { const StoreReference storeUri; - const std::set systemTypes; + const StringSet systemTypes; const std::string sshKey; const unsigned int maxJobs; const float speedFactor; - const std::set supportedFeatures; - const std::set mandatoryFeatures; + const StringSet supportedFeatures; + const StringSet mandatoryFeatures; const std::string sshPublicHostKey; bool enabled = true; @@ -34,12 +34,12 @@ struct Machine { * @return Whether `features` is a subset of the union of `supportedFeatures` and * `mandatoryFeatures`. */ - bool allSupported(const std::set & features) const; + bool allSupported(const StringSet & features) const; /** * @return Whether `mandatoryFeatures` is a subset of `features`. */ - bool mandatoryMet(const std::set & features) const; + bool mandatoryMet(const StringSet & features) const; Machine( const std::string & storeUri, @@ -75,7 +75,7 @@ struct Machine { * with `@` are interpreted as paths to other configuration files in * the same format. */ - static Machines parseConfig(const std::set & defaultSystems, const std::string & config); + static Machines parseConfig(const StringSet & defaultSystems, const std::string & config); }; /** diff --git a/src/libstore/include/nix/store/path-with-outputs.hh b/src/libstore/include/nix/store/path-with-outputs.hh index 76c1f9f8f..368667c47 100644 --- a/src/libstore/include/nix/store/path-with-outputs.hh +++ b/src/libstore/include/nix/store/path-with-outputs.hh @@ -19,7 +19,7 @@ struct StoreDirConfig; struct StorePathWithOutputs { StorePath path; - std::set outputs; + StringSet outputs; std::string to_string(const StoreDirConfig & store) const; diff --git a/src/libstore/include/nix/store/s3-binary-cache-store.hh b/src/libstore/include/nix/store/s3-binary-cache-store.hh index 7bc04aa4a..e3c95ce78 100644 --- a/src/libstore/include/nix/store/s3-binary-cache-store.hh +++ b/src/libstore/include/nix/store/s3-binary-cache-store.hh @@ -98,7 +98,7 @@ public: return "S3 Binary Cache Store"; } - static std::set uriSchemes() + static StringSet uriSchemes() { return {"s3"}; } diff --git a/src/libstore/include/nix/store/ssh-store.hh b/src/libstore/include/nix/store/ssh-store.hh index 76e8e33a4..a0f73cc8e 100644 --- a/src/libstore/include/nix/store/ssh-store.hh +++ b/src/libstore/include/nix/store/ssh-store.hh @@ -23,7 +23,7 @@ struct SSHStoreConfig : virtual RemoteStoreConfig, virtual CommonSSHStoreConfig return "Experimental SSH Store"; } - static std::set uriSchemes() + static StringSet uriSchemes() { return {"ssh-ng"}; } @@ -45,7 +45,7 @@ struct MountedSSHStoreConfig : virtual SSHStoreConfig, virtual LocalFSStoreConfi return "Experimental SSH Store with filesystem mounted"; } - static std::set uriSchemes() + static StringSet uriSchemes() { return {"mounted-ssh-ng"}; } diff --git a/src/libstore/include/nix/store/store-api.hh b/src/libstore/include/nix/store/store-api.hh index 070abbe97..d5e3d5214 100644 --- a/src/libstore/include/nix/store/store-api.hh +++ b/src/libstore/include/nix/store/store-api.hh @@ -888,7 +888,7 @@ std::list> getDefaultSubstituters(); struct StoreFactory { - std::set uriSchemes; + StringSet uriSchemes; /** * The `authorityPath` parameter is `/`, or really * whatever comes after `://` and before `?`. diff --git a/src/libstore/include/nix/store/uds-remote-store.hh b/src/libstore/include/nix/store/uds-remote-store.hh index f7ef76058..9c85fbfb2 100644 --- a/src/libstore/include/nix/store/uds-remote-store.hh +++ b/src/libstore/include/nix/store/uds-remote-store.hh @@ -38,7 +38,7 @@ protected: static constexpr char const * scheme = "unix"; public: - static std::set uriSchemes() + static StringSet uriSchemes() { return {scheme}; } }; diff --git a/src/libstore/local-binary-cache-store.cc b/src/libstore/local-binary-cache-store.cc index 4d1be5785..30396d7b6 100644 --- a/src/libstore/local-binary-cache-store.cc +++ b/src/libstore/local-binary-cache-store.cc @@ -119,7 +119,7 @@ bool LocalBinaryCacheStore::fileExists(const std::string & path) return pathExists(binaryCacheDir + "/" + path); } -std::set LocalBinaryCacheStoreConfig::uriSchemes() +StringSet LocalBinaryCacheStoreConfig::uriSchemes() { if (getEnv("_NIX_FORCE_HTTP") == "1") return {}; diff --git a/src/libstore/local-store.cc b/src/libstore/local-store.cc index ade209b8a..f1cf466a6 100644 --- a/src/libstore/local-store.cc +++ b/src/libstore/local-store.cc @@ -535,7 +535,7 @@ void LocalStore::upgradeDBSchema(State & state) { state.db.exec("create table if not exists SchemaMigrations (migration text primary key not null);"); - std::set schemaMigrations; + StringSet schemaMigrations; { SQLiteStmt querySchemaMigrations; diff --git a/src/libstore/machines.cc b/src/libstore/machines.cc index d98d06651..0325a6a3a 100644 --- a/src/libstore/machines.cc +++ b/src/libstore/machines.cc @@ -47,7 +47,7 @@ bool Machine::systemSupported(const std::string & system) const return system == "builtin" || (systemTypes.count(system) > 0); } -bool Machine::allSupported(const std::set & features) const +bool Machine::allSupported(const StringSet & features) const { return std::all_of(features.begin(), features.end(), [&](const std::string & feature) { @@ -56,7 +56,7 @@ bool Machine::allSupported(const std::set & features) const }); } -bool Machine::mandatoryMet(const std::set & features) const +bool Machine::mandatoryMet(const StringSet & features) const { return std::all_of(mandatoryFeatures.begin(), mandatoryFeatures.end(), [&](const std::string & feature) { @@ -134,7 +134,7 @@ static std::vector expandBuilderLines(const std::string & builders) return result; } -static Machine parseBuilderLine(const std::set & defaultSystems, const std::string & line) +static Machine parseBuilderLine(const StringSet & defaultSystems, const std::string & line) { const auto tokens = tokenizeString>(line); @@ -178,7 +178,7 @@ static Machine parseBuilderLine(const std::set & defaultSystems, co // `storeUri` tokens[0], // `systemTypes` - isSet(1) ? tokenizeString>(tokens[1], ",") : defaultSystems, + isSet(1) ? tokenizeString(tokens[1], ",") : defaultSystems, // `sshKey` isSet(2) ? tokens[2] : "", // `maxJobs` @@ -186,15 +186,15 @@ static Machine parseBuilderLine(const std::set & defaultSystems, co // `speedFactor` isSet(4) ? parseFloatField(4) : 1.0f, // `supportedFeatures` - isSet(5) ? tokenizeString>(tokens[5], ",") : std::set{}, + isSet(5) ? tokenizeString(tokens[5], ",") : StringSet{}, // `mandatoryFeatures` - isSet(6) ? tokenizeString>(tokens[6], ",") : std::set{}, + isSet(6) ? tokenizeString(tokens[6], ",") : StringSet{}, // `sshPublicHostKey` isSet(7) ? ensureBase64(7) : "" }; } -static Machines parseBuilderLines(const std::set & defaultSystems, const std::vector & builders) +static Machines parseBuilderLines(const StringSet & defaultSystems, const std::vector & builders) { Machines result; std::transform( @@ -203,7 +203,7 @@ static Machines parseBuilderLines(const std::set & defaultSystems, return result; } -Machines Machine::parseConfig(const std::set & defaultSystems, const std::string & s) +Machines Machine::parseConfig(const StringSet & defaultSystems, const std::string & s) { const auto builderLines = expandBuilderLines(s); return parseBuilderLines(defaultSystems, builderLines); diff --git a/src/libstore/path-with-outputs.cc b/src/libstore/path-with-outputs.cc index 9fbbc8f46..f3fc534ef 100644 --- a/src/libstore/path-with-outputs.cc +++ b/src/libstore/path-with-outputs.cc @@ -82,9 +82,9 @@ std::pair parsePathWithOutputs(std::string_view s) { size_t n = s.find("!"); return n == s.npos - ? std::make_pair(s, std::set()) + ? std::make_pair(s, StringSet()) : std::make_pair(s.substr(0, n), - tokenizeString>(s.substr(n + 1), ",")); + tokenizeString(s.substr(n + 1), ",")); } diff --git a/src/libutil/args.cc b/src/libutil/args.cc index fb9d7163c..d8d004e6f 100644 --- a/src/libutil/args.cc +++ b/src/libutil/args.cc @@ -593,7 +593,7 @@ MultiCommand::MultiCommand(std::string_view commandName, const Commands & comman assert(!command); auto i = commands.find(s); if (i == commands.end()) { - std::set commandNames; + StringSet commandNames; for (auto & [name, _] : commands) commandNames.insert(name); auto suggestions = Suggestions::bestMatches(commandNames, s); diff --git a/src/libutil/configuration.cc b/src/libutil/configuration.cc index 0f5a6a432..314ae34db 100644 --- a/src/libutil/configuration.cc +++ b/src/libutil/configuration.cc @@ -220,7 +220,7 @@ void Config::convertToArgs(Args & args, const std::string & category) AbstractSetting::AbstractSetting( const std::string & name, const std::string & description, - const std::set & aliases, + const StringSet & aliases, std::optional experimentalFeature) : name(name) , description(stripIndentation(description)) @@ -428,7 +428,7 @@ PathSetting::PathSetting(Config * options, const Path & def, const std::string & name, const std::string & description, - const std::set & aliases) + const StringSet & aliases) : BaseSetting(def, true, name, description, aliases) { options->addSetting(this); @@ -444,7 +444,7 @@ OptionalPathSetting::OptionalPathSetting(Config * options, const std::optional & def, const std::string & name, const std::string & description, - const std::set & aliases) + const StringSet & aliases) : BaseSetting>(def, true, name, description, aliases) { options->addSetting(this); diff --git a/src/libutil/experimental-features.cc b/src/libutil/experimental-features.cc index 348caa44e..7dee1f5c7 100644 --- a/src/libutil/experimental-features.cc +++ b/src/libutil/experimental-features.cc @@ -358,7 +358,7 @@ nlohmann::json documentExperimentalFeatures() return (nlohmann::json) res; } -std::set parseFeatures(const std::set & rawFeatures) +std::set parseFeatures(const StringSet & rawFeatures) { std::set res; for (auto & rawFeature : rawFeatures) diff --git a/src/libutil/hash.cc b/src/libutil/hash.cc index 0a654b914..50516ac71 100644 --- a/src/libutil/hash.cc +++ b/src/libutil/hash.cc @@ -32,9 +32,9 @@ static size_t regularHashSize(HashAlgorithm type) { } -const std::set hashAlgorithms = {"blake3", "md5", "sha1", "sha256", "sha512" }; +const StringSet hashAlgorithms = {"blake3", "md5", "sha1", "sha256", "sha512" }; -const std::set hashFormats = {"base64", "nix32", "base16", "sri" }; +const StringSet hashFormats = {"base64", "nix32", "base16", "sri" }; Hash::Hash(HashAlgorithm algo, const ExperimentalFeatureSettings & xpSettings) : algo(algo) { diff --git a/src/libutil/include/nix/util/args.hh b/src/libutil/include/nix/util/args.hh index 463270374..f1eb96675 100644 --- a/src/libutil/include/nix/util/args.hh +++ b/src/libutil/include/nix/util/args.hh @@ -179,7 +179,7 @@ public: using ptr = std::shared_ptr; std::string longName; - std::set aliases; + StringSet aliases; char shortName = 0; std::string description; std::string category; @@ -263,7 +263,7 @@ protected: virtual Strings::iterator rewriteArgs(Strings & args, Strings::iterator pos) { return pos; } - std::set hiddenCategories; + StringSet hiddenCategories; /** * Called after all command line flags before the first non-flag diff --git a/src/libutil/include/nix/util/configuration.hh b/src/libutil/include/nix/util/configuration.hh index 34cefd73b..24b42f02c 100644 --- a/src/libutil/include/nix/util/configuration.hh +++ b/src/libutil/include/nix/util/configuration.hh @@ -179,7 +179,7 @@ public: const std::string name; const std::string description; - const std::set aliases; + const StringSet aliases; int created = 123; @@ -192,7 +192,7 @@ protected: AbstractSetting( const std::string & name, const std::string & description, - const std::set & aliases, + const StringSet & aliases, std::optional experimentalFeature = std::nullopt); virtual ~AbstractSetting(); @@ -251,7 +251,7 @@ public: const bool documentDefault, const std::string & name, const std::string & description, - const std::set & aliases = {}, + const StringSet & aliases = {}, std::optional experimentalFeature = std::nullopt) : AbstractSetting(name, description, aliases, experimentalFeature) , value(def) @@ -323,7 +323,7 @@ public: const T & def, const std::string & name, const std::string & description, - const std::set & aliases = {}, + const StringSet & aliases = {}, const bool documentDefault = true, std::optional experimentalFeature = std::nullopt) : BaseSetting(def, documentDefault, name, description, aliases, std::move(experimentalFeature)) @@ -349,7 +349,7 @@ public: const Path & def, const std::string & name, const std::string & description, - const std::set & aliases = {}); + const StringSet & aliases = {}); Path parse(const std::string & str) const override; @@ -371,7 +371,7 @@ public: const std::optional & def, const std::string & name, const std::string & description, - const std::set & aliases = {}); + const StringSet & aliases = {}); std::optional parse(const std::string & str) const override; diff --git a/src/libutil/include/nix/util/experimental-features.hh b/src/libutil/include/nix/util/experimental-features.hh index 06dd7062b..8923517ba 100644 --- a/src/libutil/include/nix/util/experimental-features.hh +++ b/src/libutil/include/nix/util/experimental-features.hh @@ -76,7 +76,7 @@ std::ostream & operator<<( * Parse a set of strings to the corresponding set of experimental * features, ignoring (but warning for) any unknown feature. */ -std::set parseFeatures(const std::set &); +std::set parseFeatures(const StringSet &); /** * An experimental feature was required for some (experimental) diff --git a/src/libutil/include/nix/util/hash.hh b/src/libutil/include/nix/util/hash.hh index f3cc4cc6c..0ae0a9564 100644 --- a/src/libutil/include/nix/util/hash.hh +++ b/src/libutil/include/nix/util/hash.hh @@ -20,7 +20,7 @@ const int sha1HashSize = 20; const int sha256HashSize = 32; const int sha512HashSize = 64; -extern const std::set hashAlgorithms; +extern const StringSet hashAlgorithms; extern const std::string nix32Chars; @@ -40,7 +40,7 @@ enum struct HashFormat : int { SRI }; -extern const std::set hashFormats; +extern const StringSet hashFormats; struct Hash { diff --git a/src/libutil/include/nix/util/strings.hh b/src/libutil/include/nix/util/strings.hh index 521e3425f..4c213de87 100644 --- a/src/libutil/include/nix/util/strings.hh +++ b/src/libutil/include/nix/util/strings.hh @@ -1,5 +1,7 @@ #pragma once +#include "nix/util/types.hh" + #include #include #include @@ -30,7 +32,7 @@ template C tokenizeString(std::string_view s, std::string_view separators = " \t\n\r"); extern template std::list tokenizeString(std::string_view s, std::string_view separators); -extern template std::set tokenizeString(std::string_view s, std::string_view separators); +extern template StringSet tokenizeString(std::string_view s, std::string_view separators); extern template std::vector tokenizeString(std::string_view s, std::string_view separators); /** @@ -44,7 +46,7 @@ template C splitString(std::string_view s, std::string_view separators); extern template std::list splitString(std::string_view s, std::string_view separators); -extern template std::set splitString(std::string_view s, std::string_view separators); +extern template StringSet splitString(std::string_view s, std::string_view separators); extern template std::vector splitString(std::string_view s, std::string_view separators); /** @@ -54,7 +56,7 @@ template std::string concatStringsSep(const std::string_view sep, const C & ss); extern template std::string concatStringsSep(std::string_view, const std::list &); -extern template std::string concatStringsSep(std::string_view, const std::set &); +extern template std::string concatStringsSep(std::string_view, const StringSet &); extern template std::string concatStringsSep(std::string_view, const std::vector &); extern template std::string concatStringsSep(std::string_view, const boost::container::small_vector &); @@ -85,7 +87,7 @@ template dropEmptyInitThenConcatStringsSep(const std::string_view sep, const C & ss); extern template std::string dropEmptyInitThenConcatStringsSep(std::string_view, const std::list &); -extern template std::string dropEmptyInitThenConcatStringsSep(std::string_view, const std::set &); +extern template std::string dropEmptyInitThenConcatStringsSep(std::string_view, const StringSet &); extern template std::string dropEmptyInitThenConcatStringsSep(std::string_view, const std::vector &); /** diff --git a/src/libutil/include/nix/util/suggestions.hh b/src/libutil/include/nix/util/suggestions.hh index 16496379c..6a76eb9d9 100644 --- a/src/libutil/include/nix/util/suggestions.hh +++ b/src/libutil/include/nix/util/suggestions.hh @@ -35,7 +35,7 @@ public: ) const; static Suggestions bestMatches ( - const std::set & allMatches, + const StringSet & allMatches, std::string_view query ); diff --git a/src/libutil/strings.cc b/src/libutil/strings.cc index 7ce37d73c..a95390089 100644 --- a/src/libutil/strings.cc +++ b/src/libutil/strings.cc @@ -26,18 +26,18 @@ __attribute__((no_sanitize("undefined"))) std::string_view toView(const std::ost } template std::list tokenizeString(std::string_view s, std::string_view separators); -template std::set tokenizeString(std::string_view s, std::string_view separators); +template StringSet tokenizeString(std::string_view s, std::string_view separators); template std::vector tokenizeString(std::string_view s, std::string_view separators); template std::list splitString(std::string_view s, std::string_view separators); -template std::set splitString(std::string_view s, std::string_view separators); +template StringSet splitString(std::string_view s, std::string_view separators); template std::vector splitString(std::string_view s, std::string_view separators); template std::list basicSplitString(std::basic_string_view s, std::basic_string_view separators); template std::string concatStringsSep(std::string_view, const std::list &); -template std::string concatStringsSep(std::string_view, const std::set &); +template std::string concatStringsSep(std::string_view, const StringSet &); template std::string concatStringsSep(std::string_view, const std::vector &); template std::string concatStringsSep(std::string_view, const boost::container::small_vector &); @@ -49,7 +49,7 @@ typedef std::string_view strings_4[4]; template std::string concatStringsSep(std::string_view, const strings_4 &); template std::string dropEmptyInitThenConcatStringsSep(std::string_view, const std::list &); -template std::string dropEmptyInitThenConcatStringsSep(std::string_view, const std::set &); +template std::string dropEmptyInitThenConcatStringsSep(std::string_view, const StringSet &); template std::string dropEmptyInitThenConcatStringsSep(std::string_view, const std::vector &); /** diff --git a/src/libutil/suggestions.cc b/src/libutil/suggestions.cc index 0105c30e7..aee23d45e 100644 --- a/src/libutil/suggestions.cc +++ b/src/libutil/suggestions.cc @@ -38,7 +38,7 @@ int levenshteinDistance(std::string_view first, std::string_view second) } Suggestions Suggestions::bestMatches ( - const std::set & allMatches, + const StringSet & allMatches, std::string_view query) { std::set res; diff --git a/src/nix-build/nix-build.cc b/src/nix-build/nix-build.cc index 231b320ac..1becac2b8 100644 --- a/src/nix-build/nix-build.cc +++ b/src/nix-build/nix-build.cc @@ -147,7 +147,7 @@ static void main_nix_build(int argc, char * * argv) std::string outLink = "./result"; // List of environment variables kept for --pure - std::set keepVars{ + StringSet keepVars{ "HOME", "XDG_RUNTIME_DIR", "USER", "LOGNAME", "DISPLAY", "WAYLAND_DISPLAY", "WAYLAND_SOCKET", "PATH", "TERM", "IN_NIX_SHELL", "NIX_SHELL_PRESERVE_PROMPT", "TZ", "PAGER", "NIX_BUILD_SHELL", "SHLVL", diff --git a/src/nix-env/nix-env.cc b/src/nix-env/nix-env.cc index 021619ada..ff629d430 100644 --- a/src/nix-env/nix-env.cc +++ b/src/nix-env/nix-env.cc @@ -238,9 +238,9 @@ static void checkSelectorUse(DrvNames & selectors) namespace { -std::set searchByPrefix(const PackageInfos & allElems, std::string_view prefix) { +StringSet searchByPrefix(const PackageInfos & allElems, std::string_view prefix) { constexpr std::size_t maxResults = 3; - std::set result; + StringSet result; for (const auto & packageInfo : allElems) { const auto drvName = DrvName { packageInfo.queryName() }; if (hasPrefix(drvName.name, prefix)) { diff --git a/src/nix/develop.cc b/src/nix/develop.cc index af32c8386..b525e5de3 100644 --- a/src/nix/develop.cc +++ b/src/nix/develop.cc @@ -67,7 +67,7 @@ struct BuildEnvironment { BuildEnvironment res; - std::set exported; + StringSet exported; for (auto & [name, info] : json["variables"].items()) { std::string type = info["type"]; @@ -151,7 +151,7 @@ struct BuildEnvironment return structuredAttrs->second; } - void toBash(std::ostream & out, const std::set & ignoreVars) const + void toBash(std::ostream & out, const StringSet & ignoreVars) const { for (auto & [name, value] : vars) { if (!ignoreVars.count(name)) { @@ -308,7 +308,7 @@ static StorePath getDerivationEnvironment(ref store, ref evalStore struct Common : InstallableCommand, MixProfile { - std::set ignoreVars{ + StringSet ignoreVars{ "BASHOPTS", "HOME", // FIXME: don't ignore in pure mode? "NIX_BUILD_TOP", diff --git a/src/nix/diff-closures.cc b/src/nix/diff-closures.cc index c4d21db6f..ff9f9db40 100644 --- a/src/nix/diff-closures.cc +++ b/src/nix/diff-closures.cc @@ -47,10 +47,10 @@ GroupedPaths getClosureInfo(ref store, const StorePath & toplevel) return groupedPaths; } -std::string showVersions(const std::set & versions) +std::string showVersions(const StringSet & versions) { if (versions.empty()) return "∅"; - std::set versions2; + StringSet versions2; for (auto & version : versions) versions2.insert(version.empty() ? "ε" : version); return concatStringsSep(", ", versions2); @@ -65,7 +65,7 @@ void printClosureDiff( auto beforeClosure = getClosureInfo(store, beforePath); auto afterClosure = getClosureInfo(store, afterPath); - std::set allNames; + StringSet allNames; for (auto & [name, _] : beforeClosure) allNames.insert(name); for (auto & [name, _] : afterClosure) allNames.insert(name); @@ -87,11 +87,11 @@ void printClosureDiff( auto sizeDelta = (int64_t) afterSize - (int64_t) beforeSize; auto showDelta = std::abs(sizeDelta) >= 8 * 1024; - std::set removed, unchanged; + StringSet removed, unchanged; for (auto & [version, _] : beforeVersions) if (!afterVersions.count(version)) removed.insert(version); else unchanged.insert(version); - std::set added; + StringSet added; for (auto & [version, _] : afterVersions) if (!beforeVersions.count(version)) added.insert(version); diff --git a/src/nix/flake.cc b/src/nix/flake.cc index 4586ebfbe..351b2caaf 100644 --- a/src/nix/flake.cc +++ b/src/nix/flake.cc @@ -386,7 +386,7 @@ struct CmdFlakeCheck : FlakeCommand } }; - std::set omittedSystems; + StringSet omittedSystems; // FIXME: rewrite to use EvalCache. diff --git a/src/nix/profile.cc b/src/nix/profile.cc index a0b6ee6c2..51de2f789 100644 --- a/src/nix/profile.cc +++ b/src/nix/profile.cc @@ -67,7 +67,7 @@ struct ProfileElement * Return a string representing an installable corresponding to the current * element, either a flakeref or a plain store path */ - std::set toInstallables(Store & store) + StringSet toInstallables(Store & store) { if (source) return {source->to_string()}; @@ -600,7 +600,7 @@ public: }); } - std::set getMatchingElementNames(ProfileManifest & manifest) { + StringSet getMatchingElementNames(ProfileManifest & manifest) { if (_matchers.empty()) { throw UsageError("No packages specified."); } @@ -614,7 +614,7 @@ public: return {}; } - std::set result; + StringSet result; for (auto & matcher : _matchers) { bool foundMatch = false; for (auto & [name, element] : manifest.elements) { diff --git a/src/nix/why-depends.cc b/src/nix/why-depends.cc index 5de32caae..3aac45d34 100644 --- a/src/nix/why-depends.cc +++ b/src/nix/why-depends.cc @@ -193,7 +193,7 @@ struct CmdWhyDepends : SourceExprCommand, MixOperateOnOptions /* Sort the references by distance to `dependency` to ensure that the shortest path is printed first. */ std::multimap refs; - std::set hashes; + StringSet hashes; for (auto & ref : node.refs) { if (ref == node.path && packagePath != dependencyPath) continue; From 55815ec22545a560435cc8b5963bf4efb0f3fca0 Mon Sep 17 00:00:00 2001 From: Sergei Zimmerman Date: Fri, 2 May 2025 17:40:31 +0000 Subject: [PATCH 2/4] treewide: Use PathSet alias consistently instead of std::set --- src/libstore/builtins/buildenv.cc | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/libstore/builtins/buildenv.cc b/src/libstore/builtins/buildenv.cc index 74036d22e..eeaccb24c 100644 --- a/src/libstore/builtins/buildenv.cc +++ b/src/libstore/builtins/buildenv.cc @@ -123,7 +123,7 @@ void buildProfile(const Path & out, Packages && pkgs) { State state; - std::set done, postponed; + PathSet done, postponed; auto addPkg = [&](const Path & pkgDir, int priority) { if (!done.insert(pkgDir).second) return; @@ -157,7 +157,7 @@ void buildProfile(const Path & out, Packages && pkgs) */ auto priorityCounter = 1000; while (!postponed.empty()) { - std::set pkgDirs; + PathSet pkgDirs; postponed.swap(pkgDirs); for (const auto & pkgDir : pkgDirs) addPkg(pkgDir, priorityCounter++); From 5278cd23966359a3971858dca01491cf3a9bba6a Mon Sep 17 00:00:00 2001 From: Sergei Zimmerman Date: Fri, 2 May 2025 17:40:34 +0000 Subject: [PATCH 3/4] libstore: Introduce WorkerProto::FeatureSet alias Unfortunately Feature is just an alias to `std::string` and not a new-type, so a ton of code relies on it being exactly a `std::string`. Using transparent comparators just for StringSet necessitates using it here as well. --- src/libstore-tests/worker-protocol.cc | 4 ++-- .../nix/store/worker-protocol-connection.hh | 16 ++++--------- .../include/nix/store/worker-protocol.hh | 3 ++- src/libstore/worker-protocol-connection.cc | 23 +++++++++---------- 4 files changed, 20 insertions(+), 26 deletions(-) diff --git a/src/libstore-tests/worker-protocol.cc b/src/libstore-tests/worker-protocol.cc index 80d314b1c..4baf8a325 100644 --- a/src/libstore-tests/worker-protocol.cc +++ b/src/libstore-tests/worker-protocol.cc @@ -685,7 +685,7 @@ TEST_F(WorkerProtoTest, handshake_features) toClient.create(); toServer.create(); - std::tuple> clientResult; + std::tuple clientResult; auto clientThread = std::thread([&]() { FdSink out { toServer.writeSide.get() }; @@ -703,7 +703,7 @@ TEST_F(WorkerProtoTest, handshake_features) EXPECT_EQ(clientResult, daemonResult); EXPECT_EQ(std::get<0>(clientResult), 123u); - EXPECT_EQ(std::get<1>(clientResult), std::set({"bar", "xyzzy"})); + EXPECT_EQ(std::get<1>(clientResult), WorkerProto::FeatureSet({"bar", "xyzzy"})); } /// Has to be a `BufferedSink` for handshake. diff --git a/src/libstore/include/nix/store/worker-protocol-connection.hh b/src/libstore/include/nix/store/worker-protocol-connection.hh index df2fe0ec2..11f112a71 100644 --- a/src/libstore/include/nix/store/worker-protocol-connection.hh +++ b/src/libstore/include/nix/store/worker-protocol-connection.hh @@ -26,7 +26,7 @@ struct WorkerProto::BasicConnection /** * The set of features that both sides support. */ - std::set features; + FeatureSet features; /** * Coercion to `WorkerProto::ReadConn`. This makes it easy to use the @@ -92,11 +92,8 @@ struct WorkerProto::BasicClientConnection : WorkerProto::BasicConnection * @param supportedFeatures The protocol features that we support. */ // FIXME: this should probably be a constructor. - static std::tuple> handshake( - BufferedSink & to, - Source & from, - WorkerProto::Version localVersion, - const std::set & supportedFeatures); + static std::tuple handshake( + BufferedSink & to, Source & from, WorkerProto::Version localVersion, const FeatureSet & supportedFeatures); /** * After calling handshake, must call this to exchange some basic @@ -155,11 +152,8 @@ struct WorkerProto::BasicServerConnection : WorkerProto::BasicConnection * @param supportedFeatures The protocol features that we support. */ // FIXME: this should probably be a constructor. - static std::tuple> handshake( - BufferedSink & to, - Source & from, - WorkerProto::Version localVersion, - const std::set & supportedFeatures); + static std::tuple handshake( + BufferedSink & to, Source & from, WorkerProto::Version localVersion, const FeatureSet & supportedFeatures); /** * After calling handshake, must call this to exchange some basic diff --git a/src/libstore/include/nix/store/worker-protocol.hh b/src/libstore/include/nix/store/worker-protocol.hh index 3060681b8..f96d41c71 100644 --- a/src/libstore/include/nix/store/worker-protocol.hh +++ b/src/libstore/include/nix/store/worker-protocol.hh @@ -135,8 +135,9 @@ struct WorkerProto } using Feature = std::string; + using FeatureSet = std::set; - static const std::set allFeatures; + static const FeatureSet allFeatures; }; enum struct WorkerProto::Op : uint64_t diff --git a/src/libstore/worker-protocol-connection.cc b/src/libstore/worker-protocol-connection.cc index d83be10e6..d07dc8163 100644 --- a/src/libstore/worker-protocol-connection.cc +++ b/src/libstore/worker-protocol-connection.cc @@ -5,7 +5,7 @@ namespace nix { -const std::set WorkerProto::allFeatures{}; +const WorkerProto::FeatureSet WorkerProto::allFeatures{}; WorkerProto::BasicClientConnection::~BasicClientConnection() { @@ -146,21 +146,20 @@ void WorkerProto::BasicClientConnection::processStderr( } } -static std::set -intersectFeatures(const std::set & a, const std::set & b) +static WorkerProto::FeatureSet intersectFeatures(const WorkerProto::FeatureSet & a, const WorkerProto::FeatureSet & b) { - std::set res; + WorkerProto::FeatureSet res; for (auto & x : a) if (b.contains(x)) res.insert(x); return res; } -std::tuple> WorkerProto::BasicClientConnection::handshake( +std::tuple WorkerProto::BasicClientConnection::handshake( BufferedSink & to, Source & from, WorkerProto::Version localVersion, - const std::set & supportedFeatures) + const WorkerProto::FeatureSet & supportedFeatures) { to << WORKER_MAGIC_1 << localVersion; to.flush(); @@ -178,21 +177,21 @@ std::tuple> WorkerProto::Ba auto protoVersion = std::min(daemonVersion, localVersion); /* Exchange features. */ - std::set daemonFeatures; + WorkerProto::FeatureSet daemonFeatures; if (GET_PROTOCOL_MINOR(protoVersion) >= 38) { to << supportedFeatures; to.flush(); - daemonFeatures = readStrings>(from); + daemonFeatures = readStrings(from); } return {protoVersion, intersectFeatures(daemonFeatures, supportedFeatures)}; } -std::tuple> WorkerProto::BasicServerConnection::handshake( +std::tuple WorkerProto::BasicServerConnection::handshake( BufferedSink & to, Source & from, WorkerProto::Version localVersion, - const std::set & supportedFeatures) + const WorkerProto::FeatureSet & supportedFeatures) { unsigned int magic = readInt(from); if (magic != WORKER_MAGIC_1) @@ -204,9 +203,9 @@ std::tuple> WorkerProto::Ba auto protoVersion = std::min(clientVersion, localVersion); /* Exchange features. */ - std::set clientFeatures; + WorkerProto::FeatureSet clientFeatures; if (GET_PROTOCOL_MINOR(protoVersion) >= 38) { - clientFeatures = readStrings>(from); + clientFeatures = readStrings(from); to << supportedFeatures; to.flush(); } From ebb836d499e8395bb1f6cb18de09346fc7e761ac Mon Sep 17 00:00:00 2001 From: Sergei Zimmerman Date: Fri, 2 May 2025 17:43:02 +0000 Subject: [PATCH 4/4] Use transparent comparators for std::set (NFC) This patch finally applies the transition to std::less<>, which is a transparent comparator. There's no functional change and string lookups in sets are now more efficient and don't produce temporaries (e.g. set.find(std::string_view{"key"})). --- .../include/nix/store/common-protocol-impl.hh | 4 ++-- .../include/nix/store/common-protocol.hh | 6 ++--- src/libstore/include/nix/store/derivations.hh | 2 +- .../store/length-prefixed-protocol-helper.hh | 20 ++++++++--------- .../include/nix/store/outputs-spec.hh | 16 +++++++++----- .../include/nix/store/serve-protocol-impl.hh | 4 +++- .../include/nix/store/serve-protocol.hh | 6 ++--- .../include/nix/store/worker-protocol-impl.hh | 4 +++- .../include/nix/store/worker-protocol.hh | 8 +++---- src/libutil/include/nix/util/json-utils.hh | 4 ++-- src/libutil/include/nix/util/topo-sort.hh | 10 ++++----- src/libutil/include/nix/util/types.hh | 22 +++++++++++++++++-- 12 files changed, 66 insertions(+), 40 deletions(-) diff --git a/src/libstore/include/nix/store/common-protocol-impl.hh b/src/libstore/include/nix/store/common-protocol-impl.hh index 171b4c6a5..18e63ac33 100644 --- a/src/libstore/include/nix/store/common-protocol-impl.hh +++ b/src/libstore/include/nix/store/common-protocol-impl.hh @@ -25,11 +25,11 @@ namespace nix { LengthPrefixedProtoHelper::write(store, conn, t); \ } +#define COMMA_ , COMMON_USE_LENGTH_PREFIX_SERIALISER(template, std::vector) -COMMON_USE_LENGTH_PREFIX_SERIALISER(template, std::set) +COMMON_USE_LENGTH_PREFIX_SERIALISER(template, std::set) COMMON_USE_LENGTH_PREFIX_SERIALISER(template, std::tuple) -#define COMMA_ , COMMON_USE_LENGTH_PREFIX_SERIALISER( template, std::map) diff --git a/src/libstore/include/nix/store/common-protocol.hh b/src/libstore/include/nix/store/common-protocol.hh index b464cda67..7887120b5 100644 --- a/src/libstore/include/nix/store/common-protocol.hh +++ b/src/libstore/include/nix/store/common-protocol.hh @@ -72,14 +72,14 @@ DECLARE_COMMON_SERIALISER(DrvOutput); template<> DECLARE_COMMON_SERIALISER(Realisation); +#define COMMA_ , template DECLARE_COMMON_SERIALISER(std::vector); -template -DECLARE_COMMON_SERIALISER(std::set); +template +DECLARE_COMMON_SERIALISER(std::set); template DECLARE_COMMON_SERIALISER(std::tuple); -#define COMMA_ , template DECLARE_COMMON_SERIALISER(std::map); #undef COMMA_ diff --git a/src/libstore/include/nix/store/derivations.hh b/src/libstore/include/nix/store/derivations.hh index 01ff337f6..46a9e2d02 100644 --- a/src/libstore/include/nix/store/derivations.hh +++ b/src/libstore/include/nix/store/derivations.hh @@ -343,7 +343,7 @@ struct Derivation : BasicDerivation /** * inputs that are sub-derivations */ - DerivedPathMap> inputDrvs; + DerivedPathMap>> inputDrvs; /** * Print a derivation. diff --git a/src/libstore/include/nix/store/length-prefixed-protocol-helper.hh b/src/libstore/include/nix/store/length-prefixed-protocol-helper.hh index 664841aae..a83635aa4 100644 --- a/src/libstore/include/nix/store/length-prefixed-protocol-helper.hh +++ b/src/libstore/include/nix/store/length-prefixed-protocol-helper.hh @@ -52,8 +52,10 @@ struct LengthPrefixedProtoHelper; template LENGTH_PREFIXED_PROTO_HELPER(Inner, std::vector); -template -LENGTH_PREFIXED_PROTO_HELPER(Inner, std::set); +#define COMMA_ , +template +LENGTH_PREFIXED_PROTO_HELPER(Inner, std::set); +#undef COMMA_ template LENGTH_PREFIXED_PROTO_HELPER(Inner, std::tuple); @@ -86,12 +88,11 @@ LengthPrefixedProtoHelper>::write( } } -template -std::set -LengthPrefixedProtoHelper>::read( +template +std::set LengthPrefixedProtoHelper>::read( const StoreDirConfig & store, typename Inner::ReadConn conn) { - std::set resSet; + std::set resSet; auto size = readNum(conn.from); while (size--) { resSet.insert(S::read(store, conn)); @@ -99,10 +100,9 @@ LengthPrefixedProtoHelper>::read( return resSet; } -template -void -LengthPrefixedProtoHelper>::write( - const StoreDirConfig & store, typename Inner::WriteConn conn, const std::set & resSet) +template +void LengthPrefixedProtoHelper>::write( + const StoreDirConfig & store, typename Inner::WriteConn conn, const std::set & resSet) { conn.to << resSet.size(); for (auto & key : resSet) { diff --git a/src/libstore/include/nix/store/outputs-spec.hh b/src/libstore/include/nix/store/outputs-spec.hh index b89f425c2..b47f26542 100644 --- a/src/libstore/include/nix/store/outputs-spec.hh +++ b/src/libstore/include/nix/store/outputs-spec.hh @@ -27,20 +27,24 @@ struct OutputsSpec { /** * A non-empty set of outputs, specified by name */ - struct Names : std::set { - using std::set::set; + struct Names : std::set> { + private: + using BaseType = std::set>; + + public: + using BaseType::BaseType; /* These need to be "inherited manually" */ - Names(const std::set & s) - : std::set(s) + Names(const BaseType & s) + : BaseType(s) { assert(!empty()); } /** * Needs to be "inherited manually" */ - Names(std::set && s) - : std::set(s) + Names(BaseType && s) + : BaseType(std::move(s)) { assert(!empty()); } /* This set should always be non-empty, so we delete this diff --git a/src/libstore/include/nix/store/serve-protocol-impl.hh b/src/libstore/include/nix/store/serve-protocol-impl.hh index 769b9ae2b..4ab164721 100644 --- a/src/libstore/include/nix/store/serve-protocol-impl.hh +++ b/src/libstore/include/nix/store/serve-protocol-impl.hh @@ -26,7 +26,9 @@ namespace nix { } SERVE_USE_LENGTH_PREFIX_SERIALISER(template, std::vector) -SERVE_USE_LENGTH_PREFIX_SERIALISER(template, std::set) +#define COMMA_ , +SERVE_USE_LENGTH_PREFIX_SERIALISER(template, std::set) +#undef COMMA_ SERVE_USE_LENGTH_PREFIX_SERIALISER(template, std::tuple) #define SERVE_USE_LENGTH_PREFIX_SERIALISER_COMMA , diff --git a/src/libstore/include/nix/store/serve-protocol.hh b/src/libstore/include/nix/store/serve-protocol.hh index 76f0ecd49..6f6bf6b60 100644 --- a/src/libstore/include/nix/store/serve-protocol.hh +++ b/src/libstore/include/nix/store/serve-protocol.hh @@ -180,12 +180,12 @@ DECLARE_SERVE_SERIALISER(ServeProto::BuildOptions); template DECLARE_SERVE_SERIALISER(std::vector); -template -DECLARE_SERVE_SERIALISER(std::set); +#define COMMA_ , +template +DECLARE_SERVE_SERIALISER(std::set); template DECLARE_SERVE_SERIALISER(std::tuple); -#define COMMA_ , template DECLARE_SERVE_SERIALISER(std::map); #undef COMMA_ diff --git a/src/libstore/include/nix/store/worker-protocol-impl.hh b/src/libstore/include/nix/store/worker-protocol-impl.hh index 337c245e2..908a9323e 100644 --- a/src/libstore/include/nix/store/worker-protocol-impl.hh +++ b/src/libstore/include/nix/store/worker-protocol-impl.hh @@ -26,7 +26,9 @@ namespace nix { } WORKER_USE_LENGTH_PREFIX_SERIALISER(template, std::vector) -WORKER_USE_LENGTH_PREFIX_SERIALISER(template, std::set) +#define COMMA_ , +WORKER_USE_LENGTH_PREFIX_SERIALISER(template, std::set) +#undef COMMA_ WORKER_USE_LENGTH_PREFIX_SERIALISER(template, std::tuple) #define WORKER_USE_LENGTH_PREFIX_SERIALISER_COMMA , diff --git a/src/libstore/include/nix/store/worker-protocol.hh b/src/libstore/include/nix/store/worker-protocol.hh index f96d41c71..1b188806d 100644 --- a/src/libstore/include/nix/store/worker-protocol.hh +++ b/src/libstore/include/nix/store/worker-protocol.hh @@ -135,7 +135,7 @@ struct WorkerProto } using Feature = std::string; - using FeatureSet = std::set; + using FeatureSet = std::set>; static const FeatureSet allFeatures; }; @@ -273,12 +273,12 @@ DECLARE_WORKER_SERIALISER(WorkerProto::ClientHandshakeInfo); template DECLARE_WORKER_SERIALISER(std::vector); -template -DECLARE_WORKER_SERIALISER(std::set); +#define COMMA_ , +template +DECLARE_WORKER_SERIALISER(std::set); template DECLARE_WORKER_SERIALISER(std::tuple); -#define COMMA_ , template DECLARE_WORKER_SERIALISER(std::map); #undef COMMA_ diff --git a/src/libutil/include/nix/util/json-utils.hh b/src/libutil/include/nix/util/json-utils.hh index bcae46a0a..37f4d58f8 100644 --- a/src/libutil/include/nix/util/json-utils.hh +++ b/src/libutil/include/nix/util/json-utils.hh @@ -90,8 +90,8 @@ struct json_avoids_null> : std::true_type {}; template struct json_avoids_null> : std::true_type {}; -template -struct json_avoids_null> : std::true_type {}; +template +struct json_avoids_null> : std::true_type {}; template struct json_avoids_null> : std::true_type {}; diff --git a/src/libutil/include/nix/util/topo-sort.hh b/src/libutil/include/nix/util/topo-sort.hh index 77a9ce421..6ba6fda71 100644 --- a/src/libutil/include/nix/util/topo-sort.hh +++ b/src/libutil/include/nix/util/topo-sort.hh @@ -5,13 +5,13 @@ namespace nix { -template -std::vector topoSort(std::set items, - std::function(const T &)> getChildren, +template +std::vector topoSort(std::set items, + std::function(const T &)> getChildren, std::function makeCycleError) { std::vector sorted; - std::set visited, parents; + decltype(items) visited, parents; std::function dfsVisit; @@ -21,7 +21,7 @@ std::vector topoSort(std::set items, if (!visited.insert(path).second) return; parents.insert(path); - std::set references = getChildren(path); + auto references = getChildren(path); for (auto & i : references) /* Don't traverse into items that don't exist in our starting set. */ diff --git a/src/libutil/include/nix/util/types.hh b/src/libutil/include/nix/util/types.hh index 9f5c75827..5139256ca 100644 --- a/src/libutil/include/nix/util/types.hh +++ b/src/libutil/include/nix/util/types.hh @@ -12,17 +12,35 @@ namespace nix { typedef std::list Strings; -typedef std::set StringSet; typedef std::map StringMap; typedef std::map StringPairs; +/** + * Alias to ordered set container with transparent comparator. + * + * Used instead of std::set to use C++14 N3657 [1] + * heterogenous lookup consistently across the whole codebase. + * Transparent comparators get rid of creation of unnecessary + * temporary variables when looking up keys by `std::string_view` + * or C-style `const char *` strings. + * + * [1]: https://www.open-std.org/jtc1/sc22/wg21/docs/papers/2013/n3657.htm + */ +using StringSet = std::set>; + /** * Paths are just strings. */ typedef std::string Path; typedef std::string_view PathView; typedef std::list Paths; -typedef std::set PathSet; + +/** + * Alias to an ordered set of `Path`s. Uses transparent comparator. + * + * @see StringSet + */ +using PathSet = std::set>; typedef std::vector> Headers;