Skip to content

O(n^2) complexity in json_next_elem #80

@numo68

Description

@numo68

I am trying to iterate through an array using json_next_elem(). The function returns a handle suitable to be passed to the next invocation, which is a common idiom and the user's expectation is definitely that this handle remembers, where to continue with the next search, and such iteration is thus O(n).

If I try the following code

  const char* json = "[1,2,3,4,5]";
  void* handle = NULL;
  int idx;
  struct json_token val;
  while ((handle = json_next_elem(json, strlen(json), handle, "", &idx, &val))) {
    fprintf(stderr, "idx %d\n", idx);
  }

and place a breakpoint at json_parse_value(), the method is in fact called 36 times, with the case '[' being hit 6 times, meaning that the handle in fact only records, what index I want to reach, and searches for it through the whole array, using a simple pointer compare to know where to stop. That makes the function O(n^2) and unusable for anything more than a few elements. In fact we've found the problem trying to parse a file using a simple schema with less than ten thousand elements. which resulted in calling some frozen's functions 73 million times.

I know that I can use json_walk, that is however working around an issue and gets ugly pretty quick.

If this is how the code was intended to work, or there is a way to iterate in a truly linear way, please document it to avoid surprises. If it is not, a fix would be appreciated.

Thanks

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions