Skip to content

[RF] Speed up CodegenContext::beginLoop() with single graph traversal - #23313

Merged
guitargeek merged 1 commit into
root-project:masterfrom
guitargeek:roofit-codegen-speedup
Sep 9, 2026
Merged

[RF] Speed up CodegenContext::beginLoop() with single graph traversal#23313
guitargeek merged 1 commit into
root-project:masterfrom
guitargeek:roofit-codegen-speedup

Conversation

@guitargeek

Copy link
Copy Markdown
Contributor

To figure out which vector observables to loop over, beginLoop() called RooAbsArg::dependsOn() once per vector observable. These dependsOn() walks don't deduplicate visited nodes, so their cost scales with the number of paths in the computation graph instead of the number of nodes, which gets very expensive for large models with many shared nodes.

Replace the per-observable walks with a single depth-first traversal that visits every node only once and collects the reachable vector observables, which is equivalent.

For the ATLAS VHbb benchmark workspace from rootbench, this reduces the "Function JIT time" of the codegen backend (which includes the code generation itself) from 9.4 s to 5.3 s.

🤖 Done with the help of AI

To figure out which vector observables to loop over, beginLoop() called
RooAbsArg::dependsOn() once per vector observable. These dependsOn()
walks don't deduplicate visited nodes, so their cost scales with the
number of paths in the computation graph instead of the number of
nodes, which gets very expensive for large models with many shared
nodes.

Replace the per-observable walks with a single depth-first traversal
that visits every node only once and collects the reachable vector
observables, which is equivalent.

For the ATLAS VHbb benchmark workspace from rootbench, this reduces the
"Function JIT time" of the codegen backend (which includes the code
generation itself) from 9.4 s to 5.3 s.

🤖 Done with the help of AI
@guitargeek guitargeek self-assigned this Sep 9, 2026

@vgvassilev vgvassilev left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Oh, wow, pretty significant improvement. Can we put some visitation traces so that we can detect if we are doing something else suboptimal elsewhere? LGTM!

@guitargeek

Copy link
Copy Markdown
Contributor Author

Oh, wow, pretty significant improvement. Can we put some visitation traces so that we can detect if we are doing something else suboptimal elsewhere? LGTM!

Right now I think we're good. This was the one big overhead that the profiling found. Otherwise, the time spent to setup the codegen likelihood is mostly compiling the code.

@guitargeek
guitargeek merged commit 76babf1 into root-project:master Sep 9, 2026
26 of 32 checks passed
@guitargeek
guitargeek deleted the roofit-codegen-speedup branch September 9, 2026 16:11
@guitargeek

Copy link
Copy Markdown
Contributor Author

/backport to 6.40

@root-project-bot

Copy link
Copy Markdown

Preparing to backport PR #23313 to branch 6.40 requested by guitargeek

@root-project-bot

Copy link
Copy Markdown

This PR has been backported to branch 6.40: #23326

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