Modernize mapper core types
There's a number of best practices that have evolved in our use of
flat_maps and flat_sets since this code was originally written.
First, add std::less<> to the Compare template argument. The default
for this is std::less<Key> which limits find() calls (and any lookup for
that matter) to only supporting std::string. Using std::less<> allows
lookup by std::string_view, which can prevent some copies in some cases.
Next, add std::vector<...> to the types under the AllocatorOrContainer
template arg. Per our coding standard, this overrides the default of
using boost::vector, and replaces it with std::vector, which, although
it has a very similar interface, tends to optimize better, and be better
supported overall.
The rest of this patch updates a couple places where the various types
were specifically hardcoded, so it moves to using the various using
declarations.
Tested: Unit tests; Next patch tests this more fully.
Change-Id: I11e8ecb669f31193c55dda344b25d3fa5d191502
Signed-off-by: Ed Tanous <edtanous@google.com>
diff --git a/src/main.cpp b/src/main.cpp
index 86ef055..9ceb9ab 100644
--- a/src/main.cpp
+++ b/src/main.cpp
@@ -359,11 +359,9 @@
}
}
-void addObjectMapResult(
- std::vector<InterfaceMapType::value_type>& objectMap,
- const std::string& objectPath,
- const std::pair<std::string, boost::container::flat_set<std::string>>&
- interfaceMap)
+void addObjectMapResult(std::vector<InterfaceMapType::value_type>& objectMap,
+ const std::string& objectPath,
+ const ConnectionNames::value_type& interfaceMap)
{
// Adds an object path/service name/interface list entry to
// the results of GetSubTree and GetAncestors.
@@ -494,13 +492,11 @@
return ret;
}
-boost::container::flat_map<std::string, boost::container::flat_set<std::string>>
- getObject(const InterfaceMapType& interfaceMap, const std::string& path,
- std::vector<std::string>& interfaces)
+ConnectionNames getObject(const InterfaceMapType& interfaceMap,
+ const std::string& path,
+ std::vector<std::string>& interfaces)
{
- boost::container::flat_map<std::string,
- boost::container::flat_set<std::string>>
- results;
+ ConnectionNames results;
// Interfaces need to be sorted for intersect to function
std::sort(interfaces.begin(), interfaces.end());
diff --git a/src/processing.cpp b/src/processing.cpp
index 448830f..561cbde 100644
--- a/src/processing.cpp
+++ b/src/processing.cpp
@@ -142,12 +142,9 @@
// This is all needed so that mapper operations can be done
// on the new parent paths.
using iface_map_iterator = InterfaceMapType::iterator;
- using iface_map_value_type =
- boost::container::flat_map<std::string,
- boost::container::flat_set<std::string>>;
- using name_map_iterator = iface_map_value_type::iterator;
+ using name_map_iterator = InterfaceMapType::mapped_type::iterator;
- static const boost::container::flat_set<std::string> defaultIfaces{
+ static const InterfaceNames defaultIfaces{
"org.freedesktop.DBus.Introspectable", "org.freedesktop.DBus.Peer",
"org.freedesktop.DBus.Properties"};
@@ -159,11 +156,10 @@
parent = parent.substr(0, pos);
std::pair<iface_map_iterator, bool> parentEntry =
- interfaceMap.insert(std::make_pair(parent, iface_map_value_type{}));
+ interfaceMap.try_emplace(parent);
std::pair<name_map_iterator, bool> ifaceEntry =
- parentEntry.first->second.insert(
- std::make_pair(wellKnown, defaultIfaces));
+ parentEntry.first->second.try_emplace(wellKnown, defaultIfaces);
if (!ifaceEntry.second)
{
diff --git a/src/test/name_change.cpp b/src/test/name_change.cpp
index cd491a5..860a387 100644
--- a/src/test/name_change.cpp
+++ b/src/test/name_change.cpp
@@ -30,8 +30,7 @@
boost::container::flat_map<std::string, std::string> nameOwners = {
{":1.99", defaultDbusSvc}};
std::string oldOwner = {":1.99"};
- boost::container::flat_set<std::string> assocInterfacesSet = {
- assocDefsInterface};
+ InterfaceNames assocInterfacesSet = {assocDefsInterface};
// Build up these objects so that an associated interface will match
// with the associated owner being removed
diff --git a/src/test/util/association_objects.hpp b/src/test/util/association_objects.hpp
index f6b6d1d..c547cf6 100644
--- a/src/test/util/association_objects.hpp
+++ b/src/test/util/association_objects.hpp
@@ -48,14 +48,13 @@
}
// Create a default interfaceMapType with input values
-InterfaceMapType createInterfaceMap(
- const std::string& path, const std::string& connectionName,
- const boost::container::flat_set<std::string>& interfaceNames)
+InterfaceMapType createInterfaceMap(const std::string& path,
+ const std::string& connectionName,
+ const InterfaceNames& interfaceNames)
{
- boost::container::flat_map<std::string,
- boost::container::flat_set<std::string>>
- connectionMap = {{connectionName, interfaceNames}};
- InterfaceMapType interfaceMap = {{path, connectionMap}};
+ ConnectionNames connectionMap{{connectionName, interfaceNames}};
+ InterfaceMapType interfaceMap{{path, connectionMap}};
+
return interfaceMap;
}
diff --git a/src/types.hpp b/src/types.hpp
index 3e446df..f16b413 100644
--- a/src/types.hpp
+++ b/src/types.hpp
@@ -16,9 +16,16 @@
* connection names
* interface names
*/
+using InterfaceNames = boost::container::flat_set<std::string, std::less<>,
+ std::vector<std::string>>;
+
+using ConnectionNames = boost::container::flat_map<
+ std::string, InterfaceNames, std::less<>,
+ std::vector<std::pair<std::string, InterfaceNames>>>;
+
using InterfaceMapType = boost::container::flat_map<
- std::string, boost::container::flat_map<
- std::string, boost::container::flat_set<std::string>>>;
+ std::string, ConnectionNames, std::less<>,
+ std::vector<std::pair<std::string, ConnectionNames>>>;
/**
* Associations and some metadata are stored in associationInterfaces.