Skip to content

Control amount of memory reserved in ctor of json_decoder #531

Closed
@betp

Description

@betp

Describe the proposed feature
The proposed feature is to have a mechanism to control how much memory the constructor of json_decoder reserves. The mechanism would be made available to the callers of the json::parse() function.

Reason: In my application, I need to parse many small json strings. When profiling the application, I see a lot of CPU time is spent in the constructor of json_decoder on these lines:

item_stack_.reserve(1000);
structure_stack_.reserve(100);

I assume this much memory is reserved because the focus of jsoncons is on parsing large json strings. As the json strings I need to parse are very simple, it should be sufficient to reserve much less memory, which would lead to less CPU time spent on the allocations and less memory fragmentation.

What other libraries (C++ or other) have this feature?
I don't know

Include a code fragment with sample data that illustrates the use of this feature

  • The parse() function could have two additional parameters (that would be defaulted to the original values), that would be passed into the constructor of json_decoder and control the amount of memory reserved. The caller could then do:
const size_t item_stack_reserve_size = 10;
const size_t structure_stack_reserve_size = 5;
auto json = jsoncons::json::parse(src, item_stack_reserve_size, structure_stack_reserve_size);
  • The parse() could instead take a hint of how big the json string to parse is:
auto json = jsoncons::json::parse(src, jsoncons::json::JsonStringSize::SMALL);
  • etc :)

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions