]> git.ipfire.org Git - thirdparty/sqlalchemy/sqlalchemy.git/commitdiff
A couple critical path optimizations
authorJason Kirtland <jek@discorporate.us>
Tue, 21 Aug 2007 07:55:43 +0000 (07:55 +0000)
committerJason Kirtland <jek@discorporate.us>
Tue, 21 Aug 2007 07:55:43 +0000 (07:55 +0000)
(some sql operations faster by nearly 10% wallclock, general orm around 3%)

lib/sqlalchemy/engine/default.py
lib/sqlalchemy/sql/compiler.py
lib/sqlalchemy/sql/util.py
lib/sqlalchemy/util.py

index ca22dd2a7c15d1d92b3c61e97f8d1450647dc70e..0ab0eb82be6b1cc05d2818234c251182e1876d84 100644 (file)
@@ -140,13 +140,13 @@ class DefaultExecutionContext(base.ExecutionContext):
             self.isinsert = compiled.isinsert
             self.isupdate = compiled.isupdate
             if parameters is None:
-                self.compiled_parameters = compiled.construct_params({})
+                self.compiled_parameters = compiled.construct_params()
                 self.executemany = False
             elif not isinstance(parameters, (list, tuple)):
                 self.compiled_parameters = compiled.construct_params(parameters)
                 self.executemany = False
             else:
-                self.compiled_parameters = [compiled.construct_params(m or {}) for m in parameters]
+                self.compiled_parameters = [compiled.construct_params(m) for m in parameters]
                 if len(self.compiled_parameters) == 1:
                     self.compiled_parameters = self.compiled_parameters[0]
                     self.executemany = False
index 59964178ccce360916864556fa6b8fcc2f07c16e..99cfa04703f8e937d45c3a852922fc22f683909b 100644 (file)
@@ -212,7 +212,7 @@ class DefaultCompiler(engine.Compiled, visitors.ClauseVisitor):
         
         return None
 
-    def construct_params(self, params):
+    def construct_params(self, params=None):
         """Return a sql.util.ClauseParameters object.
         
         Combines the given bind parameter dictionary (string keys to object values)
@@ -223,15 +223,20 @@ class DefaultCompiler(engine.Compiled, visitors.ClauseVisitor):
         
         d = sql_util.ClauseParameters(self.dialect, self.positiontup)
 
-        pd = self.parameters or {}
-        pd.update(params)
+        if self.parameters is None:
+            pd = {}
+        else:
+            pd = self.parameters
+        if params is not None:
+            pd.update(params)
 
+        bind_names = self.bind_names
         for key, bind in self.binds.iteritems():
-            d.set_parameter(bind, pd.get(key, bind.value), self.bind_names[bind])
+            d.set_parameter(bind, pd.get(key, bind.value), bind_names[bind])
         
         return d
 
-    params = property(lambda self:self.construct_params({}), doc="""Return the `ClauseParameters` corresponding to this compiled object.  
+    params = property(lambda self:self.construct_params(), doc="""Return the `ClauseParameters` corresponding to this compiled object.  
         A shortcut for `construct_params()`.""")
         
     def default_from(self):
index 2c7294e663c4ef3267cceff091259227fcaeec5e..a1d44ccfe67bc0734c4460520255a02a685a8d2c 100644 (file)
@@ -12,10 +12,15 @@ class ClauseParameters(object):
     the ``TypeEngine`` objects present in the ``_BindParamClause`` instances.
     """
 
+    __slots__ = 'dialect', '__binds', 'positional'
+
     def __init__(self, dialect, positional=None):
         self.dialect = dialect
         self.__binds = {}
-        self.positional = positional or []
+        if positional is None:
+            self.positional = []
+        else:
+            self.positional = positional
 
     def get_parameter(self, key):
         return self.__binds[key]
@@ -68,9 +73,15 @@ class ClauseParameters(object):
             return processors[key](self.__binds[key][2])
         else:
             return self.__binds[key][2]
-            
+
     def get_raw_list(self, processors):
-        return [self.__get_processed(key, processors) for key in self.positional]
+        binds, res = self.__binds, []
+        for key in self.positional:
+            if key in processors:
+                res.append(processors[key](binds[key][2]))
+            else:
+                res.append(binds[key][2])
+        return res
 
     def get_raw_dict(self, processors, encode_keys=False):
         if encode_keys:
index d31be6a3635b4722b3f406a022ac16e0efeaa36b..ae8205f2109052bd395a3ef10373d33b37bf188e 100644 (file)
@@ -419,30 +419,32 @@ class DictDecorator(dict):
         return dict.__repr__(self) + repr(self.decorate)
 
 class OrderedSet(Set):
-    def __init__(self, d=None, **kwargs):
-      super(OrderedSet, self).__init__(**kwargs)
-      self._list = []
-      if d: self.update(d, **kwargs)
+    def __init__(self, d=None):
+        Set.__init__(self)
+        self._list = []
+        if d is not None:
+            self.update(d)
 
     def add(self, key):
-      if key not in self:
-          self._list.append(key)
-      super(OrderedSet, self).add(key)
+        if key not in self:
+            self._list.append(key)
+        Set.add(self, key)
 
     def remove(self, element):
-      super(OrderedSet, self).remove(element)
-      self._list.remove(element)
+        Set.remove(self, element)
+        self._list.remove(element)
 
     def discard(self, element):
-      try:
-          super(OrderedSet, self).remove(element)
-      except KeyError: pass
-      else:
-          self._list.remove(element)
+        try:
+            Set.remove(self, element)
+        except KeyError:
+            pass
+        else:
+            self._list.remove(element)
 
     def clear(self):
-      super(OrderedSet, self).clear()
-      self._list=[]
+        Set.clear(self)
+        self._list = []
 
     def __getitem__(self, key):
         return self._list[key]
@@ -452,7 +454,8 @@ class OrderedSet(Set):
 
     def update(self, iterable):
       add = self.add
-      for i in iterable: add(i)
+      for i in iterable:
+          add(i)
       return self
 
     def __repr__(self):
@@ -487,14 +490,14 @@ class OrderedSet(Set):
     __ior__ = update
 
     def intersection_update(self, other):
-      super(OrderedSet, self).intersection_update(other)
+      Set.intersection_update(self, other)
       self._list = [ a for a in self._list if a in other]
       return self
 
     __iand__ = intersection_update
 
     def symmetric_difference_update(self, other):
-      super(OrderedSet, self).symmetric_difference_update(other)
+      Set.symmetric_difference_update(self, other)
       self._list =  [ a for a in self._list if a in self]
       self._list += [ a for a in other._list if a in self]
       return self
@@ -502,7 +505,7 @@ class OrderedSet(Set):
     __ixor__ = symmetric_difference_update
 
     def difference_update(self, other):
-      super(OrderedSet, self).difference_update(other)
+      Set.difference_update(self, other)
       self._list = [ a for a in self._list if a in self]
       return self