Skip to content

Incremental EntityCache refresh instead of full rebuild each tick #442

@bburda

Description

@bburda

Summary

Each discovery refresh rebuilds the whole entity cache.

GatewayNode::refresh_entity_cache() (src/ros2_medkit_gateway/src/gateway_node.cpp, around lines 1885-2081) runs the discovery and merge pipeline to build fresh area, component, app and function vectors, then calls ThreadSafeEntityCache::update_all(...). update_all replaces all primary vectors and calls rebuild_all_indexes(), which rebuilds all seven hash maps on every tick. There is no diff or change detection.

The refresh interval is the main CPU lever. A 200 ms refresh costs about 4x the CPU of the 1000 ms default, while memory differences across configs stay within a few MiB. Measured with a benchmark harness in selfpatch_demos (sweep lane).

Proposed solution

  • Compare the new entity set against the cached one and apply only the changes (add, remove, update), instead of rebuilding and swapping every refresh.
  • Skip the work when nothing changed. On a stable graph most refresh ticks produce the same entities.

Additional context

Full rebuild every tick is wasted work on a stable graph. The cost scales with graph size and refresh frequency, which is why a fast refresh is expensive.

Metadata

Metadata

Assignees

Labels

enhancementNew feature or request

Type

No type
No fields configured for issues without a type.

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions