Removes msvc warning in map (#17013)

Hi,
We get a compiler warning in MSVC that a signed/unsigned comparison is executed.

The warning is at around line 1073 in src/google/protobuf/map.h
```cpp
ABSL_DCHECK_GE(new_num_buckets, kMinTableSize); // <-- signed/unsigned comparison
const auto old_table = table_;
const map_index_t old_table_size = num_buckets_;
num_buckets_ = new_num_buckets;
```

This is due to `kMinTableSize` is a enum value. Probably a C++98 way to enforce constexpr.

This PR changes `kMinTableSize` to be a constexpr unsigned `map_index_t` and removes the casts instead of fixing the warning with a cast at ne 1073.
It seems like `kMinTableSize` is correlated with the `TableSize` of this class, which is map_index_t. Therefore the min value can at maximum the max value of map_index_t. And the min value of `kMinTableSize` can also not be negative, as this makes no sense.

It is also nicer to have fewer casts.

Closes #17013

COPYBARA_INTEGRATE_REVIEW=https://github.com/protocolbuffers/protobuf/pull/17013 from TinyTinni:removes-msvc-warning-map 4f954e8c9b
PiperOrigin-RevId: 644207177
pull/17170/head
Matthias Möller 5 months ago committed by Copybara-Service
parent 41514865eb
commit 9f6a8bd1fa
  1. 8
      src/google/protobuf/map.h

@ -560,7 +560,7 @@ class PROTOBUF_EXPORT UntypedMapBase {
protected:
// 16 bytes is the minimum useful size for the array cache in the arena.
enum { kMinTableSize = 16 / sizeof(void*) };
enum : map_index_t { kMinTableSize = 16 / sizeof(void*) };
public:
Arena* arena() const { return this->alloc_.arena(); }
@ -645,9 +645,7 @@ class PROTOBUF_EXPORT UntypedMapBase {
// Return a power of two no less than max(kMinTableSize, n).
// Assumes either n < kMinTableSize or n is a power of two.
map_index_t TableSize(map_index_t n) {
return n < static_cast<map_index_t>(kMinTableSize)
? static_cast<map_index_t>(kMinTableSize)
: n;
return n < kMinTableSize ? kMinTableSize : n;
}
template <typename T>
@ -697,7 +695,7 @@ class PROTOBUF_EXPORT UntypedMapBase {
}
TableEntryPtr* CreateEmptyTable(map_index_t n) {
ABSL_DCHECK_GE(n, map_index_t{kMinTableSize});
ABSL_DCHECK_GE(n, kMinTableSize);
ABSL_DCHECK_EQ(n & (n - 1), 0u);
TableEntryPtr* result = AllocFor<TableEntryPtr>(alloc_).allocate(n);
memset(result, 0, n * sizeof(result[0]));

Loading…
Cancel
Save