library: write the frame report to logcat once a second, not per frame (#maprender)
- Commit
18ca750c3d31bfb8ba5c302a410f6a6528217983- Author
- Vayun Mathur <[email protected]>
- When
- 2026-11-13 20:22 UTC (22 days ago)
- Parents
5c0a460
Part of the "visibly laggy when moving around" report: per-frame overhead on the frame's own thread. Root cause: `render` built and logged a ~580-byte status line on every frame (`format!` of the camera/residency/step report plus a fresh `StepTimes::report` string, a Vec copy for the NUL, then `__android_log_write`, a socket write to logd), in release builds too. At 60 fps that is 60 log lines a second while panning, enough for logd's chatty filter to start dropping them, and the cost lands inside the frame. What changed: `timing::ReportThrottle` lets the first frame report and then at most one frame per `REPORT_INTERVAL` (1 s). The report passes the window's frame count to `StepTimes::report(frames)`, which already supported windows, so the line now carries avg/max over every frame since the last one - nothing is lost: the max still flags a single slow frame, and the frame-stats overlay still reads per-frame values from `last_nanos`. Measured: building the line costs 5.2 us/frame on the host (new ignored harness `the_per_frame_report_formatting_cost`), roughly 15-20 us on a phone core, plus the logd write (typically tens of us, and logd's own CPU). That is now paid once a second instead of 60 times: per-frame cost goes to zero on 59 of 60 frames. Small next to the render-thread move, but it is pure waste on every frame and the logcat stays readable. Verified: new host test pins first-frame report, silence within the second and the 60-frame window at one second; android target type-checks.
Diff
library/map/src/main/rust/src/bridge/frame.rs
diff --git a/library/map/src/main/rust/src/bridge/frame.rs b/library/map/src/main/rust/src/bridge/frame.rsindex 44e740d543..75e296760c 100644--- a/library/map/src/main/rust/src/bridge/frame.rs+++ b/library/map/src/main/rust/src/bridge/frame.rs@@ -352,16 +352,18 @@ pub extern "system" fn Java_com_vayunmathur_library_map_MapNative_render<'l>( // // After `render`, so `draws`/`tris` and every step sample below belong to the frame this // line describes rather than the one before it.- {+ //+ // Once a second rather than every frame (see `timing::REPORT_INTERVAL`): the step report+ // then carries avg/max over the whole window, so a single slow frame still shows.+ if let Some(window) = map.report.tick(std::time::Instant::now()) { let (tiles, meshes, draws, triangles) = map.renderer.stats(); let (width_px, height_px) = map.renderer.extent(); // `meshes` is what is resident, `draws` what the frame just submitted. They // differ wherever the authored style ramps a layer's width to zero, so reporting only // the first would claim roads are being drawn at zooms where they are gated out. //- // The step report is built (and its window reset) every frame: `avg/max`- // per step in ms for this frame alone.- let steps = map.renderer.step_times.borrow_mut().report(1);+ // `avg/max` per step in ms over the frames since the last line, then reset.+ let steps = map.renderer.step_times.borrow_mut().report(window); log_info(&format!( "z{:.2} @{:.4},{:.4} b{:.0} vp {}x{}dp {}x{}px msaa {}x | resident {} tiles, {} meshes, \ {} draws, {} tris | {} in flight, {} absent, {} stale-dropped | archive z{}..{} | {}",
library/map/src/main/rust/src/bridge/handle.rs
diff --git a/library/map/src/main/rust/src/bridge/handle.rs b/library/map/src/main/rust/src/bridge/handle.rsindex 3e749c73c5..5b06ab3f67 100644--- a/library/map/src/main/rust/src/bridge/handle.rs+++ b/library/map/src/main/rust/src/bridge/handle.rs@@ -216,6 +216,9 @@ pub(crate) struct MapHandle { /// frame; `None` after a Moon frame, so returning from the Moon always takes the /// burst path once rather than comparing against a stale Earth zoom. pub(crate) last_zoom: Option<f64>,+ /// Which frames write the per-frame report line (once a second; see+ /// `timing::REPORT_INTERVAL`).+ pub(crate) report: crate::timing::ReportThrottle, } impl MapHandle {
library/map/src/main/rust/src/bridge/lifecycle.rs
diff --git a/library/map/src/main/rust/src/bridge/lifecycle.rs b/library/map/src/main/rust/src/bridge/lifecycle.rsindex 28f921d88e..1ff9d734ea 100644--- a/library/map/src/main/rust/src/bridge/lifecycle.rs+++ b/library/map/src/main/rust/src/bridge/lifecycle.rs@@ -155,6 +155,7 @@ pub extern "system" fn Java_com_vayunmathur_library_map_MapNative_create<'l>( density: 1.0, last_zoom: None, absent_build_id: 0,+ report: crate::timing::ReportThrottle::new(), }); Box::into_raw(handle) as jlong }
library/map/src/main/rust/src/timing.rs
diff --git a/library/map/src/main/rust/src/timing.rs b/library/map/src/main/rust/src/timing.rsindex a4495dd559..25c4d0aba5 100644--- a/library/map/src/main/rust/src/timing.rs+++ b/library/map/src/main/rust/src/timing.rs@@ -249,6 +249,42 @@ impl StepTimes { } } +/// How often the per-frame report line reaches logcat.+///+/// The line used to be written on every frame: at 60 fps that is 60 `format!`s of a ~400-byte+/// line plus 60 `__android_log_write`s a second (a socket write to `logd` each), on the frame's+/// own thread, in release builds too — and enough volume for `logd`'s chatty filter to start+/// dropping the lines anyway. Once a second, carrying the window's avg/max, keeps every number+/// the per-frame line had (the max still catches a single slow frame) for 1/60th of the cost.+pub(crate) const REPORT_INTERVAL: std::time::Duration = std::time::Duration::from_secs(1);++/// Decides which frames write the report line: the first one, then at most one per+/// [`REPORT_INTERVAL`], each covering every frame since the last.+pub(crate) struct ReportThrottle {+ frames: u32,+ last: Option<std::time::Instant>,+}++impl ReportThrottle {+ pub(crate) fn new() -> Self {+ ReportThrottle { frames: 0, last: None }+ }++ /// Count one frame at `now`; `Some(frames in the window)` when this frame should report.+ pub(crate) fn tick(&mut self, now: std::time::Instant) -> Option<u32> {+ self.frames = self.frames.saturating_add(1);+ if let Some(last) = self.last {+ if now.saturating_duration_since(last) < REPORT_INTERVAL {+ return None;+ }+ }+ let frames = self.frames;+ self.frames = 0;+ self.last = Some(now);+ Some(frames)+ }+}+ /// Nanos between two `Instant` reads, saturating rather than wrapping on absurd values. pub(crate) fn nanos_since(start: std::time::Instant) -> u64 { u64::try_from(start.elapsed().as_nanos()).unwrap_or(u64::MAX)@@ -282,6 +318,45 @@ mod tests { assert!(line.contains("prs 2.5/2.5"), "got {line}"); } + #[test]+ fn the_report_line_is_written_once_a_second_not_every_frame() {+ use std::time::{Duration, Instant};+ let mut throttle = ReportThrottle::new();+ let t0 = Instant::now();+ // The first frame reports (startup is when the line matters most).+ assert_eq!(throttle.tick(t0), Some(1));+ // 59 more frames at 60 fps inside the second: silent.+ for i in 1..60u64 {+ assert_eq!(throttle.tick(t0 + Duration::from_micros(16_667 * i)), None);+ }+ // The frame at one second reports the whole window.+ assert_eq!(throttle.tick(t0 + REPORT_INTERVAL), Some(60));+ }++ #[test]+ #[ignore = "timing harness; run with --ignored --nocapture"]+ fn the_per_frame_report_formatting_cost() {+ // What every frame paid before the throttle, before the logd write itself.+ let mut t = StepTimes::new();+ let n = 20_000u32;+ let start = std::time::Instant::now();+ let mut bytes = 0usize;+ for i in 0..n {+ for step in Step::ALL {+ t.record(step, u64::from(i) * 1_000);+ }+ let line = format!(+ "z{:.2} @{:.4},{:.4} b{:.0} vp {}x{}dp {}x{}px msaa {}x | resident {} tiles, {} meshes, \+ {} draws, {} tris | {} in flight, {} absent, {} stale-dropped | archive z{}..{} | {}",+ 14.2, -122.4194, 37.7749, 0.0, 412.0, 915.0, 1080, 2400, 4, 40, 900, 1200, 400_000,+ 3, 10, 0, 0, 15, t.report(1)+ );+ bytes += line.len();+ }+ let per = start.elapsed().as_secs_f64() * 1e6 / f64::from(n);+ println!("report line: {per:.2} us/frame to build ({} bytes)", bytes / n as usize);+ }+ #[test] fn report_resets_the_window() { let mut t = StepTimes::new();