Facebook is built on open source from top to bottom, and could not exist without it. As engineers here, we use, contribute to, and release a lot of open source software, including pieces of our core infrastructure such as HipHop and Thrift.

But in our C++ services code, one clear bottleneck to releasing more work has been that any open sourced project needed to break dependencies on unreleased internal library code. To help solve that problem, today we open sourced an initial release of Folly, a collection of reusable C++ library artifacts developed and used at Facebook. This announcement was made at our C++ conference at Facebook in Menlo Park, CA.

Our primary aim with this ‘foolishness’ is to create a solution that allows us to continue open sourcing parts of our stack without resorting to reinventing some of our internal wheels. And because Folly’s components typically perform significantly faster than counterparts available elsewhere, are easy to use, and complement existing libraries, we think C++ developers might find parts of this library interesting in their own right.

Folly is fast

The utilities contained in Folly are things we use heavily in production—this is code that runs on thousands of servers doing work on behalf of 900 million users every day. These utilities are loosely connected, but the over-arching theme for all of the components is high performance at scale. Some of them will show a fairly specialized focus, like reducing contention or packing things into small amounts of memory. Others, such as our in-memory JSON manipulation library or our string-formatting library, have a larger scope. But in either case, our motivation was to build components that were faster and more efficient than what we previously used.

As an example, consider AtomicHashMap. At Facebook, many of our services have large, concurrent in-memory indexes where lookups and insertions are common, but deletion is a rare (or unnecessary) operation. Often the keys in these indexes are 32 or 64 bit integers or can be hashed or converted to such integers. With these constraints, we’ve very efficiently addressed these types of workloads with a hashtable class that is wait-free for lookups, and has fine-grained locking to protect insertions. This component has nearly eliminated contention-related performance bottlenecks in several of our search services.

Folly is easy to use

Performance is highly important, but since we have a couple hundred engineers working primarily in C++, our code also needs to be fast and reasonably easy to work with. In light of this, we make heavy use of modern C++, which has allowed us to retain a lot of programming convenience without spending much of our hardware resources on it. We believe some of these libraries even offer a step forward in terms of programmer convenience.

In particular, our string utilities are easier to use, faster, and safer than a number of existing options. Using folly::to instead of boost::lexical_cast wastes fewer CPU cycles and makes code more terse. Similarly, folly::format is less clumsy than iostream manipulators, and, unlike other options available to C++, does not sacrifice either type safety or performance. Many services spend significant portions of their CPU time manipulating strings, and programmers also tend to spend a non-trivial fraction of their time writing string manipulation code. We think these tools optimize for both processes simultaneously.

Folly is complementary

Practicality pervades Facebook engineering, and we have a low tolerance for “Not Invented Here” syndrome (after all, the company is built on open source software). So, you’ll find Folly complements some existing high quality C++ libraries, such as Boost or the Standard Library, both of which we use heavily. The cases where you’ll see Folly alternatives to an existing library are motivated by real-world drawbacks we ran into after trying existing solutions (generally performance related), or in some cases just a slightly more modern take on an old story (yes, we have a new version of the venerable ScopeGuard).

This initial release is definitely in the spirit of “release early, release often.” We will continue to update the open source tree as new Folly components are added, so you can expect to see more of this on github in the future. For an overview of the specific components in this release and to give us your feedback, check out the README on github.

Leave a Reply

To help personalize content, tailor and measure ads and provide a safer experience, we use cookies. By clicking or navigating the site, you agree to allow our collection of information on and off Facebook through cookies. Learn more, including about available controls: Cookie Policy