Skip to content

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

Open
allenporter wants to merge 1 commit into
Python-roborock:mainfrom
allenporter:perf-q10-base-map-caching
Open

allenporter wants to merge 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

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Q10 B01 Q10 platform Small PR Small pull request (<200 lines)

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants