Skip to content

Commit

Permalink
Cosmetics:
Browse files Browse the repository at this point in the history
* Use `ABSL_IS_{LITTLE,BIG}_ENDIAN` in `CompactString` instead of an optimizable
  runtime function.

* Add definitions of static member variables for pre-C++17.

PiperOrigin-RevId: 629003784
  • Loading branch information
QrczakMK committed Apr 29, 2024
1 parent 71fe39c commit 67c8f95
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 14 deletions.
1 change: 1 addition & 0 deletions riegeli/base/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -429,6 +429,7 @@ cc_library(
":compare",
":estimated_allocated_size",
":new_aligned",
"@com_google_absl//absl/base:config",
"@com_google_absl//absl/base:core_headers",
"@com_google_absl//absl/hash",
"@com_google_absl//absl/strings",
Expand Down
10 changes: 10 additions & 0 deletions riegeli/base/compact_string.cc
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

#include "riegeli/base/compact_string.h"

#include <stddef.h> // IWYU pragma: keep
#include <stdint.h>

#include <cstring>
Expand All @@ -27,6 +28,15 @@

namespace riegeli {

// Before C++17 if a constexpr static data member is ODR-used, its definition at
// namespace scope is required. Since C++17 these definitions are deprecated:
// http://en.cppreference.com/w/cpp/language/static
#if !__cpp_inline_variables
constexpr uintptr_t CompactString::kDefaultRepr;
constexpr size_t CompactString::kInlineCapacity;
constexpr size_t CompactString::kInlineDataOffset;
#endif

void CompactString::AssignSlow(absl::string_view src) {
const size_t old_capacity = capacity();
DeleteRepr(std::exchange(
Expand Down
24 changes: 10 additions & 14 deletions riegeli/base/compact_string.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
#include <utility>

#include "absl/base/attributes.h"
#include "absl/base/config.h"
#include "absl/base/optimization.h"
#include "absl/hash/hash.h"
#include "absl/strings/string_view.h"
Expand Down Expand Up @@ -236,30 +237,25 @@ class
static constexpr size_t kInlineCapacity =
UnsignedMin(sizeof(uintptr_t) - 1, size_t{0xff >> 3});

// For Little Endian, returns 1.
// For Big Endian, returns 0.
//
// TODO: Use `std::endian` instead when C++20 is available.
//
// With the following implementation the result is not known at preprocessing
// time nor during constexpr evaluation, but this function is in practice
// optimized out to a constant.
static size_t InlineDataOffset() {
const uintptr_t value = 1;
return size_t{reinterpret_cast<const unsigned char*>(&value)[0]};
}
#if ABSL_IS_LITTLE_ENDIAN
static constexpr size_t kInlineDataOffset = 1;
#elif ABSL_IS_BIG_ENDIAN
static constexpr size_t kInlineDataOffset = 0;
#else
#error Unknown endianness
#endif

static char* inline_data(uintptr_t& repr) {
RIEGELI_ASSERT_EQ(repr & 7, 1u)
<< "Failed precondition of CompactString::inline_data(): "
"representation not inline";
return reinterpret_cast<char*>(&repr) + InlineDataOffset();
return reinterpret_cast<char*>(&repr) + kInlineDataOffset;
}
static const char* inline_data(const uintptr_t& repr) {
RIEGELI_ASSERT_EQ(repr & 7, 1u)
<< "Failed precondition of CompactString::inline_data(): "
"representation not inline";
return reinterpret_cast<const char*>(&repr) + InlineDataOffset();
return reinterpret_cast<const char*>(&repr) + kInlineDataOffset;
}

char* inline_data() { return inline_data(repr_); }
Expand Down

0 comments on commit 67c8f95

Please sign in to comment.