Skip to content

Commit c9e4d3d

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. For example, I suspect that the tag count fields are 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 af857c2 commit c9e4d3d

2 files changed

Lines changed: 20 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: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -280,13 +280,26 @@ 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
289+
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)
288294

289-
patches = context['filters'].apply(patches)
295+
paginator = Paginator(request, patch_ids)
296+
patch_ids = list(
297+
paginator.current_page.object_list.values_list('id', flat=True)
298+
)
299+
300+
# Aggregation
301+
302+
patches = patches.filter(id__in=patch_ids)
290303
if not editable_order:
291304
patches = order.apply(patches)
292305

@@ -321,11 +334,13 @@ def generic_list(
321334
)
322335
)
323336

324-
paginator = Paginator(request, patches)
337+
# annotate with tag counts
338+
patches = patches.with_tag_counts(project)
325339

326340
context.update(
327341
{
328342
'page': paginator.current_page,
343+
'patches': patches,
329344
'patch_form': properties_form,
330345
'create_bundle_form': create_bundle_form,
331346
'project': project,

0 commit comments

Comments
 (0)