Convert Figma logo to code with AI

facebook logofolly

An open-source C++ library developed and used at Facebook.

28,292
5,550
28,292
314

Top Related Projects

10,351

Apache Thrift

65,463

Protocol Buffers - Google's data interchange format

FlatBuffers: Memory Efficient Serialization Library

20,408

A modern formatting library

23,832

Fast C++ logging library.

Quick Overview

Folly is an open-source C++ library developed by Facebook that provides a collection of core components and utilities. It aims to improve productivity and performance in C++ development by offering optimized implementations of common data structures, algorithms, and utility functions.

Pros

  • High performance: Folly is designed for efficiency and optimized for modern hardware
  • Extensive functionality: Offers a wide range of utilities, from string manipulation to concurrency primitives
  • Battle-tested: Used extensively in Facebook's production systems
  • Regular updates: Actively maintained with frequent improvements and bug fixes

Cons

  • Steep learning curve: Due to its extensive API and advanced features
  • Large codebase: Can be overwhelming for smaller projects
  • Dependencies: Requires several external libraries, which may complicate integration
  • Primarily focused on Linux and macOS: Windows support is limited

Code Examples

  1. Using folly::Optional:
#include <folly/Optional.h>

folly::Optional<int> maybeGetValue(bool condition) {
    if (condition) {
        return 42;
    }
    return folly::none;
}

auto result = maybeGetValue(true);
if (result.has_value()) {
    std::cout << "Value: " << *result << std::endl;
}
  1. Using folly::Future:
#include <folly/futures/Future.h>

folly::Future<int> asyncComputation() {
    return folly::makeFuture(42)
        .then([](int value) {
            return value * 2;
        });
}

asyncComputation()
    .then([](int result) {
        std::cout << "Result: " << result << std::endl;
    });
  1. Using folly::FBString:
#include <folly/FBString.h>

folly::fbstring str1 = "Hello";
folly::fbstring str2 = " World";
folly::fbstring result = str1 + str2;

std::cout << result << std::endl;

Getting Started

To use Folly in your project, follow these steps:

  1. Install dependencies (on Ubuntu):

    sudo apt-get install g++ cmake libboost-all-dev libevent-dev libdouble-conversion-dev libgoogle-glog-dev libgflags-dev libiberty-dev liblz4-dev liblzma-dev libsnappy-dev make zlib1g-dev binutils-dev libjemalloc-dev libssl-dev pkg-config
    
  2. Clone and build Folly:

    git clone https://github.com/facebook/folly.git
    cd folly
    mkdir _build && cd _build
    cmake ..
    make -j $(nproc)
    sudo make install
    
  3. In your project's CMakeLists.txt, add:

    find_package(folly REQUIRED)
    target_link_libraries(your_target PRIVATE Folly::folly)
    

Competitor Comparisons

10,351

Apache Thrift

Pros of Thrift

  • Cross-language support: Thrift supports multiple programming languages, making it ideal for heterogeneous systems
  • Built-in RPC framework: Provides a complete solution for remote procedure calls
  • Compact binary protocol: Efficient data serialization for network communication

Cons of Thrift

  • Steeper learning curve: Requires understanding of IDL and generated code
  • Less active development: Fewer updates and contributions compared to Folly
  • Limited to specific use cases: Primarily focused on RPC and serialization

Code Comparison

Thrift IDL example:

struct User {
  1: i32 id
  2: string name
  3: string email
}

service UserService {
  User getUser(1: i32 id)
}

Folly example (C++):

#include <folly/dynamic.h>

folly::dynamic user = folly::dynamic::object
  ("id", 1)
  ("name", "John Doe")
  ("email", "john@example.com");

While Thrift focuses on defining services and data structures for RPC, Folly provides a more general-purpose library with utilities like dynamic typing. Thrift generates code for multiple languages, whereas Folly is primarily C++-oriented. Thrift is better suited for building distributed systems with cross-language communication, while Folly offers a broader set of utilities for C++ development within the Facebook ecosystem.

65,463

Protocol Buffers - Google's data interchange format

Pros of Protocol Buffers

  • Language-agnostic serialization format, supporting multiple programming languages
  • Efficient binary encoding, resulting in smaller message sizes
  • Strong typing and schema evolution capabilities

Cons of Protocol Buffers

  • Limited to structured data; not suitable for unstructured or dynamic data
  • Requires compilation step for schema changes
  • Less human-readable compared to text-based formats like JSON

