Skip to content

Your Android phone and your Windows PC, working as one. Local-first, end-to-end encrypted, no account.

perf(android): compile the app ahead of time once after each update

Beef04d947853ce
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 5dc75d7..ceb30e5 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -109,6 +109,13 @@ has a Stop button. Choosing another
### Changed
+- **The phone app starts about twice as fast right after an update.**
+ Android compiles an app installed outside the Play Store only during
+ idle maintenance, often days later. With Elevated set up, NectarLink
+ asks for that compile a couple of minutes after each update.
+- **Mirroring and control from the PC use less CPU.** Each mirrored frame
+ takes about 14% less work to draw, and permission checks for every
+ touch and message are read from memory instead of the database.
- **4K for Phone as webcam shows on the PC only when the phone can stream
it.** The PC offered 4K for every phone and camera; picked for one that
can't, the stream stayed at 1080p and the choice moved back. The phone
diff --git a/android/app/src/main/kotlin/app/nectarlink/android/elevated/Elevated.kt b/android/app/src/main/kotlin/app/nectarlink/android/elevated/Elevated.kt
index 354b349..076efd3 100644
--- a/android/app/src/main/kotlin/app/nectarlink/android/elevated/Elevated.kt
+++ b/android/app/src/main/kotlin/app/nectarlink/android/elevated/Elevated.kt
@@ -77,6 +77,10 @@ object Elevated {
private const val ROOT = "root"
/** The shell user, which the helper runs as however it's started. */
private const val SHELL_UID = "2000"
+ /** The version [compileIfUpdated] last asked Android to compile. */
+ private const val COMPILED_VERSION = "compiled_version"
+ /** How long after the helper starts the compile waits, so Android has recorded which code starting up uses. */
+ private const val COMPILE_AFTER_MS = 2 * 60_000L
private val _state = MutableStateFlow<State>(State.NotSetUp)
val state: StateFlow<State> = _state.asStateFlow()
@@ -468,6 +472,7 @@ object Elevated {
input = writer
_state.value = State.Running
if (screenOffRequested) send(writer, "P 0")
+ compileIfUpdated()
Thread {
runCatching {
local.inputStream.bufferedReader().lineSequence().forEach { line ->
@@ -502,6 +507,23 @@ object Elevated {
onChange()
}
+ private var compileJob: Job? = null
+
+ /**
+ * A sideloaded app starts slowly after each install or update, about twice
+ * as slowly, until Android compiles it during idle maintenance, often days
+ * later. With the helper running, ask for that compile now, once per version.
+ */
+ private fun compileIfUpdated() {
+ val version = app.nectarlink.android.BuildConfig.VERSION_CODE
+ if (app.nectarlink.android.BuildConfig.DEBUG || compileJob?.isActive == true) return
+ if (prefs().getInt(COMPILED_VERSION, -1) == version) return
+ compileJob = scope.launch {
+ delay(COMPILE_AFTER_MS)
+ if (runCommand(COMPILE_APP_COMMAND)) prefs().edit { putInt(COMPILED_VERSION, version) }
+ }
+ }
+
/** Attaches to an [InputServer] started directly from `adb shell`. */
suspend fun attachFromShell(appContext: Context, port: Int, token: String): Boolean = withContext(Dispatchers.IO) {
if (!::context.isInitialized) init(appContext)
diff --git a/android/app/src/main/kotlin/app/nectarlink/android/elevated/InputServer.kt b/android/app/src/main/kotlin/app/nectarlink/android/elevated/InputServer.kt
index 48da266..af7cdfb 100644
--- a/android/app/src/main/kotlin/app/nectarlink/android/elevated/InputServer.kt
+++ b/android/app/src/main/kotlin/app/nectarlink/android/elevated/InputServer.kt
@@ -38,6 +38,13 @@ import java.security.MessageDigest
* (see [AppDisplay]); a `K` line asks for a keyframe; closing the
* connection closes the window.
*/
+/**
+ * Compiles NectarLink ahead of time from its startup profile, as Android does
+ * on its own only during idle maintenance. Asked once per version by
+ * [Elevated]; allowed by name in the helper.
+ */
+internal const val COMPILE_APP_COMMAND = "cmd package compile -m speed-profile app.nectarlink"
+
object InputServer {
private const val POWER_MODE_OFF = 0
private const val POWER_MODE_NORMAL = 2
@@ -558,6 +565,8 @@ object InputServer {
when (rest) {
"hotspot on" -> setHotspot(true)
"hotspot off" -> setHotspot(false)
+ // Takes up to a minute: off this thread, which also carries input.
+ COMPILE_APP_COMMAND -> Thread { runCatching { Runtime.getRuntime().exec(rest.split(' ').toTypedArray()).waitFor() } }.start()
else -> Runtime.getRuntime().exec(rest.split(' ').toTypedArray()).waitFor()
}
}
@@ -763,6 +772,8 @@ object InputServer {
"cmd wifi stop-softap",
// Pause for banking apps: seeing which app is in front.
"appops set app.nectarlink GET_USAGE_STATS allow",
+ // Fast starts right after an install or update.
+ COMPILE_APP_COMMAND,
)
private companion object {