hdl.ast: remove quadratic time complexity in Statement.cast().

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.
This commit is contained in:
Anton Blanchard 2021-09-27 11:00:56 +10:00 committed by GitHub
parent 9834b7e95f
commit 9f78ac0691
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -4,6 +4,7 @@ import functools
from collections import OrderedDict from collections import OrderedDict
from collections.abc import Iterable, MutableMapping, MutableSet, MutableSequence from collections.abc import Iterable, MutableMapping, MutableSet, MutableSequence
from enum import Enum from enum import Enum
from itertools import chain
from .. import tracer from .. import tracer
from .._utils import * from .._utils import *
@ -1404,7 +1405,7 @@ class Statement:
@staticmethod @staticmethod
def cast(obj): def cast(obj):
if isinstance(obj, Iterable): 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: else:
if isinstance(obj, Statement): if isinstance(obj, Statement):
return _StatementList([obj]) return _StatementList([obj])