Optimisation in prefetch_related_objects

This commit is contained in:
Luke Plant 2013-05-24 11:19:44 +01:00
parent 4fd94969d8
commit 17559e6eb0
1 changed files with 22 additions and 25 deletions

View File

@ -1545,8 +1545,18 @@ def prefetch_related_objects(result_cache, related_lookups):
if len(obj_list) == 0: if len(obj_list) == 0:
break 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 good_objects = True
for obj in obj_list: 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'): if not hasattr(obj, '_prefetched_objects_cache'):
try: try:
obj._prefetched_objects_cache = {} obj._prefetched_objects_cache = {}
@ -1557,14 +1567,6 @@ def prefetch_related_objects(result_cache, related_lookups):
# now. # now.
good_objects = False good_objects = False
break 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: if not good_objects:
break break
@ -1589,11 +1591,6 @@ def prefetch_related_objects(result_cache, related_lookups):
"prefetch_related()." % lookup) "prefetch_related()." % lookup)
if prefetcher is not None and not is_fetched: 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) obj_list, additional_prl = prefetch_one_level(obj_list, prefetcher, attr)
# We need to ensure we don't keep adding lookups from the # We need to ensure we don't keep adding lookups from the
# same relationships to stop infinite recursion. So, if we # same relationships to stop infinite recursion. So, if we