From bdb6f2db4a1f98484b222d61abceab8adb74dfd1 Mon Sep 17 00:00:00 2001 From: Alexander Belyaev <32522095+pifon2a@users.noreply.github.com> Date: Mon, 8 Oct 2018 10:47:28 +0200 Subject: [PATCH] [ABSL] Use flat_hash_map instead of unordered_map. (#1439) --- bazel/repositories.bzl | 6 +++--- cartographer/BUILD.bazel | 1 + .../internal/handlers/add_sensor_data_batch_handler.h | 3 ++- cartographer/cloud/metrics/prometheus/family_factory.cc | 9 ++++----- cartographer/common/thread_pool.h | 4 ++-- cartographer/io/points_processor_pipeline_builder.h | 4 ++-- cartographer/io/serialization_format_migration.cc | 4 ++-- cartographer/mapping/internal/2d/pose_graph_2d.h | 4 ++-- cartographer/mapping/internal/3d/pose_graph_3d.h | 4 ++-- cartographer/mapping/internal/connected_components.cc | 2 +- cartographer/mapping/internal/connected_components.h | 2 +- cartographer/mapping/pose_graph.h | 2 +- cartographer/sensor/internal/collator.h | 4 ++-- cartographer/sensor/internal/ordered_multi_queue.h | 2 ++ cartographer/sensor/internal/trajectory_collator.h | 8 ++++---- cmake/modules/FindAbseil.cmake | 3 ++- 16 files changed, 33 insertions(+), 29 deletions(-) diff --git a/bazel/repositories.bzl b/bazel/repositories.bzl index 9345026..5a0f49a 100644 --- a/bazel/repositories.bzl +++ b/bazel/repositories.bzl @@ -256,9 +256,9 @@ def cartographer_repositories(): _maybe(native.http_archive, name = "com_google_absl", - sha256 = "387cf016ab1ab8530d1cea8975276ce8d8bff355133776129bdc400d05519eb6", - strip_prefix = "abseil-cpp-44aa275286baf97fc13529aca547a88b180beb08", - urls = ["https://github.com/abseil/abseil-cpp/archive/44aa275286baf97fc13529aca547a88b180beb08.tar.gz"], + sha256 = "c8ba586a9ab12bc4a67bb419fc0d2146200942b072bac95f50490f977b7fb04f", + strip_prefix = "abseil-cpp-5441bbe1db5d0f2ca24b5b60166367b0966790af", + urls = ["https://github.com/abseil/abseil-cpp/archive/5441bbe1db5d0f2ca24b5b60166367b0966790af.tar.gz"], ) # TODO(rodrigoq): remove these binds once grpc#14140 has been merged, as well diff --git a/cartographer/BUILD.bazel b/cartographer/BUILD.bazel index 3cfba9b..063489a 100644 --- a/cartographer/BUILD.bazel +++ b/cartographer/BUILD.bazel @@ -97,6 +97,7 @@ cc_library( "@boost//:iostreams", "@com_google_absl//absl/base", "@com_google_absl//absl/strings", + "@com_google_absl//absl/container:flat_hash_map", "@com_google_absl//absl/synchronization", "@com_google_absl//absl/types:optional", "@com_google_glog//:glog", diff --git a/cartographer/cloud/internal/handlers/add_sensor_data_batch_handler.h b/cartographer/cloud/internal/handlers/add_sensor_data_batch_handler.h index 0c9b37c..2173465 100644 --- a/cartographer/cloud/internal/handlers/add_sensor_data_batch_handler.h +++ b/cartographer/cloud/internal/handlers/add_sensor_data_batch_handler.h @@ -20,6 +20,7 @@ #include #include +#include "absl/container/flat_hash_map.h" #include "async_grpc/rpc_handler.h" #include "cartographer/cloud/proto/map_builder_service.pb.h" #include "cartographer/metrics/counter.h" @@ -59,7 +60,7 @@ class AddSensorDataBatchHandler counter_metrics_family_; // Holds individual metrics for each client. - std::unordered_map> + absl::flat_hash_map> client_metric_map_; }; diff --git a/cartographer/cloud/metrics/prometheus/family_factory.cc b/cartographer/cloud/metrics/prometheus/family_factory.cc index eac853a..d9e1329 100644 --- a/cartographer/cloud/metrics/prometheus/family_factory.cc +++ b/cartographer/cloud/metrics/prometheus/family_factory.cc @@ -14,10 +14,9 @@ * limitations under the License. */ -#include - #include "cartographer/cloud/metrics/prometheus/family_factory.h" +#include "absl/container/flat_hash_map.h" #include "absl/memory/memory.h" #include "prometheus/counter.h" #include "prometheus/family.h" @@ -76,7 +75,7 @@ class CounterFamily private: ::prometheus::Family<::prometheus::Counter>* prometheus_; std::mutex wrappers_mutex_; - std::unordered_map<::prometheus::Counter*, std::unique_ptr> + absl::flat_hash_map<::prometheus::Counter*, std::unique_ptr> wrappers_; }; @@ -108,7 +107,7 @@ class GaugeFamily private: ::prometheus::Family<::prometheus::Gauge>* prometheus_; std::mutex wrappers_mutex_; - std::unordered_map<::prometheus::Gauge*, std::unique_ptr> wrappers_; + absl::flat_hash_map<::prometheus::Gauge*, std::unique_ptr> wrappers_; }; class Histogram : public ::cartographer::metrics::Histogram { @@ -137,7 +136,7 @@ class HistogramFamily : public ::cartographer::metrics::Family< private: ::prometheus::Family<::prometheus::Histogram>* prometheus_; std::mutex wrappers_mutex_; - std::unordered_map<::prometheus::Histogram*, std::unique_ptr> + absl::flat_hash_map<::prometheus::Histogram*, std::unique_ptr> wrappers_; const BucketBoundaries boundaries_; }; diff --git a/cartographer/common/thread_pool.h b/cartographer/common/thread_pool.h index 1c7c352..3f6b94c 100644 --- a/cartographer/common/thread_pool.h +++ b/cartographer/common/thread_pool.h @@ -21,9 +21,9 @@ #include #include #include -#include #include +#include "absl/container/flat_hash_map.h" #include "absl/synchronization/mutex.h" #include "cartographer/common/task.h" @@ -76,7 +76,7 @@ class ThreadPool : public ThreadPoolInterface { bool running_ GUARDED_BY(mutex_) = true; std::vector pool_ GUARDED_BY(mutex_); std::deque> task_queue_ GUARDED_BY(mutex_); - std::unordered_map> tasks_not_ready_ + absl::flat_hash_map> tasks_not_ready_ GUARDED_BY(mutex_); }; diff --git a/cartographer/io/points_processor_pipeline_builder.h b/cartographer/io/points_processor_pipeline_builder.h index fe5fd19..2e576d4 100644 --- a/cartographer/io/points_processor_pipeline_builder.h +++ b/cartographer/io/points_processor_pipeline_builder.h @@ -18,9 +18,9 @@ #define CARTOGRAPHER_IO_POINTS_PROCESSOR_PIPELINE_BUILDER_H_ #include -#include #include +#include "absl/container/flat_hash_map.h" #include "cartographer/common/lua_parameter_dictionary.h" #include "cartographer/io/file_writer.h" #include "cartographer/io/points_processor.h" @@ -55,7 +55,7 @@ class PointsProcessorPipelineBuilder { common::LuaParameterDictionary* dictionary) const; private: - std::unordered_map factories_; + absl::flat_hash_map factories_; }; // Register all 'PointsProcessor' that ship with Cartographer with this diff --git a/cartographer/io/serialization_format_migration.cc b/cartographer/io/serialization_format_migration.cc index 558b45c..186fb92 100644 --- a/cartographer/io/serialization_format_migration.cc +++ b/cartographer/io/serialization_format_migration.cc @@ -16,9 +16,9 @@ #include "cartographer/io/serialization_format_migration.h" -#include #include +#include "absl/container/flat_hash_map.h" #include "cartographer/mapping/3d/submap_3d.h" #include "cartographer/mapping/internal/3d/scan_matching/rotational_scan_matcher.h" #include "cartographer/mapping/probability_values.h" @@ -32,7 +32,7 @@ namespace io { namespace { using mapping::proto::SerializedData; -using ProtoMap = std::unordered_map>; +using ProtoMap = absl::flat_hash_map>; bool ReadPoseGraph(cartographer::io::ProtoStreamReaderInterface* const input, ProtoMap* proto_map) { diff --git a/cartographer/mapping/internal/2d/pose_graph_2d.h b/cartographer/mapping/internal/2d/pose_graph_2d.h index 2fcca2c..75c1d6d 100644 --- a/cartographer/mapping/internal/2d/pose_graph_2d.h +++ b/cartographer/mapping/internal/2d/pose_graph_2d.h @@ -23,11 +23,11 @@ #include #include #include -#include #include #include "Eigen/Core" #include "Eigen/Geometry" +#include "absl/container/flat_hash_map.h" #include "absl/synchronization/mutex.h" #include "cartographer/common/fixed_ratio_sampler.h" #include "cartographer/common/thread_pool.h" @@ -246,7 +246,7 @@ class PoseGraph2D : public PoseGraph { std::unique_ptr work_queue_ GUARDED_BY(work_queue_mutex_); // We globally localize a fraction of the nodes from each trajectory. - std::unordered_map> + absl::flat_hash_map> global_localization_samplers_ GUARDED_BY(mutex_); // Number of nodes added since last loop closure. diff --git a/cartographer/mapping/internal/3d/pose_graph_3d.h b/cartographer/mapping/internal/3d/pose_graph_3d.h index 0195c84..6a1b862 100644 --- a/cartographer/mapping/internal/3d/pose_graph_3d.h +++ b/cartographer/mapping/internal/3d/pose_graph_3d.h @@ -23,11 +23,11 @@ #include #include #include -#include #include #include "Eigen/Core" #include "Eigen/Geometry" +#include "absl/container/flat_hash_map.h" #include "absl/synchronization/mutex.h" #include "cartographer/common/fixed_ratio_sampler.h" #include "cartographer/common/thread_pool.h" @@ -248,7 +248,7 @@ class PoseGraph3D : public PoseGraph { std::unique_ptr work_queue_ GUARDED_BY(work_queue_mutex_); // We globally localize a fraction of the nodes from each trajectory. - std::unordered_map> + absl::flat_hash_map> global_localization_samplers_ GUARDED_BY(mutex_); // Number of nodes added since last loop closure. diff --git a/cartographer/mapping/internal/connected_components.cc b/cartographer/mapping/internal/connected_components.cc index 4e00bf4..6306b0b 100644 --- a/cartographer/mapping/internal/connected_components.cc +++ b/cartographer/mapping/internal/connected_components.cc @@ -77,7 +77,7 @@ bool ConnectedComponents::TransitivelyConnected(const int trajectory_id_a, std::vector> ConnectedComponents::Components() { // Map from cluster exemplar -> growing cluster. - std::unordered_map> map; + absl::flat_hash_map> map; absl::MutexLock locker(&lock_); for (const auto& trajectory_id_entry : forest_) { map[FindSet(trajectory_id_entry.first)].push_back( diff --git a/cartographer/mapping/internal/connected_components.h b/cartographer/mapping/internal/connected_components.h index cb5df96..05f327e 100644 --- a/cartographer/mapping/internal/connected_components.h +++ b/cartographer/mapping/internal/connected_components.h @@ -18,8 +18,8 @@ #define CARTOGRAPHER_MAPPING_INTERNAL_CONNECTED_COMPONENTS_H_ #include -#include +#include "absl/container/flat_hash_map.h" #include "absl/synchronization/mutex.h" #include "cartographer/mapping/proto/connected_components.pb.h" #include "cartographer/mapping/submaps.h" diff --git a/cartographer/mapping/pose_graph.h b/cartographer/mapping/pose_graph.h index 1dbf52a..d6597b0 100644 --- a/cartographer/mapping/pose_graph.h +++ b/cartographer/mapping/pose_graph.h @@ -19,10 +19,10 @@ #include #include -#include #include #include +#include "absl/container/flat_hash_map.h" #include "cartographer/common/lua_parameter_dictionary.h" #include "cartographer/mapping/id.h" #include "cartographer/mapping/pose_graph_interface.h" diff --git a/cartographer/sensor/internal/collator.h b/cartographer/sensor/internal/collator.h index 38d2b0c..053b27e 100644 --- a/cartographer/sensor/internal/collator.h +++ b/cartographer/sensor/internal/collator.h @@ -19,10 +19,10 @@ #include #include -#include #include #include +#include "absl/container/flat_hash_map.h" #include "cartographer/sensor/collator_interface.h" #include "cartographer/sensor/data.h" #include "cartographer/sensor/internal/ordered_multi_queue.h" @@ -54,7 +54,7 @@ class Collator : public CollatorInterface { OrderedMultiQueue queue_; // Map of trajectory ID to all associated QueueKeys. - std::unordered_map> queue_keys_; + absl::flat_hash_map> queue_keys_; }; } // namespace sensor diff --git a/cartographer/sensor/internal/ordered_multi_queue.h b/cartographer/sensor/internal/ordered_multi_queue.h index f43b933..36f17e3 100644 --- a/cartographer/sensor/internal/ordered_multi_queue.h +++ b/cartographer/sensor/internal/ordered_multi_queue.h @@ -51,6 +51,8 @@ class OrderedMultiQueue { using Callback = std::function)>; OrderedMultiQueue(); + OrderedMultiQueue(OrderedMultiQueue&& queue) = default; + ~OrderedMultiQueue(); // Adds a new queue with key 'queue_key' which must not already exist. diff --git a/cartographer/sensor/internal/trajectory_collator.h b/cartographer/sensor/internal/trajectory_collator.h index 85f58fb..135a60d 100644 --- a/cartographer/sensor/internal/trajectory_collator.h +++ b/cartographer/sensor/internal/trajectory_collator.h @@ -18,9 +18,9 @@ #define CARTOGRAPHER_SENSOR_INTERNAL_TRAJECTORY_COLLATOR_H_ #include -#include #include +#include "absl/container/flat_hash_map.h" #include "cartographer/metrics/counter.h" #include "cartographer/metrics/family_factory.h" #include "cartographer/sensor/collator_interface.h" @@ -64,12 +64,12 @@ class TrajectoryCollator : public CollatorInterface { collator_metrics_family_; // Holds individual counters for each trajectory/sensor pair. - std::unordered_map metrics_map_; + absl::flat_hash_map metrics_map_; - std::unordered_map trajectory_to_queue_; + absl::flat_hash_map trajectory_to_queue_; // Map of trajectory ID to all associated QueueKeys. - std::unordered_map> trajectory_to_queue_keys_; + absl::flat_hash_map> trajectory_to_queue_keys_; }; } // namespace sensor diff --git a/cmake/modules/FindAbseil.cmake b/cmake/modules/FindAbseil.cmake index e058399..3b21472 100644 --- a/cmake/modules/FindAbseil.cmake +++ b/cmake/modules/FindAbseil.cmake @@ -33,6 +33,7 @@ if(NOT TARGET standalone_absl) "${ABSEIL_PROJECT_BUILD_DIR}/absl/strings/${prefix}str_format_internal${suffix}" "${ABSEIL_PROJECT_BUILD_DIR}/absl/strings/${prefix}str_format_extension_internal${suffix}" "${ABSEIL_PROJECT_BUILD_DIR}/absl/strings/${prefix}absl_str_format${suffix}" + "${ABSEIL_PROJECT_BUILD_DIR}/absl/hash/${prefix}absl_hash${suffix}" "${ABSEIL_PROJECT_BUILD_DIR}/absl/algorithm/${prefix}absl_algorithm${suffix}" "${ABSEIL_PROJECT_BUILD_DIR}/absl/base/${prefix}absl_base${suffix}" "${ABSEIL_PROJECT_BUILD_DIR}/absl/base/${prefix}absl_dynamic_annotations${suffix}" @@ -63,7 +64,7 @@ if(NOT TARGET standalone_absl) ExternalProject_Add(${ABSEIL_PROJECT_NAME} PREFIX ${ABSEIL_PROJECT_NAME} GIT_REPOSITORY https://github.com/abseil/abseil-cpp.git - GIT_TAG 44aa275286baf97fc13529aca547a88b180beb08 + GIT_TAG 5441bbe1db5d0f2ca24b5b60166367b0966790af INSTALL_COMMAND "" BUILD_COMMAND ${CMAKE_COMMAND} --build "${ABSEIL_PROJECT_BUILD_DIR}" CMAKE_CACHE_ARGS "-DCMAKE_POSITION_INDEPENDENT_CODE:BOOL=ON;-DBUILD_TESTING:BOOL=OFF;-DCMAKE_BUILD_TYPE:STRING=Release"