From 04f23958e63a5c74134fd2e9376fa8ed00eabbb2 Mon Sep 17 00:00:00 2001 From: Dirk Mueller Date: Tue, 26 Mar 2019 11:18:51 +0100 Subject: [PATCH] Avoid iterating over all of the segment data just for counting getting the full collection (which includes iterating over it and populating attributes) just to gather the count of it is wasteful. we can just use the count api. Change-Id: I1b216cb2c8c5b612f12554454d5721a14975f138 Closes-Bug: #1821708 --- neutron/services/segments/plugin.py | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/neutron/services/segments/plugin.py b/neutron/services/segments/plugin.py index eadfa8ef286..d7fa96f05a7 100644 --- a/neutron/services/segments/plugin.py +++ b/neutron/services/segments/plugin.py @@ -442,6 +442,9 @@ class SegmentHostRoutes(object): def _get_subnets(self, context, network_id): return subnet_obj.Subnet.get_objects(context, network_id=network_id) + def _count_subnets(self, context, network_id): + return subnet_obj.Subnet.count(context, network_id=network_id) + def _calculate_routed_network_host_routes(self, context, ip_version, network_id=None, subnet_id=None, segment_id=None, @@ -596,7 +599,8 @@ class SegmentHostRoutes(object): subnet = kwargs['subnet'] # If there are other subnets on the network and subnet has segment_id # ensure host routes for all subnets are updated. - if (len(self._get_subnets(context, subnet['network_id'])) > 1 and + + if (self._count_subnets(context, subnet['network_id']) > 1 and subnet.get('segment_id')): self._update_routed_network_host_routes(context, subnet['network_id'])