Fix performance regression in CSPLayout pass #5514
Merged
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.
Summary
In #5183 the inner graph object of the CouplingMap object was switched
to retworkx. While generally faster there are a few different edge cases
where retworkx performance can be worse than networkx. Namely, any place
where large lists are returned, for example lists of all nodes or edges
in a graph. In those cases the conversion from the Rust objects to the
Python objects can cumulatively be expensive. To avoid this conversion
overhead in every case retworkx has custom return types that are python
sequences but defer type conversion to
__getitem__
, meaning that thereturn is fast but complete traversal can be slow, especially when done
multiple times. In such cases it's easier to cast the sequence to a
Python object such as a set or a list so the conversion is only done
once. However, in the CSPLayout pass the edges list returned from
retworkx was searched multiple times as part of the constraint function
which resulted in a large performance regression. This commit fixes this
by casting the local edges to a set(). This has 2 advantages, first it
avoids the multiple traversals of the graph which removes the conversion
overhead. The second advantage is that by using a set the lookups are
constant time instead of iterating over the full list every time. This
results in a performance improvement over the performance prior to #5183
and the introduction of the regression.
Details and comments