Skip to content

Evaluate assignment targets once in both models - #679

Open
perfloop-agent wants to merge 25 commits into
Cpp2Rust:masterfrom
perfloop-oss:perfloop-pr-open-62xd58j7qq
Open

perfloop-agent wants to merge 25 commits into
Cpp2Rust:masterfrom
perfloop-oss:perfloop-pr-open-62xd58j7qq

Conversation

@perfloop-agent

@perfloop-agent perfloop-agent commented Nov 24, 2026 •

Copy link
Copy Markdown
Contributor

Unsigned and promoted compound assignments can update the wrong element when the target has side effects. Using their result can also produce Rust with unit type instead of the assigned value, while value-used simple assignments can evaluate the target again. Both models now keep the selected target for the store and result when needed.

For an array starting with {10, 20, 30, 40} and i = 0, a[i++] += 5 previously read the first element but stored 15 in the second, leaving i = 2. The translation now updates the first element and leaves i = 1, as native C++ does.

For independent selectors, output changes only when the target or right operand has a side effect, or when a value-used unsigned or promoted compound produced (). Value-used assignments also keep the binding when the store can change the index, as in y = (a[a[0]] += 1).

Cost check on entries[0].value += delta(i) (global array, call on the right), refcount model, ten calls of 100,000 updates: elapsed medians 2.193 ms on the base and 1.749 ms with this change, 21.0% faster across 28 pairs. The new form borrows the global once per update instead of twice.

The compact regression in tests/unit/assignment_single_eval.cpp covers the value, sequencing and target-selection cases, including references and pointers returned by a temporary owner. Those owners now stay alive through the full expression. All pre-existing expected outputs remain unchanged.

Testing: the scoped Debug and Release lit runs each report 51 passed and one expected failure, comparing native C++ with both models.

Cost-check figures

Elapsed time: Base median 2,193,203 ns; This change median 1,749,155 ns. Paired median change −460,423 ns, with sequential bounds −644,933 to −251,058 ns from 28 complete pairs.

Case and evidence


Made by Perfloop. Human sponsor: Tomás Senart.

@nunoplopes

Copy link
Copy Markdown
Contributor

@perfloop-agent fix merge conflicts

@nunoplopes

Copy link
Copy Markdown
Contributor

This change is way too disruptive. It introduces a lot of churn for code without side effects.

Signed-off-by: Perfloop Agent <agent@perfloop.ai>
@perfloop-agent

Copy link
Copy Markdown
Contributor Author

The churn comes from applying the target-binding block even when neither operand has side effects. I propose keeping the existing lowering for those cases, adding the missing result for unsigned/promoted value assignments, and reserving the binding for effectful operands. Broken value expressions would still need different output; how does that sound?

Signed-off-by: Perfloop Agent <agent@perfloop.ai>
@nunoplopes

Copy link
Copy Markdown
Contributor

The churn comes from applying the target-binding block even when neither operand has side effects. I propose keeping the existing lowering for those cases, adding the missing result for unsigned/promoted value assignments, and reserving the binding for effectful operands. Broken value expressions would still need different output; how does that sound?

Yes, change the output only when needed (i.e., when there are side effects).

Signed-off-by: Perfloop Agent <agent@perfloop.ai>
Signed-off-by: Perfloop Agent <agent@perfloop.ai>
Signed-off-by: Perfloop Agent <agent@perfloop.ai>
Signed-off-by: Perfloop Agent <agent@perfloop.ai>
Signed-off-by: Perfloop Agent <agent@perfloop.ai>
Signed-off-by: Perfloop Agent <agent@perfloop.ai>
Signed-off-by: Perfloop Agent <agent@perfloop.ai>
Signed-off-by: Perfloop Agent <agent@perfloop.ai>
Signed-off-by: Perfloop Agent <agent@perfloop.ai>
Signed-off-by: Perfloop Agent <agent@perfloop.ai>
Signed-off-by: Perfloop Agent <agent@perfloop.ai>
Signed-off-by: Perfloop Agent <agent@perfloop.ai>
Signed-off-by: Perfloop Agent <agent@perfloop.ai>
Signed-off-by: Perfloop Agent <agent@perfloop.ai>
Signed-off-by: Perfloop Agent <agent@perfloop.ai>
Signed-off-by: Perfloop Agent <agent@perfloop.ai>
@perfloop
perfloop Bot force-pushed the perfloop-pr-open-62xd58j7qq branch from f483ee5 to 1cbc95b Compare November 29, 2026 02:34
Signed-off-by: Perfloop Agent <agent@perfloop.ai>
Signed-off-by: Perfloop Agent <agent@perfloop.ai>
Signed-off-by: Perfloop Agent <agent@perfloop.ai>
Signed-off-by: Perfloop Agent <agent@perfloop.ai>
Signed-off-by: Perfloop Agent <agent@perfloop.ai>
Signed-off-by: Perfloop Agent <agent@perfloop.ai>
Signed-off-by: Perfloop Agent <agent@perfloop.ai>
@perfloop-agent

Copy link
Copy Markdown
Contributor Author

The update left the existing assign_as_value expected outputs unchanged, and no other existing expected output changed either. For independent selectors, output differed only for assignments whose target or right operand had side effects, plus value-used unsigned or promoted compound assignments that previously produced ().

This branch has not been deployed

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants