Commit b300658f authored by Benoit Lize's avatar Benoit Lize Committed by Commit Bot

base/allocator: Remove '%' from aligned allocations.

Modulo operations are very slow, replace it with a subtraction and a
bitmask operation.

Bug: 998048
Change-Id: Iff08febd395c7735c416ced4914e32fcf4d9f81a
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2309696
Commit-Queue: Benoit L <lizeb@chromium.org>
Reviewed-by: default avatarBartek Nowierski <bartekn@chromium.org>
Cr-Commit-Position: refs/heads/master@{#790425}
parent 26dde916
...@@ -863,7 +863,7 @@ ALWAYS_INLINE void* PartitionRoot<thread_safe>::AlignedAlloc(size_t alignment, ...@@ -863,7 +863,7 @@ ALWAYS_INLINE void* PartitionRoot<thread_safe>::AlignedAlloc(size_t alignment,
// Handle cases such as size = 16, alignment = 64. // Handle cases such as size = 16, alignment = 64.
// Wastes memory when a large alignment is requested with a small size, but // Wastes memory when a large alignment is requested with a small size, but
// this is hard to avoid, and should not be too common. // this is hard to avoid, and should not be too common.
if (size < alignment) { if (UNLIKELY(size < alignment)) {
ptr = Alloc(alignment, ""); ptr = Alloc(alignment, "");
} else { } else {
// PartitionAlloc only guarantees alignment for power-of-two sized // PartitionAlloc only guarantees alignment for power-of-two sized
...@@ -875,7 +875,10 @@ ALWAYS_INLINE void* PartitionRoot<thread_safe>::AlignedAlloc(size_t alignment, ...@@ -875,7 +875,10 @@ ALWAYS_INLINE void* PartitionRoot<thread_safe>::AlignedAlloc(size_t alignment,
ptr = Alloc(size_rounded_up, ""); ptr = Alloc(size_rounded_up, "");
} }
PA_CHECK(reinterpret_cast<uintptr_t>(ptr) % alignment == 0ull); // |alignment| is a power of two, but the compiler doesn't necessarily know
// that. A regular % operation is very slow, make sure to use the equivalent,
// faster form.
PA_CHECK(!(reinterpret_cast<uintptr_t>(ptr) & (alignment - 1)));
return ptr; return ptr;
} }
......
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