Skip to content

Performance issue: using sum with sympy expressions is slow #181

@dexter2206

Description

@dexter2206

Describe the bug

In several places of our code we use sum with results of backend.as_expression. However, it turns out that summing over a list of sympy expressions is extremely slow. One can easily verify that this is the case by launching an interpreter and attempting to add 10000 symbols using sum.

The problem occurs in those places:

  • Propagating child resources bartiq.preprocessing.propagate_child_resources
  • Computing highwater: functions _inflow and _outflow in bartiq.derived_resources.

The solution would be to add a sum method to SymbolicBackend interface, and use that instead of builtin sum. For SympyBackend, we could just implement it like this:

def sum(self, *args):
    return sympy.Add(*args)

How to Reproduce

  • Create a routine with 10000 children, with each of children having few additive resources.
  • Don't put the same resources in parent, so that propagation of resources is triggered in preprocessing
  • Attempt to compile the routine.
  • Observe your computer melt from the effort of parsing and adding expressions.

Expected behavior

Compilation of a routine described above finishes in reasonable time (say under 30s)

Actual behavior

Compilation of a routine described above finishes in extremely long time (I don't know how long it would take, I gave up waiting after an hour). By using print-debugging or dropping into debugger one can verify that the hang-up occurs in sum present in propagate_child_resources.

Environment:**

This occurs in every environment.

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions