Skip to content

point_hint too slow for certain cases #1717

Closed

Description

@karussell

The newly introduced number handling caused an issue. If I just remove

try {
    if (Integer.parseInt(rewrite) > 0)
        isNumber = true;
} catch (NumberFormatException ex) {
}

routing requests like /maps/?point=1.346528%2C103.753174&point=1.348105%2C103.747788&point_hint=&point_hint=Singapore are already much faster. Will see what else can be done.

Activity

  1. added this to the 0.13 milestone on Sep 9, 2019
  2. otbutz commented on Sep 9, 2019

    @otbutz
    Contributor

    I suspect that creating the NumberFormatException is a rather costly operation.

    Ignoring any leading - or + signs:

    private String prepareName(String name) {
        // \s = A whitespace character: [ \t\n\x0B\f\r]
        String[] arr = name.split("\\s");
        List<String> list = new ArrayList<>(arr.length);
        for (int i = 0; i < arr.length; i++) {
            String rewrite = NON_WORD_CHAR.matcher(toLowerCase(arr[i])).replaceAll("");
            String tmp = rewriteMap.get(rewrite);
            if (tmp != null)
                rewrite = tmp;
            
            // Ignore matching short frases like de, la, ... except it is a number
            if (rewrite.length() > 2 || StringUtils.isNumeric(rewrite)) {
                list.add(rewrite);
            }
        }
        return listToString(list);
    }

    Bonus point: it should be only evaluated for the rewrite.length() <= 2 case now

  3. karussell commented on Sep 9, 2019

    @karussell
    MemberAuthor

    Yeah, I was really surprised to see that this was so slow. In general the point_hint parsing is very costly. See #1718, where I tried to improve some other parts.

    One of the causes for the slowness seems to be the rather expensive NON_WORD_CHAR.replaceAll.

    Or even a cache for "name->prepareName" could make sense (that we could try to reuse for different edges but also for different waypoints somehow, although I would try to avoid this as caching is hard)

  4. otbutz commented on Sep 9, 2019

    @otbutz
    Contributor

    Maybe it's better to create a WORD_CHAR matcher for the whole name and iterate over the positive matches.

  5. otbutz commented on Sep 9, 2019

    @otbutz
    Contributor
    Pattern WORD_CHAR = Pattern.compile("\\p{LD}+");
    Matcher wordCharMatcher = WORD_CHAR.matcher(name);
    List<String> list = new ArrayList<>();
    while (wordCharMatcher.find()) {
        String rewrite = toLowerCase(wordCharMatcher.group());
        String tmp = rewriteMap.get(rewrite);
        if (tmp != null)
            rewrite = tmp;
        
        // Ignore matching short frases like de, la, ... except it is a number
        if (rewrite.length() > 2 || StringUtils.isNumeric(rewrite)) {
            list.add(rewrite);
        }
    }
    return listToString(list);
  6. otbutz commented on Sep 9, 2019

    @otbutz
    Contributor

    If we'd like to ignore cases of alphanumerical characters intermixed with removeable ones: Pattern.compile("(?<=^|\\s)\\p{LD}+(?=\\s|$)")

  7. karussell commented on Sep 9, 2019

    @karussell
    MemberAuthor

    That is a good idea that I tried. Although it has the same speed I prefer it over the current solution due to its simplicity. The only failing test is now:

    Expected :i28north
    Actual   :28north
    	at com.graphhopper.routing.util.NameSimilarityEdgeFilterTest.normalization(NameSimilarityEdgeFilterTest.java:139)
    

    This is due to the double cleaning (once with string.split and once with matcher). I'm unsure if this is important as e.g. "B 97" is also removed to "97".

  8. otbutz commented on Sep 9, 2019

    @otbutz
    Contributor

    It seems to be the only test case which fails and I wouldn't have expected it to return i28north to be honest. Blindly adding the i from I- could be dangerous.

  9. karussell commented on Sep 9, 2019

    @karussell
    MemberAuthor

    Ok

    btw: compared to the old master the PR this matcher loop (and also the "complicated split PR") is a bit faster :). So the point_hint slowed down the CH requests on old master by roughly 35%. Now it is "only" 31% slower.

  10. easbar commented on Sep 9, 2019

    @easbar
    Member

    Why is point_hint so expensive ? Haven't looked into this, but I thought we 'only' need to look up the street names for a few edges (the ones that are close to the snapped location after the location index lookup) ?

  11. otbutz commented on Sep 9, 2019

    @otbutz
    Contributor

    @karussell it might be worth to write a quick JMH benchmark to track this down properly.

  12. karussell commented on Sep 9, 2019

    @karussell
    MemberAuthor

    @karussell it might be worth to write a quick JMH benchmark to track this down properly.

    I used our Measurement class. It kind of pre-dates JMH but does everything on real data. Also to know the global context i.e. how heavy something is compared to something else to avoid micro tuning.

    Why is point_hint so expensive ?

    First, because we loop over so many edges in the neighborhood and second, because CH is extremely fast ;)

  13. otbutz commented on Sep 9, 2019

    @otbutz
    Contributor

    Could you profile the execution with jvisualvm? Would be nice to see where we're loosing performance exactly.

  14. otbutz commented on Sep 9, 2019

    @otbutz
    Contributor

    Sidenote: we seem to support Unicode Strings here but use the regular String similarity metrics without any Unicode normalization

  15. nilsnolde commented on Sep 9, 2019

    @nilsnolde

    Can I sponsor you a Rocket.Chat server for chats? ;)

  16. karussell commented on Sep 9, 2019

    @karussell
    MemberAuthor

    A simple LinkedHashMap (LRU) reduces this now to only +20%

    Could you profile the execution with jvisualvm? Would be nice to see where we're loosing performance exactly.

    Sure. JaroWinkler.similarity is one and the other is still prepareName

    but use the regular String similarity metrics without any Unicode normalization

    What do you mean here?

    @otbutz let us go to https://discuss.graphhopper.com/t/improve-speed-of-point-hint/4728

    (btw: I really dislike that github sends one email per issue entry. Does not make sense if there are multiple per minute)

  17. easbar commented on Sep 9, 2019

    @easbar
    Member

    Can I sponsor you a Rocket.Chat server for chats? ;)

    its easy enough to unsubscribe an issue when it becomes too noisy / is not of your interest ?
    but at least I got to know rocket chat now :)

  18. nilsnolde commented on Sep 9, 2019

    @nilsnolde

    You won't be disappointed, better than Slack IMHO.

    Sorry, I usually don't spam other repos with my personal opinion. And you know I love your work here. I do feel generally though that several discussions in issues/PRs in GH repo is often more an internal discussion, better suited for your forum (still public at least) or chat. Putting only the milestone comments to GH would make it more understandable for other people too, instead of having to go through 50+ comments (you gotta admit, not that uncommon). Anyways, just wanted to mention it:)

  19. otbutz commented on Sep 9, 2019

    @otbutz
    Contributor

    A simple LinkedHashMap (LRU) reduces this now to only +20%

    Wouldn't this imply that there are a lot of entities sharing the exact same name?

    but use the regular String similarity metrics without any Unicode normalization

    What do you mean here?

    https://withblue.ink/2019/03/11/why-you-need-to-normalize-unicode-strings.html
    https://www.w3.org/TR/charmod-norm/#matchingAlgorithm

    In other words: the current approach is only suitable if the names largely consist of ASCII characters as there are multiple ways to generate the same letter in UTF-8. We would need to normalize and apply ASCII-folding to the names and the search term if we want to use in regions with other scripts.

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

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions