Skip to content

Commit

Permalink
move MememtoRingBuffer
Browse files Browse the repository at this point in the history
  • Loading branch information
dpcollins-google committed Apr 26, 2022
1 parent f0c96ba commit 76e6850
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 40 deletions.
10 changes: 5 additions & 5 deletions src/core/ext/transport/chttp2/transport/hpack_parser_table.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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_;
Expand All @@ -50,21 +50,21 @@ 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_;
--num_entries_;
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<Memento> entries;
entries.reserve(num_entries_);
Expand Down Expand Up @@ -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;
}
Expand Down
68 changes: 33 additions & 35 deletions src/core/ext/transport/chttp2/transport/hpack_parser_table.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,40 +30,6 @@

namespace grpc_core {

using Memento = ParsedMetadata<grpc_metadata_batch>;

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<Memento> entries_;
};

// HPACK header table
class HPackTable {
public:
Expand All @@ -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<grpc_metadata_batch>;

// Lookup, but don't ref.
const Memento* Lookup(uint32_t index) const {
Expand Down Expand Up @@ -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<Memento> 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);
Expand Down

0 comments on commit 76e6850

Please sign in to comment.