c++ - 我们能打败编译器吗?

标签 c++ memory

今天在写Pool Allocator的时候遇到了一个问题:
有可能打败编译器吗?

打败编译器我的意思是编写的代码比最简单的版本(堆栈上的分配变量,一个接一个)更快地执行内存分配(更少的时钟周期)。

所以我想出了一个非常简单的字节池:

template <size_t PoolSize>
class BytePool
{
public:
    template <typename T>
    T& At(size_t p_index)
    {
        return (T&)m_data[p_index * sizeof(T)];
    }

private:
    std::byte m_data[PoolSize];
};

这个简单的代码允许我在堆栈上分配一个字节数组一次并像访问 T 的数组一样访问它

为了操作这个数组,我做了一个宏:

#define is(type, slot) bytePool.At<type>(slot)

这个宏允许我写:#define a is (int, 0x0000) 比如a是一个伪变量,它指向bytePool[ sizeof(int) * 0x0000].

使用这个宏,我做了一段简单的代码,用一些数字执行基本操作(有些在编译时定义,有些在运行时定义,比如bc):

BytePool<sizeof(int) * 6> bytePool;

#define is(type, slot) bytePool.At<type>(slot)

#define a is (int, 0x0000)
#define b is (int, 0x0001)
#define c is (int, 0x0002)
#define d is (int, 0x0003)
#define e is (int, 0x0004)
#define f is (int, 0x0004)

a = 0;
b = (int)time(nullptr);
c = (int)__rdtsc();
d = 2 * b;
e = c - 3;
f = 18 ^ 2;

a = ~(b * c) * d + e / f;

#undef a
#undef b
#undef c
#undef d
#undef e
#undef f

有趣!这段代码看起来像是我手动为我的变量分配了内存槽。

不使用 ByteAllocator 的等价物是:

int a;
int b;
int c;
int d;
int e;
int f;

a = 0;
b = (int)time(nullptr);
c = (int)__rdtsc();
d = 2 * b;
e = c - 3;
f = 18 ^ 2;

a = ~(b * c) * d + e / f;

此时我问自己的问题是:
这些方法中哪一种更好?

  • 在堆栈上分配 sizeof(int) 6 次
  • 在堆栈上分配 sizeof(int) * 6 1 次

当然,我确信分配一次内存会更快。所以我猜我的 BytePool 方法更快。

现在,让我们听听编译器的声音。我写了一些代码来进行基准测试:

#include <iostream>
#include <intrin.h>
#include <ctime>

template <size_t PoolSize>
class BytePool
{
public:
    template <typename T>
    T& At(size_t p_index)
    {
        return (T&)m_data[p_index * sizeof(T)];
    }

private:
    std::byte m_data[PoolSize];
};

void Stack()
{
    int a;
    int b;
    int c;
    int d;
    int e;
    int f;

    a = 0;
    b = (int)time(nullptr);
    c = (int)__rdtsc();
    d = 2 * b;
    e = c - 3;
    f = 18 ^ 2;

    a = ~(b * c) * d + e / f;
}

void Pool()
{
    BytePool<sizeof(int) * 6> bytePool;

    #define is(type, slot) bytePool.At<type>(slot)

    #define a is (int, 0x0000)
    #define b is (int, 0x0001)
    #define c is (int, 0x0002)
    #define d is (int, 0x0003)
    #define e is (int, 0x0004)
    #define f is (int, 0x0004)

    a = 0;
    b = (int)time(nullptr);
    c = (int)__rdtsc();
    d = 2 * b;
    e = c - 3;
    f = 18 ^ 2;

    a = ~(b * c) * d + e / f;

    #undef a
    #undef b
    #undef c
    #undef d
    #undef e
    #undef f
}

void FastPool()
{
    int fastBytePool[6];

    #define a   *(fastBytePool)
    #define b   *(fastBytePool + 0x0001)
    #define c   *(fastBytePool + 0x0002)
    #define d   *(fastBytePool + 0x0003)
    #define e   *(fastBytePool + 0x0004)
    #define f   *(fastBytePool + 0x0005)

    a = 0;
    b = (int)time(nullptr);
    c = (int)__rdtsc();
    d = 2 * b;
    e = c - 3;
    f = 18 ^ 2;

    a = ~(b * c) * d + e / f;

    #undef a
    #undef b
    #undef c
    #undef d
    #undef e
    #undef f
}

