• Victor Zverovich's avatar
    Deprecate folly::format · 4e249e08
    Victor Zverovich authored
    Summary:
    `folly::format` is a problematic API because it returns a `Formatter` object that may store references to arguments as its comment warns:
    
    ```
     * Formatter class.
     *
     * Note that this class is tricky, as it keeps *references* to its lvalue
     * arguments (while it takes ownership of the temporaries), and it doesn't
     * copy the passed-in format string. Thankfully, you can't use this
     * directly, you have to use format(...) below.
    ```
    
    This has negative safety and performance (encourages reuse of the same formatter) implications because contrary to what the comment says you can use the object directly since it's returned from `folly::format`. For example
    ```
    auto f = folly::format(std::string("{}"), 42);
    f.str();
    ```
    is a UB with no compile-time diagnostic (only caught by ASAN).
    
    It's also unnecessary because the `Formatter` object is usually converted to string straight away via `str()` or written to an output stream. Reusing the formatter doesn't make much sense either because the expensive part is formatting, not capturing arguments.
    
    This diff deprecates `folly::format` suggesting `fmt::format` as a potential replacement. `fmt::format` doesn't have the above problem because arguments are always in scope. It also has the following advantages:
    
    * Better compile times.
    * Compile-time format string checks.
    * Compatibility with C++20 `std::format`.
    * Better performance, particularly with format string compilation which eliminates format string processing overhead altogether.
    * Smaller binary footprint.
    
    Also remove `folly::writeTo` which is no longer used and the `format_nested_fbstrings` benchmark which is identical to `format_nested_strings` since there is no `fbstr()` any more.
    
    Reviewed By: yfeldblum
    
    Differential Revision: D26391489
    
    fbshipit-source-id: f0309e78db0eb6d8c22b426d4cc333a17c53f73e
    4e249e08
FormatBenchmark.cpp 9.17 KB