GhPython slows down each execution

Hello,

I have grasshopper definition with a large python component inside. Every time I run the script, the execution becomes slower. The slowing down seems to be fairly linear - see graph below.

runtimes

After a Rhino restart everything is ‘fast’ again. I monitored the CPU usage of each run, but could not detect significant differences between them. I have this problem both with single- and multithreaded versions of my code.

I found a thread with a similar problem description, but no apparent solution besides a restart of Rhino (Python scripts with ghpython components slow down grasshopper - Grasshopper).

Is there anything I can do to prevent this behaviour, or can someone explain what may cause this slow down with repeated executions?

It’s hard to say without knowing exactly what you do, but are you collecting garbage anywhere?

1 Like

Many things can cause this. A simple motivation might be that there’s some additional work to do at each iteration.

If not, it might also the the IronPython engine. It does not, generally, like nested functions and nested classes, especially if they capture variables / with “closures”. That being said, in this case it would be an IronPython issue. You should try to understand where in the code that happens, because this is mostly speculation.

Thank you both for the feedback :slight_smile: I will try to narrow the problem down more, to see what function or code block is getting particularly slow over time.

1 Like

We saw the exact same behavior of execution time increasing with each execution. In our python block the culprit was an isinstance() function calling what I assume is some IronPython funkiness:

from collections import Iterable

# Problem code
if isinstance(input_var, Iterable):
    ...

I suggest checking isinstance() calls, especially those involving the IronPython standard library.

2 Likes

Hi Damien, thank’s for the suggestion, I will check my code for isinstance calls :+1:

I replaced the isinstance-calls for list-checking with this function:
isinstance alternative

Unfortunately, this does not seem to change the slowdown of execution times in my case.

If you’re by any chance implementing collections.OrderedDict in your code, that might be a culprit.

Edit: Also, you could use type() to check if something is a list:

2021-10-20 13_07_26-Window

1 Like

Hi, I also tried using type(x) before the hacky isList() solution. I have no collections.OrderedDict in my code, but thanks for the suggestion nevertheless:)

1 Like