Skip to content

Avoid inner-loop allocations in longest_path#1577

Open
jakelishman wants to merge 1 commit intoQiskit:mainfrom
jakelishman:faster-longest-path
Open

Avoid inner-loop allocations in longest_path#1577
jakelishman wants to merge 1 commit intoQiskit:mainfrom
jakelishman:faster-longest-path

Conversation

@jakelishman
Copy link
Copy Markdown
Member

The only use of the per-node Vec is to consume it immediately into a max_by iterator. Rewriting the allocating loop as a single try_fold removes the unnecessary allocation.

On a test 1.6 million gate circuit over on Qiskit, this sped up our use of longest_path from 150ms to 50ms, so a 3x improvement.

@jakelishman jakelishman force-pushed the faster-longest-path branch 2 times, most recently from 7d885a7 to 1600fc9 Compare April 16, 2026 08:27
The only use of the per-node `Vec` is to consume it immediately into a
`max_by` iterator.  Rewriting the allocating loop as a single `try_fold`
removes the unnecessary allocation.

On a test 1.6 million gate circuit over on Qiskit, this sped up our use
of `longest_path` from 150ms to 100ms, and there's further follow-on
improvements available by changing the interfaces to allow path-length
calculation without constructing the path itself.
@jakelishman jakelishman force-pushed the faster-longest-path branch 2 times, most recently from 7d81b85 to f08f2ad Compare April 16, 2026 13:49
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.

1 participant