Skip to content

Commit

Permalink
[ABSL] Use flat_hash_map instead of unordered_map. (#1439)
Browse files Browse the repository at this point in the history
  • Loading branch information
pifon2a committed Oct 8, 2018
1 parent 8285673 commit bdb6f2d
Show file tree
Hide file tree
Showing 16 changed files with 33 additions and 29 deletions.
6 changes: 3 additions & 3 deletions bazel/repositories.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions cartographer/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
#include <memory>
#include <string>

#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"
Expand Down Expand Up @@ -59,7 +60,7 @@ class AddSensorDataBatchHandler
counter_metrics_family_;

// Holds individual metrics for each client.
std::unordered_map<std::string, std::unique_ptr<ClientMetrics>>
absl::flat_hash_map<std::string, std::unique_ptr<ClientMetrics>>
client_metric_map_;
};

Expand Down
9 changes: 4 additions & 5 deletions cartographer/cloud/metrics/prometheus/family_factory.cc
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,9 @@
* limitations under the License.
*/

#include <unordered_map>

#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"
Expand Down Expand Up @@ -76,7 +75,7 @@ class CounterFamily
private:
::prometheus::Family<::prometheus::Counter>* prometheus_;
std::mutex wrappers_mutex_;
std::unordered_map<::prometheus::Counter*, std::unique_ptr<Counter>>
absl::flat_hash_map<::prometheus::Counter*, std::unique_ptr<Counter>>
wrappers_;
};

Expand Down Expand Up @@ -108,7 +107,7 @@ class GaugeFamily
private:
::prometheus::Family<::prometheus::Gauge>* prometheus_;
std::mutex wrappers_mutex_;
std::unordered_map<::prometheus::Gauge*, std::unique_ptr<Gauge>> wrappers_;
absl::flat_hash_map<::prometheus::Gauge*, std::unique_ptr<Gauge>> wrappers_;
};

class Histogram : public ::cartographer::metrics::Histogram {
Expand Down Expand Up @@ -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<Histogram>>
absl::flat_hash_map<::prometheus::Histogram*, std::unique_ptr<Histogram>>
wrappers_;
const BucketBoundaries boundaries_;
};
Expand Down
4 changes: 2 additions & 2 deletions cartographer/common/thread_pool.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,9 @@
#include <functional>
#include <memory>
#include <thread>
#include <unordered_map>
#include <vector>

#include "absl/container/flat_hash_map.h"
#include "absl/synchronization/mutex.h"
#include "cartographer/common/task.h"

Expand Down Expand Up @@ -76,7 +76,7 @@ class ThreadPool : public ThreadPoolInterface {
bool running_ GUARDED_BY(mutex_) = true;
std::vector<std::thread> pool_ GUARDED_BY(mutex_);
std::deque<std::shared_ptr<Task>> task_queue_ GUARDED_BY(mutex_);
std::unordered_map<Task*, std::shared_ptr<Task>> tasks_not_ready_
absl::flat_hash_map<Task*, std::shared_ptr<Task>> tasks_not_ready_
GUARDED_BY(mutex_);
};

Expand Down
4 changes: 2 additions & 2 deletions cartographer/io/points_processor_pipeline_builder.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@
#define CARTOGRAPHER_IO_POINTS_PROCESSOR_PIPELINE_BUILDER_H_

#include <string>
#include <unordered_map>
#include <vector>

#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"
Expand Down Expand Up @@ -55,7 +55,7 @@ class PointsProcessorPipelineBuilder {
common::LuaParameterDictionary* dictionary) const;

private:
std::unordered_map<std::string, FactoryFunction> factories_;
absl::flat_hash_map<std::string, FactoryFunction> factories_;
};

// Register all 'PointsProcessor' that ship with Cartographer with this
Expand Down
4 changes: 2 additions & 2 deletions cartographer/io/serialization_format_migration.cc
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@

#include "cartographer/io/serialization_format_migration.h"

#include <unordered_map>
#include <vector>

