maps: use less memory when routing
- Commit
64fc152ff2ef55d38de038c5cd133d48a735115c- Author
- Vayun Mathur <[email protected]>
- When
- 2026-03-18 00:56 UTC (8 months ago)
- Parents
1e1c3b7
Diff
maps/src/main/cpp/CMakeLists.txt
diff --git a/maps/src/main/cpp/CMakeLists.txt b/maps/src/main/cpp/CMakeLists.txtindex 100a8eb3da..48439b8d9d 100644--- a/maps/src/main/cpp/CMakeLists.txt+++ b/maps/src/main/cpp/CMakeLists.txt@@ -1,7 +1,7 @@ cmake_minimum_required(VERSION 3.10.2) project("offlinerouter") -add_library(offlinerouter SHARED native-lib.cpp)+add_library(offlinerouter SHARED native-lib.cpp scratchpad.cpp) find_library(log-lib log) target_link_libraries(offlinerouter ${log-lib})\ No newline at end of file
maps/src/main/cpp/native-lib.cpp
diff --git a/maps/src/main/cpp/native-lib.cpp b/maps/src/main/cpp/native-lib.cppindex cb1351efdf..64b72ccafa 100644--- a/maps/src/main/cpp/native-lib.cpp+++ b/maps/src/main/cpp/native-lib.cpp@@ -9,8 +9,11 @@ #include <algorithm> #include <android/log.h> -#define TAG "OfflineRouterNative"-#define LOGD(...) __android_log_print(ANDROID_LOG_DEBUG, TAG, __VA_ARGS__)+#include "scratchpad.h"++#define LOG_TAG "OfflineRouterNative"+#define LOGD(...) __android_log_print(ANDROID_LOG_DEBUG, LOG_TAG, __VA_ARGS__)+#define LOGE(...) __android_log_print(ANDROID_LOG_ERROR, LOG_TAG, __VA_ARGS__) // Must match Kotlin RouteService.TravelMode ordinal enum TravelMode { WALK = 2, BICYCLE = 3 };@@ -133,64 +136,94 @@ Java_com_vayunmathur_maps_OfflineRouter_init(JNIEnv* env, jobject thiz, jstring return g_nodes && g_edge_index && g_edges && g_spatial; } +static RoutingScratchpad g_scratchpad(20);+ extern "C" JNIEXPORT jdoubleArray JNICALL Java_com_vayunmathur_maps_OfflineRouter_findShortestRouteNative(JNIEnv* env, jobject thiz, jdouble sLat, jdouble sLon, jdouble eLat, jdouble eLon, jint mode) {+ LOGD("Starting route search: (%f, %f) -> (%f, %f), mode: %d", sLat, sLon, eLat, eLon, mode);++ // 1. Reset the scratchpad (increments generation ID, O(1) clear)+ g_scratchpad.reset();+ SnappedEdge start = find_nearest_edge(sLat, sLon, mode); SnappedEdge end = find_nearest_edge(eLat, eLon, mode); - std::vector<uint32_t> g_score(g_node_count, 0xFFFFFFFF);- std::vector<uint32_t> parent(g_node_count, 0xFFFFFFFF); using NodeDist = std::pair<uint32_t, uint32_t>; std::priority_queue<NodeDist, std::vector<NodeDist>, std::greater<NodeDist>> pq; - g_score[start.nodeA] = start.distToA;- g_score[start.nodeB] = start.distToB;+ // 2. Access metadata using the overloaded operator[]+ g_scratchpad[start.nodeA].g_score = start.distToA;+ g_scratchpad[start.nodeB].g_score = start.distToB; uint32_t hA = haversine_mm(g_nodes[start.nodeA].lat_e7, g_nodes[start.nodeA].lon_e7, end.proj_lat, end.proj_lon); uint32_t hB = haversine_mm(g_nodes[start.nodeB].lat_e7, g_nodes[start.nodeB].lon_e7, end.proj_lat, end.proj_lon); - pq.push({g_score[start.nodeA] + hA, start.nodeA});- pq.push({g_score[start.nodeB] + hB, start.nodeB});+ pq.push({g_scratchpad[start.nodeA].g_score + hA, start.nodeA});+ pq.push({g_scratchpad[start.nodeB].g_score + hB, start.nodeB}); uint32_t found_end_node = 0xFFFFFFFF;+ int iterations = 0; while (!pq.empty()) {- uint32_t u = pq.top().second; pq.pop();- if (u == end.nodeA || u == end.nodeB) { found_end_node = u; break; }+ NodeDist current = pq.top();+ uint32_t u = current.second;+ uint32_t f_score = current.first;+ pq.pop();++ iterations++;++ if (iterations % 500 == 0) {+ LOGD("Iteration %d: current_f=%u, node=%u", iterations, f_score, u);+ }++ if (u == end.nodeA || u == end.nodeB) {+ found_end_node = u;+ LOGD("Target found! Iterations: %d, final_g: %u", iterations, g_scratchpad[u].g_score);+ break;+ } if (u + 1 >= g_node_count) continue;+ for (uint64_t i = g_edge_index[u]; i < g_edge_index[u + 1]; ++i) { Edge& e = g_edges[i]; if (!is_accessible(e.type, mode)) continue; uint32_t mod_dist = (uint32_t)(e.dist_mm * get_weight_modifier(e.type, mode));- if (g_score[u] + mod_dist < g_score[e.target]) {- g_score[e.target] = g_score[u] + mod_dist;- parent[e.target] = u;++ // Check if current path to target is better than previously recorded+ if (g_scratchpad[u].g_score + mod_dist < g_scratchpad[e.target].g_score) {+ g_scratchpad[e.target].g_score = g_scratchpad[u].g_score + mod_dist;+ g_scratchpad[e.target].parent_id = u;+ uint32_t h = haversine_mm(g_nodes[e.target].lat_e7, g_nodes[e.target].lon_e7, end.proj_lat, end.proj_lon);- pq.push({g_score[e.target] + h, e.target});+ pq.push({g_scratchpad[e.target].g_score + h, e.target}); } }++ if (iterations > 1000000) {+ LOGE("Critical: A* search timed out at 1M iterations.");+ break;+ } } std::vector<double> path;- // 1. Output Start Snapped Point (LON, LAT) path.push_back(start.proj_lon / 1e7); path.push_back(start.proj_lat / 1e7); if (found_end_node != 0xFFFFFFFF) { std::vector<uint32_t> nodes;- for (uint32_t c = found_end_node; c != 0xFFFFFFFF; c = parent[c]) nodes.push_back(c);+ // 3. Reconstruct path using parent_id stored in scratchpad+ for (uint32_t c = found_end_node; c != 0xFFFFFFFF; c = g_scratchpad[c].parent_id) {+ nodes.push_back(c);+ } std::reverse(nodes.begin(), nodes.end()); for (uint32_t id : nodes) {- // 2. Output Intermediate Nodes (LON, LAT) path.push_back(g_nodes[id].lon_e7 / 1e7); path.push_back(g_nodes[id].lat_e7 / 1e7); } } - // 3. Output End Snapped Point (LON, LAT) path.push_back(end.proj_lon / 1e7); path.push_back(end.proj_lat / 1e7);
maps/src/main/cpp/scratchpad.cpp
diff --git a/maps/src/main/cpp/scratchpad.cpp b/maps/src/main/cpp/scratchpad.cppnew file mode 100644index 0000000000..e162c46eef--- /dev/null+++ b/maps/src/main/cpp/scratchpad.cpp@@ -0,0 +1,37 @@+#include "scratchpad.h"++RoutingScratchpad::RoutingScratchpad(uint32_t power_of_two_size)+ : m_current_search_id(0) {+ m_size = 1 << power_of_two_size;+ m_mask = m_size - 1;+ m_buffer.resize(m_size, {0, 0xFFFFFFFF, 0xFFFFFFFF, 0});+}++void RoutingScratchpad::reset() {+ m_current_search_id++;+ if (m_current_search_id == 0) {+ for (auto& entry : m_buffer) entry.search_id = 0;+ m_current_search_id = 1;+ }+}++RoutingScratchpad::Entry& RoutingScratchpad::operator[](uint32_t node_id) {+ return get_entry(node_id);+}++RoutingScratchpad::Entry& RoutingScratchpad::get_entry(uint32_t node_id) {+ uint32_t h = hash(node_id);+ while (true) {+ Entry& entry = m_buffer[h];+ if (entry.search_id != m_current_search_id || entry.node_id == node_id) {+ if (entry.search_id != m_current_search_id) {+ entry.node_id = node_id;+ entry.g_score = 0xFFFFFFFF;+ entry.parent_id = 0xFFFFFFFF;+ entry.search_id = m_current_search_id;+ }+ return entry;+ }+ h = (h + 1) & m_mask;+ }+}\ No newline at end of file
maps/src/main/cpp/scratchpad.h
diff --git a/maps/src/main/cpp/scratchpad.h b/maps/src/main/cpp/scratchpad.hnew file mode 100644index 0000000000..5a8803f671--- /dev/null+++ b/maps/src/main/cpp/scratchpad.h@@ -0,0 +1,47 @@+#ifndef SCRATCHPAD_H+#define SCRATCHPAD_H++#include <vector>+#include <cstdint>++/**+ * @brief A high-performance, non-allocating scratchpad for A* search metadata.+ */+class RoutingScratchpad {+public:+ struct Entry {+ uint32_t node_id;+ uint32_t g_score;+ uint32_t parent_id;+ uint32_t search_id;+ };++ explicit RoutingScratchpad(uint32_t power_of_two_size = 20);++ /**+ * @brief "Clears" the scratchpad by incrementing the generation ID.+ */+ void reset();++ /**+ * @brief Access metadata for a specific node ID using array syntax.+ */+ Entry& operator[](uint32_t node_id);++ /**+ * @brief Explicitly gets or initializes metadata for a node.+ */+ Entry& get_entry(uint32_t node_id);++private:+ uint32_t m_size;+ uint32_t m_mask;+ uint32_t m_current_search_id;+ std::vector<Entry> m_buffer;++ inline uint32_t hash(uint32_t x) const {+ return x & m_mask;+ }+};++#endif // SCRATCHPAD_H\ No newline at end of file
maps/src/main/java/com/vayunmathur/maps/MainActivity.kt
diff --git a/maps/src/main/java/com/vayunmathur/maps/MainActivity.kt b/maps/src/main/java/com/vayunmathur/maps/MainActivity.ktindex 9ef4019463..e320b05244 100644--- a/maps/src/main/java/com/vayunmathur/maps/MainActivity.kt+++ b/maps/src/main/java/com/vayunmathur/maps/MainActivity.kt@@ -40,6 +40,7 @@ import com.vayunmathur.maps.ui.SearchPage import kotlinx.coroutines.delay import kotlinx.coroutines.runBlocking import kotlinx.serialization.Serializable+import org.maplibre.android.log.Logger import java.io.File fun ensurePmtilesReady(context: Context): String {@@ -64,6 +65,7 @@ class MainActivity : ComponentActivity() { val ds = DataStoreUtils.getInstance(this) ensurePmtilesReady(this) println(OfflineRouter.checkFiles(this))+ Logger.setVerbosity(Logger.INFO) runBlocking { // File(getExternalFilesDir(null)!!, "edge_index.bin").delete() // File(getExternalFilesDir(null)!!, "edges.bin").delete()