Skip to content

Replace trove4j with hppc - #878

Closed
karussell wants to merge 8 commits into
masterfrom
trove_hppc
Closed

karussell wants to merge 8 commits into
masterfrom
trove_hppc

Conversation

@karussell

@karussell karussell commented Nov 20, 2016 •

Copy link
Copy Markdown
Member

This fixes an early issue #34 and improves #562

The advantages:

  • core no longer relies on any LGPL dependency, which makes using the library a no-brainer then on Android & iOS (license-wise one could have argumented that e.g. dalvik merges trove with our core and 'infects' it and for iOS it could be even worse)
  • hppc is a smaller library (<2MB instead of >3MB)
  • seems to work very similar fast now these days

Disadvantages

  • took a bit time to make it working, see here for the detailed issues
  • some unknown bugs or performance changes?

Not yet done:

  • hppc has only 0 for default value but -1 was required in constructor -> GHTBitSet (TIntHashSet); OSMShapeReader -> TObjectIntHashMap; OSMReader -> edgeIdToOsmWayIdMap (GHIntLongHashMap);
  • fix some license headers (GHIntHashSet.java)
  • review by community
  • deployed and tested to staging server world wide
  • deploy to production (also ww)
  • is hashmixing only important for mixing two hashes or also when resizing a hash? Or other way around if trove4j also this problem we can probably ignore it for now but must be aware of it e.g. if there are performance problems.
  • fix android docs about trove (docs/android/index.md#Problems)
  • fix iOS with @clns its help

@karussell karussell added this to the 0.9 milestone Nov 20, 2016
@karussell karussell mentioned this pull request Nov 21, 2016
4 of 5 tasks
@karussell

Copy link
Copy Markdown
Member Author

Can I interpret the two thumbs as review :) ? @devemux86 @boldtrn? Just wanted someone to have a quick scan over it that I did not introduce very obvious problems :) (will also have another look now)

