Pull from FB rev 63ce89e2f2301e6bba44a111cc7d4218022156f6
[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.
81
82 #### `Bits.h`
83
84 Bit-twiddling functions that wrap the
85 [ffsl(l)](http://linux.die.net/man/3/ffsll) primitives in a uniform
86 interface.
87
88 #### `ConcurrentSkipList.h`
89
90 An implementation of the structure described in [A Provably Correct
91 Scalable Concurrent Skip
92 List](http://www.cs.tau.ac.il/~shanir/nir-pubs-web/Papers/OPODIS2006-BA.pdf)
93 by Herlihy et al.
94
95 #### [`Conv.h`](Conv.md)
96
97 A variety of data conversion routines (notably to and from string),
98 optimized for speed and safety.
99
100 #### `DiscriminatedPtr.h`
101
102 Similar to `boost::variant`, but restricted to pointers only. Uses the
103 highest-order unused 16 bits in a pointer as discriminator. So
104 `sizeof(DiscriminatedPtr<int, string, Widget>) == sizeof(void*)`.
105
106 #### [`dynamic.h`](Dynamic.md)
107
108 Dynamically-typed object, created with JSON objects in mind.
109
110 #### `Endian.h`
111
112 Endian conversion primitives.
113
114 ####`Escape.h`
115
116 Escapes a string in C style.
117
118 ####`eventfd.h`
119
120 Wrapper around the
121 [`eventfd`](http://www.kernel.org/doc/man-pages/online/pages/man2/eventfd.2.html)
122 system call.
123
124 ####[`FBString.h`](FBString.md)
125
126 A drop-in implementation of `std::string` with a variety of optimizations.
127
128 ####[`FBVector.h`](FBVector.md)
129
130 A mostly drop-in implementation of `std::vector` with a variety of
131 optimizations.
132
133 ####`Foreach.h`
134
135 Pseudo-statements (implemented as macros) for iteration.
136
137 ####[`Format.h`](Format.md)
138
139 Python-style formatting utilities.
140
141 ####[`GroupVarint.h`](GroupVarint.md)
142
143 [Group Varint
144 encoding](http://www.ir.uwaterloo.ca/book/addenda-06-index-compression.html)
145 for 32-bit values.
146
147 ####`Hash.h`
148
149 Various popular hash function implementations.
150
151 ####[`Histogram.h`](Histogram.md)
152
153 A simple class for collecting histogram data.
154
155 ####`IntrusiveList.h`
156
157 Convenience type definitions for using `boost::intrusive_list`.
158
159 ####`json.h`
160
161 JSON serializer and deserializer. Uses `dynamic.h`.
162
163 ####`Likely.h`
164
165 Wrappers around [`__builtin_expect`](http://gcc.gnu.org/onlinedocs/gcc/Other-Builtins.html).
166
167 ####`Malloc.h`
168
169 Memory allocation helpers, particularly when using jemalloc.
170
171 ####`MapUtil.h`
172
173 Helpers for finding items in associative containers (such as
174 `std::map` and `std::unordered_map`).
175
176 ####[`PackedSyncPtr.h`](PackedSyncPtr.md)
177
178 A highly specialized data structure consisting of a pointer, a 1-bit
179 spin lock, and a 15-bit integral, all inside one 64-bit word.
180
181 ####`Preprocessor.h`
182
183 Necessarily evil stuff.
184
185 ####`PrettyPrint.h`
186
187 Pretty-printer for numbers that appends suffixes of unit used: bytes
188 (kb, MB, ...), metric suffixes (k, M, G, ...), and time (s, ms, us,
189 ns, ...).
190
191 ####[`ProducerConsumerQueue.h`](ProducerConsumerQueue.md)
192
193 Lock free single-reader, single-writer queue.
194
195 ####`Random.h`
196
197 Defines only one function---`randomNumberSeed()`.
198
199 ####`Range.h`
200
201 Boost-style range facility and the `StringPiece` specialization.
202
203 ####`RWSpinLock.h`
204
205 Fast and compact reader-writer spin lock.
206
207 ####`ScopeGuard.h`
208
209 C++11 incarnation of the old [ScopeGuard](http://drdobbs.com/184403758) idiom.
210
211 ####[`SmallLocks.h`](SmallLocks.md)
212
213 Very small spin locks (1 byte and 1 bit).
214
215 ####`small_vector.h`
216
217 Vector with the small buffer optimization and an ptional embedded
218 `PicoSpinLock`.
219
220 ####`sorted_vector_types.h`
221
222 Collections similar to `std::map` but implemented as sorted vectors.
223
224 ####`StlAllocator.h`
225
226 STL allocator wrapping a simple allocate/deallocate interface.
227
228 ####`String.h`
229
230 String utilities that connect `folly::fbstring` with `std::string`.
231
232 ####[`Synchronized.h`](Synchronized.md)
233
234 High-level synchronization library.
235
236 ####`System.h`
237
238 Demangling and errno utilities.
239
240 ####[`ThreadCachedInt.h`](ThreadCachedInt.md)
241
242 High-performance atomic increment using thread caching.
243
244 ####[`ThreadLocal.h`](ThreadLocal.md)
245
246 Improved thread local storage for non-trivial types.
247
248 ####`TimeoutQueue.h`
249
250 Queue with per-item timeout.
251
252 ####`Traits.h`
253
254 Type traits that complement those defined in the standard C++11 header
255 `<traits>`.
256
257 ####`Unicode.h`
258
259 Defines the `codePointToUtf8` function.