libimobiledevice / libplist

A library to handle Apple Property List format in binary or XML
https://libimobiledevice.org
GNU Lesser General Public License v2.1
535 stars 304 forks source link

plist_dict_next_item very inefficient #131

Closed xdeng closed 5 years ago

xdeng commented 5 years ago

When the amount of data exceeds 10,000, every time you look from scratch, the efficiency is very low. I tried to change it to

void plist_dict_next_item(plist_t node, plist_dict_iter iter, char **key, plist_t *val)
{
    uint32_t* iter_int = (uint32_t*) iter;

    if (key)
    {
        *key = NULL;
    }
    if (val)
    {
        *val = NULL;
    }

    if (node && PLIST_DICT == plist_get_node_type(node) && *iter_int < node_n_children(node))
    {
        node_t* child = node_nth_child(node, *iter_int);
        if (key)
        {
            plist_get_key_val((plist_t)child, key);
        }

        if (val)
        {
            *val = (plist_t)node_next_sibling(child);
        }

        *iter_int += 2;
    }
    return;
}

Run time can be reduced by half, But still not fast enough

xdeng commented 5 years ago

The best way is plist_dict_iter == node_t* This is the fastest, Time complexity is O(1).

nikias commented 5 years ago

See 3f967317479acc4b98594b4b86a2787cbd2aa1f1.

xdeng commented 5 years ago

@nikias Great! The code is the same as I thought.