{"id":1751,"date":"2024-06-21T07:44:41","date_gmt":"2024-06-21T07:44:41","guid":{"rendered":"https:\/\/cppdepend.com\/blog\/?p=1751"},"modified":"2024-07-09T10:50:36","modified_gmt":"2024-07-09T10:50:36","slug":"boosting-c-performance-with-folly","status":"publish","type":"post","link":"https:\/\/cppdepend.com\/blog\/boosting-c-performance-with-folly\/","title":{"rendered":"Boosting C++ Performance with Folly"},"content":{"rendered":"\n<p>The <a href=\"https:\/\/github.com\/facebook\/folly\">Folly<\/a> library, developed by Facebook, is a collection of reusable C++ library components designed to complement the C++ standard library and boost the performance of C++ applications. Folly focuses on efficiency, providing highly optimized components that are particularly useful in performance-critical applications. Here are some key aspects of performance in the Folly library:<\/p>\n\n\n\n<!--more-->\n\n\n\n<h3 class=\"wp-block-heading\" id=\"h-1-efficient-data-structures\">1. <strong>Efficient Data Structures<\/strong><\/h3>\n\n\n\n<p>Folly provides several data structures optimized for performance:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong><code>folly::small_vector<\/code><\/strong>: An optimized vector-like container that includes a small buffer for inline storage. This reduces heap allocations for small sizes, improving cache locality and performance.<\/li>\n<\/ul>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;showPanel&quot;:false,&quot;languageLabel&quot;:&quot;language&quot;,&quot;fullScreenButton&quot;:true,&quot;copyButton&quot;:true,&quot;mode&quot;:&quot;clike&quot;,&quot;mime&quot;:&quot;text\/x-c++src&quot;,&quot;theme&quot;:&quot;material&quot;,&quot;lineNumbers&quot;:false,&quot;styleActiveLine&quot;:false,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:true,&quot;fileName&quot;:&quot;C++&quot;,&quot;language&quot;:&quot;C++&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;cpp&quot;}\">  #include &lt;folly\/small_vector.h&gt;\n\n  void example() {\n      folly::small_vector&lt;int, 5&gt; vec;\n      for (int i = 0; i &lt; 10; ++i) {\n          vec.push_back(i);\n      }\n  }<\/pre><\/div>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong><code>folly::F14<\/code> Hash Tables<\/strong>: Highly efficient hash table implementations (<code>F14ValueMap<\/code>, <code>F14NodeMap<\/code>, <code>F14VectorMap<\/code>, and their set equivalents). These hash tables offer superior performance in terms of insertion, lookup, and memory usage compared to traditional hash tables.<\/li>\n<\/ul>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;showPanel&quot;:false,&quot;languageLabel&quot;:&quot;language&quot;,&quot;fullScreenButton&quot;:true,&quot;copyButton&quot;:true,&quot;mode&quot;:&quot;clike&quot;,&quot;mime&quot;:&quot;text\/x-c++src&quot;,&quot;theme&quot;:&quot;material&quot;,&quot;lineNumbers&quot;:false,&quot;styleActiveLine&quot;:false,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:true,&quot;fileName&quot;:&quot;C++&quot;,&quot;language&quot;:&quot;C++&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;cpp&quot;}\">  #include &lt;folly\/container\/F14Map.h&gt;\n\n  void example() {\n      folly::F14FastMap&lt;int, std::string&gt; map;\n      map[1] = &quot;one&quot;;\n      map[2] = &quot;two&quot;;\n  }<\/pre><\/div>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong><code>folly::AtomicHashMap<\/code><\/strong>: A highly concurrent hash map designed for scenarios requiring fast, lock-free operations.<\/li>\n<\/ul>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;showPanel&quot;:false,&quot;languageLabel&quot;:&quot;language&quot;,&quot;fullScreenButton&quot;:true,&quot;copyButton&quot;:true,&quot;mode&quot;:&quot;clike&quot;,&quot;mime&quot;:&quot;text\/x-c++src&quot;,&quot;theme&quot;:&quot;material&quot;,&quot;lineNumbers&quot;:false,&quot;styleActiveLine&quot;:false,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:true,&quot;fileName&quot;:&quot;C++&quot;,&quot;language&quot;:&quot;C++&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;cpp&quot;}\">  #include &lt;folly\/AtomicHashMap.h&gt;\n\n  void example() {\n      folly::AtomicHashMap&lt;int, std::string&gt; map(100);\n      map.insert(1, &quot;one&quot;);\n      map.insert(2, &quot;two&quot;);\n  }<\/pre><\/div>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"h-2-concurrency-and-parallelism\">2. <strong>Concurrency and Parallelism<\/strong><\/h3>\n\n\n\n<p>Folly offers advanced concurrency primitives and utilities that enhance performance in multithreaded environments:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong><code>folly::Future<\/code> and <code>folly::Promise<\/code><\/strong>: These provide a way to handle asynchronous computations and callbacks, enabling efficient and scalable concurrency.<\/li>\n<\/ul>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;showPanel&quot;:false,&quot;languageLabel&quot;:&quot;language&quot;,&quot;fullScreenButton&quot;:true,&quot;copyButton&quot;:true,&quot;mode&quot;:&quot;clike&quot;,&quot;mime&quot;:&quot;text\/x-c++src&quot;,&quot;theme&quot;:&quot;material&quot;,&quot;lineNumbers&quot;:false,&quot;styleActiveLine&quot;:false,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:true,&quot;fileName&quot;:&quot;C++&quot;,&quot;language&quot;:&quot;C++&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;cpp&quot;}\">  #include &lt;folly\/futures\/Future.h&gt;\n  #include &lt;iostream&gt;\n\n  void example() {\n      folly::Promise&lt;int&gt; promise;\n      auto future = promise.getFuture();\n\n      future.thenValue([](int value) {\n          std::cout &lt;&lt; &quot;Received: &quot; &lt;&lt; value &lt;&lt; std::endl;\n      });\n\n      promise.setValue(42);\n  }<\/pre><\/div>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong><code>folly::CPUThreadPoolExecutor<\/code><\/strong>: A thread pool implementation optimized for CPU-bound tasks, offering configurable concurrency levels and efficient task scheduling.<\/li>\n<\/ul>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;showPanel&quot;:false,&quot;languageLabel&quot;:&quot;language&quot;,&quot;fullScreenButton&quot;:true,&quot;copyButton&quot;:true,&quot;mode&quot;:&quot;clike&quot;,&quot;mime&quot;:&quot;text\/x-c++src&quot;,&quot;theme&quot;:&quot;material&quot;,&quot;lineNumbers&quot;:false,&quot;styleActiveLine&quot;:false,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:true,&quot;fileName&quot;:&quot;C++&quot;,&quot;language&quot;:&quot;C++&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;cpp&quot;}\">  #include &lt;folly\/executors\/CPUThreadPoolExecutor.h&gt;\n  #include &lt;folly\/futures\/Future.h&gt;\n  #include &lt;iostream&gt;\n\n  void example() {\n      folly::CPUThreadPoolExecutor executor(4); \/\/ 4 threads\n      auto future = folly::makeFuture()\n                      .via(&amp;executor)\n                      .thenValue([](auto) {\n                          std::cout &lt;&lt; &quot;Task executed in thread pool.&quot; &lt;&lt; std::endl;\n                          return 42;\n                      });\n      future.wait();\n  }<\/pre><\/div>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"h-3-memory-management\">3. <strong>Memory Management<\/strong><\/h3>\n\n\n\n<p>Folly includes utilities and allocators that optimize memory management, reducing fragmentation and improving allocation\/deallocation performance:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong><code>folly::Arena<\/code><\/strong>: An efficient memory allocator designed for scenarios where many small allocations and deallocations occur, minimizing overhead and improving cache locality.<\/li>\n<\/ul>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;showPanel&quot;:false,&quot;languageLabel&quot;:&quot;language&quot;,&quot;fullScreenButton&quot;:true,&quot;copyButton&quot;:true,&quot;mode&quot;:&quot;clike&quot;,&quot;mime&quot;:&quot;text\/x-c++src&quot;,&quot;theme&quot;:&quot;material&quot;,&quot;lineNumbers&quot;:false,&quot;styleActiveLine&quot;:false,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:true,&quot;fileName&quot;:&quot;C++&quot;,&quot;language&quot;:&quot;C++&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;cpp&quot;}\">  #include &lt;folly\/memory\/Arena.h&gt;\n\n  void example() {\n      folly::SysArena arena;\n      int* p = static_cast&lt;int*&gt;(arena.allocate(sizeof(int)));\n      *p = 42;\n  }<\/pre><\/div>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"h-4-string-manipulation\">4. <strong>String Manipulation<\/strong><\/h3>\n\n\n\n<p>Folly offers several optimized string utilities that outperform their standard library counterparts:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong><code>folly::fbstring<\/code><\/strong>: A highly optimized string implementation designed to outperform <code>std::string<\/code> in many scenarios, particularly in terms of small string optimization and reduced memory overhead.<\/li>\n<\/ul>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;showPanel&quot;:false,&quot;languageLabel&quot;:&quot;language&quot;,&quot;fullScreenButton&quot;:true,&quot;copyButton&quot;:true,&quot;mode&quot;:&quot;clike&quot;,&quot;mime&quot;:&quot;text\/x-c++src&quot;,&quot;theme&quot;:&quot;material&quot;,&quot;lineNumbers&quot;:false,&quot;styleActiveLine&quot;:false,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:true,&quot;fileName&quot;:&quot;C++&quot;,&quot;language&quot;:&quot;C++&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;cpp&quot;}\">  #include &lt;folly\/FBString.h&gt;\n\n  void example() {\n      folly::fbstring str = &quot;Hello, Folly!&quot;;\n      std::cout &lt;&lt; str &lt;&lt; std::endl;\n  }<\/pre><\/div>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"h-5-serialization-and-deserialization\">5. <strong>Serialization and Deserialization<\/strong><\/h3>\n\n\n\n<p>Folly provides efficient serialization and deserialization utilities:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong><code>folly::io::Cursor<\/code><\/strong> and <strong><code>folly::io::QueueAppender<\/code><\/strong>: These classes provide efficient mechanisms for reading from and writing to byte streams, which is critical for high-performance network applications.<\/li>\n<\/ul>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;showPanel&quot;:false,&quot;languageLabel&quot;:&quot;language&quot;,&quot;fullScreenButton&quot;:true,&quot;copyButton&quot;:true,&quot;mode&quot;:&quot;clike&quot;,&quot;mime&quot;:&quot;text\/x-c++src&quot;,&quot;theme&quot;:&quot;material&quot;,&quot;lineNumbers&quot;:false,&quot;styleActiveLine&quot;:false,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:true,&quot;fileName&quot;:&quot;C++&quot;,&quot;language&quot;:&quot;C++&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;cpp&quot;}\">  #include &lt;folly\/io\/IOBuf.h&gt;\n  #include &lt;folly\/io\/Cursor.h&gt;\n\n  void example() {\n      auto buf = folly::IOBuf::create(100);\n      folly::io::QueueAppender appender(buf.get(), 0);\n      appender.writeBE&lt;uint32_t&gt;(42);\n\n      folly::io::Cursor cursor(buf.get());\n      uint32_t value = cursor.readBE&lt;uint32_t&gt;();\n      std::cout &lt;&lt; value &lt;&lt; std::endl;\n  }<\/pre><\/div>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"h-6-benchmarking-and-testing\">6. <strong>Benchmarking and Testing<\/strong><\/h3>\n\n\n\n<p>Folly includes utilities for benchmarking and testing, helping developers measure and optimize performance effectively:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong><code>folly::Benchmark<\/code><\/strong>: A lightweight benchmarking framework to measure the performance of code snippets.<\/li>\n<\/ul>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;showPanel&quot;:false,&quot;languageLabel&quot;:&quot;language&quot;,&quot;fullScreenButton&quot;:true,&quot;copyButton&quot;:true,&quot;mode&quot;:&quot;clike&quot;,&quot;mime&quot;:&quot;text\/x-c++src&quot;,&quot;theme&quot;:&quot;material&quot;,&quot;lineNumbers&quot;:false,&quot;styleActiveLine&quot;:false,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:true,&quot;fileName&quot;:&quot;C++&quot;,&quot;language&quot;:&quot;C++&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;cpp&quot;}\">  #include &lt;folly\/Benchmark.h&gt;\n\n  void example() {\n      folly::runBenchmarks();\n  }<\/pre><\/div>\n\n\n\n<p>By leveraging these components and utilities from the Folly library, C++ developers can write high-performance, scalable, and maintainable applications. The combination of optimized data structures, efficient memory management, advanced concurrency primitives, and powerful string manipulation tools makes Folly an invaluable asset for performance-critical software development.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>The Folly library, developed by Facebook, is a collection of reusable C++ library components designed to complement the C++ standard library and boost the performance of C++ applications. Folly focuses on efficiency, providing highly optimized components that are particularly useful in performance-critical applications. Here are some key aspects of performance in the Folly library:<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[763,769,759,767,756,67,211,762,765,139,755,311,758,16,764,15,760,303,76,768,770,717,125,761,766,771,754,757,753,164],"class_list":["post-1751","post","type-post","status-publish","format-standard","hentry","category-uncategorized","tag-arena","tag-asynchronous-programming","tag-atomichashmap","tag-benchmark","tag-benchmarking","tag-c-2","tag-concurrency","tag-cputhreadpoolexecutor","tag-cursor","tag-data-structures","tag-deserialization","tag-efficiency","tag-f14-hash-tables","tag-facebook","tag-fbstring","tag-folly","tag-future","tag-high-performance-computing","tag-memory-management","tag-multithreading","tag-optimized-containers","tag-parallelism","tag-performance","tag-promise","tag-queueappender","tag-reusable-components","tag-serialization","tag-small_vector","tag-string-manipulation","tag-testing"],"_links":{"self":[{"href":"https:\/\/cppdepend.com\/blog\/wp-json\/wp\/v2\/posts\/1751","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/cppdepend.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/cppdepend.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/cppdepend.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/cppdepend.com\/blog\/wp-json\/wp\/v2\/comments?post=1751"}],"version-history":[{"count":3,"href":"https:\/\/cppdepend.com\/blog\/wp-json\/wp\/v2\/posts\/1751\/revisions"}],"predecessor-version":[{"id":1911,"href":"https:\/\/cppdepend.com\/blog\/wp-json\/wp\/v2\/posts\/1751\/revisions\/1911"}],"wp:attachment":[{"href":"https:\/\/cppdepend.com\/blog\/wp-json\/wp\/v2\/media?parent=1751"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/cppdepend.com\/blog\/wp-json\/wp\/v2\/categories?post=1751"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/cppdepend.com\/blog\/wp-json\/wp\/v2\/tags?post=1751"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}