dcsctp: Removing all references to unordered_map

Replacing with std::map and webrtc::flat_map where applicable.

Bug: webrtc:12689
Change-Id: Id0fdb88bd3d52957b1616911eb487fc581d3b7d8
Reviewed-on: https://webrtc-review.googlesource.com/c/src/+/229182
Reviewed-by: Mirko Bonadei <mbonadei@webrtc.org>
Commit-Queue: Victor Boivie <boivie@webrtc.org>
Cr-Commit-Position: refs/heads/master@{#34797}
This commit is contained in:
Victor Boivie 2021-08-18 15:22:42 +02:00 committed by WebRTC LUCI CQ
parent 51b96a7b65
commit 3ec9e03f73
19 changed files with 17 additions and 129 deletions

View File

@ -21,11 +21,6 @@ rtc_source_set("math") {
sources = [ "math.h" ]
}
rtc_source_set("pair_hash") {
deps = []
sources = [ "pair_hash.h" ]
}
rtc_source_set("sequence_numbers") {
deps = [ ":internal_types" ]
sources = [ "sequence_numbers.h" ]
@ -44,7 +39,6 @@ if (rtc_include_tests) {
defines = []
deps = [
":math",
":pair_hash",
":sequence_numbers",
":str_join",
"../../../api:array_view",
@ -55,7 +49,6 @@ if (rtc_include_tests) {
]
sources = [
"math_test.cc",
"pair_hash_test.cc",
"sequence_numbers_test.cc",
"str_join_test.cc",
]

View File

@ -38,13 +38,5 @@ using VerificationTag = StrongAlias<class VerificationTagTag, uint32_t>;
// Tie Tag, used as a nonce when connecting.
using TieTag = StrongAlias<class TieTagTag, uint64_t>;
// Hasher for separated ordered/unordered stream identifiers.
struct UnorderedStreamHash {
size_t operator()(const std::pair<IsUnordered, StreamID>& p) const {
return std::hash<IsUnordered::UnderlyingType>{}(*p.first) ^
(std::hash<StreamID::UnderlyingType>{}(*p.second) << 1);
}
};
} // namespace dcsctp
#endif // NET_DCSCTP_COMMON_INTERNAL_TYPES_H_

View File

@ -1,31 +0,0 @@
/*
* Copyright (c) 2021 The WebRTC project authors. All Rights Reserved.
*
* Use of this source code is governed by a BSD-style license
* that can be found in the LICENSE file in the root of the source
* tree. An additional intellectual property rights grant can be found
* in the file PATENTS. All contributing project authors may
* be found in the AUTHORS file in the root of the source tree.
*/
#ifndef NET_DCSCTP_COMMON_PAIR_HASH_H_
#define NET_DCSCTP_COMMON_PAIR_HASH_H_
#include <stddef.h>
#include <functional>
#include <utility>
namespace dcsctp {
// A custom hash function for std::pair, to be able to be used as key in a
// std::unordered_map. If absl::flat_hash_map would ever be used, this is
// unnecessary as it already has a hash function for std::pair.
struct PairHash {
template <class T1, class T2>
size_t operator()(const std::pair<T1, T2>& p) const {
return (3 * std::hash<T1>{}(p.first)) ^ std::hash<T2>{}(p.second);
}
};
} // namespace dcsctp
#endif // NET_DCSCTP_COMMON_PAIR_HASH_H_

View File

@ -1,48 +0,0 @@
/*
* Copyright (c) 2021 The WebRTC project authors. All Rights Reserved.
*
* Use of this source code is governed by a BSD-style license
* that can be found in the LICENSE file in the root of the source
* tree. An additional intellectual property rights grant can be found
* in the file PATENTS. All contributing project authors may
* be found in the AUTHORS file in the root of the source tree.
*/
#include "net/dcsctp/common/pair_hash.h"
#include <unordered_map>
#include <unordered_set>
#include "test/gmock.h"
namespace dcsctp {
namespace {
TEST(PairHashTest, CanInsertIntoSet) {
using MyPair = std::pair<int, int>;
std::unordered_set<MyPair, PairHash> pairs;
pairs.insert({1, 2});
pairs.insert({3, 4});
EXPECT_NE(pairs.find({1, 2}), pairs.end());
EXPECT_NE(pairs.find({3, 4}), pairs.end());
EXPECT_EQ(pairs.find({1, 3}), pairs.end());
EXPECT_EQ(pairs.find({3, 3}), pairs.end());
}
TEST(PairHashTest, CanInsertIntoMap) {
using MyPair = std::pair<int, int>;
std::unordered_map<MyPair, int, PairHash> pairs;
pairs[{1, 2}] = 99;
pairs[{3, 4}] = 100;
EXPECT_EQ((pairs[{1, 2}]), 99);
EXPECT_EQ((pairs[{3, 4}]), 100);
EXPECT_EQ(pairs.find({1, 3}), pairs.end());
EXPECT_EQ(pairs.find({3, 3}), pairs.end());
}
} // namespace
} // namespace dcsctp

View File

@ -92,6 +92,7 @@ if (rtc_include_tests) {
"../../../rtc_base:checks",
"../../../rtc_base:gunit_helpers",
"../../../rtc_base:rtc_base_approved",
"../../../rtc_base/containers:flat_map",
"../../../test:test_support",
]
sources = [

View File

@ -67,15 +67,6 @@ class StrongAlias {
return value_ >= other.value_;
}
// Hasher to use in std::unordered_map, std::unordered_set, etc.
struct Hasher {
using argument_type = StrongAlias;
using result_type = std::size_t;
result_type operator()(const argument_type& id) const {
return std::hash<UnderlyingType>()(id.value());
}
};
protected:
UnderlyingType value_;
};

View File

@ -15,9 +15,9 @@
#include <memory>
#include <string>
#include <type_traits>
#include <unordered_map>
#include <utility>
#include "rtc_base/containers/flat_map.h"
#include "rtc_base/gunit.h"
#include "test/gmock.h"
@ -280,9 +280,9 @@ TEST(StrongAliasTest, CanWrapComplexStructures) {
// namespace. So we can't print ComplexAlias.
}
TYPED_TEST(StrongAliasTest, CanBeKeysInStdUnorderedMap) {
TYPED_TEST(StrongAliasTest, CanBeKeysInFlatMap) {
using FooAlias = StrongAlias<class FooTag, TypeParam>;
std::unordered_map<FooAlias, std::string, typename FooAlias::Hasher> map;
webrtc::flat_map<FooAlias, std::string> map;
FooAlias k1(GetExampleValue<TypeParam>(0));
FooAlias k2(GetExampleValue<TypeParam>(1));

View File

@ -16,7 +16,6 @@
#include <iterator>
#include <map>
#include <numeric>
#include <unordered_map>
#include <utility>
#include <vector>

View File

@ -14,7 +14,6 @@
#include <map>
#include <string>
#include <unordered_map>
#include "absl/strings/string_view.h"
#include "api/array_view.h"
@ -108,10 +107,8 @@ class TraditionalReassemblyStreams : public ReassemblyStreams {
const OnAssembledMessage on_assembled_message_;
// All unordered and ordered streams, managing not-yet-assembled data.
std::unordered_map<StreamID, UnorderedStream, StreamID::Hasher>
unordered_streams_;
std::unordered_map<StreamID, OrderedStream, StreamID::Hasher>
ordered_streams_;
std::map<StreamID, UnorderedStream> unordered_streams_;
std::map<StreamID, OrderedStream> ordered_streams_;
};
} // namespace dcsctp

View File

@ -52,6 +52,7 @@ rtc_library("stream_reset_handler") {
"../../../rtc_base",
"../../../rtc_base:checks",
"../../../rtc_base:rtc_base_approved",
"../../../rtc_base/containers:flat_set",
"../common:internal_types",
"../common:str_join",
"../packet:chunk",

View File

@ -11,7 +11,6 @@
#include <cstdint>
#include <memory>
#include <unordered_set>
#include <utility>
#include <vector>

View File

@ -13,7 +13,6 @@
#include <cstdint>
#include <memory>
#include <string>
#include <unordered_set>
#include <utility>
#include <vector>
@ -33,6 +32,7 @@
#include "net/dcsctp/socket/context.h"
#include "net/dcsctp/timer/timer.h"
#include "net/dcsctp/tx/retransmission_queue.h"
#include "rtc_base/containers/flat_set.h"
namespace dcsctp {
@ -207,7 +207,7 @@ class StreamResetHandler {
// Outgoing streams that have been requested to be reset, but hasn't yet
// been included in an outgoing request.
std::unordered_set<StreamID, StreamID::Hasher> streams_to_reset_;
webrtc::flat_set<StreamID> streams_to_reset_;
// The next sequence number for outgoing stream requests.
ReconfigRequestSN next_outgoing_req_seq_nbr_;

View File

@ -14,6 +14,8 @@ rtc_library("timer") {
"../../../rtc_base",
"../../../rtc_base:checks",
"../../../rtc_base:rtc_base_approved",
"../../../rtc_base/containers:flat_map",
"../../../rtc_base/containers:flat_set",
"../public:socket",
"../public:strong_alias",
"../public:types",

View File

@ -14,13 +14,13 @@
#include <functional>
#include <limits>
#include <memory>
#include <unordered_set>
#include <utility>
#include <vector>
#include "absl/types/optional.h"
#include "net/dcsctp/public/timeout.h"
#include "rtc_base/checks.h"
#include "rtc_base/containers/flat_set.h"
namespace dcsctp {
@ -93,7 +93,7 @@ class FakeTimeoutManager {
private:
const std::function<TimeMs()> get_time_;
std::unordered_set<FakeTimeout*> timers_;
webrtc::flat_set<FakeTimeout*> timers_;
};
} // namespace dcsctp

View File

@ -13,7 +13,6 @@
#include <cstdint>
#include <limits>
#include <memory>
#include <unordered_map>
#include <utility>
#include "absl/memory/memory.h"

View File

@ -14,9 +14,9 @@
#include <algorithm>
#include <functional>
#include <map>
#include <memory>
#include <string>
#include <unordered_map>
#include <utility>
#include "absl/strings/string_view.h"
@ -176,7 +176,7 @@ class TimerManager {
private:
const std::function<std::unique_ptr<Timeout>()> create_timeout_;
std::unordered_map<TimerID, Timer*, TimerID::Hasher> timers_;
std::map<TimerID, Timer*> timers_;
TimerID next_id_ = TimerID(0);
};

View File

@ -26,7 +26,6 @@ rtc_library("rr_send_queue") {
"../../../api:array_view",
"../../../rtc_base:checks",
"../../../rtc_base:rtc_base_approved",
"../common:pair_hash",
"../packet:data",
"../public:socket",
"../public:types",
@ -76,7 +75,6 @@ rtc_library("retransmission_queue") {
"../../../rtc_base:checks",
"../../../rtc_base:rtc_base_approved",
"../common:math",
"../common:pair_hash",
"../common:sequence_numbers",
"../common:str_join",
"../packet:chunk",

View File

@ -16,7 +16,6 @@
#include <map>
#include <set>
#include <string>
#include <unordered_map>
#include <utility>
#include <vector>
@ -25,7 +24,6 @@
#include "absl/types/optional.h"
#include "api/array_view.h"
#include "net/dcsctp/common/math.h"
#include "net/dcsctp/common/pair_hash.h"
#include "net/dcsctp/common/sequence_numbers.h"
#include "net/dcsctp/common/str_join.h"
#include "net/dcsctp/packet/chunk/data_chunk.h"
@ -846,8 +844,7 @@ size_t RetransmissionQueue::max_bytes_to_send() const {
}
ForwardTsnChunk RetransmissionQueue::CreateForwardTsn() const {
std::unordered_map<StreamID, SSN, StreamID::Hasher>
skipped_per_ordered_stream;
std::map<StreamID, SSN> skipped_per_ordered_stream;
UnwrappedTSN new_cumulative_ack = last_cumulative_tsn_ack_;
for (const auto& elem : outstanding_data_) {
@ -873,8 +870,7 @@ ForwardTsnChunk RetransmissionQueue::CreateForwardTsn() const {
}
IForwardTsnChunk RetransmissionQueue::CreateIForwardTsn() const {
std::unordered_map<std::pair<IsUnordered, StreamID>, MID, UnorderedStreamHash>
skipped_per_stream;
std::map<std::pair<IsUnordered, StreamID>, MID> skipped_per_stream;
UnwrappedTSN new_cumulative_ack = last_cumulative_tsn_ack_;
for (const auto& elem : outstanding_data_) {

View File

@ -20,7 +20,6 @@
#include "absl/strings/string_view.h"
#include "absl/types/optional.h"
#include "api/array_view.h"
#include "net/dcsctp/common/pair_hash.h"
#include "net/dcsctp/public/dcsctp_message.h"
#include "net/dcsctp/public/dcsctp_socket.h"
#include "net/dcsctp/public/types.h"