提交 59efbc13 authored 作者: Frederic Bastien's avatar Frederic Bastien

Use dict for the pprint to speed its condition checking.

上级 ca58eeb6
...@@ -513,12 +513,13 @@ class LeafPrinter: ...@@ -513,12 +513,13 @@ class LeafPrinter:
class PPrinter: class PPrinter:
def __init__(self): def __init__(self):
self.printers = [] self.printers = []
self.printers_dict = {}
def assign(self, condition, printer): def assign(self, condition, printer):
if isinstance(condition, gof.Op): # condition can be a class or an instance of an Op.
op = condition if isinstance(condition, (gof.Op, type)):
condition = (lambda pstate, r: r.owner is not None and self.printers_dict[condition] = printer
r.owner.op == op) return
self.printers.insert(0, (condition, printer)) self.printers.insert(0, (condition, printer))
def process(self, r, pstate=None): def process(self, r, pstate=None):
...@@ -526,6 +527,11 @@ class PPrinter: ...@@ -526,6 +527,11 @@ class PPrinter:
pstate = PrinterState(pprinter=self) pstate = PrinterState(pprinter=self)
elif isinstance(pstate, dict): elif isinstance(pstate, dict):
pstate = PrinterState(pprinter=self, **pstate) pstate = PrinterState(pprinter=self, **pstate)
if getattr(r, 'owner', None) is not None:
if r.owner.op in self.printers_dict:
return self.printers_dict[r.owner.op].process(r, pstate)
if type(r.owner.op) in self.printers_dict:
return self.printers_dict[type(r.owner.op)].process(r, pstate)
for condition, printer in self.printers: for condition, printer in self.printers:
if condition(pstate, r): if condition(pstate, r):
return printer.process(r, pstate) return printer.process(r, pstate)
...@@ -533,6 +539,7 @@ class PPrinter: ...@@ -533,6 +539,7 @@ class PPrinter:
def clone(self): def clone(self):
cp = copy(self) cp = copy(self)
cp.printers = list(self.printers) cp.printers = list(self.printers)
cp.printers_dict = dict(self.printers_dict)
return cp return cp
def clone_assign(self, condition, printer): def clone_assign(self, condition, printer):
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论