From a865049c4f39cb7773f97a67cfa12f5b650a86ee Mon Sep 17 00:00:00 2001 From: Robert Hensing Date: Wed, 20 Mar 2024 23:06:23 +0100 Subject: [PATCH] tryEval: Allocate true and false once --- src/libexpr/eval.cc | 2 ++ src/libexpr/eval.hh | 18 +++++++++++++++++- src/libexpr/primops.cc | 7 ++++--- 3 files changed, 23 insertions(+), 4 deletions(-) diff --git a/src/libexpr/eval.cc b/src/libexpr/eval.cc index a6e8a4a8b..a62cee299 100644 --- a/src/libexpr/eval.cc +++ b/src/libexpr/eval.cc @@ -437,6 +437,8 @@ EvalState::EvalState( vEmptyList.mkList(buildList(0)); vNull.mkNull(); + vTrue.mkBool(true); + vFalse.mkBool(false); vStringRegular.mkString("regular"); vStringDirectory.mkString("directory"); vStringSymlink.mkString("symlink"); diff --git a/src/libexpr/eval.hh b/src/libexpr/eval.hh index a405888c1..eac83fe34 100644 --- a/src/libexpr/eval.hh +++ b/src/libexpr/eval.hh @@ -187,10 +187,26 @@ public: Value vEmptyList; /** - * Null constant. + * `null` constant. + * + * This is _not_ a singleton. Pointer equality is _not_ sufficient. */ Value vNull; + /** + * `true` constant. + * + * This is _not_ a singleton. Pointer equality is _not_ sufficient. + */ + Value vTrue; + + /** + * `true` constant. + * + * This is _not_ a singleton. Pointer equality is _not_ sufficient. + */ + Value vFalse; + /** `"regular"` */ Value vStringRegular; /** `"directory"` */ diff --git a/src/libexpr/primops.cc b/src/libexpr/primops.cc index 2022f6dcf..0f2aaa83f 100644 --- a/src/libexpr/primops.cc +++ b/src/libexpr/primops.cc @@ -896,10 +896,11 @@ static void prim_tryEval(EvalState & state, const PosIdx pos, Value * * args, Va try { state.forceValue(*args[0], pos); attrs.insert(state.sValue, args[0]); - attrs.alloc("success").mkBool(true); + attrs.insert(state.symbols.create("success"), &state.vTrue); } catch (AssertionError & e) { - attrs.alloc(state.sValue).mkBool(false); - attrs.alloc("success").mkBool(false); + // `value = false;` is unfortunate but removing it is a breaking change. + attrs.insert(state.sValue, &state.vFalse); + attrs.insert(state.symbols.create("success"), &state.vFalse); } // restore the debugRepl pointer if we saved it earlier.