From ef45787aae1c91dffce1e95db3d46740b0165319 Mon Sep 17 00:00:00 2001 From: pennae Date: Wed, 12 Jan 2022 18:17:59 +0100 Subject: [PATCH] avoid string copies in attrNames sort comparison symbols can also be cast to string_view, which compares the same but doesn't require a copy of both symbol names on every comparison. --- src/libexpr/primops.cc | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/src/libexpr/primops.cc b/src/libexpr/primops.cc index 7b0e6e7df..365595c92 100644 --- a/src/libexpr/primops.cc +++ b/src/libexpr/primops.cc @@ -2163,7 +2163,10 @@ static void prim_attrValues(EvalState & state, const Pos & pos, Value * * args, v.listElems()[n++] = (Value *) &i; std::sort(v.listElems(), v.listElems() + n, - [](Value * v1, Value * v2) { return (string) ((Attr *) v1)->name < (string) ((Attr *) v2)->name; }); + [](Value * v1, Value * v2) { + std::string_view s1 = ((Attr *) v1)->name, s2 = ((Attr *) v2)->name; + return s1 < s2; + }); for (unsigned int i = 0; i < n; ++i) v.listElems()[i] = ((Attr *) v.listElems()[i])->value;