summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAlex Gaynor <alex.gaynor@gmail.com>2011-01-19 14:39:54 +0000
committerAlex Gaynor <alex.gaynor@gmail.com>2011-01-19 14:39:54 +0000
commita38da1ae0d6cbd6411f98e6578f0a4f50c12f6b1 (patch)
treecc53d0cbf45c6bd6c27078d8516532eb22675acc
parent457aaca7fa99d3630b3fabd1bedd4bbad4242f83 (diff)
Avoid doing quadratic amounts of work during object deletion.
git-svn-id: http://code.djangoproject.com/svn/django/trunk@15243 bcc190cf-cafb-0310-a4f2-bffc1f526a37
-rw-r--r--django/db/models/deletion.py11
1 files changed, 6 insertions, 5 deletions
diff --git a/django/db/models/deletion.py b/django/db/models/deletion.py
index f7b218472f..fd4d2c83a3 100644
--- a/django/db/models/deletion.py
+++ b/django/db/models/deletion.py
@@ -58,7 +58,8 @@ def force_managed(func):
class Collector(object):
def __init__(self, using):
self.using = using
- self.data = {} # {model: [instances]}
+ # Initially, {model: set([instances])}, later values become lists.
+ self.data = {}
self.batches = {} # {model: {field: set([instances])}}
self.field_updates = {} # {model: {(field, value): set([instances])}}
self.dependencies = {} # {model: set([models])}
@@ -75,11 +76,11 @@ class Collector(object):
return []
new_objs = []
model = objs[0].__class__
- instances = self.data.setdefault(model, [])
+ instances = self.data.setdefault(model, set())
for obj in objs:
if obj not in instances:
new_objs.append(obj)
- instances.extend(new_objs)
+ instances.update(new_objs)
# Nullable relationships can be ignored -- they are nulled out before
# deleting, and therefore do not affect the order in which objects have
# to be deleted.
@@ -190,8 +191,8 @@ class Collector(object):
@force_managed
def delete(self):
# sort instance collections
- for instances in self.data.itervalues():
- instances.sort(key=attrgetter("pk"))
+ for model, instances in self.data.items():
+ self.data[model] = sorted(instances, key=attrgetter("pk"))
# if possible, bring the models in an order suitable for databases that
# don't support transactions or cannot defer contraint checks until the