Give Android a native code cache and time picture loads

A profile of Breeze starting on a Pixel 4a found the slow parts of its
first second and first frame; plans/2026-11-18-breeze-startup-speed.md
lists them.

- Ziran keeps the machine code it compiles from each app bundle in a disk
  cache under XDG_CACHE_HOME or HOME. Android sets neither, so the cache
  was off and every start compiled every bundle again. Breeze now points
  ZIRAN_NATIVE_CACHE at the app's cache folder. Ziran stores entries
  there; it still rejects them on later Android starts, which is reported
  to Ziran.
- Debug Android builds log each picture's load time as an APP PROBE line.
  They showed the 2048x1024 practice banners decoding for about 760 ms
  each, and the WHM banner decoding twice.

Local tests: the Android debug APK starts on the Pixel 4a, writes its
native cache and logs picture times; the Linux build, its window test and
the web and Windows builds are unaffected.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
This commit is contained in:
waoziandClaude Opus 5.5 committed 2026-11-18 17:14:03 -03:00
1 parent 3997d80e8c
commit 5ad38eab2d
4 files changed
+98 -1

No files matched your search

+33
View File
@@ -0,0 +1,33 @@
# Breeze startup speed
Owner, 2026-11-18: "can you optimze all things that are lslow". Measured on
the Pixel 4a with the arm64 debug APK and NDK simpleperf. The app's own code
builds at -O2 there as in release; only libwebp is unoptimized in debug. A
start spends about 1.9 s opening the root package before its first frame,
then about 3 s drawing that frame.
## Open work
- [ ] Asset digests hash one byte at a time (Ziran). `AppsOpenPackage` →
`ZxExecRead` → `AssetsDigest` feeds every asset byte through `ShaByte`,
about 40 MB/s. The root package is 75 MB because it nests the apps
(`cells/practices.zx` 36.6 MB, `assets/apps/bundled/library.zx` 18.3 MB),
and the preload hashes each nested app again when it opens. Ziran should
hash whole blocks, or use the hasher in `cmd/zir/zir_sha256.c`.
- [ ] Android native cache misses (Ziran). Breeze points
`ZIRAN_NATIVE_CACHE` at `cache/ziran-native`. Entries are stored, but every
start rejects and rewrites them, so compiling apps
(`NativeBundleSlotCode` under `AppsFeatureFinish` and
`AchievementCatalogsLoad`) stays in the first frames. Linux hits the same
cache on its second start.
- [ ] Kryon's `AndroidProcessPrepare` sets `TMPDIR` for that compile but no
cache folder. Once Kryon sets one for every app, drop
`AndroidNativeCachePrepare` from `src/platform/main_platform.zi`.
- [ ] Practice banners are 2048×1024 WebP for a card a few hundred pixels
tall: about 760 ms each in the debug build. Decode at the drawn size
(Kryon's Pixmap) or ship smaller banners.
- [ ] The WHM banner decodes twice. When the Practices app opens after the
start, `practice_downloads.zi` reports `assets/practices` as changed, which
reloads pictures whose bytes stayed the same.
- [ ] Measure a gplay release on the owner's Pixel 3a once these land; debug
builds log each picture's load time as `APP PROBE: picture`.
+18 -1
View File
@@ -34,9 +34,14 @@
#import "c_string"
#import "byte_text_linux"
#if #defined(ANDROID_DEBUG) {
#import, file "host_clock.zi";
#import, file "trace_log.zi";
}
// A picture the app carries, such as one inside its bundles, decodes
// from those bytes; any other comes from the APK's assets or the disk.
LoadAssetPicture :: (path: string) -> bool {
LoadPictureBytes :: (path: string) -> bool {
name: [4096]u8
if CopyCString(path, name[:]) {
embedded := asset_lookup(*name[0])
@@ -48,6 +53,18 @@
return Pictures.AndroidPictureLoad(path)
}
// Debug builds log each picture's load time, so slow decodes show up.
LoadAssetPicture :: (path: string) -> bool {
#if #defined(ANDROID_DEBUG) {
started := HostSeconds()
loaded := LoadPictureBytes(path)
TraceLogPicture(3, path, HostSeconds() - started, loaded)
return loaded
} else {
return LoadPictureBytes(path)
}
}
InstallAssetFiles :: () {
load: PictureSource.PixmapImageLoad = LoadAssetPicture
unused PictureSource.PixmapImageSourceUse(load)
+32
View File
@@ -59,6 +59,7 @@ FindText :: (text: *u8, needle: *u8) -> *u8 #foreign c "strstr";
} else {
SetEnvironmentBytes :: (name: *u8, value: *u8,
overwrite: s32) -> s32 #foreign c "setenv";
EnvironmentBytes :: (name: *u8) -> *u8 #foreign c "getenv";
ProcessId :: () -> s32 #foreign c "getpid";
}
#if #defined(__GLIBC__) {
@@ -286,6 +287,36 @@ native_window_environment :: () {
}
}
#if #defined(ANDROID_BUILD) {
// Ziran keeps the machine code it compiles from each app bundle in a disk
// cache under XDG_CACHE_HOME or HOME. Android sets neither, so every start
// compiled every bundle again. The cache lives in the app's cache folder
// beside FILES, which Android may clear when storage runs low.
AndroidNativeCachePrepare :: (files: *u8) {
name: [24]u8
if files == null || !CopyCString("ZIRAN_NATIVE_CACHE", name[:]) ||
EnvironmentBytes(*name[0]) != null {
return
}
folder := TextFromCString(files)
suffix: string = "/files"
cache: string = "/cache/ziran-native"
if folder.count <= suffix.count || folder[folder.count - suffix.count:] != suffix {
return
}
base := folder[:folder.count - suffix.count]
path: [512]u8
if base.count + cache.count >= path.count {
return
}
length: s64 = 0
for byte: base { path[length] = byte; length += 1 }
for byte: cache { path[length] = byte; length += 1 }
path[length] = 0
unused SetEnvironmentBytes(*name[0], *path[0], 1)
}
}
#program_export
native_before_window :: () {
InstallAssetFiles()
@@ -303,6 +334,7 @@ native_before_window :: () {
// The package's own files directory: the debug and Play builds use
// other application IDs than the release path.
files := AndroidInternalDataPath()
AndroidNativeCachePrepare(files)
if files == null || ChangeDirectory(files) != 0 {
TraceLog(4, "APP: failed to switch to Android files directory")
}
+15
View File
@@ -65,3 +65,18 @@ TraceLogTwoInts :: (level: s32, format_text: string,
unused TraceFormat(*line[0], 256, *format[0], first, second)
TraceLog(level, TextFromCString(*line[0]))
}
// How long a picture took to load, which debug Android builds log as a
// startup probe beside the other APP PROBE lines.
TraceLogPicture :: (level: s32, path: string, seconds: float64, loaded: bool) {
format: [48]u8
name: [256]u8
if !CopyCString("APP PROBE: picture %s %.1f ms loaded=%d", format[:]) ||
!CopyCString(path, name[:]) {
return
}
line: [384]u8
unused TraceFormat(*line[0], 384, *format[0], *name[0], seconds * 1000.0,
ifx loaded then cast(s32)1 else cast(s32)0)
TraceLog(level, TextFromCString(*line[0]))
}