Commits

Pierre Carbonnelle committed 3d49b7e

speed: use tuple instead of string for key. Remove _remove

  • Participants
  • Parent commits b7b13af

Comments (0)

Files changed (2)

 the other one, when compared using structural equality.  The
 variant tag is used as a key by the subgoal table.
 
-Element        id            key            tag
--------        --            ---            ---
-variable_1     variable_1    f1             v1
-X              X             v2             v2
-1              1             c2             c3
-1.0            1.0           c4             c4
-'a'            'a'           c5             c5
-('a', X)       ('a', X)      (c5v2)         (c5v1)
-X+1            (X+1)         (v2+c1)        (v2+c1)
+Element        id            key               tag
+-------        --            ---               ---
+variable_1     variable_1    (f,1)             (v,1)
+X              X             (v,2)             (v,2)
+1              1             (c,2)             (c,3)
+1.0            1.0           (c,4)             (c,4)
+'a'            'a'           (c,5)             (c,5)
+('a', X)       ('a', X)      ((c,5),(v,2))     ((c,5),(v,1))
+X+1            (X+1)         ((v,2),+,(c,1))   ((v,2),+,(c,1))
+
 lambda X:..    (c1l1v2)      (c1l1v2)       (c1l1v2)
 X[0]           (X[0)         (v2[c2)        (v2[c2)
 range_(N)      (c1..v2)      (c1..v2)       (c1..v1)

pyDatalog/pyEngine.py

     __slots__ = ['id', 'key']
     counter = util.Counter()  
     def __init__(self):
-        self.id = 'f' + str(Fresh_var.counter.next()) #id
+        self.id = ('f', Fresh_var.counter.next()) #id
         self.key = self.id #id
     
     def is_const(self):
 
 class Var(Fresh_var, Interned):
     """ A variable in a clause or query """
-    __slots__ = ['id', 'key', '_remove']
+    __slots__ = ['id', 'key']
     lock = threading.RLock()
     registry = weakref.WeakValueDictionary()
     counter = util.Counter()
             if o is Interned.notFound:
                 o = object.__new__(cls) # o is the ref that keeps it alive
                 o.id = _id #id
-                o.key = 'v' + str(Var.counter.next()) #id
+                o.key = ('v' , Var.counter.next()) #id
                 cls.registry[_id] = o
         return o
     def __init__(self, name):
 
 class Const(Interned):
     """ a constant """
-    __slots__ = ['id', 'key', '_remove']
+    __slots__ = ['id', 'key']
     lock = threading.RLock()
     registry = weakref.WeakValueDictionary()
     counter = util.Counter()
             if o is Interned.notFound: 
                 o = object.__new__(cls) # o is the ref that keeps it alive
                 o.id = _id #id
-                o.key = 'c' + str(Const.counter.next())
+                o.key = ('c' , Const.counter.next())
                 cls.registry[r] = o
         return o
     is_constant = True
 
 class VarTuple(Interned):
     """ a tuple / list of variables, constants or tuples """
-    __slots__ = ['id', 'key', 'is_constant', '_remove']
+    __slots__ = ['id', 'key', 'is_constant']
     lock = threading.RLock()
     registry = weakref.WeakValueDictionary()
     def __new__(cls,  _id):
             if o is Interned.notFound: 
                 o = object.__new__(cls) # o is the ref that keeps it alive
                 o._id = _id #id
-                o.key = '('+ ''.join([e.key for e in _id]) + ')' #id
+                o.key = tuple(e.key for e in _id) #id
                 o.id = tuple([element.id for element in _id])
                 o.is_constant = all(element.is_constant for element in _id)
                 cls.registry[_id] = o
     def get_tag(self, env): #id
         if self.is_constant: # can use lazy property only for constants
             return self.key
-        return repr([t.get_tag(env) for t in self._id])#TODO
+        return tuple(t.get_tag(env) for t in self._id)#TODO
     
     def subst(self, env): #unify
         if self.is_constant: # can use lazy property only for constants
         self.lhs = lhs
         self.rhs = rhs
         self.is_constant = False
-        self.id = "(%s%s%s)" % (self.lhs.id, self.operator_id, self.rhs.id) #id
-        self.key = "(%s%s%s)" % (self.lhs.key, self.operator_id, self.rhs.key) #id
+        self.id = (self.lhs.id, self.operator_id, self.rhs.id) #id
+        self.key = (self.lhs.key, self.operator_id, self.rhs.key) #id
     
     def get_tag(self, env): #id
-        return "(%s%s%s)" % (self.lhs.get_tag(env), self.operator_id, self.rhs.get_tag(env))
+        return (self.lhs.get_tag(env), self.operator_id, self.rhs.get_tag(env))
     
     def subst(self, env): #unify
         lhs = self.lhs.subst(env)
     if not hasattr(literal, 'key'): # cached
         terms = literal.terms
         if len(terms) == literal.pred.prearity:
-            literal.key = literal.pred.id + ''.join([term.key for term in literal.terms])
+            literal.key = (literal.pred.id,) + tuple(term.key for term in literal.terms)
         else:
-            literal.key = literal.pred.id + ''.join([terms[i].key for i in range(literal.pred.prearity)])
+            literal.key = (literal.pred.id,) + tuple(terms[i].key for i in range(literal.pred.prearity))
     return literal.key
     
 
     """ the tag is used as a key by the subgoal table """
     if not hasattr(literal, 'tag'): # cached
         env = {}
-        literal.tag = literal.pred.id + ''.join([term.get_tag(env) for term in literal.terms])
+        literal.tag = (literal.pred.id,) + tuple(term.get_tag(env) for term in literal.terms)
     return literal.tag
      
 
         if they have the same id.  A clause's id is used as a key into the
         clause database. """
     if not hasattr(clause, 'id'): # cached
-        clause.id = get_key(clause.head) + '<=' + '&'.join([get_key(bodi) for bodi in clause.body])
+        clause.id = (get_key(clause.head),) + tuple(get_key(bodi) for bodi in clause.body)
     return clause.id
     
 def subst_in_clause(clause, env, parent_class=None):