#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"
Expand All @@ -32,7 +32,7 @@ namespace io {
namespace {

using mapping::proto::SerializedData;
using ProtoMap = std::unordered_map<int, std::vector<SerializedData>>;
using ProtoMap = absl::flat_hash_map<int, std::vector<SerializedData>>;

bool ReadPoseGraph(cartographer::io::ProtoStreamReaderInterface* const input,
ProtoMap* proto_map) {
Expand Down
4 changes: 2 additions & 2 deletions cartographer/mapping/internal/2d/pose_graph_2d.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,11 @@
#include <map>
#include <memory>
#include <set>
#include <unordered_map>
#include <vector>

#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"
Expand Down Expand Up @@ -246,7 +246,7 @@ class PoseGraph2D : public PoseGraph {
std::unique_ptr<WorkQueue> work_queue_ GUARDED_BY(work_queue_mutex_);

// We globally localize a fraction of the nodes from each trajectory.
std::unordered_map<int, std::unique_ptr<common::FixedRatioSampler>>
absl::flat_hash_map<int, std::unique_ptr<common::FixedRatioSampler>>
global_localization_samplers_ GUARDED_BY(mutex_);

// Number of nodes added since last loop closure.
Expand Down
4 changes: 2 additions & 2 deletions cartographer/mapping/internal/3d/pose_graph_3d.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,11 @@
#include <map>
#include <memory>
#include <set>
#include <unordered_map>
#include <vector>

#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"
Expand Down Expand Up @@ -248,7 +248,7 @@ class PoseGraph3D : public PoseGraph {
std::unique_ptr<WorkQueue> work_queue_ GUARDED_BY(work_queue_mutex_);

// We globally localize a fraction of the nodes from each trajectory.
std::unordered_map<int, std::unique_ptr<common::FixedRatioSampler>>
absl::flat_hash_map<int, std::unique_ptr<common::FixedRatioSampler>>
global_localization_samplers_ GUARDED_BY(mutex_);

// Number of nodes added since last loop closure.
Expand Down
2 changes: 1 addition & 1 deletion cartographer/mapping/internal/connected_components.cc
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ bool ConnectedComponents::TransitivelyConnected(const int trajectory_id_a,

std::vector<std::vector<int>> ConnectedComponents::Components() {
// Map from cluster exemplar -> growing cluster.
std::unordered_map<int, std::vector<int>> map;
absl::flat_hash_map<int, std::vector<int>> map;
absl::MutexLock locker(&lock_);
for (const auto& trajectory_id_entry : forest_) {
map[FindSet(trajectory_id_entry.first)].push_back(
Expand Down
2 changes: 1 addition & 1 deletion cartographer/mapping/internal/connected_components.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@
#define CARTOGRAPHER_MAPPING_INTERNAL_CONNECTED_COMPONENTS_H_

#include <map>
#include <unordered_map>

#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"
Expand Down
2 changes: 1 addition & 1 deletion cartographer/mapping/pose_graph.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,10 @@

#include <memory>
#include <set>
#include <unordered_map>
#include <utility>
#include <vector>

#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"
Expand Down
4 changes: 2 additions & 2 deletions cartographer/sensor/internal/collator.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,10 @@

#include <functional>
#include <memory>
#include <unordered_map>
#include <unordered_set>
#include <vector>

#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"
Expand Down Expand Up @@ -54,7 +54,7 @@ class Collator : public CollatorInterface {
OrderedMultiQueue queue_;

// Map of trajectory ID to all associated QueueKeys.
std::unordered_map<int, std::vector<QueueKey>> queue_keys_;
absl::flat_hash_map<int, std::vector<QueueKey>> queue_keys_;
};

} // namespace sensor
Expand Down
2 changes: 2 additions & 0 deletions cartographer/sensor/internal/ordered_multi_queue.h
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,8 @@ class OrderedMultiQueue {
using Callback = std::function<void(std::unique_ptr<Data>)>;

OrderedMultiQueue();
OrderedMultiQueue(OrderedMultiQueue&& queue) = default;

~OrderedMultiQueue();

// Adds a new queue with key 'queue_key' which must not already exist.
Expand Down
8 changes: 4 additions & 4 deletions cartographer/sensor/internal/trajectory_collator.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@
#define CARTOGRAPHER_SENSOR_INTERNAL_TRAJECTORY_COLLATOR_H_

#include <memory>
#include <unordered_map>
#include <vector>

#include "absl/container/flat_hash_map.h"
#include "cartographer/metrics/counter.h"
#include "cartographer/metrics/family_factory.h"
#include "cartographer/sensor/collator_interface.h"
Expand Down Expand Up @@ -64,12 +64,12 @@ class TrajectoryCollator : public CollatorInterface {
collator_metrics_family_;

// Holds individual counters for each trajectory/sensor pair.
std::unordered_map<std::string, metrics::Counter*> metrics_map_;
absl::flat_hash_map<std::string, metrics::Counter*> metrics_map_;

std::unordered_map<int, OrderedMultiQueue> trajectory_to_queue_;
absl::flat_hash_map<int, OrderedMultiQueue> trajectory_to_queue_;

// Map of trajectory ID to all associated QueueKeys.
std::unordered_map<int, std::vector<QueueKey>> trajectory_to_queue_keys_;
absl::flat_hash_map<int, std::vector<QueueKey>> trajectory_to_queue_keys_;
};

} // namespace sensor
Expand Down
3 changes: 2 additions & 1 deletion cmake/modules/FindAbseil.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -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}"
Expand Down Expand Up @@ -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"
Expand Down

0 comments on commit bdb6f2d

Please sign in to comment.