From d592736252b362fe8ca2ad18e38da20c282c44c1 Mon Sep 17 00:00:00 2001 From: Victor Shcherb Date: Sun, 12 Jul 2020 23:29:11 +0200 Subject: [PATCH] Support sharing native context and passing road id to native (to speed up search) --- .../main/java/net/osmand/NativeLibrary.java | 2 ++ .../osmand/router/RoutePlannerFrontEnd.java | 4 ++++ .../osmand/router/RouteResultPreparation.java | 2 +- .../java/net/osmand/router/RoutingContext.java | 18 +++++++++++++++++- 4 files changed, 24 insertions(+), 2 deletions(-) diff --git a/OsmAnd-java/src/main/java/net/osmand/NativeLibrary.java b/OsmAnd-java/src/main/java/net/osmand/NativeLibrary.java index 60a5b3dd10..0189d38378 100644 --- a/OsmAnd-java/src/main/java/net/osmand/NativeLibrary.java +++ b/OsmAnd-java/src/main/java/net/osmand/NativeLibrary.java @@ -153,6 +153,8 @@ public class NativeLibrary { protected static native NativeRouteSearchResult loadRoutingData(RouteRegion reg, String regName, int regfp, RouteSubregion subreg, boolean loadObjects); + public static native void deleteNativeRoutingContext(long handle); + protected static native void deleteRenderingContextHandle(long handle); protected static native void deleteRouteSearchResult(long searchResultHandle); diff --git a/OsmAnd-java/src/main/java/net/osmand/router/RoutePlannerFrontEnd.java b/OsmAnd-java/src/main/java/net/osmand/router/RoutePlannerFrontEnd.java index fb5f73c460..eb1d3b9352 100644 --- a/OsmAnd-java/src/main/java/net/osmand/router/RoutePlannerFrontEnd.java +++ b/OsmAnd-java/src/main/java/net/osmand/router/RoutePlannerFrontEnd.java @@ -217,6 +217,7 @@ public class RoutePlannerFrontEnd { if (gctx.ctx.calculationProgress == null) { gctx.ctx.calculationProgress = new RouteCalculationProgress(); } + gctx.ctx.keepNativeRoutingContext = true; List gpxPoints = generageGpxPoints(points, gctx); GpxPoint start = null; GpxPoint prev = null; @@ -281,6 +282,7 @@ public class RoutePlannerFrontEnd { if(gctx.ctx.calculationProgress != null) { gctx.ctx.calculationProgress.timeToCalculate = System.nanoTime() - timeToCalculate; } + gctx.ctx.deleteNativeRoutingContext(); BinaryRoutePlanner.printDebugMemoryInformation(gctx.ctx); calculateGpxRoute(gctx, gpxPoints); if (!gctx.res.isEmpty()) { @@ -538,6 +540,8 @@ public class RoutePlannerFrontEnd { // for native routing this is possible when point lies on intersection of 2 lines // solution here could be to pass to native routing id of the route // though it should not create any issue + System.out.println("??? not found " + start.pnt.getRoad().getId() + " instead " + + res.get(0).getObject().getId()); } } start.routeToTarget = res; diff --git a/OsmAnd-java/src/main/java/net/osmand/router/RouteResultPreparation.java b/OsmAnd-java/src/main/java/net/osmand/router/RouteResultPreparation.java index 9892bfb730..d7ebbaba8f 100644 --- a/OsmAnd-java/src/main/java/net/osmand/router/RouteResultPreparation.java +++ b/OsmAnd-java/src/main/java/net/osmand/router/RouteResultPreparation.java @@ -395,7 +395,7 @@ public class RouteResultPreparation { List result = new ArrayList(); if (finalSegment != null) { ctx.routingTime += finalSegment.distanceFromStart; - println("Routing calculated time distance " + finalSegment.distanceFromStart); + // println("Routing calculated time distance " + finalSegment.distanceFromStart); // Get results from opposite direction roads RouteSegment segment = finalSegment.reverseWaySearch ? finalSegment : finalSegment.opposite.getParentRoute(); diff --git a/OsmAnd-java/src/main/java/net/osmand/router/RoutingContext.java b/OsmAnd-java/src/main/java/net/osmand/router/RoutingContext.java index 46eda15f90..922e225855 100644 --- a/OsmAnd-java/src/main/java/net/osmand/router/RoutingContext.java +++ b/OsmAnd-java/src/main/java/net/osmand/router/RoutingContext.java @@ -48,6 +48,10 @@ public class RoutingContext { public final Map> map = new LinkedHashMap>(); public final Map reverseMap = new LinkedHashMap(); + // 0. Reference to native routingcontext for multiple routes + public long nativeRoutingContext; + public boolean keepNativeRoutingContext; + // 1. Initial variables public int startX; public int startY; @@ -62,11 +66,13 @@ public class RoutingContext { public boolean publicTransport; + public RouteCalculationProgress calculationProgress; public boolean leftSideNavigation; public List previouslyCalculatedRoute; public PrecalculatedRouteDirection precalculatedRouteDirection; + // 2. Routing memory cache (big objects) TLongObjectHashMap> indexedSubregions = new TLongObjectHashMap>(); @@ -812,7 +818,17 @@ public class RoutingContext { return 0; } + public synchronized void deleteNativeRoutingContext() { + if (nativeRoutingContext != 0) { + NativeLibrary.deleteNativeRoutingContext(nativeRoutingContext); + } + nativeRoutingContext = 0; + } - + @Override + protected void finalize() throws Throwable { + deleteNativeRoutingContext(); + super.finalize(); + } }