hdl.ast, hdl.xfrm: various microoptimizations to speed up pysim.

This commit is contained in:
whitequark 2018-12-18 15:06:02 +00:00
parent c5f169988b
commit 7341d0d7ef
2 changed files with 21 additions and 18 deletions

View file

@ -980,7 +980,7 @@ class ValueKey:
if isinstance(self.value, Const): if isinstance(self.value, Const):
return hash(self.value.value) return hash(self.value.value)
elif isinstance(self.value, Signal): elif isinstance(self.value, Signal):
return hash(id(self.value)) return hash(self.value.duid)
elif isinstance(self.value, Operator): elif isinstance(self.value, Operator):
return hash((self.value.op, tuple(ValueKey(o) for o in self.value.operands))) return hash((self.value.op, tuple(ValueKey(o) for o in self.value.operands)))
elif isinstance(self.value, Slice): elif isinstance(self.value, Slice):
@ -998,15 +998,15 @@ class ValueKey:
.format(self.value)) .format(self.value))
def __eq__(self, other): def __eq__(self, other):
if not isinstance(other, ValueKey): if type(other) is not ValueKey:
return False return False
if type(self.value) != type(other.value): if type(self.value) is not type(other.value):
return False return False
if isinstance(self.value, Const): if isinstance(self.value, Const):
return self.value.value == other.value.value return self.value.value == other.value.value
elif isinstance(self.value, Signal): elif isinstance(self.value, Signal):
return id(self.value) == id(other.value) return self.value is other.value
elif isinstance(self.value, Operator): elif isinstance(self.value, Operator):
return (self.value.op == other.value.op and return (self.value.op == other.value.op and
len(self.value.operands) == len(other.value.operands) and len(self.value.operands) == len(other.value.operands) and
@ -1066,7 +1066,7 @@ class ValueSet(_MappedKeySet):
class SignalKey: class SignalKey:
def __init__(self, signal): def __init__(self, signal):
if not isinstance(signal, Signal): if type(signal) is not Signal:
raise TypeError("Object '{!r}' is not an nMigen signal") raise TypeError("Object '{!r}' is not an nMigen signal")
self.signal = signal self.signal = signal
@ -1074,10 +1074,12 @@ class SignalKey:
return hash(self.signal.duid) return hash(self.signal.duid)
def __eq__(self, other): def __eq__(self, other):
return isinstance(other, SignalKey) and self.signal.duid == other.signal.duid if type(other) is not SignalKey:
return False
return self.signal is other.signal
def __lt__(self, other): def __lt__(self, other):
if not isinstance(other, SignalKey): if type(other) is not SignalKey:
raise TypeError("Object '{!r}' cannot be compared to a SignalKey") raise TypeError("Object '{!r}' cannot be compared to a SignalKey")
return self.signal.duid < other.signal.duid return self.signal.duid < other.signal.duid

View file

@ -60,25 +60,25 @@ class AbstractValueTransformer(metaclass=ABCMeta):
raise TypeError("Cannot transform value '{!r}'".format(value)) # :nocov: raise TypeError("Cannot transform value '{!r}'".format(value)) # :nocov:
def on_value(self, value): def on_value(self, value):
if isinstance(value, Const): if type(value) is Const:
new_value = self.on_Const(value) new_value = self.on_Const(value)
elif isinstance(value, Signal): elif type(value) is Signal:
new_value = self.on_Signal(value) new_value = self.on_Signal(value)
elif isinstance(value, ClockSignal): elif type(value) is ClockSignal:
new_value = self.on_ClockSignal(value) new_value = self.on_ClockSignal(value)
elif isinstance(value, ResetSignal): elif type(value) is ResetSignal:
new_value = self.on_ResetSignal(value) new_value = self.on_ResetSignal(value)
elif isinstance(value, Operator): elif type(value) is Operator:
new_value = self.on_Operator(value) new_value = self.on_Operator(value)
elif isinstance(value, Slice): elif type(value) is Slice:
new_value = self.on_Slice(value) new_value = self.on_Slice(value)
elif isinstance(value, Part): elif type(value) is Part:
new_value = self.on_Part(value) new_value = self.on_Part(value)
elif isinstance(value, Cat): elif type(value) is Cat:
new_value = self.on_Cat(value) new_value = self.on_Cat(value)
elif isinstance(value, Repl): elif type(value) is Repl:
new_value = self.on_Repl(value) new_value = self.on_Repl(value)
elif isinstance(value, ArrayProxy): elif type(value) is ArrayProxy:
new_value = self.on_ArrayProxy(value) new_value = self.on_ArrayProxy(value)
else: else:
new_value = self.on_unknown_value(value) new_value = self.on_unknown_value(value)
@ -140,9 +140,10 @@ class AbstractStatementTransformer(metaclass=ABCMeta):
raise TypeError("Cannot transform statement '{!r}'".format(stmt)) # :nocov: raise TypeError("Cannot transform statement '{!r}'".format(stmt)) # :nocov:
def on_statement(self, stmt): def on_statement(self, stmt):
if isinstance(stmt, Assign): if type(stmt) is Assign:
return self.on_Assign(stmt) return self.on_Assign(stmt)
elif isinstance(stmt, Switch): elif isinstance(stmt, Switch):
# Uses `isinstance()` and not `type() is` because nmigen.compat requires it.
return self.on_Switch(stmt) return self.on_Switch(stmt)
elif isinstance(stmt, Iterable): elif isinstance(stmt, Iterable):
return self.on_statements(stmt) return self.on_statements(stmt)