saipubw 1c06165c9b
[libc++] Make std::align an inline function (#167472)
`std::align` is heavily used in memory allocators. When we attempted to
switch from libstdc++ to libc++, we observed a **50%** performance
regression in a database query bench: the issue is that `std::align` in
libc++ is not an inline function, which prevents the compiler from
performing inlining optimizations.

make `std::align` an inline function will run about 2x faster. See
[benchmark
result](https://quick-bench.com/q/wPTnt9JCGn2S-3bu5gY9YrEf6KU).

---------

Co-authored-by: Louis Dionne <ldionne.2@gmail.com>
2025-12-18 17:55:28 +08:00

40 lines
1.1 KiB
C++

//===----------------------------------------------------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
// UNSUPPORTED: c++03
#include <memory>
#include <iostream>
#include "benchmark/benchmark.h"
struct Input {
std::size_t align;
std::size_t size;
void* ptr;
std::size_t buffer_size;
};
static void BM_align(benchmark::State& state) {
char buffer[1024];
Input input{};
void* ptr = buffer + 123;
std::size_t buffer_size = sizeof(buffer) - 123;
input.align = state.range();
input.size = state.range();
for (auto _ : state) {
input.ptr = ptr;
input.buffer_size = buffer_size;
benchmark::DoNotOptimize(input);
benchmark::DoNotOptimize(std::align(input.align, input.size, input.ptr, input.buffer_size));
}
}
BENCHMARK(BM_align)->Range(1, 256);
BENCHMARK_MAIN();