Skip to content

Change the cache from a Boolean array to a byte array for GraphOrdering - #914

Merged
jsichi merged 9 commits into
jgrapht:masterfrom
iotamudelta:better_caching
Apr 30, 2020
Merged

jsichi merged 9 commits into
jgrapht:masterfrom
iotamudelta:better_caching

Conversation

@iotamudelta

@iotamudelta iotamudelta commented Apr 24, 2020 •

Copy link
Copy Markdown

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

Due to the missing object header, this reduces the memory expense per entry to 1 byte from 16 bytes (64bit JVM).


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.
@iotamudelta

iotamudelta commented Apr 24, 2020 •

Copy link
Copy Markdown
Author

@d-michail this addresses the significant overhead in hasEdge().

I'd like to also address getEdge(). I'd like to introduce another caching data structure of type Edge to hold that data directly (or more precisely, the references to these). Obviously, that'll scale O(N**2) in space complexity. I guess an argument could be made to unify these two data structures (thereby regressing performance on hasEdge() again. Let me know what you think.

@d-michail

Copy link
Copy Markdown
Member

You could also use a byte for the adjMatrix. This would also save some space.

Regarding caching the getEdge() query, indeed it makes sense. But now as you state the question is whether we want to merge the two caches.

Another option would be to have two different GraphOrdering subclasses, one using the simple caching with the byte and one using both the byte and the Edge. The selection of which to use could happen in the VF2AbstractIsomorphismInspector constructor. Perhaps adding one more constructor parameter, something like aggressivelyCacheEdges.

@jsichi Any ideas on this one?

@iotamudelta

Copy link
Copy Markdown
Author

Yeah, I like the byte idea a lot. I'll update this PR later today with it.

I'll make the argument that if you want the performance of cached edges and (currently) could afford N*N of objects (Boolean), adding one byte per entry (for the byte + Edge caching matrices) will be acceptable and is the typical use case while improving performance for both hasEdge() and getEdge().

Note that with the additional Edge[][] cache, I see the following routines in my use case (first number number of events, all routines their self time):

1005 VF2SubgraphIsomorphismState.isFeasiblePair()
201 VF2State.nextPair()
98 VF2State.backtrack()
89 VF2State.addPair()
49 VF2ubgraphMappingIterator.match()

So self time of isFeasiblePair() is still rather high?

@iotamudelta

iotamudelta commented Apr 25, 2020 •

Copy link
Copy Markdown
Author

Thinking more about it, it should be less:
per element of Boolean matrix (current solution) - 16 bytes (minimum 64 bit object size)
per element of byte matrix - 1 byte
per element of Edge[][] reference (since there would be no copy) - 4 bytes (assuming JVM compressed pointers)

so the combined byte + Edge[][] solution should arrive at 5 bytes per element vs the current 16 bytes.

Reduces memory footprint by 2x.
@iotamudelta iotamudelta changed the title Change the cache from a Boolean array to a short array for GraphOrdering Change the cache from a Boolean array to a byte array for GraphOrdering Apr 25, 2020
@iotamudelta iotamudelta mentioned this pull request Apr 25, 2020
5 of 7 tasks
@jsichi

jsichi commented Apr 27, 2020

Copy link
Copy Markdown
Member

I agree with @iotamudelta that all-or-nothing caching should be good enough; fine-grained control over what to cache seems like it would be overkill here.

@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.

private int[][] incomingEdges;
private Boolean[][] adjMatrix;
/*
* if caching is enabled, this contains cached information on edges existing, values:

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.

Convert this into a Javadoc comment and use proper formatting (unordered list HTML tags).

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.

done.


if (cacheEdges) {
containsEdge = adjMatrix[v1Number][v2Number];
final byte cache = adjMatrix[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.

Save the address computation for use in the update below.

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.

done

@iotamudelta

Copy link
Copy Markdown
Author

@jsichi concerning BitSet - wouldn't this be again an object w/ associated overhead?

@jsichi

jsichi commented Apr 27, 2020

Copy link
Copy Markdown
Member

It would be a single object for the entire ordering.

@iotamudelta

Copy link
Copy Markdown
Author

@jsichi see updated implementation. It doesn't make a big performance difference compared to byte in my tests but the tighter representation is then indeed at an advantage.

Let me know if that's what you were looking for, then I'll adjust the PR title and summary accordingly.

@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.

@d-michail are you OK with the latest code?

Boolean containsEdge = null;


final int cacheIndex = 2*(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.

Initialize to 0 and defer computation. (Probably the compiler is smart enough to do this anyway, but hey.

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.

yeah, I'm rather sure the JIT would have even figured out the double cacheIndex computation before. but better be safe than sorry.

done.

@iotamudelta iotamudelta changed the title Change the cache from a Boolean array to a byte array for GraphOrdering Change the cache from a Boolean array to a BitSet for GraphOrdering Apr 28, 2020
@iotamudelta

Copy link
Copy Markdown
Author

Also updated the title and PR summary here to reflect use of BitSet

@iotamudelta

Copy link
Copy Markdown
Author

@jsichi I've done some more benchmarking on this solution and unfortunately it seems to be slower than the byte one. I'd hence propose to go back to that before merging. It's only 6 bit more expensive than the BitSet one and much faster.

Thoughts?

@iotamudelta iotamudelta changed the title Change the cache from a Boolean array to a BitSet for GraphOrdering Change the cache from a Boolean array to a byte array for GraphOrdering Apr 29, 2020
@jsichi

jsichi commented Apr 29, 2020

Copy link
Copy Markdown
Member

That's fine; thanks for benchmarking. Just curious--was it a noticeable difference for the isomorphism detection as a whole, or only when microbenchmarking directly against GraphOrdering?

@iotamudelta

Copy link
Copy Markdown
Author

Since the rest of the isomorphism is still rather expensive, it didn't yield a larger overall impact (which is why I missed it first). However, looking closer at GraphOrdering and BitSet itself, they combined got much more expensive. So it would only show up in microbenchmarking currently.

I think once this and #918 is settled, there is more to look at (isFeasiblePair, nextPair, addPair and backtrack self times, as well as the GC pressure coming from creation of VF2SubgraphIsomorphismState objects - 6 GB per minute).

@jsichi

jsichi commented Apr 30, 2020

Copy link
Copy Markdown
Member

Thanks!

@jsichi
jsichi merged commit d4a2c1a into jgrapht:master Apr 30, 2020
@d-michail

Copy link
Copy Markdown
Member

Looks good!

@iotamudelta
iotamudelta deleted the better_caching branch April 30, 2020 16:19
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.

3 participants