mirror of
https://github.com/sqlalchemy/sqlalchemy.git
synced 2026-06-05 23:37:20 -04:00
A couple critical path optimizations
(some sql operations faster by nearly 10% wallclock, general orm around 3%)
This commit is contained in:
@@ -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
|
||||
|
||||
@@ -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):
|
||||
|
||||
@@ -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:
|
||||
|
||||
+23
-20
@@ -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
|
||||
|
||||
|
||||
Reference in New Issue
Block a user