db12cf3c94f33b23a02326671ae9e43db04ee06b
[folly.git] / folly / docs / Overview.md
1 `folly/`
2 ------
3
4 ### Introduction
5
6 Folly (acronymed loosely after Facebook Open Source Library) is a
7 library of C++11 components designed with practicality and efficiency
8 in mind. It complements (as opposed to competing against) offerings
9 such as Boost and of course `std`. In fact, we embark on defining our
10 own component only when something we need is either not available, or
11 does not meet the needed performance profile.
12
13 Performance concerns permeate much of Folly, sometimes leading to
14 designs that are more idiosyncratic than they would otherwise be (see
15 e.g. `PackedSyncPtr.h`, `SmallLocks.h`). Good performance at large
16 scale is a unifying theme in all of Folly.
17
18 ### Logical Design
19
20 Folly is a collection of relatively independent components, some as
21 simple as a few symbols. There is no restriction on internal
22 dependencies, meaning that a given folly module may use any other
23 folly components.
24
25 All symbols are defined in the top-level namespace `folly`, except of
26 course macros. Macro names are ALL_UPPERCASE. Namespace `folly`
27 defines other internal namespaces such as `internal` or `detail`. User
28 code should not depend on symbols in those namespaces.
29
30 ### Physical Design
31
32 At the top level Folly uses the classic "stuttering" scheme
33 `folly/folly` used by Boost and others. The first directory serves as
34 an installation root of the library (with possible versioning a la
35 `folly-1.0/`), and the second is to distinguish the library when
36 including files, e.g. `#include "folly/FBString.h"`.
37
38 The directory structure is flat (mimicking the namespace structure),
39 i.e. we don't have an elaborate directory hierarchy (it is possible
40 this will change in future versions). The subdirectory `experimental`
41 contains files that are used inside folly and possibly at Facebook but
42 not considered stable enough for client use. Your code should not use
43 files in `folly/experimental` lest it may break when you update Folly.
44
45 The `folly/folly/test` subdirectory includes the unittests for all
46 components, usually named `ComponentXyzTest.cpp` for each
47 `ComponentXyz.*`. The `folly/folly/docs` directory contains
48 documentation.
49
50 ### Compatibility
51
52 Currently, `folly` has been tested on gcc 4.6 on 64-bit installations
53 of Fedora 17, Ubuntu 12.04, and Debian wheezy. It might work unmodified
54 on other 64-bit Linux platforms.
55
56 ### Components
57
58 Below is a list of Folly components in alphabetical order, along with
59 a brief description of each.
60
61 #### `Arena.h`, `ThreadCachedArena.h`
62
63 Simple arena for memory allocation: multiple allocations get freed all
64 at once. With threaded version.
65
66 #### [`AtomicHashMap.h`, `AtomicHashArray.h`](AtomicHashMap.md)
67
68 High-performance atomic hash map with almost lock-free operation.
69
70 #### [`Benchmark.h`](Benchmark.md)
71
72 A small framework for benchmarking code. Client code registers
73 benchmarks, optionally with an argument that dictates the scale of the
74 benchmark (iterations, working set size etc). The framework runs
75 benchmarks (subject to a command-line flag) and produces formatted
76 output with timing information.
77
78 #### `Bits.h`
79
80 Various bit manipulation utilities optimized for speed; includes functions
81 that wrap the
82 [ffsl(l)](http://linux.die.net/man/3/ffsll) primitives in a uniform
83 interface.
84
85 #### `ConcurrentSkipList.h`
86
87 An implementation of the structure described in [A Provably Correct
88 Scalable Concurrent Skip
89 List](http://www.cs.tau.ac.il/~shanir/nir-pubs-web/Papers/OPODIS2006-BA.pdf)
90 by Herlihy et al.
91
92 #### [`Conv.h`](Conv.md)
93
94 A variety of data conversion routines (notably to and from string),
95 optimized for speed and safety.
96
97 #### `DiscriminatedPtr.h`
98
99 Similar to `boost::variant`, but restricted to pointers only. Uses the
100 highest-order unused 16 bits in a pointer as discriminator. So
101 `sizeof(DiscriminatedPtr<int, string, Widget>) == sizeof(void*)`.
102
103 #### [`dynamic.h`](Dynamic.md)
104
105 Dynamically-typed object, created with JSON objects in mind.
106
107 #### `Endian.h`
108
109 Endian conversion primitives.
110
111 ####`Escape.h`
112
113 Escapes a string in C style.
114
115 ####`eventfd.h`
116
117 Wrapper around the
118 [`eventfd`](http://www.kernel.org/doc/man-pages/online/pages/man2/eventfd.2.html)
119 system call.
120
121 ####[`FBString.h`](FBString.md)
122
123 A drop-in implementation of `std::string` with a variety of optimizations.
124
125 ####[`FBVector.h`](FBVector.md)
126
127 A mostly drop-in implementation of `std::vector` with a variety of
128 optimizations.
129
130 ####`Foreach.h`
131
132 Pseudo-statements (implemented as macros) for iteration.
133
134 ####[`Format.h`](Format.md)
135
136 Python-style formatting utilities.
137
138 ####[`GroupVarint.h`](GroupVarint.md)
139
140 [Group Varint
141 encoding](http://www.ir.uwaterloo.ca/book/addenda-06-index-compression.html)
142 for 32-bit values.
143
144 ####`Hash.h`
145
146 Various popular hash function implementations.
147
148 ####[`Histogram.h`](Histogram.md)
149
150 A simple class for collecting histogram data.
151
152 ####`IntrusiveList.h`
153
154 Convenience type definitions for using `boost::intrusive_list`.
155
156 ####`json.h`
157
158 JSON serializer and deserializer. Uses `dynamic.h`.
159
160 ####`Likely.h`
161
162 Wrappers around [`__builtin_expect`](http://gcc.gnu.org/onlinedocs/gcc/Other-Builtins.html).
163
164 ####`Malloc.h`
165
166 Memory allocation helpers, particularly when using jemalloc.
167
168 ####`MapUtil.h`
169
170 Helpers for finding items in associative containers (such as
171 `std::map` and `std::unordered_map`).
172
173 ####[`PackedSyncPtr.h`](PackedSyncPtr.md)
174
175 A highly specialized data structure consisting of a pointer, a 1-bit
176 spin lock, and a 15-bit integral, all inside one 64-bit word.
177
178 ####`Preprocessor.h`
179
180 Necessarily evil stuff.
181
182 ####`PrettyPrint.h`
183
184 Pretty-printer for numbers that appends suffixes of unit used: bytes
185 (kb, MB, ...), metric suffixes (k, M, G, ...), and time (s, ms, us,
186 ns, ...).
187
188 ####[`ProducerConsumerQueue.h`](ProducerConsumerQueue.md)
189
190 Lock free single-reader, single-writer queue.
191
192 ####`Random.h`
193
194 Defines only one function---`randomNumberSeed()`.
195
196 ####`Range.h`
197
198 Boost-style range facility and the `StringPiece` specialization.
199
200 ####`RWSpinLock.h`
201
202 Fast and compact reader-writer spin lock.
203
204 ####`ScopeGuard.h`
205
206 C++11 incarnation of the old [ScopeGuard](http://drdobbs.com/184403758) idiom.
207
208 ####[`SmallLocks.h`](SmallLocks.md)
209
210 Very small spin locks (1 byte and 1 bit).
211
212 ####`small_vector.h`
213
214 Vector with the small buffer optimization and an optional embedded
215 `PicoSpinLock`.
216
217 ####`sorted_vector_types.h`
218
219 Collections similar to `std::map` but implemented as sorted vectors.
220
221 ####`StlAllocator.h`
222
223 STL allocator wrapping a simple allocate/deallocate interface.
224
225 ####`String.h`
226
227 String utilities that connect `folly::fbstring` with `std::string`.
228
229 ####`Subprocess.h`
230
231 Subprocess library, modeled after Python's subprocess module.
232
233 ####[`Synchronized.h`](Synchronized.md)
234
235 High-level synchronization library.
236
237 ####`System.h`
238
239 Demangling and errno utilities.
240
241 ####[`ThreadCachedInt.h`](ThreadCachedInt.md)
242
243 High-performance atomic increment using thread caching.
244
245 ####[`ThreadLocal.h`](ThreadLocal.md)
246
247 Improved thread local storage for non-trivial types.
248
249 ####`TimeoutQueue.h`
250
251 Queue with per-item timeout.
252
253 ####`Traits.h`
254
255 Type traits that complement those defined in the standard C++11 header
256 `<traits>`.
257
258 ####`Unicode.h`
259
260 Defines the `codePointToUtf8` function.