Skip to content

AbstractNonCHBidirAlgo: improve PriorityQueue handling #2408

Closed

Description

@otbutz

We currently remove and readd modified SPTEntries(L163-165) within the loop in order to update their position in the queue:

private void fillEdges(SPTEntry currEdge, PriorityQueue<SPTEntry> prioQueue, IntObjectMap<SPTEntry> bestWeightMap, boolean reverse) {
EdgeIterator iter = edgeExplorer.setBaseNode(currEdge.adjNode);
while (iter.next()) {
if (!accept(iter, currEdge.edge))
continue;
final double weight = calcWeight(iter, currEdge, reverse);
if (Double.isInfinite(weight)) {
continue;
}
final int traversalId = traversalMode.createTraversalId(iter, reverse);
SPTEntry entry = bestWeightMap.get(traversalId);
if (entry == null) {
entry = createEntry(iter, weight, currEdge, reverse);
bestWeightMap.put(traversalId, entry);
prioQueue.add(entry);
} else if (entry.getWeightOfVisitedPath() > weight) {
prioQueue.remove(entry);
updateEntry(entry, iter, weight, currEdge, reverse);
prioQueue.add(entry);
} else
continue;
if (updateBestPath) {
// only needed for edge-based -> skip the calculation and use dummy value otherwise
double edgeWeight = traversalMode.isEdgeBased() ? weighting.calcEdgeWeight(iter, reverse) : Double.POSITIVE_INFINITY;
// todo: performance - if bestWeightMapOther.get(traversalId) == null, updateBestPath will exit early and we might
// have calculated the edgeWeight unnecessarily
updateBestPath(edgeWeight, entry, EdgeIterator.NO_EDGE, traversalId, reverse);
}
}
}

This is rather expensive as PriorityQueue.remove(Object) has a complexity of O(n), checking each element in the queue for equality.

We could mark those entries instead (e.g add them to a Set) and update the Queue after the loop has finished:

Set<SPTEntry> modified = new HashSet<>();
while (iter.next()) {
        //...
        updateEntry(entry, iter.getEdge(), iter.getAdjNode(), origEdgeId, weight, currEdge, reverse);
        modified.add(entry);
        // ...
}
prioQueue.removeAll(modified);
prioQueue.addAll(modified);

Activity

  1. easbar commented on Sep 28, 2021

    @easbar
    Member

    Ok, I see removeAll is optimized for bulk removing multiple elements. addAll is just calling add for each element.
    Did you try this yet? Btw another option is to set a 'removed' flag for the queue entries instead of actually removing them. The flagged entries are then rejected when they are polled from the queue. I think we tried this before, but not sure how much of a difference it made and maybe it is worth trying this again.

  2. karussell commented on Sep 28, 2021

    @karussell
    Member

    Could be interesting. See also #2092, #2107, #475 and #489.

  3. easbar commented on Sep 28, 2021

    @easbar
    Member

    I tried this here: 04011ef
    Will post benchmarks when they are ready

  4. easbar commented on Sep 29, 2021

    @easbar
    Member

    Hm, unfortunately this is not faster and maybe even slightly slower, like 1.5ms vs 1.4ms. At least the way I implemented this. But maybe this can still be improved?

    The strange thing I do not understand is that for non-CH my change yields a massive slow down, like 3.5s vs 0.1s. @otbutz you opened this issue for CH especially. Did you also experience that this tweak does not work for non-CH?

  5. otbutz commented on Sep 29, 2021

    @otbutz
    ContributorAuthor

    I did not check it yet. Just noticed PriorityQueue.remove(Object) taking some noticeable amount of time.

  6. karussell commented on Sep 29, 2021

    @karussell
    Member

    The strange thing I do not understand is that for non-CH my change yields a massive slow down, like 3.5s vs 0.1s.

    The problem might be that PriorityQueue is extremely well tuned and hard to beat. OR that the bigger queue size is bad for iteration and polling OR that the Set makes the problems. Maybe you initialize the Set with 1000 or something or even use an ArrayList?

  7. otbutz commented on Sep 29, 2021

    @otbutz
    ContributorAuthor
  8. easbar commented on Sep 30, 2021

    @easbar
    Member

    I tried using an ArrayList instead of a HashSet for non-CH and this was much faster (around x2), but still much slower than master. For CH it was still around the same, so no real improvement compared to master. The issue with non-CH is that removeAll traverses all elements of the queue (not only the ones of the given Collection that shall be removed), so if the queue is large it takes a long time.

    Feel free to profile this in more detail using JMC, and re-open this issue, but currently I do not feel like trying further in this direction. If we actually wanted to speed up CH we should rather think about not using the BaseGraph/CH hybrid we are using at the moment and re-labelling the nodes according to the CH level. This would easily improve speed by around a factor of two and probably more (but require more memory).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

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