Fixed #17634 -- Optimized the performance of MultiValueDict by using append instead of copy and by minimizing the number of dict lookups. Refs #736.

git-svn-id: http://code.djangoproject.com/svn/django/trunk@17464 bcc190cf-cafb-0310-a4f2-bffc1f526a37
This commit is contained in:
Aymeric Augustin 2012-02-09 18:41:20 +00:00
parent f9ec6bd3df
commit bd58612514
2 changed files with 18 additions and 9 deletions

View File

@ -312,9 +312,9 @@ class MultiValueDict(dict):
try: try:
return super(MultiValueDict, self).__getitem__(key) return super(MultiValueDict, self).__getitem__(key)
except KeyError: except KeyError:
if default is not None: if default is None:
return default return []
return [] return default
def setlist(self, key, list_): def setlist(self, key, list_):
super(MultiValueDict, self).__setitem__(key, list_) super(MultiValueDict, self).__setitem__(key, list_)
@ -322,17 +322,20 @@ class MultiValueDict(dict):
def setdefault(self, key, default=None): def setdefault(self, key, default=None):
if key not in self: if key not in self:
self[key] = default self[key] = default
return default
return self[key] return self[key]
def setlistdefault(self, key, default_list=()): def setlistdefault(self, key, default_list=None):
if key not in self: if key not in self:
if default_list is None:
default_list = []
self.setlist(key, default_list) self.setlist(key, default_list)
return default_list
return self.getlist(key) return self.getlist(key)
def appendlist(self, key, value): def appendlist(self, key, value):
"""Appends an item to the internal list associated with key.""" """Appends an item to the internal list associated with key."""
self.setlistdefault(key, []) self.setlistdefault(key).append(value)
super(MultiValueDict, self).__setitem__(key, self.getlist(key) + [value])
def items(self): def items(self):
""" """
@ -381,15 +384,15 @@ class MultiValueDict(dict):
other_dict = args[0] other_dict = args[0]
if isinstance(other_dict, MultiValueDict): if isinstance(other_dict, MultiValueDict):
for key, value_list in other_dict.lists(): for key, value_list in other_dict.lists():
self.setlistdefault(key, []).extend(value_list) self.setlistdefault(key).extend(value_list)
else: else:
try: try:
for key, value in other_dict.items(): for key, value in other_dict.items():
self.setlistdefault(key, []).append(value) self.setlistdefault(key).append(value)
except TypeError: except TypeError:
raise ValueError("MultiValueDict.update() takes either a MultiValueDict or dictionary") raise ValueError("MultiValueDict.update() takes either a MultiValueDict or dictionary")
for key, value in kwargs.iteritems(): for key, value in kwargs.iteritems():
self.setlistdefault(key, []).append(value) self.setlistdefault(key).append(value)
def dict(self): def dict(self):
""" """

View File

@ -206,6 +206,12 @@ class MultiValueDictTests(SimpleTestCase):
self.assertEqual(list(d.itervalues()), self.assertEqual(list(d.itervalues()),
['Developer', 'Simon', 'Willison']) ['Developer', 'Simon', 'Willison'])
def test_appendlist(self):
d = MultiValueDict()
d.appendlist('name', 'Adrian')
d.appendlist('name', 'Simon')
self.assertEqual(d.getlist('name'), ['Adrian', 'Simon'])
def test_copy(self): def test_copy(self):
for copy_func in [copy.copy, lambda d: d.copy()]: for copy_func in [copy.copy, lambda d: d.copy()]:
d1 = MultiValueDict({ d1 = MultiValueDict({