Replaced deque() with list() in prefetch_related_objects().

deque() isn't needed since algorithm doesn't require FIFO.
This commit is contained in:
Sergey Fedoseev 2017-09-06 17:13:18 +05:00 committed by Tim Graham
parent a8ad1e3216
commit 379caf397e
1 changed files with 5 additions and 7 deletions

View File

@ -6,7 +6,7 @@ import copy
import operator import operator
import sys import sys
import warnings import warnings
from collections import OrderedDict, deque from collections import OrderedDict
from contextlib import suppress from contextlib import suppress
from itertools import chain from itertools import chain
@ -1382,8 +1382,6 @@ def prefetch_related_objects(model_instances, *related_lookups):
if len(model_instances) == 0: if len(model_instances) == 0:
return # nothing to do return # nothing to do
related_lookups = normalize_prefetch_lookups(related_lookups)
# We need to be able to dynamically add to the list of prefetch_related # We need to be able to dynamically add to the list of prefetch_related
# lookups that we look up (see below). So we need some book keeping to # lookups that we look up (see below). So we need some book keeping to
# ensure we don't do duplicate work. # ensure we don't do duplicate work.
@ -1392,9 +1390,9 @@ def prefetch_related_objects(model_instances, *related_lookups):
auto_lookups = set() # we add to this as we go through. auto_lookups = set() # we add to this as we go through.
followed_descriptors = set() # recursion protection followed_descriptors = set() # recursion protection
all_lookups = deque(related_lookups) all_lookups = normalize_prefetch_lookups(reversed(related_lookups))
while all_lookups: while all_lookups:
lookup = all_lookups.popleft() lookup = all_lookups.pop()
if lookup.prefetch_to in done_queries: if lookup.prefetch_to in done_queries:
if lookup.queryset: if lookup.queryset:
raise ValueError("'%s' lookup was already seen with a different queryset. " raise ValueError("'%s' lookup was already seen with a different queryset. "
@ -1467,9 +1465,9 @@ def prefetch_related_objects(model_instances, *related_lookups):
# the new lookups from relationships we've seen already. # the new lookups from relationships we've seen already.
if not (lookup in auto_lookups and descriptor in followed_descriptors): if not (lookup in auto_lookups and descriptor in followed_descriptors):
done_queries[prefetch_to] = obj_list done_queries[prefetch_to] = obj_list
new_lookups = normalize_prefetch_lookups(additional_lookups, prefetch_to) new_lookups = normalize_prefetch_lookups(reversed(additional_lookups), prefetch_to)
auto_lookups.update(new_lookups) auto_lookups.update(new_lookups)
all_lookups.extendleft(new_lookups) all_lookups.extend(new_lookups)
followed_descriptors.add(descriptor) followed_descriptors.add(descriptor)
else: else:
# Either a singly related object that has already been fetched # Either a singly related object that has already been fetched