Skip to content

perf(map): memoize Q10 room-color palette across map renders - #974

Merged
Lash-L merged 1 commit into
Python-roborock:mainfrom
allenporter:perf-q10-base-map-caching
Nov 22, 2026
Merged

Lash-L merged 1 commit into
Python-roborock:mainfrom
allenporter:perf-q10-base-map-caching

Conversation

@allenporter

@allenporter allenporter commented Sep 28, 2026 •

Copy link
Copy Markdown
Contributor

Description

During an active cleaning run, the robot pushes Q10TracePacket trajectory updates every 1–2 seconds, triggering render_q10_map. Profiling _render_base_grid (9.03 ms on a 200×200 floorplan at scale=4) shows that 94% (8.51 ms) is spent inside _build_palette (adjacency_aware_room_colors scanning 40,000 grid cells in Python), while PIL's C-level frombytes("P"), convert("RGBA"), and resize take only 0.52 ms.

This change:

  1. Memoizes _build_palette(grid, width, *, show_walls, show_rooms) -> bytes with @lru_cache(maxsize=16) and returns the flattened 1,024-byte RGBA palette directly to p_img.putpalette(..., rawmode="RGBA").
  2. Keeps MapContentTrait and render_q10_map's public signature untouched while bounding cached state to ~41 KB per map (40 KB grid key + 1 KB palette bytes).

Performance Impact

  • Base palette construction (200×200 grid): 8.51 ms $\rightarrow$ < 0.001 ms on repeated trace/overlay renders
  • Base grid rasterization (200×200, scale 4): 9.03 ms $\rightarrow$ 0.52 ms (17x faster)
  • Small fixture + trace (render_q10_map): 0.275 ms $\rightarrow$ 0.095 ms (2.9x faster)

Verification

  • uv run pre-commit run --all-files
  • uv run pytest tests/map/ tests/devices/traits/b01/q10/ tests/conformance/

Copilot AI lite review requested due to automatic review settings September 28, 2026 03:34

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 Medium severity

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.

Comment thread roborock/devices/traits/b01/q10/map.py Resolved
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)
@allenporter
allenporter marked this pull request as draft September 28, 2026 03:46
@allenporter
allenporter force-pushed the perf-q10-base-map-caching branch 3 times, most recently from e255fad to d086d5d Compare October 4, 2026 22:40
@allenporter
allenporter force-pushed the perf-q10-base-map-caching branch 2 times, most recently from 05c3968 to 7c2907b Compare October 12, 2026 02:16
@allenporter
allenporter force-pushed the perf-q10-base-map-caching branch from 7c2907b to d3ef605 Compare October 13, 2026 13:06
@allenporter
allenporter force-pushed the perf-q10-base-map-caching branch from d3ef605 to 36df1ad Compare October 17, 2026 22:35

@swastiktawate swastiktawate left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

@Lash-L Lash-L added Small PR Small pull request (<200 lines) Q10 B01 Q10 platform labels Nov 15, 2026
@allenporter
allenporter force-pushed the perf-q10-base-map-caching branch from 36df1ad to 6bf9b6b Compare November 16, 2026 05:43
@allenporter
allenporter marked this pull request as ready for review November 16, 2026 05:43
@allenporter
allenporter force-pushed the perf-q10-base-map-caching branch from 6bf9b6b to ccfd59e Compare November 16, 2026 07:05
@allenporter allenporter changed the title perf(trait): cache rendered base map on Q10 trace updates perf(map): memoize Q10 room-color palette across map renders Nov 16, 2026

@Lash-L Lash-L left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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_map and MapContentTrait remain untouched).
  • Priority 2 — Data Lifecycle, State & Render Concurrency:
    • Render & Cache Immutability: _build_palette accepts immutable bytes (packet.grid) and returns an immutable flattened 1,024-byte bytes RGBA palette passed directly to Image.putpalette(..., rawmode="RGBA"). _render_base_grid continues to return a fresh PIL.Image.Image per render, so downstream overlay drawing cannot mutate cached state.
    • Cache Invalidation: _build_palette is a pure function of (grid, width, show_walls, show_rooms), and every input affecting the palette is part of the lru_cache key. Map updates, erased_packet(...) grid blanking (which produces a new bytes grid), and MapParserConfig flag 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 typical 200×200 floorplan), and avoids keeping heavy work on the event loop during high-frequency trace updates.
  • Priority 3 — Wire Protocols, Codecs & Resilient Enums: No wire protocol or enum changes.
  • Tests & CI: test_render_memoizes_palette_across_trace_updates is colocated in tests/map/test_b01_q10_render.py, clears the cache for test isolation, and verifies palette reuse across trace updates via render_q10_map. All 8 CI checks (commitlint, lint, pytest 3.11/3.14, MQTT tests, build, semantic-release) pass on ccfd59e.

@Lash-L Lash-L added /review run Code review skill (/review) has been run Passed /review Passed the /review skill check labels Nov 21, 2026
@allenporter
allenporter requested a review from Lash-L November 22, 2026 04:32
@Lash-L
Lash-L merged commit f2a59b0 into Python-roborock:main Nov 22, 2026
9 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Passed /review Passed the /review skill check Q10 B01 Q10 platform /review run Code review skill (/review) has been run Small PR Small pull request (<200 lines)

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants