- 07 Apr, 2016 3 commits
-
-
Tom Jackson authored
Summary: Primarily to support slightly more flexible implementations of `split()`; Reviewed By: ot Differential Revision: D3116763 fb-gh-sync-id: 69023c0f26058516f25b9c1f9824055efc7021f9 fbshipit-source-id: 69023c0f26058516f25b9c1f9824055efc7021f9
-
Alexey Spiridonov authored
Summary:We have a bunch of code that manually parses `folly::dynamic`s into program structures. I can be quite hard to get this parsing to be good, user-friendly, and concise. This diff was primarily motivated by the mass of JSON-parsing done by Bistro, but this pattern recurs in other pieces of internal code that parse dynamics. This diff **not** meant to replace using Thrift structs with Thrift's JSON serialization / deserialization. When all you have to deal with is correct, structured plain-old-data objects produced by another program -- **not** manually entered user input -- Thrift + JSON is perfect. Go use that. However, sometimes you need to parse human-edited configuration. The input JSON might have complex semantics, and require validation beyond type-checking. The UI for editing your configs can easily enforce correct JSON syntax. Perhaps, you can use `folly/experimental/JSONSchema.h` to have your edit UI provide type correctness. Despite all this, people can still make semantic errors, and those can be impossible to detect until you interpret the config at runtime. Also, as your system evolves, sometimes you need to break semantic backwards-compatibility for the sake of moving forward ? thus making previously valid configurations invalid, and requiring them to be fixed up manually. So, people end up needing to write manual parsers for `dynamic`s. These all have very similar recurring issues: - Verbose: to get an int field out of an object, typical code: (i) tests if the field is present, (ii) checks if the field is an integer, (iii) extracts the integer. Sometimes, you also want to handle exceptions, and compose helpful error messages. This makes the code far longer than its intent, and encourages people to write bad parsers. - Unsystematic: sometimes, we use `if (const auto* p = dyn_obj.get_ptr("key")) { ... }`, other times we use `dyn_obj.getDefault()` or `if (dyn_obj.count())`, and so on. The patterns differ subtly in meaning. Exceptions sometimes get thrown, leading to error messages that cannot be understood by the user. - Imperative parses: a typical parse proceeds step by step, and throws at the earliest error. This is bad because (i) errors have to be fixed one-by-one, instead of getting a full list upfront, (ii) even if 99% of the config is parseable, the imperative code has no way of recording the information it would have parsed after the first error. `DynamicParser` fixes all of the above, and makes your parsing so clean that you might not even bother with `JSONSchema` as your first line of defense -- type-coercing, type-enforcing, friendly-error-generating C++ ends up being more concise. Besides all the sweet syntax sugar, `DynamicParser` lets you parse **all** the valid data in your config, while recording *all* the errors in a way that does not lose the original, buggy config. This means your code can parse a config that has errors, and still be able to meaningfully export it back to JSON. As a result, stateless clients (think REST APIs) can provide a far better user experience than just discarding the user?s input, and returning a cryptic error message. For the details, read the docs (and see the example) in `DynamicParser.h`. Here are the principles of `DynamicParser::RECORD` mode in a nutshell: - Pre-populate your program struct with meaningful defaults **before** you parse. - Any config part that fails to parse will keep the default. - Any config part that parses successfully will get to update the program struct. - Any errors will be recorded with a helpful error message, the portion of the dynamic that caused the error, and the path through the dynamic to that portion. I ported Bistro to use this in D3136954. I looked at using this for JSONSchema's parsing of schemas, but it seemed like too much trouble for the gain, since it would require major surgery on the code. Reviewed By: yfeldblum Differential Revision: D2906819 fb-gh-sync-id: aa997b0399b17725f38712111715191ffe7f27aa fbshipit-source-id: aa997b0399b17725f38712111715191ffe7f27aa
-
Yedidya Feldblum authored
Summary:[Folly] Use `folly::Function` in `folly::EventBase`. `folly::Function` is moveable but noncopyable and therefore supports wrapping moveable but noncopyable lambdas - like the kind that arises when move-capturing a `std::unique_ptr`. `std::function` is copyable - therefore it does not support wrapping such noncopyable lambdas. Switching `folly::EventBase` to use it will allow callers to pass such noncopyable lambdas, allowing, e.g.: ``` auto numptr = folly::make_unique<int>(7); // unique_ptr is noncopyable folly::EventBase eb; eb.runInLoop([numptr = std::move(numptr)] { // therefore lambda is noncopyable int num = *numptr; }); eb.loop(); ``` This allows us to move away from the `folly::MoveWrapper` hack, which worked like: ``` auto numptr = folly::make_unique<int>(7); // unique_ptr is noncopyable auto numptrw = folly::makeMoveWrapper(std::move(numptr)); // MoveWrapper is "copyable" - hacky folly::EventBase eb; eb.runInLoop([numptrw] { // therefore lambda is "copyable" - hacky int num = **numptrw; }); ``` We needed to do that hack while: But neither condition is true anymore. Reviewed By: spacedentist Differential Revision: D3143931 fb-gh-sync-id: 4fbdf5fb77eb5848ed1c6de942b022382141577f fbshipit-source-id: 4fbdf5fb77eb5848ed1c6de942b022382141577f
-
- 06 Apr, 2016 5 commits
-
-
Giuseppe Ottaviano authored
Reviewed By: luciang Differential Revision: D3145033 fb-gh-sync-id: f37d0a2c6e22c1866134159b78722ff43d70918b fbshipit-source-id: f37d0a2c6e22c1866134159b78722ff43d70918b
-
Bert Maher authored
Summary: Use malloc/malloc.h there instead Reviewed By: Orvid Differential Revision: D3143768 fb-gh-sync-id: 3258b68df49cf7fbff28a3853663d39c147aee8b fbshipit-source-id: 3258b68df49cf7fbff28a3853663d39c147aee8b
-
Michael Lee authored
Summary: Sometimes variable shadowing is fine, sometimes it can cause subtle mistakes. Reviewed By: yfeldblum Differential Revision: D3140450 fb-gh-sync-id: acf7195ad65614d4f012bef8ce7dccb3a3038456 fbshipit-source-id: acf7195ad65614d4f012bef8ce7dccb3a3038456
-
Sven Over authored
Summary:We are jumping through some ugly hoops in the implementation of fibers to allow for non-copyable functors/lambdas. We can get rid of those by using folly::Function instead of std::function to store functions in folly::fibers::Fiber. This involves one observable interface change: the virtual function folly::fibers::InlineFunctionRunner::run must take a folly::Function<void()> argument instead of a std::function<void()>. Reviewed By: andriigrynenko Differential Revision: D3102711 fb-gh-sync-id: 56705b972dc24cc0da551109ed44732b97cb6e13 fbshipit-source-id: 56705b972dc24cc0da551109ed44732b97cb6e13
-
Dmitry Pleshkov authored
Summary: For exceptions aggregation conveniece Reviewed By: philippv Differential Revision: D3128132 fb-gh-sync-id: 48c72df364228c1d2c8f29161c2b85c131b4fea8 fbshipit-source-id: 48c72df364228c1d2c8f29161c2b85c131b4fea8
-
- 05 Apr, 2016 3 commits
-
-
James Sedgwick authored
Summary:This is an ugly but occassionally useful hack to accomodate cases where we want to propagate folly::Function but run into the brick wall that is std::function @override-unit-failures Reviewed By: spacedentist Differential Revision: D3118432 fb-gh-sync-id: 80ed56494dfcf60e0f266013d880107acabc7dcf fbshipit-source-id: 80ed56494dfcf60e0f266013d880107acabc7dcf
-
Giuseppe Ottaviano authored
Summary: Allow ASan to detect access to invalidated strings even when they are small (in-situ). See D3114022 for details. Reviewed By: luciang Differential Revision: D3118161 fb-gh-sync-id: 3b83e3309d510ee9721dd505d176b09bdb7fd42d fbshipit-source-id: 3b83e3309d510ee9721dd505d176b09bdb7fd42d
-
Sven Over authored
Summary:Now that folly::Future uses folly::Function, we can use non-copyable callbacks. That allows us to get rid of folly::MoveWrapper in the implementaion. This diff also enforces perfect forwarding in the implementation of folly::Future, thereby reducing the number of times that a callable that is passed to Future::then et al. gets passed by value. Before folly::Function, Future::then(callback) has invoked the move constructor of the callback type 5 times for small callback objects (fitting into the in-place storage inside folly::detail::Core) and 6 times for large callback objects. This has been reduced to 5 times in all cases with the switch to UniqueFunction. This diff reduces it to 2 times. Reviewed By: yfeldblum Differential Revision: D2976647 fb-gh-sync-id: 9da470d7e9130bd7ad8af762fd238ef9a3ac5892 fbshipit-source-id: 9da470d7e9130bd7ad8af762fd238ef9a3ac5892
-
- 04 Apr, 2016 1 commit
-
-
Sven Over authored
Summary:By using folly::Function instead of std::function to internally store functions in FunctionScheduler, this diff allows users to pass non-copyable lambdas to FunctionScheduler::addFunction. All exisiting unit tests still pass. Also, passing std::function to addFunction still works, as the std::function will be implicitly converted (i.e. wrapped) in a folly::Function. However, this implies a performance penalty. Reviewed By: fugalh Differential Revision: D3092944 fb-gh-sync-id: 1e8081e70d4717025f2eadbb6b6da173460d4373 fbshipit-source-id: 1e8081e70d4717025f2eadbb6b6da173460d4373
-
- 02 Apr, 2016 5 commits
-
-
Giuseppe Ottaviano authored
Summary:Instead of using preprocessor to disable SSO, use a default argument. Also reimplement `insertImplDiscr` to mirror `insertImpl`. Reviewed By: philippv Differential Revision: D3130901 fb-gh-sync-id: a5b0ba97b3c7d91323be01ab617ca9b09dbb0fd2 fbshipit-source-id: a5b0ba97b3c7d91323be01ab617ca9b09dbb0fd2
-
Christopher Dykes authored
Summary: The only thing that matters in it is dirname, and Windows doesn't have it. Reviewed By: yfeldblum Differential Revision: D2977655 fb-gh-sync-id: eb5703485089f5c66882fbc9cc142686214c1148 fbshipit-source-id: eb5703485089f5c66882fbc9cc142686214c1148
-
Christopher Dykes authored
Summary: Windows doesn't have it, so stub it out for now, because not getting output to the system log is a minor inconvience, not a breaking issue. Reviewed By: yfeldblum Differential Revision: D2977537 fb-gh-sync-id: b05d9d3c722ad4f3953a979f91024602d17d3ed0 fbshipit-source-id: b05d9d3c722ad4f3953a979f91024602d17d3ed0
-
Christopher Dykes authored
Summary: MSVC doesn't like this and complains about ambigious overloads. Reviewed By: ericniebler Differential Revision: D3107438 fb-gh-sync-id: 597ba85fdb7640b0a0fda352bf53ce31cf0e38b7 fbshipit-source-id: 597ba85fdb7640b0a0fda352bf53ce31cf0e38b7
-
Sven Over authored
Summary:The swap function belongs in the same namespace as Function: folly. Also, to avoid amibiguities with a generic swap function in <algorithm>, we need two variants: one for identical types of folly::Function, and one for folly::Functions with different configurations. Reviewed By: ericniebler Differential Revision: D3106429 fb-gh-sync-id: 11b04e9bc709d52016ac94c078278410f5ee43c6 fbshipit-source-id: 11b04e9bc709d52016ac94c078278410f5ee43c6
-
- 01 Apr, 2016 8 commits
-
-
Adam Simpkins authored
Summary:Update setFromPath() to accept a StringPiece rather than just std::string or a plain const char*. Also fix two other minor issues: - Leave the old address untouched on failure. Previously it could leave the SocketAddress in a partially updated state. - Don't assume the input is nul terminated. Previously the input code read one past the specified input length, and copied this into the address, assuming it was a nul terminator. The new code explicitly writes a 0 byte. Reviewed By: yfeldblum Differential Revision: D3119882 fb-gh-sync-id: 3e2258f42034b4f470ade0a23ea085e132a3dd0f fbshipit-source-id: 3e2258f42034b4f470ade0a23ea085e132a3dd0f
-
Christopher Dykes authored
Summary: Because we don't have these builtins under MSVC, and not having to deal with the differences in API in the places that use these builtins is good. Reviewed By: yfeldblum Differential Revision: D2984842 fb-gh-sync-id: 34db7455debf81e4abffe57c154eb731ae097ff6 fbshipit-source-id: 34db7455debf81e4abffe57c154eb731ae097ff6
-
Christopher Dykes authored
Summary: We don't have dirent.h on Windows, but we can emulate its behavior. Reviewed By: yfeldblum Differential Revision: D2978570 fb-gh-sync-id: af5ade0ea64ba22900440250e7125aa039a77f62 fbshipit-source-id: af5ade0ea64ba22900440250e7125aa039a77f62
-
Christopher Dykes authored
Summary: The primary issue is that the pthread implementation we use for Windows defines `pthread_t` as a struct (yes, it is allowed to do this), which breaks a lot of things. Reviewed By: yfeldblum Differential Revision: D2862671 fb-gh-sync-id: 551569b6a9e2e374cf77e186e148b6b397f8f25f fbshipit-source-id: 551569b6a9e2e374cf77e186e148b6b397f8f25f
-
Michael Lee authored
Summary: `strndup` is defined on modern versions of OSX Reviewed By: Orvid, grp Differential Revision: D3122635 fb-gh-sync-id: 678792765addf2fb226e835d3b7a67e155ed6dc5 fbshipit-source-id: 678792765addf2fb226e835d3b7a67e155ed6dc5
-
Sven Over authored
Summary:Most header files in folly already used "#pragma once" to protect against multiple inclusion. This diff removes old-style ifndef/define/endif header guards and replaces them with pragma once. In some cases the defined symbol is tested in other header files. In those cases the "#define" is kept. Reviewed By: igorsugak Differential Revision: D3054492 fb-gh-sync-id: 20aa0b9b926a30dd021e4b8f5440e8888874681c fbshipit-source-id: 20aa0b9b926a30dd021e4b8f5440e8888874681c
-
Sven Over authored
Summary: This diff adds folly/docs/Function.md Reviewed By: yfeldblum Differential Revision: D3120617 fb-gh-sync-id: fecc0e507e05016aaac43ba981eab49431229bd7 fbshipit-source-id: fecc0e507e05016aaac43ba981eab49431229bd7
-
Andrii Grynenko authored
Summary:This adds basic print functions for FiberManager, Fiber and FiberManager map. It also adds a global list of fibers to FiberManager. Fibers are only removed from that list on Fiber object destruction, so it shouldn't have any perf impact. Inspired by tao/server/scripts/fiber_bt.gdb FiberManager map example: (gdb) print_folly_fiber_manager_map Global FiberManager map has 2 entries. (folly::EventBase*)0x7fffffffdb60 -> (folly::fibers::FiberManager*)0x7ffff5b58480 (folly::EventBase*)0x7fffffffd930 -> (folly::fibers::FiberManager*)0x7ffff5b58300 FiberManager example: (gdb) print_folly_fiber_manager &manager (folly::fibers::FiberManager*)0x7fffffffdbe0 Fibers active: 3 Fibers allocated: 3 Fibers pool size: 0 Active fiber: (folly::fibers::Fiber*)(nil) Current fiber: (folly::fibers::Fiber*)(nil) Active fibers: (folly::fibers::Fiber*)0x7ffff5b5b000 State: Awaiting (folly::fibers::Fiber*)0x7ffff5b5b300 State: Awaiting (folly::fibers::Fiber*)0x7ffff5b5b600 State: Awaiting Fiber example: P56244621 Reviewed By: yfeldblum Differential Revision: D3119616 fb-gh-sync-id: defa27b84aafbd4333b2ca301f07c226f0386f44 fbshipit-source-id: defa27b84aafbd4333b2ca301f07c226f0386f44
-
- 31 Mar, 2016 10 commits
-
-
Nicholas Ormrod authored
Summary:gcc-5.0 introduced non-relocatable strings in libgcc. Only treat gcc < 5 strings as relocatable. Enable relocation for fbstrings via typedef. Re https://github.com/facebook/folly/issues/316, thanks tomhughes for reporting this. Reviewed By: snarkmaster, ot Differential Revision: D3115580 fb-gh-sync-id: d8aff947d55a0a0c57f6b997f651a190e05f2779 fbshipit-source-id: d8aff947d55a0a0c57f6b997f651a190e05f2779
-
Giuseppe Ottaviano authored
Summary:The current implementation of `insertImpl` assumes that `expand_noinit` does not reallocate if the `size() + delta <= capacity()`, but D3114022 makes this assumption invalid when compiling with ASan. It also doesn't guarantee exponential growth, so repeated inserting at the end could trigger quadratic behavior. The new implementation fixes the problems above, and it's much simpler. Reviewed By: yfeldblum, Orvid Differential Revision: D3119813 fb-gh-sync-id: 946ebeeaf924a531f7f03fb7e79c75e352a50c58 fbshipit-source-id: 946ebeeaf924a531f7f03fb7e79c75e352a50c58
-
Kyle Nekritz authored
Summary: Alerts may be sent by clients, potentially letting us know why connections fail. Reviewed By: siyengar Differential Revision: D3117395 fb-gh-sync-id: bddf51f2399eb9e7e397981d5440adb3e815d6d2 fbshipit-source-id: bddf51f2399eb9e7e397981d5440adb3e815d6d2
-
Christopher Dykes authored
Summary: Windows doesn't have it, so be nice and at least stub it out. Reviewed By: yfeldblum Differential Revision: D2984232 fb-gh-sync-id: 3e8871ab78c5d7c8785a52af24548245f842f19b fbshipit-source-id: 3e8871ab78c5d7c8785a52af24548245f842f19b
-
Christopher Dykes authored
Summary:Let's break OSX! Alright, maybe not. Neither OSX nor Windows define malloc_usable_size, so we implement them based on what is available on the respective platforms. This moves the implementation for OSX out of Portability.h and into the new header, so it likely breaks something on OSX, although I'm not sure what. Reviewed By: yfeldblum Differential Revision: D3019938 fb-gh-sync-id: df95faef09535098fb73b7b3479d7a73f6b49712 fbshipit-source-id: df95faef09535098fb73b7b3479d7a73f6b49712
-
Andrii Grynenko authored
Summary: Adding a benchmark for one the most common scenarios (used in Futures, EventBase, fibers etc). Reviewed By: yfeldblum Differential Revision: D3106365 fb-gh-sync-id: 8cb55959b3803e8836ab5a5bdf43fdfc1db02d4c fbshipit-source-id: 8cb55959b3803e8836ab5a5bdf43fdfc1db02d4c
-
Christopher Dykes authored
Summary:It was mistakenly assuming the length passed in included the null terminator. This also makes the portability implementation of `strndup` available to OSX and FreeBSD, where they weren't present, and where HHVM had a wrapper for them. This also removes the extra pair of conditions around `memrchr`, as the main define should always be getting set. Reviewed By: yfeldblum Differential Revision: D3116467 fb-gh-sync-id: 243dd4dace219efab2c2bf2f383202e70fbec4de fbshipit-source-id: 243dd4dace219efab2c2bf2f383202e70fbec4de
-
Christopher Dykes authored
Summary:MSVC has support in the compiler for the intrinsics required, but both refuses to tell us that, and also gives them proper names. The code already checks for runtime support, this just enables compiling the SSE 4.2 version in the first place. Reviewed By: yfeldblum Differential Revision: D3104296 fb-gh-sync-id: 9143240bede9b756817691fdd86818001267dac1 fbshipit-source-id: 9143240bede9b756817691fdd86818001267dac1
-
Neel Goyal authored
Summary: OpenSSLPtrTypes.h uses CHECK_EQ which is included in glog/logging. Reviewed By: knekritz Differential Revision: D3118577 fb-gh-sync-id: f8a00aa5a523bf88a3f783433c6699d555799225 fbshipit-source-id: f8a00aa5a523bf88a3f783433c6699d555799225
-
Christopher Dykes authored
Summary: This adds Windows support to the portability header for sys/syscall.h, which was previously named portability/Syscall.h, which is inconsistent with the naming of the other headers, so it's now also been renamed to SysSyscall.h, and the one place that used it updated to reflect the new name. Reviewed By: yfeldblum Differential Revision: D2984383 fb-gh-sync-id: c0e4ce55c7bc2aa6db9b084e09d9762ba587002d fbshipit-source-id: c0e4ce55c7bc2aa6db9b084e09d9762ba587002d
-
- 30 Mar, 2016 3 commits
-
-
Giuseppe Ottaviano authored
Summary:`fbstring`'s small string optimization prevents ASan to catch invalid accesses to the data of a destroyed string, for example if a `StringPiece` is initialized from a temporary string. This diff disables building a string with the small category when compiled with ASan: small strings will be constructed as `Medium`-category strings and heap-allocated. This is done by only changing the behavior of construction and resizing, so that the ABI is preserved and it is still possible to link an ASan-enabled object file with a library that wasn't compiled with ASan. The diff also fixes a blind spot in `fbstring_core`'s constructor, which disabled ASan altogether in order to allow fast word-aligned copy of small strings. Since small string construction is now disabled under ASan, we don't need to disable it anymore. Lastly, it always clears moved-from strings, even when they are small. This improves the performance of the move constructor (no more conditional needed) and it uncovers another class of potential bugs. Reviewed By: luciang Differential Revision: D3114022 fb-gh-sync-id: 4e180fbf2b8aced3b977afc985d26fdf244d9598 fbshipit-source-id: 4e180fbf2b8aced3b977afc985d26fdf244d9598
-
Yang Chi authored
Summary: Some compiler will complain about this when calling std::min with a size_t and a uint64_t. So use unit64_t for numElements in HHWheelTimer. Reviewed By: mzlee Differential Revision: D3116346 fb-gh-sync-id: 67a9eebf4f9e8fe0e732a7292af55122be04163b fbshipit-source-id: 67a9eebf4f9e8fe0e732a7292af55122be04163b
-
Misha Shneerson authored
Summary:We should be able to use HHWheelTimer in fibers. So it should use way less stack. Alternative solution to D3112305 Reviewed By: haijunz Differential Revision: D3112558 fb-gh-sync-id: 3a52a37d9f9347639005fdf84524f7f8c3041918 fbshipit-source-id: 3a52a37d9f9347639005fdf84524f7f8c3041918
-
- 29 Mar, 2016 2 commits
-
-
Yedidya Feldblum authored
Summary:[Folly] `IPAddressV[46]::validate(StringPiece)`. Just perform the check without allocations or throws. Reviewed By: meyering Differential Revision: D3103545 fb-gh-sync-id: 2918c1398935738f597b9045cf7dadbe42da2527 fbshipit-source-id: 2918c1398935738f597b9045cf7dadbe42da2527
-
Neel Goyal authored
Summary:Use a ThreadLocalPRNG insteaad of a per context RNG. This avoids calls to Random::seed on context creation which can get expensive when many are created in an application. Reviewed By: siyengar Differential Revision: D3105501 fb-gh-sync-id: 92d987c27a1f190a98035ca25c23b994ca915007 fbshipit-source-id: 92d987c27a1f190a98035ca25c23b994ca915007
-