pass recursive lambda self param by reference#222
Merged
Conversation
added 2 commits
July 6, 2026 10:45
# Conflicts: # .verify-helper/timestamps.remote.json
This was referenced Jul 6, 2026
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.
Changes the self parameter of every self-passing recursive lambda from
auto(by value) toauto&&(by reference) — 20 lambda sites across 18 files.Why
A
[&]closure is one word per captured entity. With the by-value form, every recursive call copies the whole closure into the new stack frame (and it must stay live across the recursive call, so it can't be elided). Withauto&&, each frame holds only a reference. Call sites are unchanged.Measured impact
Benchmark: recursive lambda capturing 4 locals (same shape as this library's DFS lambdas), measuring the address delta of a local between consecutive recursion depths. GCC 16, arm64 macOS:
auto dfs(self by value)auto&& dfs(self by ref)-O2-O0The closure copy is 32 B, but it forces additional spills/alignment on top, tripling the frame at
-O2. Exact numbers are ABI/capture-count dependent (x86-64 judges will differ), but the direction and rough ratio are robust.Practical consequences:
ulimit -s), this is the difference between crashing at depth ~60k vs ~200k on a path graph.Cost
+2 characters per lambda. No behavior change; all call sites (
dfs(dfs, ...)) are identical.If the library later moves to C++23 deducing this, the equivalent recommended form is
this auto&& dfs.Verification
clang-format --dry-run --Werror(dev.clang-format) passes on all 18 filesg++ -std=c++20 -O2 -Wall -Wextra(lca, edge_cd, centroid decomp, kth_path x4, shallowest, subtree isomorphism, scc x2, offline incremental scc, euler walk, two-edge cc, bcc x2, min-plus convolution)