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 i.name.startswith("VIA"):
      i.clear()

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 i.name.startswith("VIA"):
      i.clear()

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?

Thanks,
Austin

Comments

  • 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 (https://www.klayout.de/doc-qt5/code/class_Layout.html#method117) to freeze a layout's state. This will block re-evaluation of "is_top" until you call Layout#end_changes.

    Matthias

  • Thanks Matthias, that makes sense.

Sign In or Register to comment.