void FastHeapPool()
{
    int* fastBytePool = new int[6];

    #define a   *(fastBytePool)
    #define b   *(fastBytePool + 0x0001)
    #define c   *(fastBytePool + 0x0002)
    #define d   *(fastBytePool + 0x0003)
    #define e   *(fastBytePool + 0x0004)
    #define f   *(fastBytePool + 0x0005)

    a = 0;
    b = (int)time(nullptr);
    c = (int)__rdtsc();
    d = 2 * b;
    e = c - 3;
    f = 18 ^ 2;

    a = ~(b * c) * d + e / f;

    #undef a
    #undef b
    #undef c
    #undef d
    #undef e
    #undef f

    delete[] fastBytePool;
}

size_t Benchmark(void (p_function)(), size_t p_iterations)
{
    size_t cycleSum = 0;

    for (size_t it = 0; it < p_iterations; ++it)
    {
        size_t startCycles = __rdtsc();
        p_function();
        cycleSum += __rdtsc() - startCycles;
    }

    return cycleSum / p_iterations;
}

int main()
{
    const size_t iterations = 100000;

    while (true)
    {
        std::cout << "Stack():        \t" << Benchmark(Stack, iterations)           <<  "\tcycles\n";
        std::cout << "Pool():         \t" << Benchmark(Pool, iterations)            <<  "\tcycles\n";
        std::cout << "FastPool():     \t" << Benchmark(FastPool, iterations)        <<  "\tcycles\n";
        std::cout << "FastHeapPool(): \t" << Benchmark(FastHeapPool, iterations)    <<  "\tcycles\n";

        std::cin.get();

        system("CLS");
    }

    return 0;
}

这 4 个测试是:

  • 堆栈(经典方式)
  • 池(在栈上预分配一个字节池)
  • FastPool(在没有类抽象,没有方法调用的情况下在堆栈上预分配一个字节池)
  • FastHeapPool(在堆上预先分配一个字节池,没有类抽象,没有方法调用)

这是使用 C++17 的 MSVC v142 的结果:

调试
Debug Benchmark

发布
Release Benchmark

嗯...不是我所期望的!

  • FastPool 的出现等同于经典方式。这意味着 6 次分配与 1 次大分配没有太大区别。
  • 简单的池(使用 BytePool 类)非常慢,我猜这是由于方法调用,它似乎在 Release模式下进行了优化。
  • FastHeapPool 是一场灾难,即使在 Release模式下,堆分配和访问似乎也很慢(这是我所期望的)

那么现在,我的问题是:

是否有任何方法可以击败经典方法(堆栈上的 6 次分配),以及为什么分配 6 倍 int 的大小等于分配一次 6 int 的大小

我只谈内存,不谈操作优化

最佳答案

您的测试存在严重缺陷。 Stack()、Pool() 和 FastPool() 方法将归结为 NOP(它们不会任何事情!!)。然而,new/delete 可能会产生副作用,因此这会导致发布性能差异。现在,您可能需要了解堆栈分配的实际作用了!如果在方法中使用堆栈分配的变量,它很可能是一个寄存器(除非它是具有副作用的非 pod 类型),并且无论你试图创建什么疯狂的概念来模仿内存,都将只是命令由于延迟、缓存未命中等原因,速度较慢。

过去,我们使用 register 关键字来区分堆栈分配的 var 和寄存器。不再了,因为它基本上没有意义。如今,堆栈分配仅在您用完寄存器时发生,并且您需要将寄存器值换出到堆栈空间。

关于c++ - 我们能打败编译器吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56963055/

相关文章:

linux - anon-rss 和 total-vm 是什么意思

iis - 成功加载到内存后,创建对象突然开始失败

c++ - 用 C++ 重写 grep

c++ - 使用 GNU make 创建多个预编译头文件

c++ - 视觉 C++ : creating a dll using libcurl

memory - 解释 ptxas 的详细输出,第一部分

c++ - unique_ptr 矩阵

memory - 在cuda中使用静态分配内存时的全局设备内存大小限制

c++ - 数据访问器类型的常量正确性 - 更好的解决方案?

c++ - 可以打开头文件,还是找不到文件