]> git.ipfire.org Git - thirdparty/sqlalchemy/sqlalchemy.git/commitdiff
few more perf improvements to selectinload
authorFederico Caselli <cfederico87@gmail.com>
Tue, 30 Jun 2026 19:56:23 +0000 (21:56 +0200)
committerFederico Caselli <cfederico87@gmail.com>
Tue, 30 Jun 2026 20:44:31 +0000 (22:44 +0200)
avout named tuple attribute access in a loop, remove unneded sort
follow up of I2e772c7ee9fa9e1b50026cab8c7997b861f1acda

Change-Id: I58b691e25e7e34452d7754a8325e13373a87632b

lib/sqlalchemy/orm/strategies.py

index c5e35a395cb52169e5ebd3661096f44b5fa0aca7..017a05200fca4fcbc5add646c79b30face83f072 100644 (file)
@@ -3417,15 +3417,15 @@ class _SelectInLoader(_PostLoader, util.MemoizedSlots):
     ):
         uselist = self.uselist
         n_pk = query_info.n_pk
+        zero_idx = query_info.zero_idx
 
-        # this sort is really for the benefit of the unit tests
-        our_keys = sorted(our_states)
+        # historically this used sorted instead of list to add determinism in
+        # tests. Since dicts are now ordered it's likely no longer needed
+        our_keys = list(our_states)
         while our_keys:
             chunk = our_keys[0:chunksize]
             our_keys = our_keys[chunksize:]
-            primary_keys = [
-                key[0] if query_info.zero_idx else key for key in chunk
-            ]
+            primary_keys = [key[0] if zero_idx else key for key in chunk]
             result = context.session.execute(
                 q,
                 params={"primary_keys": primary_keys},
@@ -3467,6 +3467,7 @@ class _SelectInLoader(_PostLoader, util.MemoizedSlots):
     ):
         uselist = self.uselist
         n_pk = query_info.n_pk
+        zero_idx = query_info.zero_idx
         _empty_result = () if uselist else None
 
         while our_states:
@@ -3474,8 +3475,7 @@ class _SelectInLoader(_PostLoader, util.MemoizedSlots):
             our_states = our_states[chunksize:]
 
             primary_keys = [
-                key[0] if query_info.zero_idx else key
-                for key, state, state_dict, overwrite in chunk
+                item[0][0] if zero_idx else item[0] for item in chunk
             ]
 
             result = context.session.execute(