Commit 17559e6e authored by Luke Plant's avatar Luke Plant
Browse files

Optimisation in prefetch_related_objects

parent 4fd94969
Loading
Loading
Loading
Loading
+22 −25
Original line number Diff line number Diff line
@@ -1545,8 +1545,18 @@ def prefetch_related_objects(result_cache, related_lookups):
            if len(obj_list) == 0:
                break

            current_lookup = LOOKUP_SEP.join(attrs[0:level+1])
            if current_lookup in done_queries:
                # Skip any prefetching, and any object preparation
                obj_list = done_queries[current_lookup]
                continue

            # Prepare objects:
            good_objects = True
            for obj in obj_list:
                # Since prefetching can re-use instances, it is possible to have
                # the same instance multiple times in obj_list, so obj might
                # already be prepared.
                if not hasattr(obj, '_prefetched_objects_cache'):
                    try:
                        obj._prefetched_objects_cache = {}
@@ -1557,14 +1567,6 @@ def prefetch_related_objects(result_cache, related_lookups):
                        # now.
                        good_objects = False
                        break
                else:
                    # Since prefetching can re-use instances, it is possible to
                    # have the same instance multiple times in obj_list. So we
                    # can reach this branch either because we did all of
                    # obj_list already, or because we did 'obj' earlier in this
                    # iteration over obj_list. In the first case we could
                    # shortcut and exit the loop, but not in the second.
                    continue
            if not good_objects:
                break

@@ -1589,11 +1591,6 @@ def prefetch_related_objects(result_cache, related_lookups):
                                 "prefetch_related()." % lookup)

            if prefetcher is not None and not is_fetched:
                # Check we didn't do this already
                current_lookup = LOOKUP_SEP.join(attrs[0:level+1])
                if current_lookup in done_queries:
                    obj_list = done_queries[current_lookup]
                else:
                obj_list, additional_prl = prefetch_one_level(obj_list, prefetcher, attr)
                # We need to ensure we don't keep adding lookups from the
                # same relationships to stop infinite recursion. So, if we