Skip to content

Commit

Permalink
Use more efficient collections (#782)
Browse files Browse the repository at this point in the history
* Replace `std::map` with `std::unordered_map` in all places where applicable

* Replace most of `std::map/set/unordered_map/unordered_set` with more efficient collections from abseil-cpp

Taken from 'memory optimizations' branch to make it more readable and
concise.

Co-authored-by: Nazar Mokrynskyi <[email protected]>
  • Loading branch information
jmillan and nazar-pc authored Mar 2, 2022
1 parent 6076cc9 commit ff17d13
Show file tree
Hide file tree
Showing 50 changed files with 131 additions and 128 deletions.
4 changes: 2 additions & 2 deletions worker/include/Channel/ChannelRequest.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@
#define MS_CHANNEL_REQUEST_HPP

#include "common.hpp"
#include <absl/container/flat_hash_map.h>
#include <nlohmann/json.hpp>
#include <string>
#include <unordered_map>

using json = nlohmann::json;

Expand Down Expand Up @@ -75,7 +75,7 @@ namespace Channel
};

private:
static std::unordered_map<std::string, MethodId> string2MethodId;
static absl::flat_hash_map<std::string, MethodId> string2MethodId;

public:
ChannelRequest(Channel::ChannelSocket* channel, json& jsonRequest);
Expand Down
4 changes: 2 additions & 2 deletions worker/include/DepUsrSCTP.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
#include "common.hpp"
#include "RTC/SctpAssociation.hpp"
#include "handles/Timer.hpp"
#include <unordered_map>
#include <absl/container/flat_hash_map.h>

class DepUsrSCTP
{
Expand Down Expand Up @@ -42,7 +42,7 @@ class DepUsrSCTP
thread_local static Checker* checker;
static uint64_t numSctpAssociations;
static uintptr_t nextSctpAssociationId;
static std::unordered_map<uintptr_t, RTC::SctpAssociation*> mapIdSctpAssociation;
static absl::flat_hash_map<uintptr_t, RTC::SctpAssociation*> mapIdSctpAssociation;
};

#endif
4 changes: 2 additions & 2 deletions worker/include/PayloadChannel/Notification.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@
#define MS_PAYLOAD_CHANNEL_NOTIFICATION_HPP

#include "common.hpp"
#include <absl/container/flat_hash_map.h>
#include <nlohmann/json.hpp>
#include <string>
#include <unordered_map>

using json = nlohmann::json;

Expand All @@ -24,7 +24,7 @@ namespace PayloadChannel
static bool IsNotification(json& jsonNotification);

private:
static std::unordered_map<std::string, EventId> string2EventId;
static absl::flat_hash_map<std::string, EventId> string2EventId;

public:
explicit Notification(json& jsonNotification);
Expand Down
4 changes: 2 additions & 2 deletions worker/include/PayloadChannel/PayloadChannelRequest.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@
#define MS_PAYLOAD_CHANNEL_REQUEST_HPP

#include "common.hpp"
#include <absl/container/flat_hash_map.h>
#include <nlohmann/json.hpp>
#include <string>
#include <unordered_map>

using json = nlohmann::json;

Expand All @@ -26,7 +26,7 @@ namespace PayloadChannel
static bool IsRequest(json& jsonRequest);

private:
static std::unordered_map<std::string, MethodId> string2MethodId;
static absl::flat_hash_map<std::string, MethodId> string2MethodId;

public:
PayloadChannelRequest(PayloadChannel::PayloadChannelSocket* channel, json& jsonRequest);
Expand Down
4 changes: 2 additions & 2 deletions worker/include/RTC/ActiveSpeakerObserver.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@

#include "RTC/RtpObserver.hpp"
#include "handles/Timer.hpp"
#include <absl/container/flat_hash_map.h>
#include <nlohmann/json.hpp>
#include <unordered_map>
#include <utility>
#include <vector>

Expand Down Expand Up @@ -89,7 +89,7 @@ namespace RTC
std::string dominantId{ "" };
Timer* periodicTimer{ nullptr };
uint16_t interval{ 300u };
std::unordered_map<std::string, struct ProducerSpeaker> mapProducerSpeaker;
absl::flat_hash_map<std::string, struct ProducerSpeaker> mapProducerSpeaker;
uint64_t lastLevelIdleTime{ 0 };
};
} // namespace RTC
Expand Down
4 changes: 2 additions & 2 deletions worker/include/RTC/AudioLevelObserver.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@

#include "RTC/RtpObserver.hpp"
#include "handles/Timer.hpp"
#include <absl/container/flat_hash_map.h>
#include <nlohmann/json.hpp>
#include <unordered_map>

using json = nlohmann::json;

Expand Down Expand Up @@ -48,7 +48,7 @@ namespace RTC
// Allocated by this.
Timer* periodicTimer{ nullptr };
// Others.
std::unordered_map<RTC::Producer*, DBovs> mapProducerDBovs;
absl::flat_hash_map<RTC::Producer*, DBovs> mapProducerDBovs;
bool silence{ true };
};
} // namespace RTC
Expand Down
4 changes: 2 additions & 2 deletions worker/include/RTC/Consumer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@
#include "RTC/RtpPacket.hpp"
#include "RTC/RtpStream.hpp"
#include "RTC/RtpStreamSend.hpp"
#include <absl/container/flat_hash_set.h>
#include <nlohmann/json.hpp>
#include <string>
#include <unordered_set>
#include <vector>

using json = nlohmann::json;
Expand Down Expand Up @@ -182,7 +182,7 @@ namespace RTC
struct RTC::RtpHeaderExtensionIds rtpHeaderExtensionIds;
const std::vector<uint8_t>* producerRtpStreamScores{ nullptr };
// Others.
std::unordered_set<uint8_t> supportedCodecPayloadTypes;
absl::flat_hash_set<uint8_t> supportedCodecPayloadTypes;
uint64_t lastRtcpSentTime{ 0u };
uint16_t maxRtcpInterval{ 0u };
bool externallyManagedBitrate{ false };
Expand Down
8 changes: 4 additions & 4 deletions worker/include/RTC/DtlsTransport.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
#include <openssl/bio.h>
#include <openssl/ssl.h>
#include <openssl/x509.h>
#include <map>
#include <absl/container/flat_hash_map.h>
#include <string>
#include <vector>

Expand Down Expand Up @@ -144,9 +144,9 @@ namespace RTC
thread_local static EVP_PKEY* privateKey;
thread_local static SSL_CTX* sslCtx;
thread_local static uint8_t sslReadBuffer[];
static std::map<std::string, Role> string2Role;
static std::map<std::string, FingerprintAlgorithm> string2FingerprintAlgorithm;
static std::map<FingerprintAlgorithm, std::string> fingerprintAlgorithm2String;
static absl::flat_hash_map<std::string, Role> string2Role;
static absl::flat_hash_map<std::string, FingerprintAlgorithm> string2FingerprintAlgorithm;
static absl::flat_hash_map<FingerprintAlgorithm, std::string> fingerprintAlgorithm2String;
thread_local static std::vector<Fingerprint> localFingerprints;
static std::vector<SrtpCryptoSuiteMapEntry> srtpCryptoSuites;

Expand Down
6 changes: 3 additions & 3 deletions worker/include/RTC/KeyFrameRequestManager.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
#define MS_KEY_FRAME_REQUEST_MANAGER_HPP

#include "handles/Timer.hpp"
#include <map>
#include <absl/container/flat_hash_map.h>

namespace RTC
{
Expand Down Expand Up @@ -122,8 +122,8 @@ namespace RTC
private:
Listener* listener{ nullptr };
uint32_t keyFrameRequestDelay{ 0u }; // 0 means disabled.
std::map<uint32_t, PendingKeyFrameInfo*> mapSsrcPendingKeyFrameInfo;
std::map<uint32_t, KeyFrameRequestDelayer*> mapSsrcKeyFrameRequestDelayer;
absl::flat_hash_map<uint32_t, PendingKeyFrameInfo*> mapSsrcPendingKeyFrameInfo;
absl::flat_hash_map<uint32_t, KeyFrameRequestDelayer*> mapSsrcKeyFrameRequestDelayer;
};
} // namespace RTC

Expand Down
8 changes: 5 additions & 3 deletions worker/include/RTC/NackGenerator.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
#include "RTC/RtpPacket.hpp"
#include "RTC/SeqManager.hpp"
#include "handles/Timer.hpp"
#include <absl/container/btree_map.h>
#include <absl/container/btree_set.h>
#include <map>
#include <set>
#include <vector>
Expand Down Expand Up @@ -75,9 +77,9 @@ namespace RTC
// Allocated by this.
Timer* timer{ nullptr };
// Others.
std::map<uint16_t, NackInfo, RTC::SeqManager<uint16_t>::SeqLowerThan> nackList;
std::set<uint16_t, RTC::SeqManager<uint16_t>::SeqLowerThan> keyFrameList;
std::set<uint16_t, RTC::SeqManager<uint16_t>::SeqLowerThan> recoveredList;
absl::btree_map<uint16_t, NackInfo, RTC::SeqManager<uint16_t>::SeqLowerThan> nackList;
absl::btree_set<uint16_t, RTC::SeqManager<uint16_t>::SeqLowerThan> keyFrameList;
absl::btree_set<uint16_t, RTC::SeqManager<uint16_t>::SeqLowerThan> recoveredList;
bool started{ false };
uint16_t lastSeq{ 0u }; // Seq number of last valid packet.
uint32_t rtt{ 0u }; // Round trip time (ms).
Expand Down
4 changes: 2 additions & 2 deletions worker/include/RTC/Parameters.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@
#define MS_RTC_PARAMETERS_HPP

#include "common.hpp"
#include <absl/container/flat_hash_map.h>
#include <nlohmann/json.hpp>
#include <string>
#include <unordered_map>
#include <vector>

using json = nlohmann::json;
Expand Down Expand Up @@ -78,7 +78,7 @@ namespace RTC
const std::vector<int32_t>& GetArrayOfIntegers(const std::string& key) const;

private:
std::unordered_map<std::string, Value> mapKeyValues;
absl::flat_hash_map<std::string, Value> mapKeyValues;
};
} // namespace RTC

Expand Down
8 changes: 4 additions & 4 deletions worker/include/RTC/PipeConsumer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -60,11 +60,11 @@ namespace RTC
// Allocated by this.
std::vector<RTC::RtpStreamSend*> rtpStreams;
// Others.
std::unordered_map<uint32_t, uint32_t> mapMappedSsrcSsrc;
std::unordered_map<uint32_t, RTC::RtpStreamSend*> mapSsrcRtpStream;
absl::flat_hash_map<uint32_t, uint32_t> mapMappedSsrcSsrc;
absl::flat_hash_map<uint32_t, RTC::RtpStreamSend*> mapSsrcRtpStream;
bool keyFrameSupported{ false };
std::unordered_map<RTC::RtpStreamSend*, bool> mapRtpStreamSyncRequired;
std::unordered_map<RTC::RtpStreamSend*, RTC::SeqManager<uint16_t>> mapRtpStreamRtpSeqManager;
absl::flat_hash_map<RTC::RtpStreamSend*, bool> mapRtpStreamSyncRequired;
absl::flat_hash_map<RTC::RtpStreamSend*, RTC::SeqManager<uint16_t>> mapRtpStreamRtpSeqManager;
};
} // namespace RTC

Expand Down
6 changes: 3 additions & 3 deletions worker/include/RTC/PlainTransport.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
#include "RTC/Transport.hpp"
#include "RTC/TransportTuple.hpp"
#include "RTC/UdpSocket.hpp"
#include <map>
#include <absl/container/flat_hash_map.h>

namespace RTC
{
Expand All @@ -19,8 +19,8 @@ namespace RTC
};

private:
static std::map<std::string, RTC::SrtpSession::CryptoSuite> string2SrtpCryptoSuite;
static std::map<RTC::SrtpSession::CryptoSuite, std::string> srtpCryptoSuite2String;
static absl::flat_hash_map<std::string, RTC::SrtpSession::CryptoSuite> string2SrtpCryptoSuite;
static absl::flat_hash_map<RTC::SrtpSession::CryptoSuite, std::string> srtpCryptoSuite2String;
static size_t srtpMasterLength;

public:
Expand Down
6 changes: 3 additions & 3 deletions worker/include/RTC/PortManager.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@
#include "common.hpp"
#include "Settings.hpp"
#include <uv.h>
#include <absl/container/flat_hash_map.h>
#include <nlohmann/json.hpp>
#include <string>
#include <unordered_map>
#include <vector>

namespace RTC
Expand Down Expand Up @@ -54,8 +54,8 @@ namespace RTC
static std::vector<bool>& GetPorts(Transport transport, const std::string& ip);

private:
thread_local static std::unordered_map<std::string, std::vector<bool>> mapUdpIpPorts;
thread_local static std::unordered_map<std::string, std::vector<bool>> mapTcpIpPorts;
thread_local static absl::flat_hash_map<std::string, std::vector<bool>> mapUdpIpPorts;
thread_local static absl::flat_hash_map<std::string, std::vector<bool>> mapTcpIpPorts;
};
} // namespace RTC

Expand Down
13 changes: 6 additions & 7 deletions worker/include/RTC/Producer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
#include "RTC/RtpHeaderExtensionIds.hpp"
#include "RTC/RtpPacket.hpp"
#include "RTC/RtpStreamRecv.hpp"
#include <map>
#include <nlohmann/json.hpp>
#include <string>
#include <vector>
Expand Down Expand Up @@ -55,7 +54,7 @@ namespace RTC
private:
struct RtpMapping
{
std::map<uint8_t, uint8_t> codecs;
absl::flat_hash_map<uint8_t, uint8_t> codecs;
std::vector<RtpEncodingMapping> encodings;
};

Expand Down Expand Up @@ -113,7 +112,7 @@ namespace RTC
{
return this->paused;
}
const std::map<RTC::RtpStreamRecv*, uint32_t>& GetRtpStreams()
const absl::flat_hash_map<RTC::RtpStreamRecv*, uint32_t>& GetRtpStreams()
{
return this->mapRtpStreamMappedSsrc;
}
Expand Down Expand Up @@ -161,7 +160,7 @@ namespace RTC
// Passed by argument.
RTC::Producer::Listener* listener{ nullptr };
// Allocated by this.
std::map<uint32_t, RTC::RtpStreamRecv*> mapSsrcRtpStream;
absl::flat_hash_map<uint32_t, RTC::RtpStreamRecv*> mapSsrcRtpStream;
RTC::KeyFrameRequestManager* keyFrameRequestManager{ nullptr };
// Others.
RTC::Media::Kind kind;
Expand All @@ -170,9 +169,9 @@ namespace RTC
struct RtpMapping rtpMapping;
std::vector<RTC::RtpStreamRecv*> rtpStreamByEncodingIdx;
std::vector<uint8_t> rtpStreamScores;
std::map<uint32_t, RTC::RtpStreamRecv*> mapRtxSsrcRtpStream;
std::map<RTC::RtpStreamRecv*, uint32_t> mapRtpStreamMappedSsrc;
std::map<uint32_t, uint32_t> mapMappedSsrcSsrc;
absl::flat_hash_map<uint32_t, RTC::RtpStreamRecv*> mapRtxSsrcRtpStream;
absl::flat_hash_map<RTC::RtpStreamRecv*, uint32_t> mapRtpStreamMappedSsrc;
absl::flat_hash_map<uint32_t, uint32_t> mapMappedSsrcSsrc;
struct RTC::RtpHeaderExtensionIds rtpHeaderExtensionIds;
bool paused{ false };
RTC::RtpPacket* currentRtpPacket{ nullptr };
Expand Down
3 changes: 2 additions & 1 deletion worker/include/RTC/RTCP/Feedback.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#include "common.hpp"
#include "RTC/RTCP/FeedbackItem.hpp"
#include "RTC/RTCP/Packet.hpp"
#include <absl/container/flat_hash_map.h>

namespace RTC
{
Expand All @@ -27,7 +28,7 @@ namespace RTC
static const std::string& MessageType2String(typename T::MessageType type);

private:
static std::map<typename T::MessageType, std::string> type2String;
static absl::flat_hash_map<typename T::MessageType, std::string> type2String;

public:
typename T::MessageType GetMessageType() const
Expand Down
2 changes: 1 addition & 1 deletion worker/include/RTC/RTCP/FeedbackRtpTransport.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,7 @@ namespace RTC
static FeedbackRtpTransportPacket* Parse(const uint8_t* data, size_t len);

private:
static std::map<Status, std::string> status2String;
static absl::flat_hash_map<Status, std::string> status2String;

public:
FeedbackRtpTransportPacket(uint32_t senderSsrc, uint32_t mediaSsrc)
Expand Down
4 changes: 2 additions & 2 deletions worker/include/RTC/RTCP/Packet.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
#define MS_RTC_RTCP_PACKET_HPP

#include "common.hpp"
#include <map>
#include <absl/container/flat_hash_map.h>
#include <string>

namespace RTC
Expand Down Expand Up @@ -72,7 +72,7 @@ namespace RTC
static const std::string& Type2String(Type type);

private:
static std::map<Type, std::string> type2String;
static absl::flat_hash_map<Type, std::string> type2String;

public:
explicit Packet(Type type) : type(type)
Expand Down
3 changes: 1 addition & 2 deletions worker/include/RTC/RTCP/Sdes.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@

#include "common.hpp"
#include "RTC/RTCP/Packet.hpp"
#include <map>
#include <string>
#include <vector>

Expand Down Expand Up @@ -77,7 +76,7 @@ namespace RTC
std::unique_ptr<uint8_t[]> raw;

private:
static std::map<SdesItem::Type, std::string> type2String;
static absl::flat_hash_map<SdesItem::Type, std::string> type2String;
};

class SdesChunk
Expand Down
Loading

0 comments on commit ff17d13

Please sign in to comment.