networklocation: swap geocoder block codec from deflate to zstd
- Commit
103408edc4e64a4be902d69277cd0f79919f9cae- Author
- Vayun Mathur <[email protected]>
- When
- 2026-08-09 22:49 UTC (3 months ago)
- Parents
a9b6e79
Diff
gradle/libs.versions.toml
diff --git a/gradle/libs.versions.toml b/gradle/libs.versions.tomlindex a2f21fd902..64778f55b9 100644--- a/gradle/libs.versions.toml+++ b/gradle/libs.versions.toml@@ -72,6 +72,8 @@ zxing = "3.5.4" libphonenumber = "9.0.34" litertlmAndroid = "0.14.0" brotli = "0.1.2"+# Zstandard JNI bindings (com.github.luben) for the offline geocoder block codec.+zstdJni = "1.5.6-9" # Standalone library from the AGP toolchain (com.android.tools.build), not the plugin — # :appstore uses it at runtime to verify APK source stamps. apksig = "8.7.3"@@ -224,6 +226,7 @@ zxing-core = { module = "com.google.zxing:core", version.ref = "zxing" } libphonenumber = { module = "com.googlecode.libphonenumber:libphonenumber", version.ref = "libphonenumber" } litertlm-android = { module = "com.google.ai.edge.litertlm:litertlm-android", version.ref = "litertlmAndroid" } brotli-dec = { module = "org.brotli:dec", version.ref = "brotli" }+zstd-jni = { module = "com.github.luben:zstd-jni", version.ref = "zstdJni" } apksig = { module = "com.android.tools.build:apksig", version.ref = "apksig" } # =============================================================================
networklocation/build.gradle.kts
diff --git a/networklocation/build.gradle.kts b/networklocation/build.gradle.ktsindex 67bd13e3c1..8c2382b6b6 100644--- a/networklocation/build.gradle.kts+++ b/networklocation/build.gradle.kts@@ -103,7 +103,8 @@ dependencies { // Reporting loop + IO for the gs-loc queries. implementation(libs.kotlinx.coroutines.android) // Geocoder DB is a self-contained mmap'd binary (see geocoder/). Block compression uses- // java.util.zip (Deflate) — no external dependency needed for the core.+ // Zstandard via zstd-jni (compress/decompress one 4096-record block fully in RAM).+ implementation(libs.zstd.jni) // kotlinx-serialization-json (from the app convention) is used only by the generator to // parse osmium's GeoJSONSeq export. }
networklocation/src/main/java/com/vayunmathur/networklocation/geocoder/GeoDb.kt
diff --git a/networklocation/src/main/java/com/vayunmathur/networklocation/geocoder/GeoDb.kt b/networklocation/src/main/java/com/vayunmathur/networklocation/geocoder/GeoDb.ktindex f712ee7025..6b6e349b71 100644--- a/networklocation/src/main/java/com/vayunmathur/networklocation/geocoder/GeoDb.kt+++ b/networklocation/src/main/java/com/vayunmathur/networklocation/geocoder/GeoDb.kt@@ -1,14 +1,12 @@ package com.vayunmathur.networklocation.geocoder +import com.github.luben.zstd.Zstd import java.io.ByteArrayOutputStream import java.io.DataOutputStream import java.io.File import java.io.RandomAccessFile import java.nio.ByteBuffer import java.nio.channels.FileChannel-import java.util.zip.Deflater-import java.util.zip.Inflater-import kotlin.math.max import kotlin.math.min import kotlin.math.roundToInt @@ -20,8 +18,8 @@ import kotlin.math.roundToInt * only a small sparse grid directory (no per-record spatial index), and * - coordinates delta-compress well because neighbours are adjacent in the file. * Every field is a dictionary index (strings deduped once), and every column is stored in- * Deflate-compressed blocks so the reader mmaps the file and inflates just one small block per- * access. Forward geocoding uses a second ordering (records sorted by+ * Zstandard-compressed blocks so the reader mmaps the file and decompresses just one small block+ * per access. Forward geocoding uses a second ordering (records sorted by * country/state/city/street/house) for binary search. * * Coordinates are micro-degrees (1e-6 deg, ~11 cm) as Int.@@ -33,7 +31,7 @@ import kotlin.math.roundToInt */ object GeoDb { const val MAGIC = 0x4D41_4745 // "MAGE"- const val VERSION = 1+ const val VERSION = 2 // v2: per-block codec is Zstandard (was Deflate in v1) const val BLOCK = 4096 // Grid: 0.05-degree cells over the whole planet.@@ -118,25 +116,14 @@ internal class VarIntReader(val a: ByteArray) { } } -private fun deflate(data: ByteArray): ByteArray {- val d = Deflater(Deflater.BEST_COMPRESSION, true)- d.setInput(data); d.finish()- val out = ByteArrayOutputStream(max(64, data.size / 2))- val buf = ByteArray(8192)- while (!d.finished()) out.write(buf, 0, d.deflate(buf))- d.end()- return out.toByteArray()-}+/** Max standard Zstandard level; the DB is built once offline, so favour ratio over speed. */+private const val ZSTD_LEVEL = 19 -internal fun inflate(data: ByteArray, expected: Int): ByteArray {- val inf = Inflater(true)- inf.setInput(data)- val out = ByteArray(expected)- var off = 0- while (off < expected && !inf.finished()) off += inf.inflate(out, off, expected - off)- inf.end()- return out-}+/** Compress one block fully in RAM. */+private fun compress(data: ByteArray): ByteArray = Zstd.compress(data, ZSTD_LEVEL)++/** Decompress one block fully in RAM; [expected] is the known original size. */+internal fun decompress(data: ByteArray, expected: Int): ByteArray = Zstd.decompress(data, expected) // --------------------------------------------------------------------------------------------- // Random-access byte source. Backs the reader by either a plain File or an mmap'd APK asset.@@ -184,7 +171,7 @@ class ChannelByteSource( // Writer. // --------------------------------------------------------------------------------------------- -/** Column encoded as delta+zigzag+varint per block, each block Deflate-compressed. */+/** Column encoded as delta+zigzag+varint per block, each block Zstandard-compressed. */ private fun encodeColumn(values: IntArray, delta: Boolean): Pair<ByteArray, IntArray> { val n = values.size val blocks = (n + GeoDb.BLOCK - 1) / GeoDb.BLOCK@@ -203,7 +190,7 @@ private fun encodeColumn(values: IntArray, delta: Boolean): Pair<ByteArray, IntA prev = v } val rawBytes = raw.toByteArray()- val comp = deflate(rawBytes)+ val comp = compress(rawBytes) rawLens[b] = rawBytes.size compLens[b] = comp.size body.write(comp)@@ -226,7 +213,7 @@ private fun encodeDict(strings: List<String>): ByteArray { d.writeInt(b.size); d.write(b) } val rawBytes = raw.toByteArray()- val comp = deflate(rawBytes)+ val comp = compress(rawBytes) val out = ByteArrayOutputStream() val h = DataOutputStream(out) h.writeInt(rawBytes.size); h.writeInt(comp.size)@@ -320,7 +307,7 @@ class GeoDbWriter { } out.writeInt(gridBytes.size()); gridBytes.writeTo(out) - // Forward ordering (delta-varint, deflated as a single column).+ // Forward ordering (delta-varint, Zstandard-compressed as a single column). val (fwdBytes, _) = encodeColumn(fwd, delta = true) out.writeInt(fwdBytes.size); out.write(fwdBytes) }
networklocation/src/main/java/com/vayunmathur/networklocation/geocoder/GeoDbReader.kt
diff --git a/networklocation/src/main/java/com/vayunmathur/networklocation/geocoder/GeoDbReader.kt b/networklocation/src/main/java/com/vayunmathur/networklocation/geocoder/GeoDbReader.ktindex 120f08e732..d117409457 100644--- a/networklocation/src/main/java/com/vayunmathur/networklocation/geocoder/GeoDbReader.kt+++ b/networklocation/src/main/java/com/vayunmathur/networklocation/geocoder/GeoDbReader.kt@@ -8,7 +8,7 @@ import kotlin.math.min /** * Reader for a [GeoDb] file, backed by a [ByteSource] (a plain file or an mmap'd APK asset). * Dictionaries and the grid directory are held in memory; address columns are read- * block-by-block on demand (positional read + inflate one block), so the bulk of the database+ * block-by-block on demand (positional read + decompress one block), so the bulk of the database * is never resident. Supports reverse (coord -> address) and structured forward geocoding. */ class GeoDbReader(private val src: ByteSource) : AutoCloseable {@@ -207,7 +207,7 @@ class GeoDbReader(private val src: ByteSource) : AutoCloseable { val rawSize = di.readInt() val compSize = di.readInt() val comp = ByteArray(compSize); di.readFully(comp)- val raw = inflate(comp, rawSize)+ val raw = decompress(comp, rawSize) val r = DataInputStream(raw.inputStream()) val count = r.readInt() return Array(count) {@@ -219,7 +219,7 @@ class GeoDbReader(private val src: ByteSource) : AutoCloseable { } /**- * On-demand column reader. Parses the column header (block sizes) up front, then inflates and+ * On-demand column reader. Parses the column header (block sizes) up front, then decompresses and * decodes a single block per access, caching the most recently used block. [get] is * synchronised so multiple binder threads can share one reader. */@@ -261,7 +261,7 @@ private class Column( private fun decode(block: Int) { val comp = src.read(blockFileOffset[block], compLens[block])- val raw = inflate(comp, rawLens[block])+ val raw = decompress(comp, rawLens[block]) val count = min(GeoDb.BLOCK, n - block * GeoDb.BLOCK) val values = IntArray(count) val vr = VarIntReader(raw)