From af71a9dbd96c282ef23096e5a3b71dd220fab3f0 Mon Sep 17 00:00:00 2001 From: John Ericson Date: Tue, 23 Sep 2025 13:05:12 -0400 Subject: [PATCH] Fix `JSON_IMPL` macro to avoid extraneous copies Should take the thing we're serializing by reference. --- src/libfetchers/fetchers.cc | 2 +- src/libstore/derivation-options.cc | 4 ++-- src/libstore/derivations.cc | 2 +- src/libstore/outputs-spec.cc | 4 ++-- src/libstore/path.cc | 2 +- src/libstore/realisation.cc | 2 +- src/libutil/include/nix/util/json-impls.hh | 18 +++++++++--------- 7 files changed, 17 insertions(+), 17 deletions(-) diff --git a/src/libfetchers/fetchers.cc b/src/libfetchers/fetchers.cc index 54013bf55..a6b5e295a 100644 --- a/src/libfetchers/fetchers.cc +++ b/src/libfetchers/fetchers.cc @@ -509,7 +509,7 @@ fetchers::PublicKey adl_serializer::from_json(const json & return res; } -void adl_serializer::to_json(json & json, fetchers::PublicKey p) +void adl_serializer::to_json(json & json, const fetchers::PublicKey & p) { json["type"] = p.type; json["key"] = p.key; diff --git a/src/libstore/derivation-options.cc b/src/libstore/derivation-options.cc index 630159629..4cb9bf726 100644 --- a/src/libstore/derivation-options.cc +++ b/src/libstore/derivation-options.cc @@ -356,7 +356,7 @@ DerivationOptions adl_serializer::from_json(const json & json }; } -void adl_serializer::to_json(json & json, DerivationOptions o) +void adl_serializer::to_json(json & json, const DerivationOptions & o) { json["outputChecks"] = std::visit( overloaded{ @@ -398,7 +398,7 @@ DerivationOptions::OutputChecks adl_serializer: }; } -void adl_serializer::to_json(json & json, DerivationOptions::OutputChecks c) +void adl_serializer::to_json(json & json, const DerivationOptions::OutputChecks & c) { json["ignoreSelfRefs"] = c.ignoreSelfRefs; json["allowedReferences"] = c.allowedReferences; diff --git a/src/libstore/derivations.cc b/src/libstore/derivations.cc index 92266b61b..a0c709791 100644 --- a/src/libstore/derivations.cc +++ b/src/libstore/derivations.cc @@ -1494,7 +1494,7 @@ Derivation adl_serializer::from_json(const json & json) return Derivation::fromJSON(json); } -void adl_serializer::to_json(json & json, Derivation c) +void adl_serializer::to_json(json & json, const Derivation & c) { json = c.toJSON(); } diff --git a/src/libstore/outputs-spec.cc b/src/libstore/outputs-spec.cc index 7f73c7d35..aacc964cd 100644 --- a/src/libstore/outputs-spec.cc +++ b/src/libstore/outputs-spec.cc @@ -150,7 +150,7 @@ OutputsSpec adl_serializer::from_json(const json & json) return OutputsSpec::Names{std::move(names)}; } -void adl_serializer::to_json(json & json, OutputsSpec t) +void adl_serializer::to_json(json & json, const OutputsSpec & t) { std::visit( overloaded{ @@ -169,7 +169,7 @@ ExtendedOutputsSpec adl_serializer::from_json(const json & } } -void adl_serializer::to_json(json & json, ExtendedOutputsSpec t) +void adl_serializer::to_json(json & json, const ExtendedOutputsSpec & t) { std::visit( overloaded{ diff --git a/src/libstore/path.cc b/src/libstore/path.cc index 942f97a88..fa430ce94 100644 --- a/src/libstore/path.cc +++ b/src/libstore/path.cc @@ -88,7 +88,7 @@ StorePath adl_serializer::from_json(const json & json) return StorePath{getString(json)}; } -void adl_serializer::to_json(json & json, StorePath storePath) +void adl_serializer::to_json(json & json, const StorePath & storePath) { json = storePath.to_string(); } diff --git a/src/libstore/realisation.cc b/src/libstore/realisation.cc index d59f4b0ea..febd67bd2 100644 --- a/src/libstore/realisation.cc +++ b/src/libstore/realisation.cc @@ -165,7 +165,7 @@ Realisation adl_serializer::from_json(const json & json0) }; } -void adl_serializer::to_json(json & json, Realisation r) +void adl_serializer::to_json(json & json, const Realisation & r) { auto jsonDependentRealisations = nlohmann::json::object(); for (auto & [depId, depOutPath] : r.dependentRealisations) diff --git a/src/libutil/include/nix/util/json-impls.hh b/src/libutil/include/nix/util/json-impls.hh index 8a6198313..751fc410f 100644 --- a/src/libutil/include/nix/util/json-impls.hh +++ b/src/libutil/include/nix/util/json-impls.hh @@ -4,13 +4,13 @@ #include // Following https://github.com/nlohmann/json#how-can-i-use-get-for-non-default-constructiblenon-copyable-types -#define JSON_IMPL(TYPE) \ - namespace nlohmann { \ - using namespace nix; \ - template<> \ - struct adl_serializer \ - { \ - static TYPE from_json(const json & json); \ - static void to_json(json & json, TYPE t); \ - }; \ +#define JSON_IMPL(TYPE) \ + namespace nlohmann { \ + using namespace nix; \ + template<> \ + struct adl_serializer \ + { \ + static TYPE from_json(const json & json); \ + static void to_json(json & json, const TYPE & t); \ + }; \ }