public void testViaEdges_WithCoordinates() {
GraphHopperStorage ghStorage = createTestStorage();
Path p = calcPath(ghStorage, 0, 1, 2, 3);
assertEquals(Helper.createTList(9, 1, 2, 8), p.calcNodes());

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

virtual nodes get a different id because of the hashmap edge2res in QueryGraph creates them in a different order

if (edgeIdToOsmWayIdMap == null)
edgeIdToOsmWayIdMap = new TIntLongHashMap(getOsmWayIdSet().size(), 0.5f, -1, -1);

edgeIdToOsmWayIdMap = new GHIntLongHashMap(getOsmWayIdSet().size(), 0.5f);

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Here we do not return -1 anymore if empty but we can easily check if the key is contained via the direct method

*/
public class OSMShapeFileReader extends ShapeFileReader {
private static final int COORD_STATE_UNKNOWN = -1;
private static final int COORD_STATE_UNKNOWN = 0;

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

empty value in the hashmap is now 0 ... maybe replace this with a contains check? But as we do not use 0 this should be fine too

@devemux86

Copy link
Copy Markdown
Contributor

Oh sorry @karussell I just expressed my opinion for your decision. 🙂

@karussell

karussell commented Nov 21, 2016 •

Copy link
Copy Markdown
Member Author

Got a bit feedback from the hppc author in the aboved linked thread.

Tried also koloboke as it was mentioned from him and it could be better in terms of speed but the perf numbers are identical at least for CH and non-CH routing. Also both communities are rather small but hppc is used more (90 deps) and from bigger projects like cassandra, ElasticSearch, solr, ... and does everything we need (at least now).

@devemux86 no problem

@boldtrn

boldtrn commented Nov 21, 2016

Copy link
Copy Markdown
Member

Oh sorry @karussell I just expressed my opinion for your decision. 🙂

Me, too :). But I will have a look at your changes and will comment if anything might be an issue.

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

I could not find any obvious issue, but I have not looked into every small change (I ignored the changes where just the dependencies changed). My comments are mostly questions, as I would have implemented some things differently, so mostly curiosity.

final int[] buffer = this.buffer;
for (int start = 0, end = size() - 1; start < end; start++, end--) {
// swap the values
int tmp = buffer[start];

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.

Maybe we should define the int outside of the for? Not sure if this gets optimized, but I introduced a "bug" with String once, so I became a bit cautious with these things.

public final GHIntArrayList shuffle(Random random) {
int[] buffer = this.buffer;
int max = size();
int maxHalf = max / 2;

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.

Why only half? For example the implementation of Collections.shuffle does it for every element except the first.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

To avoid a "re-shuffle" but this does not really matter to me

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.

Ok, so if you don't need a guaranteed complete shuffle this should be fine.


Iterator<IntCursor> iter = set.iterator();
final int val = iter.next().value;
set.removeAll(new IntPredicate() {

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.

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.

Sorry, I should have read the thread first....

It's better to use removeAll(IntPredicate predicate).

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Hmmh, now that I'm re-reading this this looks strange to me as remove(int) seems to be very similar. Maybe he misunderstand something.


/**
* @return a copy of inputArray from [offset, offset+length)
*/

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.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Ah, nice. It should be identical and so we can remove this.

@karussell

karussell commented Nov 22, 2016 •

Copy link
Copy Markdown
Member Author

Thanks for the review @boldtrn - do you understand the hashmixing problem Dawid mentioned? Here is some code&explanation for this, I do not see that this can be the case for us and reproduction of behaviour seems to me a very important feature of GH for now (and we never had problems with trove4j).

@boldtrn

boldtrn commented Nov 22, 2016

Copy link
Copy Markdown
Member

do you understand the hashmixing problem Dawid mentioned?

I am not sure. This sounds to me like a typical, hash collision scenario. Bascially with open addressing you are calculating a hash (index of an array) for a key. If you put an element into the hashtable you are putting it into the index of the array. If the index is already used, there are different strategies to resolve the issue, one is linear probing. So what you do with linear probing you just put the element at index+1 if the slot is available, if not put it at index+2, and so on. So the problem with this is that in the worst case you end up with linear put and get operations, whereas this should be constant for a hashmap.

So from what I understand is that if you are downsizing an existing map, it might happen that you create a lot of these collisions and therefore you end up with a hash map that might be as bad as a random array.

So if you are iterating over the map in key order for creating a new table, the chance for creating very bad tables is increased. Lets assume you have 3 key collisions for the first key in your map, so you start to put them at index: 0,1,2. In the bigger map that's not a problem as the next key might be at index 5, but with the smaller map this element might be put at index 1, but index 1 is occupied so you put it at index 3. This goes on and on, therefore the lookup of these elements becomes very bad. If you would look for the element that should be at index 1, you go there and compare the key, you find the key does not match so you need to check index 2, and so on.

So if you would do this with iterating randomly over the hashmap the chances for creating a bad hashmap might be decreased.

That's how I understand it, but I am not sure if he meant that :).

@karussell

karussell commented Nov 23, 2016 •

Copy link
Copy Markdown
Member Author

Yes, this part I understand. But I'm not sure if we would currently stumble over such a problem as we

  1. create a different hashing strategy depending on the size of the hash (HashOrderMixing.constant) and
  2. do not merge two hashes somewhere else (the two would even need to be of the same size)

@boldtrn

boldtrn commented Nov 23, 2016

Copy link
Copy Markdown
Member

Ok, in this case I guess we would not stumble over the problem.

This was referenced Dec 2, 2016
@karussell karussell closed this Dec 21, 2016
@karussell
karussell deleted the trove_hppc branch December 21, 2016 09:00
@devemux86

Copy link
Copy Markdown
Contributor

If I may report that using extensively GraphHopper with hppc lately and not seen so far any issues with the change. 🙂

@karussell

Copy link
Copy Markdown
Member Author

Nice & Thanks for the feedback - we did the same observation :) !

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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants