Skip to content
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

Performance improvement: improve Time Complexity from O(n) to O(1) in DataConversions#get in some cases #882

Merged
merged 2 commits into from
Jun 25, 2024

Conversation

yagagagaga
Copy link
Contributor

The implement of DataConversions#get (by default) is as follows:

public V get(Object key) {
    Iterator<Entry<K,V>> i = entrySet().iterator();
    if (key==null) {
        while (i.hasNext()) {
            Entry<K,V> e = i.next();
            if (e.getKey()==null)
                return e.getValue();
        }
    } else {
        while (i.hasNext()) {
            Entry<K,V> e = i.next();
            if (key.equals(e.getKey()))
                return e.getValue();
        }
    }
    return null;
}

which means that you need to traverse all entities when find an entity. When there are lots of elements in the map, the cost of each operation is extremely high.

For example, it will show very clearly when you Import OSM data into PostGIS.

before improvement:

# take several hours or even more
baremaps workflow execute --file workflow.json

after improvement:

# take several seconds
baremaps workflow execute --file workflow.json

Of course, considering that org.apache.baremaps.data.collection.DataMap have different implementations and behaviors, this optimization is not applicable to all subclass.

@bchapuis
Copy link
Member

Spot-on 🚀

It almost hurts 😅 I should have better tested the import before introducing the DataConversions utility.

As we can reintroduce JMH (Thank you @CalvinKirs for looking into this), it would be nice to include a couple of benchmarks or a mean to prevent this kind of regression in the future.

@bchapuis bchapuis merged commit 8716d53 into apache:main Jun 25, 2024
8 checks passed
@yagagagaga yagagagaga deleted the improve_20240626 branch June 30, 2024 13:23
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants