From 2c34f0214ba9f6c62f7f5433da46232b360b3147 Mon Sep 17 00:00:00 2001 From: David Nadlinger Date: Mon, 23 Dec 2019 01:27:18 +0000 Subject: [PATCH] compiler: Short-circuit Type.unify() with identical other type This considerably improves performance; ~15% in terms of total artiq_run-to-kernel-compiled duration in one test case. --- artiq/compiler/types.py | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/artiq/compiler/types.py b/artiq/compiler/types.py index a8a2e5703..1f2a62a05 100644 --- a/artiq/compiler/types.py +++ b/artiq/compiler/types.py @@ -81,6 +81,8 @@ class TVar(Type): return root def unify(self, other): + if other is self: + return other = other.find() if self.parent is self: @@ -124,6 +126,8 @@ class TMono(Type): return self def unify(self, other): + if other is self: + return if isinstance(other, TMono) and self.name == other.name: assert self.params.keys() == other.params.keys() for param in self.params: @@ -171,6 +175,8 @@ class TTuple(Type): return self def unify(self, other): + if other is self: + return if isinstance(other, TTuple) and len(self.elts) == len(other.elts): for selfelt, otherelt in zip(self.elts, other.elts): selfelt.unify(otherelt) @@ -237,6 +243,8 @@ class TFunction(Type): return self def unify(self, other): + if other is self: + return if isinstance(other, TFunction) and \ self.args.keys() == other.args.keys() and \ self.optargs.keys() == other.optargs.keys(): @@ -296,6 +304,8 @@ class TCFunction(TFunction): self.flags = flags def unify(self, other): + if other is self: + return if isinstance(other, TCFunction) and \ self.name == other.name: super().unify(other) @@ -324,6 +334,8 @@ class TRPC(Type): return self def unify(self, other): + if other is self: + return if isinstance(other, TRPC) and \ self.service == other.service and \ self.is_async == other.is_async: @@ -366,6 +378,8 @@ class TBuiltin(Type): return self def unify(self, other): + if other is self: + return if self != other: raise UnificationError(self, other) @@ -471,6 +485,8 @@ class TValue(Type): return self def unify(self, other): + if other is self: + return if isinstance(other, TVar): other.unify(self) elif self != other: