Migrates quic's settingsframe from std::map to quichashmap. There is no particular reason to use an ordered container, and std::map is not a very efficient data structure. Note: this change makes an effort to preserve the existing order of serialized SETTINGS. Many tests fail if SETTINGS order is permuted. PiperOrigin-RevId: 318555812 Change-Id: Ib3829c1013f70462ff90b946282933a59cf479ec
diff --git a/quic/core/http/http_encoder.cc b/quic/core/http/http_encoder.cc index 6066b26..a51abbe 100644 --- a/quic/core/http/http_encoder.cc +++ b/quic/core/http/http_encoder.cc
@@ -99,10 +99,13 @@ const SettingsFrame& settings, std::unique_ptr<char[]>* output) { QuicByteCount payload_length = 0; + std::vector<std::pair<uint64_t, uint64_t>> ordered_settings{ + settings.values.begin(), settings.values.end()}; + std::sort(ordered_settings.begin(), ordered_settings.end()); // Calculate the payload length. - for (auto it = settings.values.begin(); it != settings.values.end(); ++it) { - payload_length += QuicDataWriter::GetVarInt62Len(it->first); - payload_length += QuicDataWriter::GetVarInt62Len(it->second); + for (const auto& p : ordered_settings) { + payload_length += QuicDataWriter::GetVarInt62Len(p.first); + payload_length += QuicDataWriter::GetVarInt62Len(p.second); } QuicByteCount total_length = @@ -117,8 +120,8 @@ return 0; } - for (auto it = settings.values.begin(); it != settings.values.end(); ++it) { - if (!writer.WriteVarInt62(it->first) || !writer.WriteVarInt62(it->second)) { + for (const auto& p : ordered_settings) { + if (!writer.WriteVarInt62(p.first) || !writer.WriteVarInt62(p.second)) { QUIC_DLOG(ERROR) << "Http encoder failed when attempting to serialize " "settings frame payload."; return 0;
diff --git a/quic/core/http/http_frames.h b/quic/core/http/http_frames.h index d7d875a..242f72b 100644 --- a/quic/core/http/http_frames.h +++ b/quic/core/http/http_frames.h
@@ -65,7 +65,7 @@ // affect how endpoints communicate, such as preferences and constraints // on peer behavior -using SettingsMap = std::map<uint64_t, uint64_t>; +using SettingsMap = QuicHashMap<uint64_t, uint64_t>; struct QUIC_EXPORT_PRIVATE SettingsFrame { SettingsMap values;