Dict parsing question

Simon McVittie smcv at collabora.com
Wed Jan 3 14:39:02 UTC 2024


On Tue, 02 Jan 2024 at 09:09:56 -0300, Thiago Macieira wrote:
> I don't think this quirk was intentional. I think it was a side-effect of 
> the reference implementation that wasn't properly understood at the time and 
> therefore was baked in to the specification.

My reading had always been that the alignment requirement was designed
into the wire protocol as an optimization, to allow implementations to
do "zero-copy" reads out of messages if they want to (and, as an
implementation detail, the reference implementation does do that).

Concretely, if we did not guarantee that (for example) "double" data items
have their desired alignment, then it would not be possible to implement
dbus_message_iter_get_fixed_array() [1] without either memcpy()ing the
array into correctly-aligned storage, or requiring the caller to cope
with the possibility that we might give them a misaligned pointer from
which they must memcpy() the data into correctly-aligned storage before
they can dereference or iterate it.

The price we pay for that is that when serializing and deserializing
messages, we have to care about alignment and padding. One could argue
about whether the benefit is worth that price or not, but changing it
would be disruptive, so the status quo wins by default.

This is similar to the way we guarantee that strings in the wire-protocol
encoding are followed by at least one \0 - even though, strictly speaking,
it's redundant - so that C-based languages can point directly into the
message to access the string and pass it to C-string-oriented library
functions as-is, rather than always having to memcpy() or strndup()
the string into their own storage.

Languages that are not C-based (like Java or a pure-Python D-Bus
implementation) will generally need to copy the data *anyway*, in order
to meet their language environment's memory management expectations,
so they get little or no benefit from these optimizations; but that
doesn't mean that the optimization has no value.

    smcv

[1] https://dbus.freedesktop.org/doc/api/html/group__DBusMessage.html#gae195a3312ae445e7ef0196854f3523f8


More information about the dbus mailing list