Skip to content

Commit

Permalink
Move ABSL_HARDENING_ASSERTs in constexpr methods to their own lines.
Browse files Browse the repository at this point in the history
Prior to this change, some ABSL_HARDENING_ASSERTs were sequenced with the main
work of some constexpr methods using the comma operator in order to satisfy
C++11's constexpr requirements. However, putting the assertion and the main
work of the function on the same line complicates measuring the performance
impact of the assertions. As C++11 is no longer supported, this change moves
the assertions to their own lines in order to make measuring their performance
impact easier.

PiperOrigin-RevId: 707614464
Change-Id: Idb621bb183b80db17e2db44c3ffc671b76bba92b
  • Loading branch information
Abseil Team authored and copybara-github committed Dec 18, 2024
1 parent d6a75d9 commit f0e5905
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 12 deletions.
12 changes: 8 additions & 4 deletions absl/strings/string_view.h
Original file line number Diff line number Diff line change
Expand Up @@ -292,7 +292,8 @@ class ABSL_ATTRIBUTE_VIEW string_view {
// Returns the ith element of the `string_view` using the array operator.
// Note that this operator does not perform any bounds checking.
constexpr const_reference operator[](size_type i) const {
return ABSL_HARDENING_ASSERT(i < size()), ptr_[i];
ABSL_HARDENING_ASSERT(i < size());
return ptr_[i];
}

// string_view::at()
Expand All @@ -311,14 +312,16 @@ class ABSL_ATTRIBUTE_VIEW string_view {
//
// Returns the first element of a `string_view`.
constexpr const_reference front() const {
return ABSL_HARDENING_ASSERT(!empty()), ptr_[0];
ABSL_HARDENING_ASSERT(!empty());
return ptr_[0];
}

// string_view::back()
//
// Returns the last element of a `string_view`.
constexpr const_reference back() const {
return ABSL_HARDENING_ASSERT(!empty()), ptr_[size() - 1];
ABSL_HARDENING_ASSERT(!empty());
return ptr_[size() - 1];
}

// string_view::data()
Expand Down Expand Up @@ -664,7 +667,8 @@ class ABSL_ATTRIBUTE_VIEW string_view {
(std::numeric_limits<difference_type>::max)();

static constexpr size_type CheckLengthInternal(size_type len) {
return ABSL_HARDENING_ASSERT(len <= kMaxSize), len;
ABSL_HARDENING_ASSERT(len <= kMaxSize);
return len;
}

static constexpr size_type StrlenInternal(absl::Nonnull<const char*> str) {
Expand Down
6 changes: 4 additions & 2 deletions absl/types/optional.h
Original file line number Diff line number Diff line change
Expand Up @@ -429,14 +429,16 @@ class optional : private optional_internal::optional_data<T>,
// Accesses the underlying `T` value of an `optional`. If the `optional` is
// empty, behavior is undefined.
constexpr const T& operator*() const& ABSL_ATTRIBUTE_LIFETIME_BOUND {
return ABSL_HARDENING_ASSERT(this->engaged_), reference();
ABSL_HARDENING_ASSERT(this->engaged_);
return reference();
}
T& operator*() & ABSL_ATTRIBUTE_LIFETIME_BOUND {
ABSL_HARDENING_ASSERT(this->engaged_);
return reference();
}
constexpr const T&& operator*() const&& ABSL_ATTRIBUTE_LIFETIME_BOUND {
return ABSL_HARDENING_ASSERT(this->engaged_), std::move(reference());
ABSL_HARDENING_ASSERT(this->engaged_);
return std::move(reference());
}
T&& operator*() && ABSL_ATTRIBUTE_LIFETIME_BOUND {
ABSL_HARDENING_ASSERT(this->engaged_);
Expand Down
16 changes: 10 additions & 6 deletions absl/types/span.h
Original file line number Diff line number Diff line change
Expand Up @@ -330,7 +330,8 @@ class ABSL_ATTRIBUTE_VIEW Span {
//
// Returns a reference to the i'th element of this span.
constexpr reference operator[](size_type i) const noexcept {
return ABSL_HARDENING_ASSERT(i < size()), ptr_[i];
ABSL_HARDENING_ASSERT(i < size());
return ptr_[i];
}

// Span::at()
Expand All @@ -349,15 +350,17 @@ class ABSL_ATTRIBUTE_VIEW Span {
// Returns a reference to the first element of this span. The span must not
// be empty.
constexpr reference front() const noexcept {
return ABSL_HARDENING_ASSERT(size() > 0), *data();
ABSL_HARDENING_ASSERT(size() > 0);
return *data();
}

// Span::back()
//
// Returns a reference to the last element of this span. The span must not
// be empty.
constexpr reference back() const noexcept {
return ABSL_HARDENING_ASSERT(size() > 0), *(data() + size() - 1);
ABSL_HARDENING_ASSERT(size() > 0);
return *(data() + size() - 1);
}

// Span::begin()
Expand Down Expand Up @@ -727,8 +730,8 @@ constexpr Span<T> MakeSpan(absl::Nullable<T*> ptr, size_t size) noexcept {

template <int&... ExplicitArgumentBarrier, typename T>
Span<T> MakeSpan(absl::Nullable<T*> begin, absl::Nullable<T*> end) noexcept {
return ABSL_HARDENING_ASSERT(begin <= end),
Span<T>(begin, static_cast<size_t>(end - begin));
ABSL_HARDENING_ASSERT(begin <= end);
return Span<T>(begin, static_cast<size_t>(end - begin));
}

template <int&... ExplicitArgumentBarrier, typename C>
Expand Down Expand Up @@ -775,7 +778,8 @@ constexpr Span<const T> MakeConstSpan(absl::Nullable<T*> ptr,
template <int&... ExplicitArgumentBarrier, typename T>
Span<const T> MakeConstSpan(absl::Nullable<T*> begin,
absl::Nullable<T*> end) noexcept {
return ABSL_HARDENING_ASSERT(begin <= end), Span<const T>(begin, end - begin);
ABSL_HARDENING_ASSERT(begin <= end);
return Span<const T>(begin, end - begin);
}

template <int&... ExplicitArgumentBarrier, typename C>
Expand Down

0 comments on commit f0e5905

Please sign in to comment.