From 3c217d82f710843f1f19b8234a3a9fc5b43ac23d Mon Sep 17 00:00:00 2001 From: Francis Dupont Date: Tue, 12 Mar 2024 16:24:10 +0100 Subject: [PATCH] [#3262] Optimized getSubnet() (#3268 1st point) --- src/lib/dhcpsrv/cfg_subnets4.cc | 20 ++++---------------- src/lib/dhcpsrv/cfg_subnets4.h | 8 ++++---- src/lib/dhcpsrv/cfg_subnets6.cc | 20 ++++---------------- src/lib/dhcpsrv/cfg_subnets6.h | 8 ++++---- 4 files changed, 16 insertions(+), 40 deletions(-) diff --git a/src/lib/dhcpsrv/cfg_subnets4.cc b/src/lib/dhcpsrv/cfg_subnets4.cc index 5b91f3c916..681f4c36f0 100644 --- a/src/lib/dhcpsrv/cfg_subnets4.cc +++ b/src/lib/dhcpsrv/cfg_subnets4.cc @@ -183,13 +183,6 @@ CfgSubnets4::merge(CfgOptionDefPtr cfg_def, CfgSharedNetworks4Ptr networks, } } -ConstSubnet4Ptr -CfgSubnets4::getBySubnetId(const SubnetID& subnet_id) const { - auto const& index = subnets_.get(); - auto subnet_it = index.find(subnet_id); - return ((subnet_it != index.cend()) ? (*subnet_it) : ConstSubnet4Ptr()); -} - ConstSubnet4Ptr CfgSubnets4::getByPrefix(const std::string& subnet_text) const { auto const& index = subnets_.get(); @@ -459,15 +452,10 @@ CfgSubnets4::selectSubnet(const std::string& iface, } Subnet4Ptr -CfgSubnets4::getSubnet(const SubnetID id) const { - /// @todo: Once this code is migrated to multi-index container, use - /// an index rather than full scan. - for (auto const& subnet : subnets_) { - if (subnet->getID() == id) { - return (subnet); - } - } - return (Subnet4Ptr()); +CfgSubnets4::getSubnet(const SubnetID subnet_id) const { + auto const& index = subnets_.get(); + auto subnet_it = index.find(subnet_id); + return ((subnet_it != index.cend()) ? (*subnet_it) : Subnet4Ptr()); } Subnet4Ptr diff --git a/src/lib/dhcpsrv/cfg_subnets4.h b/src/lib/dhcpsrv/cfg_subnets4.h index 57b7e00e68..5d2519b9e6 100644 --- a/src/lib/dhcpsrv/cfg_subnets4.h +++ b/src/lib/dhcpsrv/cfg_subnets4.h @@ -135,7 +135,9 @@ public: /// /// @return Pointer to the @c Subnet4 object or null pointer if such /// subnet doesn't exist. - ConstSubnet4Ptr getBySubnetId(const SubnetID& subnet_id) const; + ConstSubnet4Ptr getBySubnetId(const SubnetID& subnet_id) const { + return (getSubnet(subnet_id)); + } /// @brief Returns const pointer to a subnet which matches the specified /// prefix in the canonical form. @@ -221,11 +223,9 @@ public: /// @brief Returns subnet with specified subnet-id value /// - /// Warning: this method uses full scan. Its use is not recommended for - /// packet processing. /// Please use @ref getBySubnetId instead when possible. /// - /// @return Subnet (or NULL) + /// @return Subnet (or null) Subnet4Ptr getSubnet(const SubnetID id) const; /// @brief Returns a pointer to a subnet if provided address is in its range. diff --git a/src/lib/dhcpsrv/cfg_subnets6.cc b/src/lib/dhcpsrv/cfg_subnets6.cc index 0b77ef43f4..704cd42cb9 100644 --- a/src/lib/dhcpsrv/cfg_subnets6.cc +++ b/src/lib/dhcpsrv/cfg_subnets6.cc @@ -185,13 +185,6 @@ CfgSubnets6::merge(CfgOptionDefPtr cfg_def, CfgSharedNetworks6Ptr networks, } } -ConstSubnet6Ptr -CfgSubnets6::getBySubnetId(const SubnetID& subnet_id) const { - auto const& index = subnets_.get(); - auto subnet_it = index.find(subnet_id); - return ((subnet_it != index.cend()) ? (*subnet_it) : ConstSubnet6Ptr()); -} - ConstSubnet6Ptr CfgSubnets6::getByPrefix(const std::string& subnet_text) const { auto const& index = subnets_.get(); @@ -375,15 +368,10 @@ CfgSubnets6::selectSubnet(const OptionPtr& interface_id, } Subnet6Ptr -CfgSubnets6::getSubnet(const SubnetID id) const { - /// @todo: Once this code is migrated to multi-index container, use - /// an index rather than full scan. - for (auto const& subnet : subnets_) { - if (subnet->getID() == id) { - return (subnet); - } - } - return (Subnet6Ptr()); +CfgSubnets6::getSubnet(const SubnetID subnet_id) const { + auto const& index = subnets_.get(); + auto subnet_it = index.find(subnet_id); + return ((subnet_it != index.cend()) ? (*subnet_it) : Subnet6Ptr()); } SubnetIDSet diff --git a/src/lib/dhcpsrv/cfg_subnets6.h b/src/lib/dhcpsrv/cfg_subnets6.h index ddf17d6499..9e618b4422 100644 --- a/src/lib/dhcpsrv/cfg_subnets6.h +++ b/src/lib/dhcpsrv/cfg_subnets6.h @@ -136,7 +136,9 @@ public: /// /// @return Pointer to the @c Subnet6 object or null pointer if such /// subnet doesn't exist. - ConstSubnet6Ptr getBySubnetId(const SubnetID& subnet_id) const; + ConstSubnet6Ptr getBySubnetId(const SubnetID& subnet_id) const { + return (getSubnet(subnet_id)); + } /// @brief Returns const pointer to a subnet which matches the specified /// prefix in the canonical form. @@ -203,11 +205,9 @@ public: /// @brief Returns subnet with specified subnet-id value /// - /// Warning: this method uses full scan. Its use is not recommended for - /// packet processing. /// Please use @ref getBySubnetId instead when possible. /// - /// @return Subnet (or NULL) + /// @return Subnet (or null) Subnet6Ptr getSubnet(const SubnetID id) const; /// @brief Selects the subnet using a specified address. -- 2.47.3