Skip to content

Cache the edge reference - #918

Merged
jsichi merged 12 commits into
jgrapht:masterfrom
iotamudelta:cache_edge
May 1, 2020
Merged

jsichi merged 12 commits into
jgrapht:masterfrom
iotamudelta:cache_edge

Conversation

@iotamudelta

Copy link
Copy Markdown

Follow-up to #914.

Avoid pointer chasing by using a flattened matrix representation. This should per reference consume 4bytes with compressed JVM pointers, 8bytes otherwise.


This eschews object overhead and resulting boxing/unboxing while retaining the ability
to cache per-entry in the adjacancy matrix (by using a short with effectively three
states)

Additionally, due to the missing object header, this will be more space efficient.
Reduces memory footprint by 2x.

@jsichi jsichi left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Regarding the array representation, java.util.BitSet would be much more compact (two bits per entry), but I dunno about the performance.

@jsichi

jsichi commented Apr 27, 2020

Copy link
Copy Markdown
Member

Meh, somehow my bitset comment for #914 ended up here on #918 instead. Sorry about that.

{

if (cacheEdges) {
final E cache = edgeCache[v1Number*vertexCount+v2Number];

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In the case where the edge doesn't exist, we will always have to re-confirm that (making the caching ineffective). If we agree to always combine the edge existence cache with the edge reference cache, then we can make use of the edge existence cache to solve that.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think getEdge is ever called if !hasEdge. That being said, would you want something like:

if(cacheEdges){
    if(!hasEdge(v1Number, v2Number) return null; // assuming this is the correct return value?
    final E cache = edgeCache[v1Number*vertexCount+v2Number];
    ....
}

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's possible to further optimize it to avoid calling both containsEdge and getEdge on the first (uncached) call. In other words:

  1. check cache bit
  2. if already cached, just do array lookup
  3. if not cached yet, call getEdge, and update both caches based on result (getEdge returns null whenever containsEdge would have returned false)

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I actually think one may want to do this in hasEdge - namely instead of calling containsEdge, call getEdge and update both cache matrices.

I believe the typical use case is calls to hasEdge followed by getEdge calls, so I'd like to optimize that path.

I'll implement it once #914 lands

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That works too.

@jsichi

jsichi commented Apr 30, 2020

Copy link
Copy Markdown
Member

OK, I merged #914, so you can go ahead and finish this one.

@iotamudelta

Copy link
Copy Markdown
Author

@jsichi done, have a look. This improves end-to-end performance in my tests for a 38 noded graph with a 10 node subgraph by a little over 20%.

@jsichi

jsichi commented May 1, 2020

Copy link
Copy Markdown
Member

Thanks!

@jsichi
jsichi merged commit 76793f4 into jgrapht:master May 1, 2020
@iotamudelta
iotamudelta deleted the cache_edge branch May 1, 2020 18:58
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