Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings
Discussion options

To iterate through an array, you should be at the beginning of the array: to warn you, an OUT_OF_ORDER_ITERATION error is generated when development checks are active.

The simdjson code will set SIMDJSON_DEVELOPMENT_CHECKS=1 in debug mode. Alternatively, you can set the macro SIMDJSON_DEVELOPMENT_CHECKS to 1 prior to including the simdjson.h header to enable these additional checks: just make sure you remove the definition once your code has been tested. When SIMDJSON_DEVELOPMENT_CHECKS is set to 1, the simdjson library runs additional (expensive) tests on your code to help ensure that you are using the library in a safe manner.

#include <iostream>
#define SIMDJSON_DEVELOPMENT_CHECKS 1
#include "simdjson.h"

int main() {
    const char *json = R"({ "people": [ { "name": "Alice", "age": 30 }, { "name": "Bob", "age": 25 } ] })";

    simdjson::ondemand::parser parser;
    simdjson::padded_string jsonbuffer = simdjson::padded_string(json, std::strlen(json));
    auto doc = parser.iterate(jsonbuffer);

    auto person_name2 = doc["people"].at(1)["name"];
    std::cout << person_name2 << std::endl;

    auto person_name = doc["people"].at(0)["name"];
    std::cout << person_name << std::endl;
    return 0;
}

I build this with g++ -std=c++2b -o sibling sibling.cpp simdjson.cpp -g -O0.

Output:
./sibling
"Bob"
"Alice"

Question> I expect to see OUT_OF_ORDER_ITERATION warning but I didn't.
What did I do wrong here?

Thank you

You must be logged in to vote

Replies: 1 comment

Comment options

I expect to see OUT_OF_ORDER_ITERATION warning. What did I do wrong here?

So the documentation you are quoting refers you to the fact that in some cases, you cannot access twice the same array. But in your code, you have two arrays.

It is important to understand that when you do...

auto person_name2 = doc["people"].at(1)["name"];

and later do...

auto person_name = doc["people"].at(0)["name"];

... you are recreating the array (doc["people"]) twice. This is typically a performance anti-pattern. Note that it is generally a performance antipattern, not just in simdjson, but in C++ more broadly. You should not access the same key twice in a sequence: the compiler is unlikely to be able to optimize this away. It is your responsibility as a programmer not to duplicate the work.

Here is a case where OUT_OF_ORDER_ITERATION will be triggered (just one array):

#include <iostream>
#define SIMDJSON_DEVELOPMENT_CHECKS 1
#include "simdjson.h"

int main() {
    const char *json = R"({ "people": [ { "name": "Alice", "age": 30 }, { "name": "Bob", "age": 25 } ] })";

    simdjson::ondemand::parser parser;
     simdjson::padded_string jsonbuffer = simdjson::padded_string(json, std::strlen(json));
    auto doc = parser.iterate(jsonbuffer);
    simdjson::ondemand::array array = doc["people"];
    auto person_name2 = array.at(1)["name"];
    std::cout << person_name2 << std::endl;

    auto person_name = array.at(0)["name"];
    std::cout << person_name << std::endl;
    return 0;
}

In this case, a proper piece of code might look as follows:

#include <iostream>
#include "simdjson.h"

int main() {
    auto json = R"({ "people": [ { "name": "Alice", "age": 30 }, { "name": "Bob", "age": 25 } ] })"_padded;

    simdjson::ondemand::parser parser;
    auto doc = parser.iterate(json);
    for(auto val : doc["people"]) {
      std::cout << val["name"] << std::endl;
    }
    return EXIT_SUCCESS;
}
You must be logged in to vote
0 replies
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
2 participants
Morty Proxy This is a proxified and sanitized view of the page, visit original site.