Skip to content

Commit

Permalink
Tweak BumpPtrAllocator to benefit the hot path (#90571)
Browse files Browse the repository at this point in the history
This takes the form of three consecutive but related changes:
- Mark the fast path of BumpPtrAllocator as likely-taken.
- Move the slow path of BumpPtrAllocator to a separate function.
- Mark the slow path of BumpPtrAllocator as noinline.

Overall, this saves geomean 0.4% userspace instructions on CTMark -O3,
and 0.98% on CTMark -O0 -g.


http://llvm-compile-time-tracker.com/compare.php?from=e1622e189e8c0ef457bfac528f90a7a930d9aad2&to=9eb53a4ed3af4a55e769ae1dd22d034b63d046e3&stat=instructions%3Au
  • Loading branch information
resistor committed May 1, 2024
1 parent d392520 commit cd46c2c
Showing 1 changed file with 8 additions and 3 deletions.
11 changes: 8 additions & 3 deletions llvm/include/llvm/Support/Allocator.h
Original file line number Diff line number Diff line change
Expand Up @@ -159,9 +159,9 @@ class BumpPtrAllocatorImpl
#endif

// Check if we have enough space.
if (Adjustment + SizeToAllocate <= size_t(End - CurPtr)
// We can't return nullptr even for a zero-sized allocation!
&& CurPtr != nullptr) {
if (LLVM_LIKELY(Adjustment + SizeToAllocate <= size_t(End - CurPtr)
// We can't return nullptr even for a zero-sized allocation!
&& CurPtr != nullptr)) {
char *AlignedPtr = CurPtr + Adjustment;
CurPtr = AlignedPtr + SizeToAllocate;
// Update the allocation point of this memory block in MemorySanitizer.
Expand All @@ -173,6 +173,11 @@ class BumpPtrAllocatorImpl
return AlignedPtr;
}

return AllocateSlow(Size, SizeToAllocate, Alignment);
}

LLVM_ATTRIBUTE_RETURNS_NONNULL LLVM_ATTRIBUTE_NOINLINE void *
AllocateSlow(size_t Size, size_t SizeToAllocate, Align Alignment) {
// If Size is really big, allocate a separate slab for it.
size_t PaddedSize = SizeToAllocate + Alignment.value() - 1;
if (PaddedSize > SizeThreshold) {
Expand Down

0 comments on commit cd46c2c

Please sign in to comment.