Skip to content

Commit

Permalink
Remove quadratic time complexity in Statement.cast()
Browse files Browse the repository at this point in the history
Using sum(lst, []) to flatten a list of lists has quadratic time
complexity. Use chain.from_iterable() instead. While not strictly
necessary to improve performance, convert to map().

A test case writing out verilog for a 512k entry FIFO is 120x faster
with this applied.
  • Loading branch information
antonblanchard committed Sep 27, 2021
1 parent 9834b7e commit e62ef89
Showing 1 changed file with 2 additions and 1 deletion.
3 changes: 2 additions & 1 deletion nmigen/hdl/ast.py
Expand Up @@ -4,6 +4,7 @@
from collections import OrderedDict
from collections.abc import Iterable, MutableMapping, MutableSet, MutableSequence
from enum import Enum
from itertools import chain

from .. import tracer
from .._utils import *
Expand Down Expand Up @@ -1404,7 +1405,7 @@ def __init__(self, *, src_loc_at=0):
@staticmethod
def cast(obj):
if isinstance(obj, Iterable):
return _StatementList(sum((Statement.cast(e) for e in obj), []))
return _StatementList(list(chain.from_iterable(map(Statement.cast, obj))))
else:
if isinstance(obj, Statement):
return _StatementList([obj])
Expand Down

0 comments on commit e62ef89

Please sign in to comment.