# HG changeset patch # User Paul Boddie # Date 1192394796 -7200 # Node ID 85000c878e8ae87f5160d32e783205f232a80192 # Parent dff259dd9f5e0f6f2ab65449c73163ec5fbf3e85 Introduced iteration over instance fixing in order to reduce the non-distinct instances as much as possible. diff -r dff259dd9f5e -r 85000c878e8a simplify/__init__.py --- a/simplify/__init__.py Sun Oct 14 22:46:10 2007 +0200 +++ b/simplify/__init__.py Sun Oct 14 22:46:36 2007 +0200 @@ -226,11 +226,27 @@ "Fix instances for all modules loaded by this importer." - for module in self.get_modules(): - simplify.fixinstances.fix_structures(module) - for module in self.get_modules(): - simplify.fixinstances.fix_signatures(module) - for module in self.get_modules(): - simplify.fixinstances.fix(module) + count = simplify.fixinstances.system.count + while 1: + for module in self.get_modules(): + simplify.fixinstances.fix_structures(module) + + print simplify.fixinstances.system.count - count, "attributes changed" + + for module in self.get_modules(): + simplify.fixinstances.fix_signatures(module) + + print simplify.fixinstances.system.count - count, "parameters changed" + + for module in self.get_modules(): + simplify.fixinstances.fix(module) + + print simplify.fixinstances.system.count - count, "sites changed" + + if simplify.fixinstances.system.count == count: + break + else: + print "Processing structures again after", simplify.fixinstances.system.count - count, "changes" + count = simplify.fixinstances.system.count # vim: tabstop=4 expandtab shiftwidth=4 diff -r dff259dd9f5e -r 85000c878e8a simplify/fixinstances.py --- a/simplify/fixinstances.py Sun Oct 14 22:46:10 2007 +0200 +++ b/simplify/fixinstances.py Sun Oct 14 22:46:36 2007 +0200 @@ -39,6 +39,29 @@ from simplify.simplified import * +class System: + + """ + A class maintaining the state of the fixing system like that used by the + annotation system. When the system counter can no longer be incremented by + any fixing operation, the system may be considered fixed. + """ + + def __init__(self): + self.count = 0 + + def fix(self, original_value, proposed_value): + + """ + Update the counter depending on the 'original_value' and the + 'proposed_value' when attempting to fix the instances of a class. + """ + + if original_value is not proposed_value: + self.count += 1 + +system = System() + # Fixing of instance information. class Fixer(Visitor): @@ -225,7 +248,8 @@ if name == "accesses": attr, accessor = item value = attr.type - new_items.append((Attribute(self._get_replacement(attr.context), self._get_replacement(value)), self._get_replacement(accessor))) + new_items.append((Attribute(self._get_replacement(attr.context), self._get_replacement(value)), + self._get_replacement(accessor))) elif name == "invocations": new_items.add(self._get_replacement(item)) else: @@ -243,6 +267,10 @@ if isinstance(value, Instance): distinct_instances = value.get_class().get_distinct_instances() + + # Make the fix by returning the proposed distinct instance. + + system.fix(value, distinct_instances[value]) return distinct_instances[value] # For subprograms, find the distinct instance's copy for the owner @@ -258,6 +286,10 @@ if value.copy_of.copies.has_key(instance): subprogram = value.copy_of.copies[instance] if subprogram.paramtypes == value.paramtypes: + + # Make the fix by returning the proposed subprogram. + + system.fix(value, subprogram) return subprogram return value