Skip to content

Commit 482a974

Browse files
ezbrcopybara-github
authored andcommitted
Call the common case of AllocateBackingArray directly instead of through the function pointer.
Also remove ABSL_ATTRIBUTE_NOINLINE from AllocateBackingArray. I think that was copied unnecessarily from DeallocateBackingArray. DeallocateBackingArray is called directly in some places that we don't want it to be inlined, but AllocateBackingArray is only called by function pointer before this change so it wouldn't have had any effect. Motivation: calling the allocation function through a function pointer prevents profile-guided-heap-optimization. PiperOrigin-RevId: 834395282 Change-Id: I930757baf27784b81ee0a8e6596d5a3f31da528a
1 parent dd2ad43 commit 482a974

File tree

2 files changed

+14
-2
lines changed

2 files changed

+14
-2
lines changed

absl/container/internal/raw_hash_set.cc

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
#include <cstddef>
2020
#include <cstdint>
2121
#include <cstring>
22+
#include <memory>
2223
#include <tuple>
2324
#include <utility>
2425

@@ -763,7 +764,16 @@ BackingArrayPtrs AllocBackingArray(CommonFields& common,
763764
void* alloc) {
764765
RawHashSetLayout layout(new_capacity, policy.slot_size, policy.slot_align,
765766
has_infoz);
766-
char* mem = static_cast<char*>(policy.alloc(alloc, layout.alloc_size()));
767+
// Perform a direct call in the common case to allow for profile-guided
768+
// heap optimization (PGHO) to understand which allocation function is used.
769+
constexpr size_t kDefaultAlignment = BackingArrayAlignment(alignof(size_t));
770+
char* mem = static_cast<char*>(
771+
ABSL_PREDICT_TRUE(
772+
policy.alloc ==
773+
(&AllocateBackingArray<kDefaultAlignment, std::allocator<char>>))
774+
? AllocateBackingArray<kDefaultAlignment, std::allocator<char>>(
775+
alloc, layout.alloc_size())
776+
: policy.alloc(alloc, layout.alloc_size()));
767777
const GenerationType old_generation = common.generation();
768778
common.set_generation_ptr(
769779
reinterpret_cast<GenerationType*>(mem + layout.generation_offset()));

absl/container/internal/raw_hash_set.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1492,10 +1492,12 @@ constexpr bool ShouldSampleHashtablezInfoForAlloc() {
14921492

14931493
// Allocates `n` bytes for a backing array.
14941494
template <size_t AlignOfBackingArray, typename Alloc>
1495-
ABSL_ATTRIBUTE_NOINLINE void* AllocateBackingArray(void* alloc, size_t n) {
1495+
void* AllocateBackingArray(void* alloc, size_t n) {
14961496
return Allocate<AlignOfBackingArray>(static_cast<Alloc*>(alloc), n);
14971497
}
14981498

1499+
// Note: we mark this function as ABSL_ATTRIBUTE_NOINLINE because we don't want
1500+
// it to be inlined into e.g. the destructor to save code size.
14991501
template <size_t AlignOfBackingArray, typename Alloc>
15001502
ABSL_ATTRIBUTE_NOINLINE void DeallocateBackingArray(
15011503
void* alloc, size_t capacity, ctrl_t* ctrl, size_t slot_size,

0 commit comments

Comments
 (0)