Performance of Cell.is_top() seems slow?

I have a script that iterates through certain layout cells and clears them (so that they can be replaced with different shapes):

for i in main_layout.each_cell():
  if not i.is_top():
    if not"VIA"):

But this loop takes a significant amount of time: more than a second per cell (my design has hundreds of cells).

I tried a different approach considering that my designs always have one top cell:

top_cell_index = main_layout.top_cell().cell_index()
for i in main_layout.each_cell():
  if i.cell_index() != top_cell_index:
    if not"VIA"):

This approach completes the whole loop in less than a second. Is is_top() really that performance intensive? Is there a better way to execute this loop?



  • Hi Austin,

    It's not "is_top" itself, but the fact that you're manipulating the cell tree while iterating. "clear" would basically also delete child cell instances, so this counts as manipulation. Every manipulation means "is_top" needs to evaluate again which is the top cell. This at least requires a full scan over the cells, so in the worst case you'll end up with a algorithm of complexity O(2) in the number of cells.

    To mitigate this issue you can chose your solution or use Layout#start_changes ( to freeze a layout's state. This will block re-evaluation of "is_top" until you call Layout#end_changes.


  • Thanks Matthias, that makes sense.

Sign In or Register to comment.