1*61c4878aSAndroid Build Coastguard Worker.. _module-pw_snapshot-design_discussion: 2*61c4878aSAndroid Build Coastguard Worker 3*61c4878aSAndroid Build Coastguard Worker================= 4*61c4878aSAndroid Build Coastguard WorkerDesign Discussion 5*61c4878aSAndroid Build Coastguard Worker================= 6*61c4878aSAndroid Build Coastguard WorkerThere were a handful of key requirements going into the design of pw_snapshot: 7*61c4878aSAndroid Build Coastguard Worker 8*61c4878aSAndroid Build Coastguard Worker* **Pre-established file format** - Building and maintaining tooling to support 9*61c4878aSAndroid Build Coastguard Worker parsing binary snapshot data is a high maintenance burden that detracts from 10*61c4878aSAndroid Build Coastguard Worker the appeal of a pre-existing widely known/supported format. 11*61c4878aSAndroid Build Coastguard Worker* **Incremental writing** - Needing to build an entire snapshot before 12*61c4878aSAndroid Build Coastguard Worker committing it as a finished file is a big limitation on embedded devices where 13*61c4878aSAndroid Build Coastguard Worker RAM is often very constrained. It is important that a snapshot can be built in 14*61c4878aSAndroid Build Coastguard Worker smaller in-memory segments that can be committed incrementally to a larger 15*61c4878aSAndroid Build Coastguard Worker sink (e.g. UART, off-chip flash). 16*61c4878aSAndroid Build Coastguard Worker* **Extensible** - Pigweed doesn't know everything users might want to capture 17*61c4878aSAndroid Build Coastguard Worker in a snapshot. It's important that users have ways to include their own 18*61c4878aSAndroid Build Coastguard Worker information into snapshots with minimal friction. 19*61c4878aSAndroid Build Coastguard Worker* **Relatively compact** - It's important that snapshots can contain useful 20*61c4878aSAndroid Build Coastguard Worker information even when they are limited to a few hundred bytes in size. 21*61c4878aSAndroid Build Coastguard Worker 22*61c4878aSAndroid Build Coastguard WorkerWhy Proto? 23*61c4878aSAndroid Build Coastguard Worker========== 24*61c4878aSAndroid Build Coastguard WorkerProtobufs are widely used and supported across many languages and platforms. 25*61c4878aSAndroid Build Coastguard WorkerThis greatly reduces the encode/decode tooling maintenance introduced by using 26*61c4878aSAndroid Build Coastguard Workercustom or unstructured formats. While using a format like JSON provides 27*61c4878aSAndroid Build Coastguard Workersimilarly wide tooling support, encoding the same information as a proto 28*61c4878aSAndroid Build Coastguard Workersignificantly reduces the final file size. 29*61c4878aSAndroid Build Coastguard Worker 30*61c4878aSAndroid Build Coastguard WorkerWhile protobuffer messages aren't truly streamable (i.e. can be written without 31*61c4878aSAndroid Build Coastguard Workerany intermediate buffers) due to how message nesting works, a large message can 32*61c4878aSAndroid Build Coastguard Workerbe incrementally written as long as there's enough buffer space for encoding the 33*61c4878aSAndroid Build Coastguard Workerlargest single sub-message in the proto. 34*61c4878aSAndroid Build Coastguard Worker 35*61c4878aSAndroid Build Coastguard WorkerWhy overlay multiple protos? 36*61c4878aSAndroid Build Coastguard Worker============================ 37*61c4878aSAndroid Build Coastguard WorkerProto 2 supported a feature called "extensions" that explicitly allowed this 38*61c4878aSAndroid Build Coastguard Workerbehavior. While proto 3 removed this feature, it doesn't disallow the old 39*61c4878aSAndroid Build Coastguard Workerbehavior of serializing two 'overlayed' protos to the same data stream. Proto 3 40*61c4878aSAndroid Build Coastguard Workerrecommends using an "Any" proto instead of extensions, as it is more explicit 41*61c4878aSAndroid Build Coastguard Workerand eliminates the issue of collisions in proto messages. Unfortunately, proto 42*61c4878aSAndroid Build Coastguard Worker'Any' messages introduce unacceptable overhead. For a single integer that would 43*61c4878aSAndroid Build Coastguard Workerencode to a few bytes using extensions, an Any submessage quickly expands to 44*61c4878aSAndroid Build Coastguard Workertens of bytes. 45*61c4878aSAndroid Build Coastguard Worker 46*61c4878aSAndroid Build Coastguard Workerpw_snapshot's proto format takes advantage of "extensions" from proto 2 without 47*61c4878aSAndroid Build Coastguard Workerexplicitly relying on the feature. To reduce the risk of colissions and maximize 48*61c4878aSAndroid Build Coastguard Workerencoding efficiency, certain ranges are reserved to allow Pigweed to grow while 49*61c4878aSAndroid Build Coastguard Workerensuring downstream customers have equivalent flexibility when using the 50*61c4878aSAndroid Build Coastguard WorkerSnapshot proto format. 51*61c4878aSAndroid Build Coastguard Worker 52*61c4878aSAndroid Build Coastguard WorkerWhy no file header? 53*61c4878aSAndroid Build Coastguard Worker=================== 54*61c4878aSAndroid Build Coastguard WorkerRight now it's assumed that anything that is storing or transferring a 55*61c4878aSAndroid Build Coastguard Workerserialized snapshot implicitly tracks its size (and a checksum, if desired). 56*61c4878aSAndroid Build Coastguard WorkerWhile a container format might be introduced independently, pw_snapshot focuses 57*61c4878aSAndroid Build Coastguard Workeron treating an encoded snapshot as raw serialized proto data. 58