Skip to content

Fix latency performance - #232

Merged
DanPeled merged 6 commits into
mainfrom
fix/latency_performance
Oct 30, 2026
Merged

DanPeled merged 6 commits into
mainfrom
fix/latency_performance

Conversation

@DanPeled

Copy link
Copy Markdown
Owner

improves around 2ms of latency per frame

sendToAllSync previously created a new asyncio task (via
run_coroutine_threadsafe) for every message. The runtime publishes metrics,
results and camera frames continuously, so that meant thousands of task
allocations per second on the websocket loop.

Messages are now handed to a single long-lived send task that drains a queue,
which also guarantees ordering and applies natural backpressure. createMessage
reuses a module-level field-name set instead of rebuilding it per message.
publishFrame always called cv2.resize, even when the captured frame already
matched the requested stream/recording size, and allocated a fresh destination
image for every published frame.

Skip the resize when dimensions already match and reuse a per-camera
destination buffer when they do not. The recording buffer is released when a
recording stops.
- Cache each camera's view id behind an NT listener instead of calling
  entry.exists()/getString() on every frame; only the listener touches
  NetworkTables now.
- Publish Apriltag hasResults and tag_id only when their value changes
  instead of re-sending them (and serializing to NT + the UI) every frame.
- In parsePipelineResult, resolve geometry through the module-level import
  and check containers before the geometry chain, since lists/dicts are the
  common result shape.
- Load fmap.json once and share it between pipeline instances instead of
  parsing the JSON for every ApriltagPipeline created.
- Cache the computed crop rectangle and only recompute it when the image
  size or crop settings change.
- Convert to grayscale into a reusable buffer rather than allocating a new
  image per frame; the OpenCV output is contiguous, so the extra
  ascontiguousarray copy is no longer needed.
- Draw the tag outline with a single cv2.polylines call and compute the
  center without a temporary numpy array.
@DanPeled
DanPeled merged commit f73dd3e into main Oct 30, 2026
16 checks passed
@DanPeled
DanPeled deleted the fix/latency_performance branch October 30, 2026 20:30
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant