Skip to content

Conversation

@Al2Klimov
Copy link
Member

@Al2Klimov Al2Klimov commented Nov 10, 2022

to avoid malloc().

Expression#GetReference() "returns" the parent object and the index (key) of a (not necessarily yet set) field.

Now it tries to return the index by ref not to have to copy it.

@Al2Klimov Al2Klimov self-assigned this Nov 10, 2022
@cla-bot cla-bot bot added the cla/signed label Nov 10, 2022
Copy link
Member

@julianbrost julianbrost left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please update the PR description to explain what it's doing and why.

Copy link
Member

@julianbrost julianbrost left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So from more looking at the code, I got some clues, but I still don't get the full picture. In my understanding, a good PR description should give me this picture, I shouldn't have to reverse-engineer it from the code change.

Your new RefIndex type is somewhat like std::variant<const String&, String>, sometimes capturing a string by reference, sometimes by value. But why is this needed? What are the situations where it can be referenced and what are those where it can't? Also, what is the purpose of the index/vindex parameters/variables that this is used for? The non-existing documentation for it doesn't help, but as you has looked into this, please share your knowledge.

@Al2Klimov
Copy link
Member Author

E.g. look at this:

11212022hannesholzmann

SetExpression#DoEvaluate() (7) requests parent (3) and index (4) from its left operand (5) via IndexerExpression#GetReference(), so it can do VMOps::SetField(parent, index, right operand, ...). Don't ask me why, it is how it is. Other construction area. (Not in terms of room, but in terms of time.) Fact is: our = gets host.vars and disk_partitions to do host.vars["disk_partitions"]="/". Same for the inner IndexerExpression (host["vars"]) and even for the VariableExpression (locals["host"]).

The current master copies the index unconditionally. #9579 is smarter. Whenever an index can be returned "by ref", that's done. The interesting (for you) changes are in VariableExpression#GetReference() and IndexerExpression#GetReference(). vindex just had to be changed along with the Expression#GetReference() interface.

@julianbrost
Copy link
Member

So in other words:

For assignments, a reference to the storage location is needed. This reference is represented by the surrounding container (parent, typically a map/dict/object/scope) and a key into that container (index).

Now consider the following two assignments:

  1. x["foobar"] = 42
  2. x["foo" + "bar"] = 42

In both examples, the reference for the left-hand side will be a reference to x and the string "foobar". In the first example, the index is a constant value somewhere inside the AST and your PR wants to use a reference to it. In the second example, that value is some temporary, so you have to return it by value.

Is that correct so far?

@Al2Klimov
Copy link
Member Author

In the second example, that value is some temporary, so you have to return it by value.

Tbh I've already played with the thought of (referenceably) caching full-constexpr never-mutated computation results. But that's another story...

@julianbrost
Copy link
Member

Which is such a common operation for compilers, it even has a name: https://en.wikipedia.org/wiki/Constant_folding

But you can still do x[random()] = 42 after all.

@Al2Klimov
Copy link
Member Author

Before/after, time icinga2 daemon -C

f59f361
real 36m38.277s
user 196m31.058s
sys 71m49.837s

real 35m14.356s
user 190m16.353s
sys 68m23.075s

real 35m35.668s
user 192m36.516s
sys 68m44.489s

0fbc443
real 35m7.132s
user 183m34.341s
sys 72m55.090s

real 34m58.088s
user 183m29.557s
sys 72m2.457s

real 34m59.477s
user 183m21.523s
sys 74m47.236s

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants