Skip to content

A generally more performant ondemand field search method. #2202

@feverzsj

Description

@feverzsj
using namespace simdjson;

int foreach_found_field(auto&& apply, ondemand::object& o, auto const&... ks)
{
    auto mks = std::array{std::pair<bool, std::string_view>{false, ks}...};
    int leftKs = mks.size();

    for(ondemand::field fld : o)
    {
        int i = 0;
        for(auto&[matched, k] : mks)
        {
            if(! matched && fld.key() == k)
            {
                apply(i, fld);
                matched = true;
                if(--leftKs == 0)
                    return 0;
                break;
            }

            ++i;
        }
    }

    return leftKs;
}

live example

For each field in object, it searches field key in target keys until all target keys are found or no more field. It scans the object at most once, which could avoid worst cases in existing find methods.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions