From 6122fd70ca82b2156a32f2b369d509b3a21a7128 Mon Sep 17 00:00:00 2001 From: whitequark Date: Fri, 27 Nov 2015 18:08:46 +0800 Subject: [PATCH] compiler.types: TVar.find: improve path compression. After this change, the compiler's complexity is nearly linear even when very large arrays are used. --- artiq/compiler/types.py | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/artiq/compiler/types.py b/artiq/compiler/types.py index 795e8f80e..88f1ae302 100644 --- a/artiq/compiler/types.py +++ b/artiq/compiler/types.py @@ -71,7 +71,13 @@ class TVar(Type): root = root.parent # path compression - self.parent = root + iter = self + while isinstance(iter, TVar): + if iter is iter.parent: + break + else: + iter, iter.parent = iter.parent, root + return root def unify(self, other):