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.
This commit is contained in:
pennae 2022-01-12 18:17:59 +01:00
parent 1bebb1095a
commit ef45787aae

View file

@ -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;