Skip to content

Commit fba4c2d

Browse files
Share common CacheHandle (#7688)
* Share common CacheHandle * Changelog * License update
1 parent 5293d3a commit fba4c2d

File tree

9 files changed

+70
-74
lines changed

9 files changed

+70
-74
lines changed

LICENSE.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ Mapbox Navigation for Android version 2.0
33

44
Mapbox Navigation Android SDK
55

6-
Copyright ©2022 - 2023 Mapbox, Inc. All rights reserved.
6+
Copyright ©2022 - 2024 Mapbox, Inc. All rights reserved.
77

88
The software and files in this repository (collectively, "Software") are licensed under the Mapbox TOS for use only with the relevant Mapbox product(s) listed at www.mapbox.com/pricing. This license allows developers with a current active Mapbox account to use and modify the authorized portions of the Software as needed for use only with the relevant Mapbox product(s) through their Mapbox account in accordance with the Mapbox TOS. This license terminates automatically if a developer no longer has a Mapbox account in good standing or breaches the Mapbox TOS. For the license terms, please see the Mapbox TOS at https://www.mapbox.com/legal/tos/ which incorporates the Mapbox Product Terms at www.mapbox.com/legal/service-terms. If this Software is a SDK, modifications that change or interfere with marked portions of the code related to billing, accounting, or data collection are not authorized and the SDK sends limited de-identified location and usage data which is used in accordance with the Mapbox TOS. [Updated 2023-04]
99

changelog/unreleased/bugfixes/7688.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
- Fixed a bug with multiple instances of cache which resulted in excessive memory consumption.

libnavigation-core/src/main/java/com/mapbox/navigation/core/MapboxNavigation.kt

Lines changed: 26 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -442,15 +442,26 @@ class MapboxNavigation @VisibleForTesting internal constructor(
442442
navigationOptions.deviceProfile,
443443
navigatorConfig,
444444
)
445+
446+
val tilesConfig = createTilesConfig(
447+
isFallback = false,
448+
tilesVersion = navigationOptions.routingTilesOptions.tilesVersion
449+
)
450+
445451
historyRecorderHandles = createHistoryRecorderHandles(config)
452+
453+
val cacheHandle = NavigatorLoader.createCacheHandle(
454+
config,
455+
tilesConfig,
456+
historyRecorderHandles.composite
457+
)
458+
446459
nativeRouter = NavigatorLoader.createNativeRouterInterface(
447-
NavigatorLoader.createConfig(navigationOptions.deviceProfile, navigatorConfig),
448-
createTilesConfig(
449-
isFallback = false,
450-
tilesVersion = navigationOptions.routingTilesOptions.tilesVersion
451-
),
460+
cacheHandle,
461+
config,
452462
historyRecorderHandles.composite,
453463
)
464+
454465
val routeParsingManager = createRouteParsingManager(
455466
navigationOptions.longRoutesOptimisationOptions
456467
)
@@ -472,12 +483,9 @@ class MapboxNavigation @VisibleForTesting internal constructor(
472483
else -> LegacyNavigationRouterAdapter(LegacyRouterAdapter(result))
473484
}
474485
navigator = NavigationComponentProvider.createNativeNavigator(
486+
cacheHandle,
475487
config,
476488
historyRecorderHandles.composite,
477-
createTilesConfig(
478-
isFallback = false,
479-
tilesVersion = navigationOptions.routingTilesOptions.tilesVersion
480-
),
481489
navigationOptions.accessToken ?: "",
482490
if (moduleRouter.isInternalImplementation()) {
483491
// We pass null to let NN know that default router is used and it can rely
@@ -2076,10 +2084,18 @@ class MapboxNavigation @VisibleForTesting internal constructor(
20762084
historyRecorderHandles = createHistoryRecorderHandles(config)
20772085

20782086
mainJobController.scope.launch {
2087+
// TODO we should also recreate router and share CacheHandle
2088+
2089+
val cacheHandle = NavigatorLoader.createCacheHandle(
2090+
config,
2091+
createTilesConfig(isFallback, tilesVersion),
2092+
historyRecorderHandles.composite
2093+
)
2094+
20792095
navigator.recreate(
2096+
cacheHandle,
20802097
config,
20812098
historyRecorderHandles.composite,
2082-
createTilesConfig(isFallback, tilesVersion),
20832099
navigationOptions.accessToken ?: "",
20842100
if (moduleRouter.isInternalImplementation()) {
20852101
nativeRouter

libnavigation-core/src/main/java/com/mapbox/navigation/core/NavigationComponentProvider.kt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,10 @@ import com.mapbox.navigation.core.trip.session.eh.EHorizonSubscriptionManagerImp
2525
import com.mapbox.navigation.navigator.internal.MapboxNativeNavigator
2626
import com.mapbox.navigation.navigator.internal.MapboxNativeNavigatorImpl
2727
import com.mapbox.navigation.utils.internal.ThreadController
28+
import com.mapbox.navigator.CacheHandle
2829
import com.mapbox.navigator.ConfigHandle
2930
import com.mapbox.navigator.HistoryRecorderHandle
3031
import com.mapbox.navigator.RouterInterface
31-
import com.mapbox.navigator.TilesConfig
3232
import kotlinx.coroutines.CoroutineScope
3333

3434
internal object NavigationComponentProvider {
@@ -38,15 +38,15 @@ internal object NavigationComponentProvider {
3838
): DirectionsSession = MapboxDirectionsSession(router)
3939

4040
fun createNativeNavigator(
41+
cacheHandle: CacheHandle,
4142
config: ConfigHandle,
4243
historyRecorderComposite: HistoryRecorderHandle?,
43-
tilesConfig: TilesConfig,
4444
accessToken: String,
4545
router: RouterInterface?,
4646
): MapboxNativeNavigator = MapboxNativeNavigatorImpl.create(
47+
cacheHandle,
4748
config,
4849
historyRecorderComposite,
49-
tilesConfig,
5050
accessToken,
5151
router,
5252
)

libnavigation-core/src/test/java/com/mapbox/navigation/core/MapboxNavigationBaseTest.kt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,9 @@ internal open class MapboxNavigationBaseTest {
151151
any(),
152152
)
153153
} returns mockk(relaxed = true)
154+
every {
155+
NavigatorLoader.createCacheHandle(any(), any(), any())
156+
} returns mockk()
154157

155158
mockkObject(MapboxSDKCommon)
156159
every {

libnavigation-core/src/test/java/com/mapbox/navigation/core/MapboxNavigationTest.kt

Lines changed: 15 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -938,15 +938,9 @@ internal class MapboxNavigationTest : MapboxNavigationBaseTest() {
938938
fun `verify tile config path`() {
939939
threadController.cancelAllUICoroutines()
940940
val slot = slot<TilesConfig>()
941-
every {
942-
NavigationComponentProvider.createNativeNavigator(
943-
any(),
944-
any(),
945-
capture(slot),
946-
any(),
947-
any(),
948-
)
949-
} returns navigator
941+
942+
every { NavigatorLoader.createCacheHandle(any(), capture(slot), any()) } returns mockk()
943+
950944
val options = navigationOptions.toBuilder()
951945
.routingTilesOptions(RoutingTilesOptions.Builder().build())
952946
.build()
@@ -960,15 +954,9 @@ internal class MapboxNavigationTest : MapboxNavigationBaseTest() {
960954
fun `verify tile config dataset`() {
961955
threadController.cancelAllUICoroutines()
962956
val slot = slot<TilesConfig>()
963-
every {
964-
NavigationComponentProvider.createNativeNavigator(
965-
any(),
966-
any(),
967-
capture(slot),
968-
any(),
969-
any(),
970-
)
971-
} returns navigator
957+
958+
every { NavigatorLoader.createCacheHandle(any(), capture(slot), any()) } returns mockk()
959+
972960
val options = navigationOptions.toBuilder()
973961
.routingTilesOptions(
974962
RoutingTilesOptions.Builder()
@@ -1252,15 +1240,9 @@ internal class MapboxNavigationTest : MapboxNavigationBaseTest() {
12521240
fun `verify tile config tilesVersion and isFallback on init`() {
12531241
threadController.cancelAllUICoroutines()
12541242
val slot = slot<TilesConfig>()
1255-
every {
1256-
NavigationComponentProvider.createNativeNavigator(
1257-
any(),
1258-
any(),
1259-
capture(slot),
1260-
any(),
1261-
any(),
1262-
)
1263-
} returns navigator
1243+
1244+
every { NavigatorLoader.createCacheHandle(any(), capture(slot), any()) } returns mockk()
1245+
12641246
val tilesVersion = "tilesVersion"
12651247
val options = navigationOptions.toBuilder()
12661248
.routingTilesOptions(
@@ -1293,15 +1275,10 @@ internal class MapboxNavigationTest : MapboxNavigationBaseTest() {
12931275
mapboxNavigation = MapboxNavigation(navigationOptions, threadController)
12941276

12951277
val tileConfigSlot = slot<TilesConfig>()
1278+
12961279
every {
1297-
navigator.recreate(
1298-
any(),
1299-
any(),
1300-
capture(tileConfigSlot),
1301-
any(),
1302-
any(),
1303-
)
1304-
} just Runs
1280+
NavigatorLoader.createCacheHandle(any(), capture(tileConfigSlot), any())
1281+
} returns mockk()
13051282

13061283
val tilesVersion = "tilesVersion"
13071284
val latestTilesVersion = "latestTilesVersion"
@@ -1333,15 +1310,10 @@ internal class MapboxNavigationTest : MapboxNavigationBaseTest() {
13331310
mapboxNavigation = MapboxNavigation(navigationOptions, threadController)
13341311

13351312
val tileConfigSlot = slot<TilesConfig>()
1313+
13361314
every {
1337-
navigator.recreate(
1338-
any(),
1339-
any(),
1340-
capture(tileConfigSlot),
1341-
any(),
1342-
any(),
1343-
)
1344-
} just Runs
1315+
NavigatorLoader.createCacheHandle(any(), capture(tileConfigSlot), any())
1316+
} returns mockk()
13451317

13461318
fallbackObserverSlot.captured.onCanReturnToLatest("")
13471319

libnavigator/src/main/java/com/mapbox/navigation/navigator/internal/MapboxNativeNavigator.kt

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@ import com.mapbox.navigator.RouteAlternativesControllerInterface
2727
import com.mapbox.navigator.RouterInterface
2828
import com.mapbox.navigator.SetRoutesReason
2929
import com.mapbox.navigator.SetRoutesResult
30-
import com.mapbox.navigator.TilesConfig
3130

3231
/**
3332
* Provides API to work with native Navigator class. Exposed for internal usage only.
@@ -38,9 +37,9 @@ interface MapboxNativeNavigator {
3837
* Initialize the navigator with a device profile
3938
*/
4039
fun create(
40+
cacheHandle: CacheHandle,
4141
config: ConfigHandle,
4242
historyRecorderComposite: HistoryRecorderHandle?,
43-
tilesConfig: TilesConfig,
4443
accessToken: String,
4544
router: RouterInterface?,
4645
): MapboxNativeNavigator
@@ -49,9 +48,9 @@ interface MapboxNativeNavigator {
4948
* Reinitialize the navigator with a device profile
5049
*/
5150
fun recreate(
51+
cacheHandle: CacheHandle,
5252
config: ConfigHandle,
5353
historyRecorderComposite: HistoryRecorderHandle?,
54-
tilesConfig: TilesConfig,
5554
accessToken: String,
5655
router: RouterInterface,
5756
)

libnavigator/src/main/java/com/mapbox/navigation/navigator/internal/MapboxNativeNavigatorImpl.kt

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,6 @@ import com.mapbox.navigator.RouterInterface
4343
import com.mapbox.navigator.SetRoutesParams
4444
import com.mapbox.navigator.SetRoutesReason
4545
import com.mapbox.navigator.SetRoutesResult
46-
import com.mapbox.navigator.TilesConfig
4746
import kotlinx.coroutines.suspendCancellableCoroutine
4847
import kotlinx.coroutines.withContext
4948
import java.util.concurrent.CopyOnWriteArraySet
@@ -78,18 +77,18 @@ object MapboxNativeNavigatorImpl : MapboxNativeNavigator {
7877
* functions within [MapboxNativeNavigatorImpl]
7978
*/
8079
override fun create(
80+
cacheHandle: CacheHandle,
8181
config: ConfigHandle,
8282
historyRecorderComposite: HistoryRecorderHandle?,
83-
tilesConfig: TilesConfig,
8483
accessToken: String,
8584
router: RouterInterface?,
8685
): MapboxNativeNavigator {
8786
navigator?.shutdown()
8887

8988
val nativeComponents = NavigatorLoader.createNavigator(
89+
cacheHandle,
9090
config,
9191
historyRecorderComposite,
92-
tilesConfig,
9392
router,
9493
)
9594
navigator = nativeComponents.navigator
@@ -107,14 +106,14 @@ object MapboxNativeNavigatorImpl : MapboxNativeNavigator {
107106
* Recreate native objects and notify listeners.
108107
*/
109108
override fun recreate(
109+
cacheHandle: CacheHandle,
110110
config: ConfigHandle,
111111
historyRecorderComposite: HistoryRecorderHandle?,
112-
tilesConfig: TilesConfig,
113112
accessToken: String,
114113
router: RouterInterface
115114
) {
116115
val storeNavSessionState = navigator!!.storeNavigationSession()
117-
create(config, historyRecorderComposite, tilesConfig, accessToken, router)
116+
create(cacheHandle, config, historyRecorderComposite, accessToken, router)
118117
navigator!!.restoreNavigationSession(storeNavSessionState)
119118
nativeNavigatorRecreationObservers.forEach {
120119
it.onNativeNavigatorRecreated()

libnavigator/src/main/java/com/mapbox/navigation/navigator/internal/NavigatorLoader.kt

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -49,39 +49,45 @@ object NavigatorLoader {
4949
}
5050

5151
internal fun createNavigator(
52+
cacheHandle: CacheHandle,
5253
config: ConfigHandle,
5354
historyRecorderComposite: HistoryRecorderHandle?,
54-
tilesConfig: TilesConfig,
5555
router: RouterInterface?,
5656
): NativeComponents {
57-
val cache = CacheFactory.build(tilesConfig, config, historyRecorderComposite)
5857
val navigator = Navigator(
5958
config,
60-
cache,
59+
cacheHandle,
6160
historyRecorderComposite,
6261
router,
6362
)
64-
val graphAccessor = GraphAccessor(cache)
65-
val roadObjectMatcher = RoadObjectMatcher(cache)
63+
val graphAccessor = GraphAccessor(cacheHandle)
64+
val roadObjectMatcher = RoadObjectMatcher(cacheHandle)
6665

6766
return NativeComponents(
6867
navigator,
6968
graphAccessor,
70-
cache,
69+
cacheHandle,
7170
roadObjectMatcher,
7271
navigator.routeAlternativesController,
7372
)
7473
}
7574

76-
fun createNativeRouterInterface(
75+
fun createCacheHandle(
7776
config: ConfigHandle,
7877
tilesConfig: TilesConfig,
7978
historyRecorder: HistoryRecorderHandle?,
79+
): CacheHandle {
80+
return CacheFactory.build(tilesConfig, config, historyRecorder)
81+
}
82+
83+
fun createNativeRouterInterface(
84+
cacheHandle: CacheHandle,
85+
config: ConfigHandle,
86+
historyRecorder: HistoryRecorderHandle?,
8087
): RouterInterface {
81-
val cache = CacheFactory.build(tilesConfig, config, historyRecorder)
8288
return RouterFactory.build(
8389
RouterType.HYBRID,
84-
cache,
90+
cacheHandle,
8591
config,
8692
historyRecorder,
8793
)

0 commit comments

Comments
 (0)