Code Comparison

Protocol Buffers:

message Person {
  string name = 1;
  int32 age = 2;
  repeated string hobbies = 3;
}

Folly (using dynamic):

folly::dynamic person = folly::dynamic::object
  ("name", "John Doe")
  ("age", 30)
  ("hobbies", folly::dynamic::array("reading", "cycling"));

Key Differences

  • Folly is a broader C++ library with various utilities, while Protocol Buffers focuses on data serialization
  • Protocol Buffers offers cross-language support, whereas Folly is primarily C++-centric
  • Folly provides more flexibility for dynamic data structures, while Protocol Buffers enforces a strict schema

Use Cases

  • Choose Protocol Buffers for cross-language, schema-based serialization needs
  • Opt for Folly when working with C++ projects requiring diverse utility functions and flexible data structures

FlatBuffers: Memory Efficient Serialization Library

Pros of FlatBuffers

  • Designed specifically for serialization, offering better performance in this area
  • Supports schema evolution, allowing for easier updates to data structures
  • Provides cross-platform support and language interoperability

Cons of FlatBuffers

  • More limited in scope, focusing primarily on serialization
  • Less comprehensive feature set compared to Folly's wide range of utilities
  • Steeper learning curve for developers new to the FlatBuffers format

Code Comparison

Folly example (error handling):

folly::Try<int> result = folly::makeTryWith([]() {
  // Some operation that might throw
  return 42;
});

FlatBuffers example (serialization):

flatbuffers::FlatBufferBuilder builder;
auto name = builder.CreateString("John Doe");
auto person = CreatePerson(builder, name, 30);
builder.Finish(person);

Summary

While FlatBuffers excels in efficient serialization and cross-platform support, Folly offers a broader range of utilities for C++ development. FlatBuffers is more specialized, whereas Folly provides a comprehensive toolkit for various programming tasks. The choice between them depends on specific project requirements and the scope of functionality needed.

20,408

A modern formatting library

Pros of fmt

  • Lightweight and focused solely on formatting, making it easier to integrate
  • Cross-platform support, including Windows, macOS, and Linux
  • Extensive documentation and examples

Cons of fmt

  • Limited to formatting functionality, lacking the broader utility of Folly
  • Smaller community and ecosystem compared to Folly

Code Comparison

fmt:

#include <fmt/core.h>

std::string s = fmt::format("The answer is {}.", 42);
fmt::print("Hello, {}!", "world");

Folly:

#include <folly/Format.h>

std::string s = folly::format("The answer is {}.", 42).str();
folly::format(&std::cout, "Hello, {}!", "world");

Both libraries offer similar syntax for formatting, but Folly provides additional utilities beyond formatting. fmt focuses on being a lightweight, standalone formatting library, while Folly is a more comprehensive collection of C++ components.

fmt is ideal for projects that require a simple, efficient formatting solution, whereas Folly is better suited for larger projects that can benefit from its extensive set of utilities and optimizations.

23,832

Fast C++ logging library.

Pros of spdlog

  • Lightweight and header-only library, making it easy to integrate into projects
  • Faster performance for logging operations, especially in multi-threaded scenarios
  • More extensive formatting options and support for custom formatters

Cons of spdlog

  • Less comprehensive feature set compared to Folly's broader utility library
  • Limited to logging functionality, while Folly offers a wide range of utilities
  • Smaller community and ecosystem compared to Folly's backing by Facebook

Code Comparison

spdlog:

#include "spdlog/spdlog.h"

int main() {
    spdlog::info("Welcome to spdlog!");
    spdlog::error("Some error message with arg: {}", 1);
}

Folly:

#include <folly/logging/xlog.h>

int main() {
    XLOG(INFO) << "Welcome to Folly logging!";
    XLOG(ERR) << "Some error message with arg: " << 1;
}

Both libraries offer simple and intuitive logging interfaces, but spdlog uses a more modern C++ approach with variadic templates for formatting, while Folly uses stream-like syntax. spdlog's API is generally considered more user-friendly and flexible for formatting options.

Convert Figma logo designs to code with AI

Visual Copilot

Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.

Try Visual Copilot

README

Folly: Facebook Open-source Library

Support Ukraine - Help Provide Humanitarian Aid to Ukraine.

What is folly?

Logo Folly

Folly (acronymed loosely after Facebook Open Source Library) is a library of C++17 components designed with practicality and efficiency in mind. Folly contains a variety of core library components used extensively at Facebook. In particular, it's often a dependency of Facebook's other open source C++ efforts and place where those projects can share code.

It complements (as opposed to competing against) offerings such as Boost and of course std. In fact, we embark on defining our own component only when something we need is either not available, or does not meet the needed performance profile. We endeavor to remove things from folly if or when std or Boost obsoletes them.

Performance concerns permeate much of Folly, sometimes leading to designs that are more idiosyncratic than they would otherwise be (see e.g. PackedSyncPtr.h, SmallLocks.h). Good performance at large scale is a unifying theme in all of Folly.

Check it out in the intro video

Explain Like I’m 5: Folly

Logical Design

Folly is a collection of relatively independent components, some as simple as a few symbols. There is no restriction on internal dependencies, meaning that a given folly module may use any other folly components.

All symbols are defined in the top-level namespace folly, except of course macros. Macro names are ALL_UPPERCASE and should be prefixed with FOLLY_. Namespace folly defines other internal namespaces such as internal or detail. User code should not depend on symbols in those namespaces.

Folly has an experimental directory as well. This designation connotes primarily that we feel the API may change heavily over time. This code, typically, is still in heavy use and is well tested.

Physical Design

At the top level Folly uses the classic "stuttering" scheme folly/folly used by Boost and others. The first directory serves as an installation root of the library (with possible versioning a la folly-1.0/), and the second is to distinguish the library when including files, e.g. #include <folly/FBString.h>.

The directory structure is flat (mimicking the namespace structure), i.e. we don't have an elaborate directory hierarchy (it is possible this will change in future versions). The subdirectory experimental contains files that are used inside folly and possibly at Facebook but not considered stable enough for client use. Your code should not use files in folly/experimental lest it may break when you update Folly.

The folly/folly/test subdirectory includes the unittests for all components, usually named ComponentXyzTest.cpp for each ComponentXyz.*. The folly/folly/docs directory contains documentation.

What's in it?

Because of folly's fairly flat structure, the best way to see what's in it is to look at the headers in top level folly/ directory. You can also check the docs folder for documentation, starting with the overview.

Folly is published on GitHub at https://github.com/facebook/folly.

Build Notes

Because folly does not provide any ABI compatibility guarantees from commit to commit, we generally recommend building folly as a static library.

folly supports gcc (5.1+), clang, or MSVC. It should run on Linux (x86-32, x86-64, and ARM), iOS, macOS, and Windows (x86-64). The CMake build is only tested on some of these platforms; at a minimum, we aim to support macOS and Linux (on the latest Ubuntu LTS release or newer.)

getdeps.py

This script is used by many of Meta's OSS tools. It will download and build all of the necessary dependencies first, and will then invoke cmake etc to build folly. This will help ensure that you build with relevant versions of all of the dependent libraries, taking into account what versions are installed locally on your system.

It's written in python so you'll need python3.6 or later on your PATH. It works on Linux, macOS and Windows.

The settings for folly's cmake build are held in its getdeps manifest build/fbcode_builder/manifests/folly, which you can edit locally if desired.

Dependencies

If on Linux or MacOS (with homebrew installed) you can install system dependencies to save building them:

# Clone the repo
git clone https://github.com/facebook/folly
# Install dependencies
cd folly
sudo ./build/fbcode_builder/getdeps.py install-system-deps --recursive

If you'd like to see the packages before installing them:

./build/fbcode_builder/getdeps.py install-system-deps --dry-run --recursive

On other platforms or if on Linux and without system dependencies getdeps.py will mostly download and build them for you during the build step.

Some of the dependencies getdeps.py uses and installs are:

  • a version of boost compiled with C++14 support.
  • googletest is required to build and run folly's tests.

Build

This script will download and build all of the necessary dependencies first, and will then invoke cmake etc to build folly. This will help ensure that you build with relevant versions of all of the dependent libraries, taking into account what versions are installed locally on your system.

getdeps.py currently requires python 3.6+ to be on your path.

getdeps.py will invoke cmake etc.

# Clone the repo
git clone https://github.com/facebook/folly
cd folly
# Build, using system dependencies if available
python3 ./build/fbcode_builder/getdeps.py --allow-system-packages build

It puts output in its scratch area:

  • installed/folly/lib/libfolly.a: Library

You can also specify a --scratch-path argument to control the location of the scratch directory used for the build. You can find the default scratch install location from logs or with python3 ./build/fbcode_builder/getdeps.py show-inst-dir.

