Homepage GitHub

Proposed change to variable-length arrays

In the spirit of simplification over optimization I think we should change the default serialization behaviour of variable-length arrays and then introduce an advanced option to reenable the existing bandwidth-optimized behaviour:

uint8[<=16] a

would silently expand to:

void3
uint8[<=16] a

Optionally the system designer could do

@unaligned
uint8[<=16] a

to reenable the behaviour defined in v1 alpha and v0.

Rationale

The current behaviour is unintuitive and requires a thorough reading of the specification by the system designer. Even then the system designer has to manually perform and remember to maintain the ceil(log2(capacity+1)) calculation needed to install the proper void padding. The implicit behaviour would always waste bits to keep the first element of the array aligned.

Complications

uint2 a
uint8[<=16] b

So what about this? One possibility is that the DSDL compiler knows to imply this:

uint2 a
void1
uint8[<=16] b

The other is that we assume unaligned is expected if the variable length array is started on an unaligned boundary implying this:

uint2 a
@unaligned
uint8[<=16] b

I breif survey might be in order to determine which of these two behaviours are “intuitive”.

I think the change proposed here would introduce a special case by making the array length field implicitly aligned unlike other types:

We briefly discussed the possibility of introducing an opposite directive @aligned later in v1.x (x>0), but there are certain issues that arise with variable-offset fields. Specifically, let’s suppose that your proposal is implemented. How would you suggest it handle the following:

uint3[<=16] a
@assert (_offset_ % 8).count > 1  # Yup, non byte-aligned.
# <-- Insert dynamic, runtime-computed padding here?
uint8[<=16] b

I think that explicit alignment with @align is more aligned :drum: with the core design goals, but this is probably debatable. At any rate, this discussion should not be focused on arrays, because they are not special. There was a misguided statement made in Big-endian vs. little-endian in the context of bit-level encoding where it was implied that the serialization rules of variable-length arrays differ from other data types – that is not true.

I think we have a real opportunity here to improve the accessibility of UAVCAN by making it easier to design zero-cost types. My focus on the variable length array is because of the implicit length field that often causes unalignment without the author realizing it. Everywhere else in a message definition the author makes visible choices that would suggest alignment issues. For example:

uint2
uint2
uint4
# I'm aligned here on and 8-bit boundary
uint8[16]
# Still aligned
uint8[<=16]
# I'll probably assume each item in this array is aligned on 8-bit boundaries. I have to get
# out the spec and a calculator to find out that I'm wrong.

I propose two modifications to v1:

  1. We add the @aligned directive immediately
  2. We implicitly add @align 8 to the implicit length field of variable length arrays. We also allow @align 1 to appear before a variable length array which will override the implicit @align 8 directive and restore the current layout behaviour.
    • The @align always applies to the start of an array and not to each element in an array. To change alignment rules for array elements authors must make the element a composite type and provide alignment directives in that type’s definition.

I agree, the implicitness here is bad.

implicitly add @align 8 to the implicit…

image

Can we please postpone this until v1.1? (There are other tabled issues out there). I am a little concerned about the dynamic nature of this @align field, it’s a rather major change (as I wrote above, if _offset_ % 8 != {0} then @aligned cannot be replaced with a voidX). It won’t affect backward compatibility though so it can be added later easily.

If you are okay with postponing it, can you please open a new issue in the Specification repository and label it future?

First: HA! +1 meme usage there.

Second: is there anyway we can avoid this pitfall in 1.0 without the @align directive? Do we just always align this field on a byte boundary and only allow tighter packing in 1.1? Do we add @align but only allow it for aligning dynamic arrays in 1.0 and expanding its role in 1.1? It’s sort of the first rock people trip over when starting to use UAVCAN so it’s low-hanging fruit (waiting for meme here) to reduce the friction to adoption.

Sigh. Okay. I suppose we could go with this one, but we should also allow its placement before the @union directive to indicate that the tag should be aligned:

@align
@union  # The tag is prepended with up to 7 bits of padding.
uint8 a
float16 b

Later we will be able to expand its functionality as follows:

  • Add an optional argument to select arbitrary alignment in bits.
  • Support the case where (_offset_ % 8).count > 1 by requiring implementations to compute the required alignment at runtime (I am worried about this runtime thing because it does complicate min/max size computation).

Is it “align” or “aligned”? I suspect “align” suits the purpose better.

Would you like to submit a PR to the spec yourself where we could then settle down the exact implementation details?