diff options
author | Daniel Cheng <dcheng@google.com> | 2023-09-20 15:45:44 -0700 |
---|---|---|
committer | Copybara-Service <copybara-worker@google.com> | 2023-09-20 15:47:39 -0700 |
commit | db08109eeb15fcd856761557f1668c2b34690036 (patch) | |
tree | 215419f577a13c27ca4219260b12c75470754794 /absl/container/internal/raw_hash_set.h | |
parent | 1f220d57f4968ac65be51dc0f4d659ce4760cfbb (diff) | |
download | abseil-db08109eeb15fcd856761557f1668c2b34690036.tar.gz abseil-db08109eeb15fcd856761557f1668c2b34690036.tar.bz2 abseil-db08109eeb15fcd856761557f1668c2b34690036.zip |
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
Diffstat (limited to 'absl/container/internal/raw_hash_set.h')
-rw-r--r-- | absl/container/internal/raw_hash_set.h | 39 |
1 files changed, 24 insertions, 15 deletions
diff --git a/absl/container/internal/raw_hash_set.h b/absl/container/internal/raw_hash_set.h index bcd889a0..5ec284e2 100644 --- a/absl/container/internal/raw_hash_set.h +++ b/absl/container/internal/raw_hash_set.h @@ -1200,13 +1200,17 @@ inline void AssertIsFull(const ctrl_t* ctrl, GenerationType generation, const GenerationType* generation_ptr, const char* operation) { if (!SwisstableDebugEnabled()) return; - if (ctrl == nullptr) { - ABSL_INTERNAL_LOG(FATAL, - std::string(operation) + " called on end() iterator."); - } - if (ctrl == EmptyGroup()) { - ABSL_INTERNAL_LOG(FATAL, std::string(operation) + - " called on default-constructed iterator."); + // `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. + if (ABSL_PREDICT_FALSE(ctrl == nullptr)) { + 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 (generation != *generation_ptr) { @@ -1222,13 +1226,13 @@ inline void AssertIsFull(const ctrl_t* ctrl, GenerationType generation, " called on invalid iterator. The element was likely erased."); } } else { - if (!IsFull(*ctrl)) { - ABSL_INTERNAL_LOG( + if (ABSL_PREDICT_FALSE(!IsFull(*ctrl))) { + ABSL_RAW_LOG( FATAL, - std::string(operation) + - " called on invalid iterator. The element might have been erased " - "or the table might have rehashed. Consider running with " - "--config=asan to diagnose rehashing issues."); + "%s called on invalid iterator. The element might have been erased " + "or the table might have rehashed. Consider running with " + "--config=asan to diagnose rehashing issues.", + operation); } } } @@ -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_b) { 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 b_is_default = ctrl_b == EmptyGroup(); - if (a_is_default != b_is_default) { - ABSL_INTERNAL_LOG( + if (ABSL_PREDICT_FALSE(a_is_default != b_is_default)) { + ABSL_RAW_LOG( FATAL, "Invalid iterator comparison. Comparing default-constructed iterator " "with non-default-constructed iterator."); |