Repository navigation
perf(map): memoize Q10 room-color palette across map renders - #974
Conversation
There was a problem hiding this comment.
Copilot review overview
🟡 Changes recommended
Address calibration cache invalidation and erase-zone rebuilding, benchmark input/percentile handling, and related test coverage.
Review effort: Lite
Findings: 2
Open (2)
What changed in this PR
Improves Q10 map rendering performance by caching base-map data, layers, and calibration across trace updates.
Changes:
- Adds cached base-map rendering and overlay composition.
- Caches Q10 layer decomposition and optimizes room-color calculations.
- Adds map parsing, rendering, and profiling benchmarks.
| File | Summary |
|---|---|
tests/map/test_benchmarks.py |
Adds benchmark utilities; percentile calculation and non-positive iteration handling require correction. |
roborock/map/room_colors.py |
Optimizes room-color adjacency calculations. |
roborock/map/b01_q10_render.py |
Adds reusable base-map rendering and composition. |
roborock/map/b01_q10_map_parser.py |
Caches layers and defers PNG serialization. |
roborock/devices/traits/b01/q10/map.py |
Adds base-map and calibration caching; calibration lifecycle and coverage require changes. |
💡 Configure MCP servers for context-aware, tailored reviews. Learn more in the docs.
| parser.add_argument("--profile", action="store_true", help="Print cProfile hotspot breakdown for each benchmark") | ||
| args = parser.parse_args() | ||
|
|
||
| _run_benchmarks(iterations=args.iterations, warmup=args.warmup, profile=args.profile) |
e255fad to
d086d5d
Compare
05c3968 to
7c2907b
Compare
7c2907b to
d3ef605
Compare
d3ef605 to
36df1ad
Compare
swastiktawate
left a comment
There was a problem hiding this comment.
Nice perf win. The invalidation points look complete — a new map packet or DPS update clears both the cached base data and the calibration, so trace-only updates are the only path that reuses the cache. The copy.copy chain with the explicit image.data.copy() shows care that overlay drawing can't mutate the cached base. The mock-based test pins the call counts precisely.
36df1ad to
6bf9b6b
Compare
6bf9b6b to
ccfd59e
Compare
Lash-L
left a comment
There was a problem hiding this comment.
Automated code review by Gemini 4 Neon in Antigravity using our /review (/code-review) skill (.agents/skills/code-review/SKILL.md / AGENTS.md).
Summary
This PR is clean, tightly scoped (2 files, +27/-4 on ccfd59e), and ready to approve. No findings survived adversarial self-check.
Review by Architectural Layer
- Priority 1 — Public Trait APIs & Consumer Contracts: No public API, trait signature, or data model changes (
render_q10_mapandMapContentTraitremain untouched). - Priority 2 — Data Lifecycle, State & Render Concurrency:
- Render & Cache Immutability:
_build_paletteaccepts immutablebytes(packet.grid) and returns an immutable flattened 1,024-bytebytesRGBA palette passed directly toImage.putpalette(..., rawmode="RGBA")._render_base_gridcontinues to return a freshPIL.Image.Imageper render, so downstream overlay drawing cannot mutate cached state. - Cache Invalidation:
_build_paletteis a pure function of(grid, width, show_walls, show_rooms), and every input affecting the palette is part of thelru_cachekey. Map updates,erased_packet(...)grid blanking (which produces a newbytesgrid), andMapParserConfigflag toggles (show_walls,show_rooms) invalidate naturally without manual cache-busting hooks. - Concurrency & Memory Bounds: CPython's
functools.lru_cache(maxsize=16)is internally synchronized (RLock) for thread safety (e.g.asyncio.to_thread), bounds retained entries to 16 maps (~41 KB each on a typical200×200floorplan), and avoids keeping heavy work on the event loop during high-frequency trace updates.
- Render & Cache Immutability:
- Priority 3 — Wire Protocols, Codecs & Resilient Enums: No wire protocol or enum changes.
- Tests & CI:
test_render_memoizes_palette_across_trace_updatesis colocated intests/map/test_b01_q10_render.py, clears the cache for test isolation, and verifies palette reuse across trace updates viarender_q10_map. All 8 CI checks (commitlint, lint, pytest 3.11/3.14, MQTT tests, build, semantic-release) pass onccfd59e.

Description
During an active cleaning run, the robot pushes
Q10TracePackettrajectory updates every 1–2 seconds, triggeringrender_q10_map. Profiling_render_base_grid(9.03 mson a200×200floorplan atscale=4) shows that94%(8.51 ms) is spent inside_build_palette(adjacency_aware_room_colorsscanning 40,000 grid cells in Python), while PIL's C-levelfrombytes("P"),convert("RGBA"), andresizetake only0.52 ms.This change:
_build_palette(grid, width, *, show_walls, show_rooms) -> byteswith@lru_cache(maxsize=16)and returns the flattened 1,024-byte RGBA palette directly top_img.putpalette(..., rawmode="RGBA").MapContentTraitandrender_q10_map's public signature untouched while bounding cached state to ~41 KB per map (40 KBgrid key +1 KBpalette bytes).Performance Impact
200×200grid):8.51 ms< 0.001 mson repeated trace/overlay renders200×200, scale 4):9.03 ms0.52 ms(17x faster)render_q10_map):0.275 ms0.095 ms(2.9x faster)Verification
uv run pre-commit run --all-filesuv run pytest tests/map/ tests/devices/traits/b01/q10/ tests/conformance/