Boost logo

Boost :

From: Vinnie Falco (vinnie.falco_at_[hidden])
Date: 2020-09-21 01:19:03

On Sun, Sep 20, 2020 at 5:57 PM Hadriel Kaplan via Boost
<boost_at_[hidden]> wrote:
> 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:


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.


Boost list run by bdawes at, gregod at, cpdaniel at, john at