Skip to content

Store distances as integers #1663

Open
Open

Description

@easbar

Using 0.1m as distance unit should be sufficiently precise also for indoor routing and the maximum distance would still be sufficiently large: 214748km so more than five times the circumference of the earth. IMO using integers is more comfortable and prevents rounding / comparison issues.

Related #435

Activity

  1. boldtrn commented on Jul 19, 2019

    @boldtrn
    Member

    I think that's a good idea! If I understand this correct, this would mean changing the INT_DISTANCE_FACTOR in EdgeAccess to 10?

  2. easbar commented on Jul 19, 2019

    @easbar
    MemberAuthor

    Yes changing the distance factor, but also I think distances should be stored as ints and any int/double conversion should happen somewhere in the top layer (osm import / user api), so internally all distances are ints.

    Or in some places as karussell suggested in #1661:

    For edge.getDistance it could be valuable to investigate if we can include it in IntsRef. (btw: there is an interesting difference to e.g. max_speed or road_class then: the distance needs to be splitted for the virtual edges in the QueryGraph. And that is also the reason I did not try this yet ;) )

  3. boldtrn commented on Jul 19, 2019

    @boldtrn
    Member

    Ah, now I understand it. Now I am not so sure if I think this is a good idea :). This change could require significant changes for everyone using GraphHopper as a dependency and would make using it feel a bit cumbersome.

    For example many existing FlagEncoders oder custom weightings could break due to that.

    What improvements do you expect from this change? Better memory efficiency of 4 vs 8 bytes and better performance by avoiding the conversion from int to double?

    If that's the case, it might make sense to have an internal method like getIntDistance and the regular getDistance? So we could use the int on the low level where performance is of the essence and use the convenient version where performance is not that critical?

  4. easbar commented on Jul 19, 2019

    @easbar
    MemberAuthor

    I think my vision would be to interpret the 4 bytes we are using for distance in EdgeAccess at the moment as integer number that tells us the distance as multiple of 0.1m. I think the main advantage of
    using int over double/float is that floating point numbers are never exact, and we have to include error margins etc. when comparing them.

    At the same time I fully agree that having EdgeIteratorState#getDistance() return the distance in multiples of 0.1m would be very ugly, because it would result in x10 errors in existing code without giving a compiler error or similar.

    So maybe having an internal method int getIntDistancel() for the edge iterator as you suggest could be a good solution, yes.

    Better memory efficiency of 4 vs 8 bytes

    Yes, in a few cases this might make a difference, not with storage, because we are already using only 4 bytes, but sometimes we keep many distances in memory.

    better performance by avoiding the conversion from int to double?

    Hmm, I do not think the conversion is critical for performance, but haven't really tried either.

    and would make using it feel a bit cumbersome.

    Do you think dealing with int in general is more cumbersome than with double ? I do not see a reason why this would be the case (except one has to pay some attention with division). In most cases int will be auto-cast to double anyway ? Or do you mean dealing with 0.1m as unit is cumbersome ? This is of course very awkward and I agree that on the public API level there should be methods returning the distance in meters.

  5. boldtrn commented on Jul 19, 2019

    @boldtrn
    Member

    Thanks for the explanation!

    Hmm, I do not think the conversion is critical for performance, but haven't really tried either.

    I haven't profiled it either. IIRC when calculating CH we would call the getDistance method quite often? If this method can avoid an unnecessary division, I guess there could be a minor performance boost. Probably not worth the effort though.

    Or do you mean dealing with 0.1m as unit is cumbersome ?

    Yes, that's what I mean with cumbersome.

  6. karussell commented on Aug 12, 2019

    @karussell
    Member

    So maybe having an internal method int getIntDistancel() for the edge iterator as you suggest could be a good solution, yes

    If distance would be implemented also as an EncodedValue we would automatically get such a method:

    edge.get(distance_enc) // returns meters as double using UnsignedDecimalEncodedValue
    edge.get((IntEncodedValue)distance_enc) // returns multiples of 0.1 as int using UnsignedIntEncodedValue
    

    But already without this, the change from 1000 to 10 is hard: several tests break :(
    In some cases even a different route is returned.

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