Commit db08109e by Daniel Cheng Committed by Copybara-Service

Use ABSL_PREDICT_FALSE and ABSL_RAW_LOG for shared safety checks in raw_hash_set.

`SwisstableDebugEnabled()` is also true for release builds with hardening
enabled. To minimize their impact in those builds:
 - use `ABSL_PREDICT_FALSE()` to provide a compiler hint for code layout
 - use `ABSL_RAW_LOG()` with a format string to reduce code size and improve
   the chances that the hot paths will be inlined.

PiperOrigin-RevId: 567102494
Change-Id: I6734bd491d7b2e1fb9df0e86f4e29e6ad0a03102
parent 1f220d57
...@@ -1200,13 +1200,17 @@ inline void AssertIsFull(const ctrl_t* ctrl, GenerationType generation, ...@@ -1200,13 +1200,17 @@ inline void AssertIsFull(const ctrl_t* ctrl, GenerationType generation,
const GenerationType* generation_ptr, const GenerationType* generation_ptr,
const char* operation) { const char* operation) {
if (!SwisstableDebugEnabled()) return; if (!SwisstableDebugEnabled()) return;
if (ctrl == nullptr) { // `SwisstableDebugEnabled()` is also true for release builds with hardening
ABSL_INTERNAL_LOG(FATAL, // enabled. To minimize their impact in those builds:
std::string(operation) + " called on end() iterator."); // - use `ABSL_PREDICT_FALSE()` to provide a compiler hint for code layout
} // - use `ABSL_RAW_LOG()` with a format string to reduce code size and improve
if (ctrl == EmptyGroup()) { // the chances that the hot paths will be inlined.
ABSL_INTERNAL_LOG(FATAL, std::string(operation) + if (ABSL_PREDICT_FALSE(ctrl == nullptr)) {
" called on default-constructed iterator."); ABSL_RAW_LOG(FATAL, "%s called on end() iterator.", operation);
}
if (ABSL_PREDICT_FALSE(ctrl == EmptyGroup())) {
ABSL_RAW_LOG(FATAL, "%s called on default-constructed iterator.",
operation);
} }
if (SwisstableGenerationsEnabled()) { if (SwisstableGenerationsEnabled()) {
if (generation != *generation_ptr) { if (generation != *generation_ptr) {
...@@ -1222,13 +1226,13 @@ inline void AssertIsFull(const ctrl_t* ctrl, GenerationType generation, ...@@ -1222,13 +1226,13 @@ inline void AssertIsFull(const ctrl_t* ctrl, GenerationType generation,
" called on invalid iterator. The element was likely erased."); " called on invalid iterator. The element was likely erased.");
} }
} else { } else {
if (!IsFull(*ctrl)) { if (ABSL_PREDICT_FALSE(!IsFull(*ctrl))) {
ABSL_INTERNAL_LOG( ABSL_RAW_LOG(
FATAL, FATAL,
std::string(operation) + "%s called on invalid iterator. The element might have been erased "
" called on invalid iterator. The element might have been erased " "or the table might have rehashed. Consider running with "
"or the table might have rehashed. Consider running with " "--config=asan to diagnose rehashing issues.",
"--config=asan to diagnose rehashing issues."); operation);
} }
} }
} }
...@@ -1287,10 +1291,15 @@ inline void AssertSameContainer(const ctrl_t* ctrl_a, const ctrl_t* ctrl_b, ...@@ -1287,10 +1291,15 @@ inline void AssertSameContainer(const ctrl_t* ctrl_a, const ctrl_t* ctrl_b,
const GenerationType* generation_ptr_a, const GenerationType* generation_ptr_a,
const GenerationType* generation_ptr_b) { const GenerationType* generation_ptr_b) {
if (!SwisstableDebugEnabled()) return; if (!SwisstableDebugEnabled()) return;
// `SwisstableDebugEnabled()` is also true for release builds with hardening
// enabled. To minimize their impact in those builds:
// - use `ABSL_PREDICT_FALSE()` to provide a compiler hint for code layout
// - use `ABSL_RAW_LOG()` with a format string to reduce code size and improve
// the chances that the hot paths will be inlined.
const bool a_is_default = ctrl_a == EmptyGroup(); const bool a_is_default = ctrl_a == EmptyGroup();
const bool b_is_default = ctrl_b == EmptyGroup(); const bool b_is_default = ctrl_b == EmptyGroup();
if (a_is_default != b_is_default) { if (ABSL_PREDICT_FALSE(a_is_default != b_is_default)) {
ABSL_INTERNAL_LOG( ABSL_RAW_LOG(
FATAL, FATAL,
"Invalid iterator comparison. Comparing default-constructed iterator " "Invalid iterator comparison. Comparing default-constructed iterator "
"with non-default-constructed iterator."); "with non-default-constructed iterator.");
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment