1
1
Fork 0
mirror of https://github.com/NixOS/nix.git synced 2025-11-15 23:12:44 +01:00

Add lazy evaluation for experimental feature reasons

Wrap fmt() calls in lambdas to defer string formatting until the
feature check fails. This avoids unnecessary string formatting in
the common case where the feature is enabled.

Addresses performance concern raised by xokdvium in PR review.
This commit is contained in:
Robert Hensing 2025-10-14 16:49:59 +02:00
parent 39c4665488
commit 1b96a704d3
4 changed files with 23 additions and 6 deletions

View file

@ -465,6 +465,19 @@ struct ExperimentalFeatureSettings : Config
*/
void require(const ExperimentalFeature &, std::string reason = "") const;
/**
* Require an experimental feature be enabled, throwing an error if it is
* not. The reason is lazily evaluated only if the feature is disabled.
*/
template<typename GetReason>
requires std::invocable<GetReason> && std::convertible_to<std::invoke_result_t<GetReason>, std::string>
void require(const ExperimentalFeature & feature, GetReason && getReason) const
{
if (isEnabled(feature))
return;
require(feature, getReason());
}
/**
* `std::nullopt` pointer means no feature, which means there is nothing that could be
* disabled, and so the function returns true in that case.