Repository navigation
Destroy for-loop body locals before the increment - #681
Merged
nunoplopes merged 2 commits intoNov 25, 2026
Merged
Conversation
Signed-off-by: Perfloop Agent <agent@perfloop.ai>
Signed-off-by: Perfloop Agent <agent@perfloop.ai>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Programs with RAII objects in a
for-loop body run the increment before those objects’ destructors in both translation models. This reverses observable side effects and can keep guards alive longer than in C++. The body now has its own drop scope, including when it exits throughcontinue.In the two-guard loop in
tests/unit/for_body_destructor.cpp, destructors print2 1and the increment prints9. Previously each iteration printed9 2 1; it now prints2 1 9, for both fallthrough andcontinue.A labeled body block lets
continueleave the body before reaching the single increment expression. Loop breaks remain directed at the loop, while switch breaks keep their existing targets. Condition variables stay outside the body block and remain alive through the increment; initializer objects retain the enclosing loop scope. Loops without an increment need no extra block. Generated expectations are updated for both models.This changes the order of emitted destructor guards, not cases where temporary destructors are already missing.
Local reduced-loop timings (
rustc -C opt-level=3, 15 interleaved pairs): guard/continuebefore→after medians were 607.207→579.908 ms (refcount, 20M iterations) and 217.026→61.854 ms (unsafe, 100M). Unsafe guards withoutcontinueinstead took 124.090→127.997 ms. Destructor-free results were inconclusive; these constructed workloads do not establish application speedups. For the exact 40M-iteration body-local destructor/continueprogram, 15 interleaved whole-process pairs per model gave local before→after medians of 1,388.633→1,409.003 ms (refcount) and 31.608→32.200 ms (unsafe); both medians increased, but the paired comparisons were inconclusive.Testing
Linux lit checks of the affected fixtures and control-flow/destructor consumers report 132 passed and 2 expected failures in each of Release and Debug. The changed C++ files pass clang-format 23.
tests/unit/socket_recvfrom_name.cdoes not pass natively on either revision and is left to target CI. The full check target is left to target CI.Case and evidence
Made by Perfloop. Human sponsor: Tomás Senart.