C++ 阅读时间 3 分钟

用 Folly 提升 C++ 性能

分享本文
Boosting C++ Performance with Folly

由 Facebook 开发的Folly库是一组可复用的 C++ 库组件,旨在补充 C++ 标准库并提升 C++ 应用的性能。Folly 专注于效率,提供高度优化的组件,在性能关键型应用中尤为有用。以下是该库的一些关键的性能导向特性:

1.高效的数据结构

Folly 提供了若干为性能优化的数据结构:

  • folly::small_vector:一种优化的类 vector 容器,包含用于内联存储的小缓冲区。这减少了小尺寸场景下的堆分配,改善了缓存局部性和性能。
  #include <folly/small_vector.h>

  void example() {
      folly::small_vector<int, 5> vec;
      for (int i = 0; i < 10; ++i) {
          vec.push_back(i);
      }
  }
  • folly::F14哈希表:高效的哈希表实现(F14ValueMap、F14NodeMap、F14VectorMap及其对应的 set 版本)。与传统哈希表相比,这些哈希表提供更好的插入和查找性能,以及更高效的内存使用。
  #include <folly/container/F14Map.h>

  void example() {
      folly::F14FastMap<int, std::string> map;
      map[1] = "one";
      map[2] = "two";
  }
  • folly::AtomicHashMap:一种高度并发的哈希映射,专为需要快速、无锁操作的场景设计。
  #include <folly/AtomicHashMap.h>

  void example() {
      folly::AtomicHashMap<int, std::string> map(100);
      map.insert(1, "one");
      map.insert(2, "two");
  }

2.并发与并行

Folly 提供先进的并发原语和实用工具,提升多线程环境中的性能:

  • folly::Future和folly::Promise:它们提供了一种处理异步计算和回调的便捷方式,实现高效且可扩展的并发。
  #include <folly/futures/Future.h>
  #include <iostream>

  void example() {
      folly::Promise<int> promise;
      auto future = promise.getFuture();

      future.thenValue([](int value) {
          std::cout << "Received: " << value << std::endl;
      });

      promise.setValue(42);
  }
  • folly::CPUThreadPoolExecutor:一种针对 CPU 密集型任务优化的线程池实现,提供可配置的并发级别和高效的任务调度。
  #include <folly/executors/CPUThreadPoolExecutor.h>
  #include <folly/futures/Future.h>
  #include <iostream>

  void example() {
      folly::CPUThreadPoolExecutor executor(4); // 4 threads
      auto future = folly::makeFuture()
                      .via(&executor)
                      .thenValue([](auto) {
                          std::cout << "Task executed in thread pool." << std::endl;
                          return 42;
                      });
      future.wait();
  }

3.内存管理

Folly 包含优化内存管理的实用工具和分配器,可减少碎片并提升分配与释放性能:

  • folly::Arena:一种高效的内存分配器,专为频繁发生大量小块分配与释放的场景设计,可最小化开销并改善缓存局部性。
  #include <folly/memory/Arena.h>

  void example() {
      folly::SysArena arena;
      int* p = static_cast<int*>(arena.allocate(sizeof(int)));
      *p = 42;
  }

4.字符串操作

Folly 提供若干优化的字符串工具,性能优于其标准库对应物:

  • folly::fbstring:一种高度优化的字符串实现,在许多场景下的性能优于std::string,特别是在小字符串优化和更低内存开销方面。
  #include <folly/FBString.h>

  void example() {
      folly::fbstring str = "Hello, Folly!";
      std::cout << str << std::endl;
  }

5.序列化与反序列化

Folly 提供高效的序列化和反序列化工具:

  • folly::io::Cursor和folly::io::QueueAppender:这些类提供了对字节流进行高效读写的机制,这在高性能网络应用中至关重要。
  #include <folly/io/IOBuf.h>
  #include <folly/io/Cursor.h>

  void example() {
      auto buf = folly::IOBuf::create(100);
      folly::io::QueueAppender appender(buf.get(), 0);
      appender.writeBE<uint32_t>(42);

      folly::io::Cursor cursor(buf.get());
      uint32_t value = cursor.readBE<uint32_t>();
      std::cout << value << std::endl;
  }

6.基准测试与测试

Folly 包含基准测试和测试工具,帮助开发者有效地衡量和优化性能:

  • folly::Benchmark:一个轻量级基准测试框架,用于衡量代码片段的性能。
  #include <folly/Benchmark.h>

  void example() {
      folly::runBenchmarks();
  }

通过利用 Folly 库中的这些组件和实用工具,C++ 开发者可以编写高性能、可扩展且可维护的应用。优化的数据结构、高效的内存管理、先进的并发原语以及强大的字符串操作工具相结合,使 Folly 成为性能关键型软件开发的宝贵资产。

分享本文