There are also --install-dir and --install-prefix arguments to provide some more fine-grained control of the installation directories. However, given that folly provides no compatibility guarantees between commits we generally recommend building and installing the libraries to a temporary location, and then pointing your project's build at this temporary location, rather than installing folly in the traditional system installation directories. e.g., if you are building with CMake you can use the CMAKE_PREFIX_PATH variable to allow CMake to find folly in this temporary installation directory when building your project.

If you want to invoke cmake again to iterate, there is a helpful run_cmake.py script output in the scratch build directory. You can find the scratch build directory from logs or with python3 ./build/fbcode_builder/getdeps.py show-build-dir.

Run tests

By default getdeps.py will build the tests for folly. To run them:

cd folly
python3 ./build/fbcode_builder/getdeps.py --allow-system-packages test

build.sh/build.bat wrapper

build.sh can be used on Linux and MacOS, on Windows use the build.bat script instead. Its a wrapper around getdeps.py.

Build with cmake directly

If you don't want to let getdeps invoke cmake for you then by default, building the tests is disabled as part of the CMake all target. To build the tests, specify -DBUILD_TESTS=ON to CMake at configure time.

NB if you want to invoke cmake again to iterate on a getdeps.py build, there is a helpful run_cmake.py script output in the scratch-path build directory. You can find the scratch build directory from logs or with python3 ./build/fbcode_builder/getdeps.py show-build-dir.

Running tests with ctests also works if you cd to the build dir, e.g. (cd $(python3 ./build/fbcode_builder/getdeps.py show-build-dir) && ctest)

Finding dependencies in non-default locations

If you have boost, gtest, or other dependencies installed in a non-default location, you can use the CMAKE_INCLUDE_PATH and CMAKE_LIBRARY_PATH variables to make CMAKE look also look for header files and libraries in non-standard locations. For example, to also search the directories /alt/include/path1 and /alt/include/path2 for header files and the directories /alt/lib/path1 and /alt/lib/path2 for libraries, you can invoke cmake as follows:

cmake \
  -DCMAKE_INCLUDE_PATH=/alt/include/path1:/alt/include/path2 \
  -DCMAKE_LIBRARY_PATH=/alt/lib/path1:/alt/lib/path2 ...

Ubuntu LTS, CentOS Stream, Fedora

Use the getdeps.py approach above. We test in CI on Ubuntu LTS, and occasionally on other distros.

If you find the set of system packages is not quite right for your chosen distro, you can specify distro version specific overrides in the dependency manifests (e.g. https://github.com/facebook/folly/blob/main/build/fbcode_builder/manifests/boost ). You could probably make it work on most recent Ubuntu/Debian or Fedora/Redhat derived distributions.

At time of writing (Dec 2021) there is a build break on GCC 11.x based systems in lang_badge_test. If you don't need badge functionality you can work around by commenting it out from CMakeLists.txt (unfortunately fbthrift does need it)

Windows (Vcpkg)

Note that many tests are disabled for folly Windows builds, you can see them in the log from the cmake configure step, or by looking for WINDOWS_DISABLED in CMakeLists.txt

That said, getdeps.py builds work on Windows and are tested in CI.

If you prefer, you can try Vcpkg. folly is available in Vcpkg and releases may be built via vcpkg install folly:x64-windows.

You may also use vcpkg install folly:x64-windows --head to build against main.

macOS

getdeps.py builds work on macOS and are tested in CI, however if you prefer, you can try one of the macOS package managers

Homebrew

folly is available as a Formula and releases may be built via brew install folly.

You may also use folly/build/bootstrap-osx-homebrew.sh to build against main:

  ./folly/build/bootstrap-osx-homebrew.sh

This will create a build directory _build in the top-level.

MacPorts

Install the required packages from MacPorts:

  sudo port install \
    boost \
    cmake \
    gflags \
    git \
    google-glog \
    libevent \
    libtool \
    lz4 \
    lzma \
    openssl \
    snappy \
    xz \
    zlib

Download and install double-conversion:

  git clone https://github.com/google/double-conversion.git
  cd double-conversion
  cmake -DBUILD_SHARED_LIBS=ON .
  make
  sudo make install

Download and install folly with the parameters listed below:

  git clone https://github.com/facebook/folly.git
  cd folly
  mkdir _build
  cd _build
  cmake ..
  make
  sudo make install