On Sun, Sep 20, 2020 at 5:57 PM Hadriel Kaplan via Boost
As an example, Boost.JSON's serializer allocates a lot more memory in the returned string than is actually used, in my testing.
Actually `json::serializer` doesn't allocate anything, it is the conversion to string algorithm that grows the string. And it uses the native growth policy using the statement `s.resize( s.capacity() + 1 );` here: https://github.com/CPPAlliance/json/blob/6ddddfb16f9b54407d153abdda11a29f746... The algorithm first tries to fit the output into a local 4kb stack buffer. For serialized JSONS smaller than this size it should result in a std::string that allocates only what is needed. I am guessing that your serialized JSONs are larger (or else the resulting string would be perfectly sized). The library implementation tries to provide good general purpose performance but there will always be use-cases that it handles sub-optimally. If you have additional information about your typical JSONs, you might see a benefit to writing your own string conversion algorithm by making a copy of the `serialize_impl` function and adjusting its heuristics to better match your data. This is precisely why `json::serializer` is exposed as a public interface. Thanks