compression_internal.cc
Go to the documentation of this file.
1 /*
2  *
3  * Copyright 2015 gRPC authors.
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17  */
18 
20 
22 
23 #include <stdlib.h>
24 #include <string.h>
25 
26 #include "absl/container/inlined_vector.h"
27 #include "absl/strings/ascii.h"
28 #include "absl/strings/str_split.h"
29 
30 #include <grpc/support/log.h>
31 
35 
36 namespace grpc_core {
37 
39  switch (algorithm) {
40  case GRPC_COMPRESS_NONE:
41  return "identity";
43  return "deflate";
44  case GRPC_COMPRESS_GZIP:
45  return "gzip";
47  default:
48  return nullptr;
49  }
50 }
51 
52 namespace {
53 class CommaSeparatedLists {
54  public:
55  CommaSeparatedLists() : lists_{}, text_buffer_{} {
56  char* text_buffer = text_buffer_;
57  auto add_char = [&text_buffer, this](char c) {
58  if (text_buffer - text_buffer_ == kTextBufferSize) abort();
59  *text_buffer++ = c;
60  };
61  for (size_t list = 0; list < kNumLists; ++list) {
62  char* start = text_buffer;
63  for (size_t algorithm = 0; algorithm < GRPC_COMPRESS_ALGORITHMS_COUNT;
64  ++algorithm) {
65  if ((list & (1 << algorithm)) == 0) continue;
66  if (start != text_buffer) {
67  add_char(',');
68  add_char(' ');
69  }
70  const char* name = CompressionAlgorithmAsString(
71  static_cast<grpc_compression_algorithm>(algorithm));
72  for (const char* p = name; *p != '\0'; ++p) {
73  add_char(*p);
74  }
75  }
76  lists_[list] = absl::string_view(start, text_buffer - start);
77  }
78  if (text_buffer - text_buffer_ != kTextBufferSize) abort();
79  }
80 
81  absl::string_view operator[](size_t list) const { return lists_[list]; }
82 
83  private:
84  static constexpr size_t kNumLists = 1 << GRPC_COMPRESS_ALGORITHMS_COUNT;
85  // Experimentally determined (tweak things until it runs).
86  static constexpr size_t kTextBufferSize = 86;
89 };
90 
91 const CommaSeparatedLists kCommaSeparatedLists;
92 } // namespace
93 
95  absl::string_view algorithm) {
96  if (algorithm == "identity") {
97  return GRPC_COMPRESS_NONE;
98  } else if (algorithm == "deflate") {
99  return GRPC_COMPRESS_DEFLATE;
100  } else if (algorithm == "gzip") {
101  return GRPC_COMPRESS_GZIP;
102  } else {
103  return absl::nullopt;
104  }
105 }
106 
110  GRPC_API_TRACE("grpc_message_compression_algorithm_for_level(level=%d)", 1,
111  ((int)level));
113  gpr_log(GPR_ERROR, "Unknown message compression level %d.",
114  static_cast<int>(level));
115  abort();
116  }
117 
119  return GRPC_COMPRESS_NONE;
120  }
121 
122  GPR_ASSERT(level > 0);
123 
124  /* Establish a "ranking" or compression algorithms in increasing order of
125  * compression.
126  * This is simplistic and we will probably want to introduce other dimensions
127  * in the future (cpu/memory cost, etc). */
130  algos;
131  for (auto algo : {GRPC_COMPRESS_GZIP, GRPC_COMPRESS_DEFLATE}) {
132  if (set_.is_set(algo)) {
133  algos.push_back(algo);
134  }
135  }
136 
137  if (algos.empty()) {
138  return GRPC_COMPRESS_NONE;
139  }
140 
141  switch (level) {
143  abort(); /* should have been handled already */
145  return algos[0];
147  return algos[algos.size() / 2];
149  return algos.back();
150  default:
151  abort();
152  };
153 }
154 
157  for (size_t i = 0; i < GRPC_COMPRESS_ALGORITHMS_COUNT; i++) {
158  if (value & (1u << i)) {
159  set.set_.set(i);
160  }
161  }
162  return set;
163 }
164 
166  const grpc_channel_args* args) {
168  static const uint32_t kEverything =
170  if (args != nullptr) {
173  grpc_integer_options{kEverything, 0, kEverything}));
174  set.Set(GRPC_COMPRESS_NONE);
175  } else {
177  }
178  return set;
179 }
180 
182 
184  std::initializer_list<grpc_compression_algorithm> algorithms) {
185  for (auto algorithm : algorithms) {
186  Set(algorithm);
187  }
188 }
189 
191  grpc_compression_algorithm algorithm) const {
192  size_t i = static_cast<size_t>(algorithm);
194  return set_.is_set(i);
195  } else {
196  return false;
197  }
198 }
199 
201  size_t i = static_cast<size_t>(algorithm);
203  set_.set(i);
204  }
205 }
206 
208  return kCommaSeparatedLists[ToLegacyBitmask()];
209 }
210 
213 }
214 
218  for (auto algorithm : absl::StrSplit(str, ',')) {
219  auto parsed =
221  if (parsed.has_value()) {
222  set.Set(*parsed);
223  }
224  }
225  return set;
226 }
227 
229  return set_.ToInt<uint32_t>();
230 }
231 
234  if (args == nullptr) return absl::nullopt;
235  for (size_t i = 0; i < args->num_args; i++) {
236  if (strcmp(args->args[i].key, GRPC_COMPRESSION_CHANNEL_DEFAULT_ALGORITHM) ==
237  0) {
238  if (args->args[i].type == GRPC_ARG_INTEGER) {
239  return static_cast<grpc_compression_algorithm>(
240  args->args[i].value.integer);
241  } else if (args->args[i].type == GRPC_ARG_STRING) {
242  return ParseCompressionAlgorithm(args->args[i].value.string);
243  }
244  }
245  }
246  return absl::nullopt;
247 }
248 
249 } // namespace grpc_core
absl::StripAsciiWhitespace
ABSL_MUST_USE_RESULT absl::string_view StripAsciiWhitespace(absl::string_view str)
Definition: abseil-cpp/absl/strings/ascii.h:225
absl::StrSplit
strings_internal::Splitter< typename strings_internal::SelectDelimiter< Delimiter >::type, AllowEmpty, absl::string_view > StrSplit(strings_internal::ConvertibleToStringView text, Delimiter d)
Definition: abseil-cpp/absl/strings/str_split.h:499
trace.h
xds_interop_client.str
str
Definition: xds_interop_client.py:487
GRPC_COMPRESS_LEVEL_MED
@ GRPC_COMPRESS_LEVEL_MED
Definition: compression_types.h:75
GRPC_COMPRESS_DEFLATE
@ GRPC_COMPRESS_DEFLATE
Definition: compression_types.h:62
log.h
GRPC_ARG_INTEGER
@ GRPC_ARG_INTEGER
Definition: grpc_types.h:81
grpc_core::BitSet::set
GRPC_BITSET_CONSTEXPR_MUTATOR void set(int i)
Definition: bitset.h:93
fix_build_deps.c
list c
Definition: fix_build_deps.py:490
GRPC_ARG_STRING
@ GRPC_ARG_STRING
Definition: grpc_types.h:80
grpc_core::ParseCompressionAlgorithm
absl::optional< grpc_compression_algorithm > ParseCompressionAlgorithm(absl::string_view algorithm)
Definition: compression_internal.cc:94
grpc_core::CompressionAlgorithmSet
Definition: compression_internal.h:52
grpc_core::DefaultCompressionAlgorithmFromChannelArgs
absl::optional< grpc_compression_algorithm > DefaultCompressionAlgorithmFromChannelArgs(const grpc_channel_args *args)
Definition: compression_internal.cc:233
grpc_core::CompressionAlgorithmSet::CompressionAlgorithmForLevel
grpc_compression_algorithm CompressionAlgorithmForLevel(grpc_compression_level level) const
Definition: compression_internal.cc:108
grpc_core
Definition: call_metric_recorder.h:31
grpc_core::Slice
Definition: src/core/lib/slice/slice.h:282
string.h
grpc_compression_algorithm
grpc_compression_algorithm
Definition: compression_types.h:60
grpc_core::slice_detail::StaticConstructors< Slice >::FromStaticString
static Slice FromStaticString(const char *s)
Definition: src/core/lib/slice/slice.h:201
absl::string_view
Definition: abseil-cpp/absl/strings/string_view.h:167
grpc_integer_options
Definition: channel_args.h:310
u
OPENSSL_EXPORT pem_password_cb void * u
Definition: pem.h:351
setup.name
name
Definition: setup.py:542
xds_manager.p
p
Definition: xds_manager.py:60
GRPC_COMPRESSION_CHANNEL_ENABLED_ALGORITHMS_BITSET
#define GRPC_COMPRESSION_CHANNEL_ENABLED_ALGORITHMS_BITSET
Definition: compression_types.h:54
GRPC_COMPRESS_NONE
@ GRPC_COMPRESS_NONE
Definition: compression_types.h:61
grpc_channel_args
Definition: grpc_types.h:132
kTextBufferSize
static constexpr size_t kTextBufferSize
Definition: compression_internal.cc:86
grpc_core::BitSet::is_set
constexpr bool is_set(int i) const
Definition: bitset.h:112
grpc_core::CompressionAlgorithmSet::ToSlice
Slice ToSlice() const
Definition: compression_internal.cc:211
uint32_t
unsigned int uint32_t
Definition: stdint-msvc2008.h:80
lists_
absl::string_view lists_[kNumLists]
Definition: compression_internal.cc:87
start
static uint64_t start
Definition: benchmark-pound.c:74
asyncio_get_stats.args
args
Definition: asyncio_get_stats.py:40
GPR_ASSERT
#define GPR_ASSERT(x)
Definition: include/grpc/impl/codegen/log.h:94
kNumLists
static constexpr size_t kNumLists
Definition: compression_internal.cc:84
grpc_core::CompressionAlgorithmSet::IsSet
bool IsSet(grpc_compression_algorithm algorithm) const
Definition: compression_internal.cc:190
gpr_log
GPRAPI void gpr_log(const char *file, int line, gpr_log_severity severity, const char *format,...) GPR_PRINT_FORMAT_CHECK(4
GRPC_COMPRESS_LEVEL_NONE
@ GRPC_COMPRESS_LEVEL_NONE
Definition: compression_types.h:73
absl::optional
Definition: abseil-cpp/absl/types/internal/optional.h:61
grpc_core::CompressionAlgorithmSet::ToString
absl::string_view ToString() const
Definition: compression_internal.cc:207
GPR_ERROR
#define GPR_ERROR
Definition: include/grpc/impl/codegen/log.h:57
GRPC_COMPRESS_LEVEL_HIGH
@ GRPC_COMPRESS_LEVEL_HIGH
Definition: compression_types.h:76
grpc_core::CompressionAlgorithmSet::Set
void Set(grpc_compression_algorithm algorithm)
Definition: compression_internal.cc:200
grpc_core::CompressionAlgorithmAsString
const char * CompressionAlgorithmAsString(grpc_compression_algorithm algorithm)
Definition: compression_internal.cc:38
text_buffer_
char text_buffer_[kTextBufferSize]
Definition: compression_internal.cc:88
grpc_core::CompressionAlgorithmSet::FromChannelArgs
static CompressionAlgorithmSet FromChannelArgs(const grpc_channel_args *args)
Definition: compression_internal.cc:165
value
const char * value
Definition: hpack_parser_table.cc:165
compression_internal.h
grpc_core::CompressionAlgorithmSet::set_
BitSet< GRPC_COMPRESS_ALGORITHMS_COUNT > set_
Definition: compression_internal.h:88
grpc_core::CompressionAlgorithmSet::FromString
static CompressionAlgorithmSet FromString(absl::string_view str)
Definition: compression_internal.cc:215
GRPC_COMPRESS_LEVEL_LOW
@ GRPC_COMPRESS_LEVEL_LOW
Definition: compression_types.h:74
grpc_compression_level
grpc_compression_level
Definition: compression_types.h:72
string_view
absl::string_view string_view
Definition: attr.cc:22
GRPC_COMPRESS_ALGORITHMS_COUNT
@ GRPC_COMPRESS_ALGORITHMS_COUNT
Definition: compression_types.h:65
GRPC_COMPRESS_GZIP
@ GRPC_COMPRESS_GZIP
Definition: compression_types.h:63
cpp.gmock_class.set
set
Definition: bloaty/third_party/googletest/googlemock/scripts/generator/cpp/gmock_class.py:44
grpc_core::CompressionAlgorithmSet::CompressionAlgorithmSet
CompressionAlgorithmSet()
grpc_core::BitSet::ToInt
std::enable_if< std::is_unsigned< Int >::value &&(sizeof(Int) *8 >=kTotalBits), Int >::type ToInt() const
Definition: bitset.h:164
channel_args.h
api_trace.h
grpc_core::CompressionAlgorithmSet::FromUint32
static CompressionAlgorithmSet FromUint32(uint32_t value)
Definition: compression_internal.cc:155
client.level
level
Definition: examples/python/async_streaming/client.py:118
absl::InlinedVector
Definition: abseil-cpp/absl/container/inlined_vector.h:69
grpc_channel_args_find_integer
int grpc_channel_args_find_integer(const grpc_channel_args *args, const char *name, const grpc_integer_options options)
Definition: channel_args.cc:425
GRPC_COMPRESSION_CHANNEL_DEFAULT_ALGORITHM
#define GRPC_COMPRESSION_CHANNEL_DEFAULT_ALGORITHM
Definition: compression_types.h:42
grpc_core::CompressionAlgorithmSet::ToLegacyBitmask
uint32_t ToLegacyBitmask() const
Definition: compression_internal.cc:228
i
uint64_t i
Definition: abseil-cpp/absl/container/btree_benchmark.cc:230
GRPC_API_TRACE
#define GRPC_API_TRACE(fmt, nargs, args)
Definition: api_trace.h:48
port_platform.h


grpc
Author(s):
autogenerated on Thu Mar 13 2025 02:58:52