]> git.ipfire.org Git - thirdparty/sqlalchemy/sqlalchemy.git/commitdiff
micro-optimize unique_list
authorGaëtan de Menten <gdementen@gmail.com>
Fri, 24 Apr 2015 12:26:42 +0000 (14:26 +0200)
committerGaëtan de Menten <gdementen@gmail.com>
Fri, 24 Apr 2015 12:26:42 +0000 (14:26 +0200)
This makes unique_list approx 2x faster in my (simple) tests

lib/sqlalchemy/util/_collections.py

index 4fb12d71b85f7d12aac343feca21c3e2209d57e2..db2c21949b1a6f9b97513d04136323d5ee34b927 100644 (file)
@@ -743,15 +743,16 @@ _property_getters = PopulateDict(
 
 
 def unique_list(seq, hashfunc=None):
-    seen = {}
+    seen = set()
+    seen_add = seen.add
     if not hashfunc:
         return [x for x in seq
                 if x not in seen
-                and not seen.__setitem__(x, True)]
+                and not seen_add(x)]
     else:
         return [x for x in seq
                 if hashfunc(x) not in seen
-                and not seen.__setitem__(hashfunc(x), True)]
+                and not seen_add(hashfunc(x))]
 
 
 class UniqueAppender(object):