# Subject-ID Assignment

**URL:** <https://forum.opencyphal.org/t/subject-id-assignment/1882>\
**Category:** Applications & Usage\
**Created:** [May 9, 2023, 1:54pm UTC](https://forum.opencyphal.org/t/subject-id-assignment/1882 "2023-05-09T13:54:15Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![JustinOng](https://forum.opencyphal.org/user_avatar/forum.opencyphal.org/justinong/32/1182_2.png) [@JustinOng](https://forum.opencyphal.org/u/JustinOng)\
**Post date:** [May 9, 2023, 1:54pm UTC](https://forum.opencyphal.org/t/subject-id-assignment/1882/1 "2023-05-09T13:54:15Z")

</div>

For an hypothetical Cyphal network with say, 500 nodes which periodically publish a sensor reading, how should the Subject-IDs be handled?

Based on the Cyphal Guide

> While it is possible to embed the means of instance identification into the service contract itself (for example, by extending the data types with a numerical instance identifier like it is sometimes done in DDS keyed topics), this practice is ill-advised because it constitutes a leaky abstraction that couples the service instance identification with its domain objects.

and the extensive discussion around the [integration of Cyphal into ArduPilot](https://discuss.ardupilot.org/t/discussion-of-cyphal-support/98016) and [DS-015](https://forum.opencyphal.org/t/problems-with-ds-015/1219/18), I understand that the intention is to assign at the time of setup, an unique Subject-ID of 5000-5499 for the output of each node.

While I see the benefits of not defining a fixed Subject-ID at the data type definition, this method of separate Subject-IDs for every reading seems to result in large runtime costs, especially on RAM: for instance, [libcanard](https://github.com/OpenCyphal/libcanard/blob/73d0a9cae94307038344b0d3eac2fd6dac44e139/libcanard/canard.h#L301) requires 0.5KiB~ per subscription. There’s also the associated [code bloat](https://github.com/ArduPilot/ardupilot/pull/20408/files#diff-cc40d1571b2c955864b6e22af7f62af909701c352fa83067d50735a684d34671R44).

I can think of a few ways to compromise:

1. Configure a single Subject-ID used by all the nodes, then use the Node-ID to identify where the message came from. Of course, this breaks when a node has more than one sensor.
2. Configure a single Subject-ID used by all the nodes, then extend the data type with an identifier

> ```auto
> uint8 sensor_id # Which specific sensor is it?
> float32 pressure_difference # [pascal]
> 
> ```

---

<div class="post-metadata">

**Author:** ![pavel.kirienko](https://forum.opencyphal.org/user_avatar/forum.opencyphal.org/pavel.kirienko/32/9_2.png) [@pavel.kirienko](https://forum.opencyphal.org/u/pavel.kirienko)\
**Post date:** [May 9, 2023, 2:10pm UTC](https://forum.opencyphal.org/t/subject-id-assignment/1882/2 "2023-05-09T14:10:11Z")

</div>

The idiomatic approach is to assign a separate Subject-ID per sensor, as you described.

The libcanard RAM utilization per session is something that may require revision. For example, in libudpard, where the maximum number of nodes is 65535 (from 0 to 65534), the static table approach adopted in libcanard is no longer viable, so we use a O(\log n) tree instead; perhaps the same approach should be applied in libcanard as well, possibly as a build option.

The compromises you listed are well-known indeed, and they are known to cause significant issues in practical deployments. I advise avoiding them.

For the bloat issue you mentioned, check out this RFC: [RFC: add array of ports](https://forum.opencyphal.org/t/rfc-add-array-of-ports/1878). Any input would be appreciated (I am yet to provide mine).
