Skip to content

Commit 12a1781

Browse files
perf(endpoint_manager): avoid double queryset evaluation in update_endpoint_status
Evaluate existing_finding_endpoint_status_list once into a list with select_related("endpoint") before the two list comprehensions, preventing a duplicate DB hit and N+1 endpoint lookups. Update expected performance test counts to reflect the reduced query counts.
1 parent d311afb commit 12a1781

2 files changed

Lines changed: 11 additions & 8 deletions

File tree

dojo/importers/endpoint_manager.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -158,8 +158,11 @@ def update_endpoint_status(
158158
"""Update the list of endpoints from the new finding with the list that is in the old finding"""
159159
# New endpoints are already added in serializers.py / views.py (see comment "# for existing findings: make sure endpoints are present or created")
160160
# So we only need to mitigate endpoints that are no longer present
161-
existing_finding_endpoint_status_list = existing_finding.status_finding.exclude(
162-
Q(false_positive=True) | Q(out_of_scope=True) | Q(risk_accepted=True),
161+
# Evaluate the queryset once into a list to avoid double DB evaluation below
162+
existing_finding_endpoint_status_list = list(
163+
existing_finding.status_finding.exclude(
164+
Q(false_positive=True) | Q(out_of_scope=True) | Q(risk_accepted=True),
165+
).select_related("endpoint"),
163166
)
164167
new_finding_endpoints_list = new_finding.unsaved_endpoints
165168
if new_finding.is_mitigated:

unittests/test_importers_performance.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -270,9 +270,9 @@ def test_import_reimport_reimport_performance_pghistory_async(self):
270270
self._import_reimport_performance(
271271
expected_num_queries1=295,
272272
expected_num_async_tasks1=6,
273-
expected_num_queries2=267,
273+
expected_num_queries2=232,
274274
expected_num_async_tasks2=17,
275-
expected_num_queries3=154,
275+
expected_num_queries3=114,
276276
expected_num_async_tasks3=16,
277277
)
278278

@@ -292,9 +292,9 @@ def test_import_reimport_reimport_performance_pghistory_no_async(self):
292292
self._import_reimport_performance(
293293
expected_num_queries1=302,
294294
expected_num_async_tasks1=6,
295-
expected_num_queries2=274,
295+
expected_num_queries2=239,
296296
expected_num_async_tasks2=17,
297-
expected_num_queries3=161,
297+
expected_num_queries3=121,
298298
expected_num_async_tasks3=16,
299299
)
300300

@@ -315,9 +315,9 @@ def test_import_reimport_reimport_performance_pghistory_no_async_with_product_gr
315315
self._import_reimport_performance(
316316
expected_num_queries1=309,
317317
expected_num_async_tasks1=8,
318-
expected_num_queries2=281,
318+
expected_num_queries2=246,
319319
expected_num_async_tasks2=19,
320-
expected_num_queries3=165,
320+
expected_num_queries3=125,
321321
expected_num_async_tasks3=18,
322322
)
323323

0 commit comments

Comments
 (0)