Skip to content

Commit c052949

Browse files
committed
views: Optimize patch list
Patch list is pretty slow partially because it filters on a query with a lot of extra fields. I suspect that the tag count fields are to be computed even for rows that did not make it into the final result, same with joins. This patch changes this by separating the filtering stage, in which only patch IDs are queried, and the aggregation stage.
1 parent 14ed4ec commit c052949

2 files changed

Lines changed: 18 additions & 5 deletions

File tree

‎patchwork/templates/patchwork/partials/patch-list.html‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,7 @@
154154
</thead>
155155

156156
<tbody>
157-
{% for patch in page.object_list %}
157+
{% for patch in patches %}
158158
<tr id="patch-row:{{patch.id}}" data-patch-id="{{patch.id}}">
159159
{% if user.is_authenticated %}
160160
<td id="select-patch:{{patch.id}}" style="text-align: center;">

‎patchwork/views/__init__.py‎

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -280,13 +280,24 @@ def generic_list(
280280
else:
281281
context['filters'].set_status(filterclass, setting)
282282

283+
# Filtering
284+
283285
if patches is None:
284286
patches = Patch.objects.filter(project=project)
285287

286-
# annotate with tag counts
287-
patches = patches.with_tag_counts(project)
288+
patch_ids = patches
288289

289-
patches = context['filters'].apply(patches)
290+
patch_ids = patch_ids.only('id')
291+
patch_ids = context['filters'].apply(patch_ids)
292+
if not editable_order:
293+
patch_ids = order.apply(patch_ids)
294+
295+
paginator = Paginator(request, patch_ids)
296+
patch_ids = list(paginator.current_page.object_list.values_list('id', flat=True))
297+
298+
# Aggregation
299+
300+
patches = patches.filter(id__in=patch_ids)
290301
if not editable_order:
291302
patches = order.apply(patches)
292303

@@ -321,11 +332,13 @@ def generic_list(
321332
)
322333
)
323334

324-
paginator = Paginator(request, patches)
335+
# annotate with tag counts
336+
patches = patches.with_tag_counts(project)
325337

326338
context.update(
327339
{
328340
'page': paginator.current_page,
341+
'patches': patches,
329342
'patch_form': properties_form,
330343
'create_bundle_form': create_bundle_form,
331344
'project': project,

0 commit comments

Comments
 (0)