hpack_encoder_table.cc
Go to the documentation of this file.
1 // Copyright 2021 gRPC authors.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
16 
18 
19 #include <algorithm>
20 #include <cstdint>
21 
22 #include <grpc/support/log.h>
23 
24 namespace grpc_core {
25 
27  uint32_t new_index = tail_remote_index_ + table_elems_ + 1;
28  GPR_DEBUG_ASSERT(element_size <= MaxEntrySize());
29 
30  if (element_size > max_table_size_) {
31  while (table_size_ > 0) {
32  EvictOne();
33  }
34  return 0;
35  }
36 
37  // Reserve space for this element in the remote table: if this overflows
38  // the current table, drop elements until it fits, matching the decompressor
39  // algorithm.
40  while (table_size_ + element_size > max_table_size_) {
41  EvictOne();
42  }
44  elem_size_[new_index % elem_size_.size()] =
45  static_cast<uint16_t>(element_size);
46  table_size_ += element_size;
47  table_elems_++;
48 
49  return new_index;
50 }
51 
53  if (max_table_size == max_table_size_) {
54  return false;
55  }
56  while (table_size_ > 0 && table_size_ > max_table_size) {
57  EvictOne();
58  }
59  max_table_size_ = max_table_size;
60  const size_t max_table_elems =
61  hpack_constants::EntriesForBytes(max_table_size);
62  // TODO(ctiller): integrate with ResourceQuota to rebuild smaller when we can.
63  if (max_table_elems > elem_size_.size()) {
64  Rebuild(std::max(max_table_elems, 2 * elem_size_.size()));
65  }
66  return true;
67 }
68 
73  auto removing_size = elem_size_[tail_remote_index_ % elem_size_.size()];
74  GPR_ASSERT(table_size_ >= removing_size);
75  table_size_ -= removing_size;
76  table_elems_--;
77 }
78 
80  decltype(elem_size_) new_elem_size(capacity);
82  for (uint32_t i = 0; i < table_elems_; i++) {
83  uint32_t ofs = tail_remote_index_ + i + 1;
84  new_elem_size[ofs % capacity] = elem_size_[ofs % elem_size_.size()];
85  }
86  elem_size_.swap(new_elem_size);
87 }
88 
89 } // namespace grpc_core
log.h
GPR_DEBUG_ASSERT
#define GPR_DEBUG_ASSERT(x)
Definition: include/grpc/impl/codegen/log.h:103
uint16_t
unsigned short uint16_t
Definition: stdint-msvc2008.h:79
capacity
uint16_t capacity
Definition: protobuf/src/google/protobuf/descriptor.cc:948
grpc_core
Definition: call_metric_recorder.h:31
grpc_core::HPackEncoderTable::tail_remote_index_
uint32_t tail_remote_index_
Definition: hpack_encoder_table.h:63
grpc_core::HPackEncoderTable::AllocateIndex
uint32_t AllocateIndex(size_t element_size)
Definition: hpack_encoder_table.cc:26
grpc_core::hpack_constants::EntriesForBytes
static constexpr uint32_t EntriesForBytes(uint32_t bytes) noexcept
Definition: hpack_constants.h:32
grpc_core::HPackEncoderTable::EvictOne
void EvictOne()
Definition: hpack_encoder_table.cc:69
uint32_t
unsigned int uint32_t
Definition: stdint-msvc2008.h:80
grpc_core::HPackEncoderTable::table_elems_
uint32_t table_elems_
Definition: hpack_encoder_table.h:65
GPR_ASSERT
#define GPR_ASSERT(x)
Definition: include/grpc/impl/codegen/log.h:94
max
int max
Definition: bloaty/third_party/zlib/examples/enough.c:170
grpc_core::HPackEncoderTable::table_size_
uint32_t table_size_
Definition: hpack_encoder_table.h:66
grpc_core::HPackEncoderTable::MaxEntrySize
static constexpr size_t MaxEntrySize()
Definition: hpack_encoder_table.h:35
absl::InlinedVector::size
size_type size() const noexcept
Definition: abseil-cpp/absl/container/inlined_vector.h:270
grpc_core::HPackEncoderTable::elem_size_
absl::InlinedVector< uint16_t, hpack_constants::kInitialTableEntries > elem_size_
Definition: hpack_encoder_table.h:69
grpc_core::HPackEncoderTable::SetMaxSize
bool SetMaxSize(uint32_t max_table_size)
Definition: hpack_encoder_table.cc:52
grpc_core::HPackEncoderTable::max_table_size_
uint32_t max_table_size_
Definition: hpack_encoder_table.h:64
absl::InlinedVector::swap
void swap(InlinedVector &other)
Definition: abseil-cpp/absl/container/inlined_vector.h:764
grpc_core::HPackEncoderTable::Rebuild
void Rebuild(uint32_t capacity)
Definition: hpack_encoder_table.cc:79
hpack_encoder_table.h
i
uint64_t i
Definition: abseil-cpp/absl/container/btree_benchmark.cc:230
port_platform.h


grpc
Author(s):
autogenerated on Thu Mar 13 2025 03:00:13