8000 [3/5] Add native Rust Arabic search engine (arm64-v8a + armeabi-v7a) with SQLite fallback by taljeri · Pull Request #4064 · quran/quran_android · GitHub
[go: up one dir, main page]

Skip to content

[3/5] Add native Rust Arabic search engine (arm64-v8a + armeabi-v7a) with SQLite fallback - #4064

Open
taljeri wants to merge 5 commits into
quran:mainfrom
taljeri:pr/3-rust-arabic-search
Open

taljeri wants to merge 5 commits into
quran:mainfrom
taljeri:pr/3-rust-arabic-search

Conversation

@taljeri
@taljeri taljeri commented Nov 26, 2026 •
Copy link
Copy Markdown

Summary

[Batch 3 of 5 — Stacked PR Series (pr/3-rust-arabic-search ← #4063 pr/2-performance-and-modernization)]

Adds a zero-crate native Rust Arabic Quran search engine (common/search/src/main/rust/) with 100% 16 KB ELF alignment across arm64-v8a and armeabi-v7a, a compiled-query JNI cache, and 100% algorithmic parity in Kotlin (ArabicMorphologicalQuery.kt) across 24 files (+6,961 / -46):

Architecture & 16 KB Native Alignment

  • 16 KB-Aligned Native Libraries (PT_LOAD p_align = 0x4000):
    • Ships common/search/src/main/jniLibs/arm64-v8a/libquran_search.so (489,680 B) and common/search/src/main/jniLibs/armeabi-v7a/libquran_search.so (327,728 B), both verified with zip_offset % 16384 == 0 and PT_LOAD p_align == 0x4000 across all segments—upgrading the earlier Stage 2 .debug armeabi-v7a/libquran_search.so (222,764 B, 0x1000 4 KB alignment) to full Android 15+ 16 KB page-size compliance, with automatic SQLite/Kotlin fallback on ABIs without prebuilt .so files.
  • Zero-Crate Rust Core (src/engine.rs, +2,831):
    • VerseIndex & CompiledArabicQuery: 64-bit letter_mask / query_mask bitmask prefiltering plus 2-step Anchor::ExactPair skip scanning across all 6,236 verses.
    • Full Universal Arabic Quran Morphology: Handles ALLAH_PROCLITICS, DEF_PROCLITICS, L_PROCLITICS (لِـ+ال elision such as لله / للجنة), INDEF_PROCLITICS, and VERB_PROCLITICS; Uthmani open-Ta (ة/ت) equivalences; bidirectional masculine/feminine singular/dual/plural forms and weak-ي stems (e.g. متقي ↔ المتقين, يد ↔ أيدي); plus curated broken-plural & LEMMA_FAMILIES_RAW lexicon sets with zero false positives (eliminating substring leaks such as يوم matching يومئذ / اليوم incorrectly).
  • Thread-Safe Compiled-Query JNI Bridge (src/jni_bridge.rs, +412; src/jni_sys.rs, +135; src/lib.rs, +855):
    • Maintains INDEX and QUERY_CACHE: RwLock<Option<(Vec<u16>, Option<Arc<CompiledArabicQuery>>)>> (compile_cached_query) so repeated searches reuse the compiled morphological automaton without recompilation.
    • Uses QUERY_STACK_CAP = 128 UTF-16 stack buffering, wraps every exported symbol in std::panic::catch_unwind, and checks ExceptionCheck after every fallible JNI call so panics never unwind across FFI.
  • 100% Kotlin Morphological Parity (ArabicMorphologicalQuery.kt, +1,752):
    • Mirrors engine.rs rule-for-rule with 4-tier matching (TIER_EXACT..TIER_FALLBACK_SUBSTR) in Kotlin, powering SQLite/fallback searches and enabling exact morphological parity for Tafsir search (DatabaseHandler.searchArabicNormalized in PR #4066) and match highlighting (SearchResultFormatter.kt).
  • JNI Bridge, Cursor, Lifecycle & ProGuard (RustArabicSearchEngine.kt, RustSearchResultsCursor.kt, ArabicSearcher.kt, ArabicCompiledQuery.kt, app/proguard.cfg):
    • Generation-safe index lifecycle guards, R8 keep rules, and transparent fallback tested by RustArabicSearchEngineAdversarialTest.kt, RustArabicSearchEngineFallbackTest.kt, ArabicCompiledQueryTest.kt, and SearchResultsCursorAndArabicHighlightTest.kt.

Freshly Measured 6,236-Verse Corpus & Moto g05 Benchmarks

1. Rust Release Corpus Benchmark (quran.ar.db, 6,236 Verses, n = 200 Runs/Query)

  • cargo test --release -- --nocapture (QURAN_AR_TSV exported from quran.ar.db, 810,133 B): 12 passed; 0 failed; 0 ignored in 0.16s (including real_quran_db_export_parity_if_present, verifying ~80 queries, exact span offsets, 0% false positives, and < 5.0 ms cap).
  • Index Load (VerseIndex::from_utf8_rows, 6,236 verses, n = 20): min = 18.952 ms, median = 23.438 ms, p95 = 38.148 ms, max = 38.646 ms.
  • :common:search:testReleaseUnitTest --rerun-tasks: 79/79 PASS (ArabicSearcherTest 12, DefaultSearcherTest 15, RustArabicSearchEngineAdversarialTest 3, RustArabicSearchEngineFallbackTest 2, SearchTextUtilTest 19, ArabicCharacterHelperTest 21, ArabicCompiledQueryTest 7).
Query Unique Verses Total Occurrences Warm Search Median (ms) Warm Search p95 (ms) Compile+Search Median (ms) Compile+Search p95 (ms)
الله 1,913 2,816 0.0338 0.0594 0.0370 0.0645
لله 145 149 0.0043 0.0059 0.0116 0.0181
الجنة 154 159 0.0236 0.0435 0.1346 0.2149
بالجنة 1 1 0.0328 0.0353 0.2279 0.2486
مسلم 42 45 0.0640 0.0846 0.4021 0.4944
مسلمين 42 45 0.0624 0.0812 0.3829 0.4622
مؤمن 200 230 0.0423 0.0579 0.2430 0.2870
المؤمنون 200 230 0.0441 0.0558 0.2564 0.2923
كافر 157 161 0.0546 0.0731 0.3334 0.3749
كافرين 157 161 0.0549 0.0712 0.3603 0.3978
متقي 49 49 0.0298 0.0373 0.1904 0.2062
المتقين 49 49 0.0042 0.0054 0.0190 0.0260
صالح 132 136 0.0423 0.0792 0.2485 0.4438
الصالحات 132 136 0.0343 0.0618 0.2037 0.3708
نبي 76 79 0.0340 0.0390 0.2040 0.2272
أنبياء 76 79 0.0344 0.0418 0.1991 0.2303
رسول 291 331 0.0622 0.0820 0.3354 0.4215
رسل 291 331 0.0269 0.0368 0.1065 0.1295
كتاب 255 280 0.0413 0.0561 0.2256 0.2760
كتب 255 280 0.0168 0.0231 0.0694 0.0893
قلب 124 132 0.0155 0.0241 0.0721 0.0867
قلوب 124 132 0.0397 0.0719 0.2289 0.3978
نفس 268 295 0.0222 0.0380 0.0998 0.1604
أنفس 268 295 0.0512 0.0886 0.2991 0.4919
رجل 66 73 0.0211 0.0293 0.1139 0.1386
رجال 54 60 0.0389 0.0495 0.2395 0.2801
يوم 377 405 0.0222 0.0305 0.0910 0.1122
أيام 377 405 0.0487 0.0884 0.2530 0.4491
اليد 108 118 0.0217 0.0282 0.1163 0.1481
أيدي 108 118 0.0138 0.0166 0.0666 0.0861

2. Live Moto g05 Comparison: Stage 2 (.debug v1 Rust) vs. Stage 3 (.hybrid v5 Universal Morphology)

(Compared against Stage 1 audit/combined SQLite "الله" = 442.1 ms; cold index load on .hybrid = 75.28 ms, cold first query = 4.95 ms; Kotlin ArabicMorphologicalQuery also powers live Tafsir parity on quran.ar.muyassar.db in PR #4066.)

Query Stage 2 .debug (v1 Rust) Hits / Warm (ms) Stage 3 .hybrid (v5 Rust) Hits / Warm (ms) Stage 3 Kotlin Tafsir Hits (الميسر) Recall / Precision Delta
الله 1,842 / 1.68 ms 1,913 / 0.50 ms 3,286 +71 verses (لله, proclitics)
الجنة 57 / 1.57 ms 154 / 2.30 ms 306 +97 verses (جنة, جنات, جنتين)
مسلم 42 / 1.46 ms 42 / 0.84 ms 103 100% parity
مؤمن 200 / 2.18 ms 200 / 1.30 ms 503 100% parity
كافر 132 / 2.14 ms 157 / 1.27 ms 485 +25 verses
متقي 43 / 1.10 ms 49 / 1.42 ms 44 +6 verses
صالح 132 / 1.28 ms 132 / 1.52 ms 252 100% parity
نبي 76 / 1.54 ms 76 / 1.77 ms 383 100% parity
رسول 215 / 1.71 ms 291 / 2.10 ms 1,429 +76 verses (رسل)
كتاب 232 / 1.33 ms 255 / 1.67 ms 280 +23 verses (كتب)
قلب 53 / 2.17 ms 124 / 2.18 ms 236 +71 verses (قلوب)
نفس 268 / 1.67 ms 268 / 0.92 ms 515 100% parity
رجل 41 / 1.38 ms 66 / 1.00 ms 116 +25 verses (رجال)
يوم 530 / 2.95 ms 377 / 0.91 ms 646 -153 false positives eliminated
اليد 2 / 0.93 ms 108 / 0.81 ms 133 +106 verses (يد, أيدي)

Stack Overview

# Branch Scope Dependency
#4062 pr/1-security-hardening Security hardening (SEC-01–SEC-22) & FTS3 CAST(? AS INTEGER) main
#4063 pr/2-performance-and-modernization Performance, memory/bitmap optimization, Qari search/coalescing & modernization Stacked on #4062
#4064 pr/3-rust-arabic-search (this PR) Native 16 KB-aligned Rust Arabic search engine + Kotlin morphological parity Stacked on #4063
#4065 pr/4-calm-themes Duo-Tone calm tonal color themes & Pure Black OLED Dark Mode (#000000) Stacked on #4064
#4066 pr/5-proposal5-hybrid-ui Proposal 5 Hybrid UI, Z0–Z4 floating layers, 2-tab Study Drawer & Tafsir search Stacked on #4065

Complete File-by-File Breakdown (24 Files, +6,961 / -46)

All 24 files in pr/2-performance-and-modernization..pr/3-rust-arabic-search (6d487bed6..b2c8b77e1)
# File Path +Added -Deleted
1 app/proguard.cfg 6 0
2 app/src/main/java/com/quran/labs/androidquran/QuranApplication.kt 6 0
3 app/src/main/java/com/quran/labs/androidquran/SearchActivity.kt 13 3
4 app/src/main/java/com/quran/labs/androidquran/database/DatabaseHandler.kt 4 0
5 app/src/main/java/com/quran/labs/androidquran/util/SearchResultFormatter.kt 52 22
6 app/src/test/java/com/quran/labs/androidquran/util/SearchResultsCursorAndArabicHighlightTest.kt 32 4
7 common/search/build.gradle 1 0
8 common/search/src/main/java/com/quran/common/search/ArabicSearcher.kt 195 10
9 common/search/src/main/java/com/quran/common/search/RustArabicSearchEngine.kt 208 0
10 common/search/src/main/java/com/quran/common/search/RustSearchResultsCursor.kt 68 0
11 common/search/src/main/java/com/quran/common/search/arabic/ArabicCharacterHelper.kt 6 4
12 common/search/src/main/java/com/quran/common/search/arabic/ArabicCompiledQuery.kt 36 3
13 common/search/src/main/java/com/quran/common/search/arabic/ArabicMorphologicalQuery.kt 1752 0
14 common/search/src/main/jniLibs/arm64-v8a/libquran_search.so - -
15 common/search/src/main/jniLibs/armeabi-v7a/libquran_search.so - -
16 common/search/src/main/rust/Cargo.lock 7 0
17 common/search/src/main/rust/Cargo.toml 17 0
18 common/search/src/main/rust/src/engine.rs 2831 0
19 common/search/src/main/rust/src/jni_bridge.rs 412 0
20 common/search/src/main/rust/src/jni_sys.rs 135 0
21 common/search/src/main/rust/src/lib.rs 855 0
22 common/search/src/test/java/com/quran/labs/androidquran/common/search/RustArabicSearchEngineAdversarialTest.kt 119 0
23 common/search/src/test/java/com/quran/labs/androidquran/common/search/RustArabicSearchEngineFallbackTest.kt 35 0
24 common/search/src/test/java/com/quran/labs/androidquran/common/search/arabic/ArabicCompiledQueryTest.kt 171 0

…c, DB handlers, and manifests

Hardens security boundaries across app, common, feature, and autoquran:
- Zip Slip / path traversal guards in archive extraction, imports, and DatabaseHandler
- Safer TLS/network client config, DNS fallback, download validation, and URI handling
- Tightened deep-link validation, backup/extraction rules, and exported manifest components
- Parameterized SQLite queries across SuraTimingDatabaseHandler and DatabaseHandler.getVersesInternal with CAST(? AS INTEGER) for FTS3/FTS4 column affinity safety
- Reduced sensitive logging in release paths and added unit test coverage across affected modules
…ze 2011 legacy code

Combines performance, animation/RTL/a11y audit fixes, ultra-light optimizations,
and legacy code modernization:
- Cold-start, RAM/bitmap (API 28+ ImageDecoder HARDWARE + WindowMetricsCalculator),
  SQLite WAL, networking, audio, presenter, and Compose optimizations
- Migrate SearchActivity off LoaderManager/ListView to RecyclerView + Coroutines
- Replace deprecated LocalBroadcastManager with SharedFlow-backed DownloadProgressBus
- Convert 17 legacy Java classes to Kotlin and remove all ActionBarSherlock relics
- Fix RTL, animation, styling, and accessibility audit items
…bi-v7a)

Adds zero-crate native Rust Arabic search engine with bitmask and 2-step ExactPair
skipping, catch_unwind FFI safety, and automatic SQLite fallback:
- Supports modern 64-bit (arm64-v8a, 16 KB ELF aligned) and 32-bit (armeabi-v7a) devices
- ~1.01 ms warm search latency for 'الله' (1,842 hits, 100% parity across all 6,236 verses)
- Includes 11 Rust unit tests and JVM fallback + adversarial unit tests
@taljeri
taljeri force-pushed the pr/3-rust-arabic-search branch from 6a3adcf to 4a88438 Compare November 26, 2026 21:47
… and Kotlin search engine

- Add Arabic morphological & proclitic query support in Rust engine (engine.rs, lib.rs) and Kotlin search helpers (ArabicMorphologicalQuery, ArabicCharacterHelper, ArabicCompiledQuery, ArabicSearcher)
- Rebuild 16 KB ELF-aligned arm64-v8a and armeabi-v7a libquran_search.so binaries
- Match proclitic/prefix/definite-article inflections cleanly ('الله' 1,913 verses / 2,816 occurrences; 'الجنة' 154 Ayah verses; 0% false positives)
- Expand Rust adversarial and ArabicCompiledQuery unit test coverage
…ty and JNI cache

- Add bidirectional masculine/feminine singular/dual/plural morphology,
  weak-ي stems, and curated broken-plural/lemma lexicon across all Quran words
  with 100% Rust <-> Kotlin/Tafsir parity (e.g. مسلم 2->42v, مؤمن 19->200v,
  قلب 19->124v, نبي 55->76v, 0% FP)
- Add JNI compiled-query cache and rebuild 16 KB page-aligned arm64-v8a and
  armeabi-v7a native libraries
- Expand ArabicCompiledQueryTest coverage for universal morphological forms
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant

0