-
-
Notifications
You must be signed in to change notification settings - Fork 173
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Control amount of memory reserved in ctor of json_decoder #531
Comments
Can you provide an example of how you're currently using jsoncons, along with a representative sample JSON file? Thanks. |
Sure:
|
You could reduce allocations a lot by reusing a void doSomethingWithValue(std::string_view val)
{
std::cout << val << "\n";
}
int main()
{
jsoncons::json_decoder<jsoncons::json> decoder;
jsoncons::json_parser parser;
for (std::size_t i = 0; i < 5; ++i)
{
std::string jsonStr = "{\"id\":1,\"version\":\"2.0\",\"value\":\"" + std::to_string(i) + "\"}";
parser.update(jsonStr.data(), jsonStr.size());
parser.parse_some(decoder);
parser.finish_parse(decoder);
parser.check_done();
if (decoder.is_valid())
{
jsoncons::json json = decoder.get_result();
const auto& value = json.at_or_null("value");
if (value.is_string())
{
doSomethingWithValue(value.as<std::string_view>());
}
}
decoder.reset();
parser.reset();
}
} |
Thank you for the suggestion. Good to know the However, implementing this approach would cause quite some changes in my code base. The parsing of the json strings happens in many places/functions. I would probably wrap the Do you see it realistic you would extend the interface of the |
I'm reluctant to add more overrides to the std::string str = R"(
{
"foo" : [1,2,3],
"bar" : [4,5,{"f":6}]
})";
auto options = jsoncons::json_options{}
.max_nesting_depth(3);
auto j = jsoncons::json::parse(str); |
Thank you for the changes! |
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 thejson::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: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
parse()
function could have two additional parameters (that would be defaulted to the original values), that would be passed into the constructor ofjson_decoder
and control the amount of memory reserved. The caller could then do:parse()
could instead take a hint of how big the json string to parse is:The text was updated successfully, but these errors were encountered: