From 76e68502b9b901ce993bd4c54f8e9e37f4dd54b1 Mon Sep 17 00:00:00 2001 From: Daniel Collins Date: Mon, 25 Apr 2022 20:48:44 -0400 Subject: [PATCH] move MememtoRingBuffer --- .../chttp2/transport/hpack_parser_table.cc | 10 +-- .../chttp2/transport/hpack_parser_table.h | 68 +++++++++---------- 2 files changed, 38 insertions(+), 40 deletions(-) diff --git a/src/core/ext/transport/chttp2/transport/hpack_parser_table.cc b/src/core/ext/transport/chttp2/transport/hpack_parser_table.cc index 2c74babc05ee4..ba32b198c65fe 100644 --- a/src/core/ext/transport/chttp2/transport/hpack_parser_table.cc +++ b/src/core/ext/transport/chttp2/transport/hpack_parser_table.cc @@ -39,7 +39,7 @@ extern grpc_core::TraceFlag grpc_http_trace; namespace grpc_core { -void MementoRingBuffer::Put(Memento m) { +void HPackTable::MementoRingBuffer::Put(Memento m) { GPR_ASSERT(num_entries_ < max_entries_); if (entries_.size() < max_entries_) { ++num_entries_; @@ -50,7 +50,7 @@ void MementoRingBuffer::Put(Memento m) { ++num_entries_; } -auto MementoRingBuffer::PopOne() -> Memento { +auto HPackTable::MementoRingBuffer::PopOne() -> Memento { GPR_ASSERT(num_entries_ > 0); size_t index = first_entry_ % max_entries_; ++first_entry_; @@ -58,13 +58,13 @@ auto MementoRingBuffer::PopOne() -> Memento { return std::move(entries_[index]); } -const Memento* MementoRingBuffer::Lookup(uint32_t index) const { +auto HPackTable::MementoRingBuffer::Lookup(uint32_t index) const -> const Memento* { if (index >= num_entries_) return nullptr; uint32_t offset = (num_entries_ - 1u - index + first_entry_) % max_entries_; return &entries_[offset]; } -void MementoRingBuffer::Rebuild(uint32_t max_entries) { +void HPackTable::MementoRingBuffer::Rebuild(uint32_t max_entries) { if (max_entries == max_entries_) return; std::vector entries; entries.reserve(num_entries_); @@ -239,7 +239,7 @@ GPR_ATTRIBUTE_NOINLINE HPackTable::Memento MakeMemento(size_t i) { } // namespace -const HPackTable::StaticMementos& HPackTable::GetStaticMementos() { +auto HPackTable::GetStaticMementos() -> const StaticMementos& { static const StaticMementos* const static_mementos = new StaticMementos(); return *static_mementos; } diff --git a/src/core/ext/transport/chttp2/transport/hpack_parser_table.h b/src/core/ext/transport/chttp2/transport/hpack_parser_table.h index 619e4402a9183..890c13c522ae5 100644 --- a/src/core/ext/transport/chttp2/transport/hpack_parser_table.h +++ b/src/core/ext/transport/chttp2/transport/hpack_parser_table.h @@ -30,40 +30,6 @@ namespace grpc_core { -using Memento = ParsedMetadata; - -class MementoRingBuffer { - public: - // Rebuild this buffer with a new max_entries_ size. - void Rebuild(uint32_t max_entries); - - // Put a new memento. - // REQUIRES: num_entries < max_entries - void Put(Memento m); - - // Pop the oldest memento. - // REQUIRES: num_entries > 0 - Memento PopOne(); - - // Lookup the entry at index, or return nullptr if none exists. - const Memento* Lookup(uint32_t index) const; - - uint32_t max_entries() const { return max_entries_; } - uint32_t num_entries() const { return num_entries_; } - - private: - // The index of the first entry in the buffer. May be greater than - // max_entries_, in which case a wraparound has occurred. - uint32_t first_entry_ = 0; - // How many entries are in the table. - uint32_t num_entries_ = 0; - // Maximum number of entries we could possibly fit in the table, given defined - // overheads. - uint32_t max_entries_ = hpack_constants::kInitialTableEntries; - - std::vector entries_; -}; - // HPACK header table class HPackTable { public: @@ -76,7 +42,7 @@ class HPackTable { void SetMaxBytes(uint32_t max_bytes); grpc_error_handle SetCurrentTableSize(uint32_t bytes); - using Memento = Memento; + using Memento = ParsedMetadata; // Lookup, but don't ref. const Memento* Lookup(uint32_t index) const { @@ -106,6 +72,38 @@ class HPackTable { }; static const StaticMementos& GetStaticMementos() GPR_ATTRIBUTE_NOINLINE; + class MementoRingBuffer { + public: + // Rebuild this buffer with a new max_entries_ size. + void Rebuild(uint32_t max_entries); + + // Put a new memento. + // REQUIRES: num_entries < max_entries + void Put(Memento m); + + // Pop the oldest memento. + // REQUIRES: num_entries > 0 + Memento PopOne(); + + // Lookup the entry at index, or return nullptr if none exists. + const Memento* Lookup(uint32_t index) const; + + uint32_t max_entries() const { return max_entries_; } + uint32_t num_entries() const { return num_entries_; } + + private: + // The index of the first entry in the buffer. May be greater than + // max_entries_, in which case a wraparound has occurred. + uint32_t first_entry_ = 0; + // How many entries are in the table. + uint32_t num_entries_ = 0; + // Maximum number of entries we could possibly fit in the table, given defined + // overheads. + uint32_t max_entries_ = hpack_constants::kInitialTableEntries; + + std::vector entries_; + }; + const Memento* LookupDynamic(uint32_t index) const { // Not static - find the value in the list of valid entries const uint32_t tbl_index = index - (hpack_constants::kLastStaticEntry + 1);