From: Mike Bayer Date: Sun, 4 Jan 2015 04:22:07 +0000 (-0500) Subject: - use a different bitwise approach here that doesn't require iterating X-Git-Tag: rel_1_0_0b1~136 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=b1928d72098dd68c8aba468d94407f991f30d465;p=thirdparty%2Fsqlalchemy%2Fsqlalchemy.git - use a different bitwise approach here that doesn't require iterating through all possible set values --- diff --git a/lib/sqlalchemy/dialects/mysql/base.py b/lib/sqlalchemy/dialects/mysql/base.py index 7836e95489..9c3f23cb2f 100644 --- a/lib/sqlalchemy/dialects/mysql/base.py +++ b/lib/sqlalchemy/dialects/mysql/base.py @@ -1496,6 +1496,10 @@ class SET(_EnumeratedValues): (value, 2 ** idx) for idx, value in enumerate(self.values) ) + self._bitmap.update( + (2 ** idx, value) + for idx, value in enumerate(self.values) + ) kw.setdefault('length', length) super(SET, self).__init__(**kw) @@ -1510,12 +1514,9 @@ class SET(_EnumeratedValues): def process(value): if value is not None: value = int(value) + return set( - [ - elem - for idx, elem in enumerate(self.values) - if value & (2 ** idx) - ] + util.map_bits(self._bitmap.__getitem__, value) ) else: return None diff --git a/lib/sqlalchemy/util/__init__.py b/lib/sqlalchemy/util/__init__.py index dfed5b90a7..7c85ef94b4 100644 --- a/lib/sqlalchemy/util/__init__.py +++ b/lib/sqlalchemy/util/__init__.py @@ -36,7 +36,7 @@ from .langhelpers import iterate_attributes, class_hierarchy, \ generic_repr, counter, PluginLoader, hybridproperty, hybridmethod, \ safe_reraise,\ get_callable_argspec, only_once, attrsetter, ellipses_string, \ - warn_limited + warn_limited, map_bits from .deprecations import warn_deprecated, warn_pending_deprecation, \ deprecated, pending_deprecation, inject_docstring_text diff --git a/lib/sqlalchemy/util/langhelpers.py b/lib/sqlalchemy/util/langhelpers.py index 7f57e501ac..b708665f9d 100644 --- a/lib/sqlalchemy/util/langhelpers.py +++ b/lib/sqlalchemy/util/langhelpers.py @@ -92,6 +92,15 @@ def _unique_symbols(used, *bases): raise NameError("exhausted namespace for symbol base %s" % base) +def map_bits(fn, n): + """Call the given function given each nonzero bit from n.""" + + while n: + b = n & (~n + 1) + yield fn(b) + n ^= b + + def decorator(target): """A signature-matching decorator factory."""