CINXE.COM
IPLD ♦
<!doctype html> <html lang="en"> <head> <meta charset="utf-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <link rel="stylesheet" href="/css/layout.css?1732633144785"> <link rel="stylesheet" href="/css/nav.css?1732633144785"> <link rel="stylesheet" href="/css/style.css?1732633144785"> <link rel="stylesheet" href="/css/prismjs@1.24-themes-prism.css"> <title>IPLD ♦ </title> </head> <body> <header> <div class="sidebar-button" onclick="document.getElementById('sidebar').classList.toggle('sidebar-open')"> <svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" role="img" viewBox="0 0 448 512" class="icon"> <path fill="currentColor" d="M436 124H12c-6.627 0-12-5.373-12-12V80c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12zm0 160H12c-6.627 0-12-5.373-12-12v-32c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12zm0 160H12c-6.627 0-12-5.373-12-12v-32c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12z"></path> </svg> </div> <a href="/" class="logo">IPLD</a> <aside id=breadcrumbs> <ul> <li><a href="/design">design</a></li> <li><a href="/design/tricky-choices">tricky-choices</a></li> <li><a href="/design/tricky-choices/ordering/">ordering</a></li> </ul> </aside> </header> <aside id=sidebar> <nav> <ul> <li> <a href="/docs/">Docs</a><ul> <li> <a href="/docs/intro/">Intro</a><ul> <li> <a href="/docs/intro/hello-world/">Hello, World</a></li> <li> <a href="/docs/intro/primer/">The Brief Primer</a></li> <li> <a href="/docs/intro/ecosystem/">InterPlanetary Ecosystem Overview</a></li> <li> <a href="/docs/intro/community/">Finding Community</a></li></ul></li> <li> <a href="/docs/motivation/">Motivation</a><ul> <li> <a href="/docs/motivation/benefits-of-content-addressing/">Benefits of Content Addressing</a></li> <li> <a href="/docs/motivation/data-to-data-structures/">From Data to Data Structures</a></li></ul></li> <li> <a href="/docs/codecs/">Codecs</a><ul> <li> <a href="/docs/codecs/known/">Known Codecs</a><ul> <li> <a href="/docs/codecs/known/dag-cbor/">DAG-CBOR</a></li> <li> <a href="/docs/codecs/known/dag-json/">DAG-JSON</a></li> <li> <a href="/docs/codecs/known/dag-pb/">DAG-PB</a></li></ul></li></ul></li> <li> <a href="/docs/data-model/">Data Model</a><ul> <li> <a href="/docs/data-model/node/">Nodes</a></li> <li> <a href="/docs/data-model/kinds/">Kinds</a></li> <li> <a href="/docs/data-model/pathing/">Pathing</a></li> <li> <a href="/docs/data-model/traversal/">Traversal</a></li></ul></li> <li> <a href="/docs/advanced-data-layouts/">Advanced Data Layouts</a><ul> <li> <a href="/docs/advanced-data-layouts/intro/">Intro to ADLs</a></li> <li> <a href="/docs/advanced-data-layouts/naming/">ADL Naming</a></li> <li> <a href="/docs/advanced-data-layouts/signalling/">Signalling ADLs</a></li> <li> <a href="/docs/advanced-data-layouts/dynamic-loading/">Dynamic Loading</a></li> <li> <a href="/docs/advanced-data-layouts/known/">Known ADLs</a></li></ul></li> <li> <a href="/docs/schemas/">Schemas</a><ul> <li> <a href="/docs/schemas/intro/">Introduction</a><ul> <li> <a href="/docs/schemas/intro/compare/">compare</a></li> <li> <a href="/docs/schemas/intro/goals/">Goals</a></li> <li> <a href="/docs/schemas/intro/feature-summary/">Feature Summary</a></li></ul></li> <li> <a href="/docs/schemas/features/">Features</a><ul> <li> <a href="/docs/schemas/features/typekinds/">Type Kinds</a></li> <li> <a href="/docs/schemas/features/representation-strategies/">Representation Strategies</a></li> <li> <a href="/docs/schemas/features/links/">Links</a></li> <li> <a href="/docs/schemas/features/indicating-adls/">Using ADLs in Schemas</a></li></ul></li> <li> <a href="/docs/schemas/using/">Using Wisely</a><ul> <li> <a href="/docs/schemas/using/authoring-guide/">Authoring Guide</a></li> <li> <a href="/docs/schemas/using/migrations/">Migrations</a></li></ul></li></ul></li> <li> <a href="/docs/synthesis/">Synthesis</a><ul> <li> <a href="/docs/synthesis/gtd/">Getting Things Done</a></li> <li> <a href="/docs/synthesis/building-in-alignment/">Building in Alignment</a></li> <li> <a href="/docs/synthesis/how-ipfs-web-gateways-work/">How IPFS Web Gateways Work</a></li> <li> <a href="/docs/synthesis/encryption/">Working With Encryption</a></li></ul></li></ul></li> <li> <a href="/specs/">Specs</a><ul> <li> <a href="/specs/about/">About the Specifications</a></li> <li> <a href="/specs/codecs/">Codecs</a><ul> <li> <a href="/specs/codecs/dag-cbor/">DAG-CBOR</a><ul> <li> <a href="/specs/codecs/dag-cbor/spec/">Spec</a></li> <li> <a href="/specs/codecs/dag-cbor/fixtures/">DAG-CBOR Test Fixtures</a><ul> <li> <a href="/specs/codecs/dag-cbor/fixtures/cross-codec/">cross-codec</a></li></ul></li></ul></li> <li> <a href="/specs/codecs/dag-cosmos/">DAG-COSMOS</a><ul> <li> <a href="/specs/codecs/dag-cosmos/basic_types/">basic_types</a></li> <li> <a href="/specs/codecs/dag-cosmos/cosmos_state/">cosmos_state</a></li> <li> <a href="/specs/codecs/dag-cosmos/crypto_types/">crypto_types</a></li> <li> <a href="/specs/codecs/dag-cosmos/tendermint_chain/">tendermint_chain</a></li> <li> <a href="/specs/codecs/dag-cosmos/typed_protobuf/">typed_protobuf</a></li></ul></li> <li> <a href="/specs/codecs/dag-eth/">DAG-ETH</a><ul> <li> <a href="/specs/codecs/dag-eth/basic_types/">basic_types</a></li> <li> <a href="/specs/codecs/dag-eth/chain/">chain</a></li> <li> <a href="/specs/codecs/dag-eth/convenience_types/">convenience_types</a></li> <li> <a href="/specs/codecs/dag-eth/state/">state</a></li></ul></li> <li> <a href="/specs/codecs/dag-jose/">DAG-JOSE</a><ul> <li> <a href="/specs/codecs/dag-jose/spec/">Spec</a></li> <li> <a href="/specs/codecs/dag-jose/fixtures/">fixtures</a></li></ul></li> <li> <a href="/specs/codecs/dag-json/">DAG-JSON</a><ul> <li> <a href="/specs/codecs/dag-json/spec/">Spec</a></li> <li> <a href="/specs/codecs/dag-json/fixtures/">DAG-JSON Test Fixtures</a><ul> <li> <a href="/specs/codecs/dag-json/fixtures/cross-codec/">cross-codec</a></li></ul></li></ul></li> <li> <a href="/specs/codecs/dag-pb/">DAG-PB</a><ul> <li> <a href="/specs/codecs/dag-pb/spec/">Spec</a></li> <li> <a href="/specs/codecs/dag-pb/fixtures/">DAG-PB Test Fixtures</a><ul> <li> <a href="/specs/codecs/dag-pb/fixtures/cross-codec/">cross-codec</a></li></ul></li></ul></li></ul></li> <li> <a href="/specs/advanced-data-layouts/">Advanced Data Layouts</a><ul> <li> <a href="/specs/advanced-data-layouts/fbl/">FBL ADL</a><ul> <li> <a href="/specs/advanced-data-layouts/fbl/spec/">spec</a></li></ul></li> <li> <a href="/specs/advanced-data-layouts/hamt/">HAMT ADL</a><ul> <li> <a href="/specs/advanced-data-layouts/hamt/spec/">spec</a></li> <li> <a href="/specs/advanced-data-layouts/hamt/fixture/">HashMap (HAMT) Test Fixtures</a><ul> <li> <a href="/specs/advanced-data-layouts/hamt/fixture/alice-words/">alice-words</a></li></ul></li></ul></li></ul></li> <li> <a href="/specs/schemas/">Schemas</a><ul> <li> <a href="/specs/schemas/prelude/">prelude</a></li></ul></li> <li> <a href="/specs/transport/">Transports</a><ul> <li> <a href="/specs/transport/car/">CAR</a><ul> <li> <a href="/specs/transport/car/carv1/">CARv1 Specification</a></li> <li> <a href="/specs/transport/car/carv2/">CARv2 Specification</a></li> <li> <a href="/specs/transport/car/fixture/">CAR Test Fixtures</a><ul> <li> <a href="/specs/transport/car/fixture/carv1-basic/">carv1-basic</a></li> <li> <a href="/specs/transport/car/fixture/carv2-basic/">carv2-basic</a></li></ul></li></ul></li> <li> <a href="/specs/transport/graphsync/">Graphsync</a><ul> <li> <a href="/specs/transport/graphsync/known_extensions/">known_extensions</a></li></ul></li> <li> <a href="/specs/transport/trustless-pathing/">Trustless Pathing</a><ul> <li> <a href="/specs/transport/trustless-pathing/fixtures/">Trustless Pathing Fixtures</a><ul> <li> <a href="/specs/transport/trustless-pathing/fixtures/unixfs_20m_variety/">unixfs_20m_variety</a></li></ul></li></ul></li></ul></li> <li> <a href="/specs/selectors/">Selectors</a><ul> <li> <a href="/specs/selectors/fixtures/">fixtures</a><ul> <li> <a href="/specs/selectors/fixtures/selector-fixtures-1/">selector-fixtures-1</a></li> <li> <a href="/specs/selectors/fixtures/selector-fixtures-adl/">selector-fixtures-adl</a></li> <li> <a href="/specs/selectors/fixtures/selector-fixtures-recursion/">selector-fixtures-recursion</a></li></ul></li></ul></li> <li> <a href="/specs/patch/">Patch</a><ul> <li> <a href="/specs/patch/fixtures/">IPLD Patch Test Fixtures</a><ul> <li> <a href="/specs/patch/fixtures/fixtures-1/">fixtures-1</a></li></ul></li></ul></li></ul></li> <li> <a href="/libraries/">Libraries</a><ul> <li> <a href="/libraries/golang/">Golang</a></li> <li> <a href="/libraries/javascript/">JavaScript</a></li> <li> <a href="/libraries/python/">Python</a></li> <li> <a href="/libraries/rust/">Rust</a></li></ul></li> <li> <a href="/design/">Design</a><ul> <li> <a href="/design/objectives/">Objectives</a></li> <li> <a href="/design/concepts/">Concepts</a><ul> <li> <a href="/design/concepts/type-theory-glossary/">type-theory-glossary</a></li></ul></li> <li> <a href="/design/libraries/">Libraries</a><ul> <li> <a href="/design/libraries/nodes-and-kinds/">nodes-and-kinds</a></li></ul></li> <li> <a href="/design/tricky-choices/">Tricky Choices</a><ul> <li> <a href="/design/tricky-choices/dag-pb-forms-impl-and-use/">dag-pb-forms-impl-and-use</a></li> <li> <a href="/design/tricky-choices/map-key-domain/">map-key-domain</a></li> <li> <a href="/design/tricky-choices/numeric-domain/">numeric-domain</a></li> <li class="active-page"> <a href="/design/tricky-choices/ordering/">ordering</a></li> <li> <a href="/design/tricky-choices/string-domain/">string-domain</a></li></ul></li> <li> <a href="/design/open-research/">Open Research</a><ul> <li> <a href="/design/open-research/ADL-autoexecution/">ADL autoexecution</a></li></ul></li></ul></li> <li> <a href="/tools/">Tools</a></li> <li> <a href="/glossary/">Glossary</a></li> <li> <a href="/media/">Media</a></li> <li> <a href="/FAQ/">FAQ</a></li></ul> </nav> </aside> <main> <div class=content> <h1>Data Model: Ordering</h1> <p>This document is part of a series on <a href="..">tricky choices</a> in IPLD design.</p> <hr> <p>Maps in IPLD are typically <em>order-preserving</em>: if you put data into them in some order, you can expect that the iterators will return that information in the same order; and transformation functions will generally operate on data without reordering it.</p> <p>(Lists are of course also order-preserving. However, that's generally considered a bit less worthy of remark. We'll spent most of this document focusing on the situation for maps.)</p> <p>This is sometimes a subject of contention, so in this document, we'll go over <em>why</em> it's useful (if not outright necessary) to define maps this way in IPLD.</p> <p>We'll also go over what you might do as a library author and a user to interact with this most productively.</p> <ul> <li><a href="#behaviors">Behaviors</a></li> <li><a href="#reasons">Reasons</a></li> <li><a href="#impacts">Impacts</a></li> <li><a href="#recommendations-for-library-authors">Recommendations for Library Authors</a></li> </ul> <h2 id="behaviors" tabindex="-1"><a class="header-anchor" href="#behaviors">Behaviors</a></h2> <h3 id="order-preserving-is-the-default" tabindex="-1"><a class="header-anchor" href="#order-preserving-is-the-default">Order-Preserving is the Default</a></h3> <p>Order-preserving is the default behavior, and what you should expect unless otherwise specified.</p> <h3 id="libraries-may-vary" tabindex="-1"><a class="header-anchor" href="#libraries-may-vary">Libraries May Vary</a></h3> <p>Library implementations may make their own choices. We would likely say that a library that doesn't support order preservation is not a fully spec-compliant IPLD library, but that doesn't mean it's not possible for such a library to exist.</p> <p>(Such a library may even still be fully spec-compliant in <em>some</em> limited subset of cases, such as when only using codecs that also have strict sorted orders, for example.)</p> <h3 id="adls-may-have-discretionary-ordering" tabindex="-1"><a class="header-anchor" href="#adls-may-have-discretionary-ordering">ADLs may have discretionary ordering</a></h3> <p><a href="/docs/advanced-data-layouts/">ADLs</a> sometimes have stable but unusual orders that are based on details of their internal structures and algorithms.</p> <p>For one example, HAMTs do not preserve order, but do have a stable and deterministic order... and yet it is also not a sorted order: rather, it is based on the hashes of the keys of the map.</p> <h3 id="codecs-may-impose-sorted-orders" tabindex="-1"><a class="header-anchor" href="#codecs-may-impose-sorted-orders">Codecs may impose sorted orders</a></h3> <p>Some <a href="/docs/codecs/">Codecs</a> have strict map entry ordering rules.</p> <p>For example, <a href="/docs/codecs/known/dag-cbor/">DAG-CBOR</a> has a canonical sorted order which is based on the CBOR RFC's recommended sorting.</p> <h2 id="reasons" tabindex="-1"><a class="header-anchor" href="#reasons">Reasons</a></h2> <p>Why do we say that order-preserving is the default? Why do we also say that discretionary rules may override this in certain contexts?</p> <ol> <li>Ordering is more fundamental than not. <ul> <li>Serial data has an order! Bytes cannot be rearranged without consequence!</li> <li>Hashes change when the ordering of data changes, because hashes are based on serial data. This affects everything in a system like IPLD.</li> <li>This argues that order-preservation should be the default, and preferred whenever possible.</li> </ul> </li> <li>Streaming operations are only possible when preserving order. <ul> <li>Tools that only work on sorted data can only start outputting results after completely loading all input. This is necessarily true, because otherwise, it's possible that the first-when-sorted data comes at the end of a stream of input; in that worse-case scenario, an algorithm either must have buffered entirely up until that point, or if it has been streaming, must halt unexpectedly.</li> <li>This argues that order-preservation should be the default, and preferred whenever possible.</li> </ul> </li> <li>Unavoidably, some ADLs have opinionated data ordering (and it's not necessarily sorted). <ul> <li>e.g. HAMTs/CHAMPs have a natural ordering which is determined by their internal hashing scheme -- it's both an ordering, and it's not even a sorted one. We cannot change this because it's in the nature of their algorithms; so, it's best if our interfaces and standards understand and describe this.</li> <li>This means that we cannot mandate order-preservation in all cases, nor can we mandate a single sorted order.</li> </ul> </li> <li>Even codecs have specified various orderings. <ul> <li>e.g. the ordering used in DAG-CBOR (which was specified by an RFC that well predates IPLD) is not what most people consider an obvious or reasonable sorting. No other codec uses it. Yet, simultaneously, it's considered a standardized ordering. Therefore, by example, it is not particularly feasible for us to declare One True Sorted Ordering; there's too much disagreement in the field before we even begin.</li> <li>This means that we cannot mandate order-preservation in all cases, nor can we mandate a single sorted order.</li> </ul> </li> <li>People sometimes like their data to stay in the order they put it. <ul> <li>Consider the case for building "<code>jq</code>-like" data processing tools: IPLD libraries are should be generally suited for building such tools; and in examining this user story, we can observe that people seem to <em>like</em> tools which can operate streamingly and do not reorder their data without cause.</li> <li>This argues that order-preservation should be the default, and preferred whenever possible.</li> </ul> </li> <li>Order-preservation implementations result in the most reusable code and lowest total implementation effort. <ul> <li>It's much easier to implement order-preservation, and then add a sorting feature... than it is to implement strict sorting (or randomly-ordered intermediate storage) and add order-preservation later.</li> <li>This argues that order-preservation should be the default, and preferred whenever possible.</li> </ul> </li> <li>Lack of control over ordering can lead to performance issues when composed with other layers of the system. For example, in IPLD Schemas, some union types can have drastically different parse costs based on the order of map entries in the serialized form of the data. <ul> <li>It would be an extremely nasty cross-cutting concern if some schemas were efficiently serializable in some codecs, but had huge performance penalties in others because a forced sorting of map entries happened to put the union discriminant hint values in an order that's expensive to parse. Changing codec shouldn't cause surprises like this. Order preservation semantics quietly avoid this issue.</li> <li>This argues that order-preservation should be the default, and preferred whenever possible. (It also very specifically argues that codecs should refrain from having order restrictions.)</li> </ul> </li> </ol> <p>In summary: there are a preponderance of reasons that it's preferable to consider order-preservation the default in IPLD systems. There's also a couple hard roadblocks which means we can't always mandate either order-preservation, nor is it viable to mandate a sorted order. That leaves us with very carefully stated rules you've seen above: the system should prefer order-preservation, except when it can't.</p> <h3 id="counter-arguments" tabindex="-1"><a class="header-anchor" href="#counter-arguments">counter-arguments</a></h3> <p>It's interesting to note there are some counter-arguments which would dis-recommend order-preservation: namely, preserving order of map entries does not assist with <em>uncoordinated convergence</em>; sorting (once you agree on a sort!) <em>does</em>.</p> <p>Accordingly, we <em>do</em> recommend that application and protocol authors consider sorting their data whenever applicable. If there's a map in a protocol that contains user-defined keys: it's recommended that it be sorted.</p> <p>However, when it comes to how we specify the range of behaviors that IPLD must describe and support, these arguments about encouraging convergence are not deciding factor, for several reasons:</p> <ul> <li>the hard limits described in the previous section are still in force (and wishing those away doesn't work);</li> <li>parts of a protocol where uncoordinated convergence in the data is relevant are often a protocol-specific detail;</li> <li>since it is still possible to do such sorting by choice within a library and ecosystem that is order-preserving (but the reverse is not true!), we overall favor order-preservation as the central design (and encourage sorting be applied in parts of protocols where uncoordinated convergence is relevant).</li> </ul> <h2 id="impacts" tabindex="-1"><a class="header-anchor" href="#impacts">Impacts</a></h2> <h3 id="hash-effects" tabindex="-1"><a class="header-anchor" href="#hash-effects">hash effects</a></h3> <p>In general, we care about order-preservation of data because we care about not changing data. Changing the order of data results in different data -- and, in IPLD, that also results in a different hash. Therefore we care about ordering pretty much ubiquitously.</p> <h3 id="libraries" tabindex="-1"><a class="header-anchor" href="#libraries">libraries</a></h3> <p>There's a couple of specific places where map ordering is going to be directly noticeable in implementations:</p> <ul> <li>iterators</li> <li>traversals (which are usually built on the iterators)</li> <li>selectors (which are usually built on the traversal system)</li> <li>... and so on, in anything higher level that builds on those primitives.</li> </ul> <h3 id="high-level-features" tabindex="-1"><a class="header-anchor" href="#high-level-features">high level features</a></h3> <p>More generally, ordering is going to be noticeable in anything <em>downstream</em> of those core behaviors. Any higher-level features which build on those core behaviors, and expect any amount of determinism, have to be specified with an understanding of how IPLD ordering works. For example:</p> <ul> <li>CAR file generation</li> <li>block visit order in Graphsync</li> <li>...etc...!</li> </ul> <h3 id="synthesis" tabindex="-1"><a class="header-anchor" href="#synthesis">synthesis</a></h3> <p>These situations where ordering is noticeable can synthesize in interesting ways.</p> <p>For example, it's interesting to note that if combining Selectors with ADLs that with opinionated orders, and then using them in a system that require deterministic ordering of the things yielded by selection (or even, say, just a deterministic ordering on the the blocks loaded during the visit)... then that deterministic ordering is not going to be a simple sort, even if there was a consistent codec used on every block in the data and it always required a sort; <em>nor</em> will it be exactly an order-preservation of entered data: it will be a mixture, as the order specified is different in different parts of the graph (sometimes the codec order will dominate; sometimes the ADL internal order will dominate).</p> <p>(This particularly fun scenario is a real one, too -- things like this <em>are</em> seen in practical systems like Graphsync and CAR file generation, when operating on very common and popular structures of data like unixfsv1!)</p> <h3 id="codec-strictness" tabindex="-1"><a class="header-anchor" href="#codec-strictness">codec strictness</a></h3> <p>Some codecs do specify their own ordering opinions.</p> <p>How exactly this works out in practice tends to require reading the implementation library's documentation carefully. Even for codecs that specify a map ordering, it's common to see implementations in the wild which don't actually enforce it -- either because it's a performance cost, or because it required more implementation work, or because the author prioritized ability to read technically-malformed documents over strictness, etc. Implementations may also have different levels of strictness on the encode direction of data flow than on the decode direction.</p> <p>There's no magic wand to simplify this further, unfortunately.</p> <h2 id="recommendations-for-library-authors" tabindex="-1"><a class="header-anchor" href="#recommendations-for-library-authors">Recommendations for Library Authors</a></h2> <ul> <li> <p>An IPLD library typically has a <a href="../../libraries/nodes-and-kinds#node">"Node" interface</a>. This interface typically has some way to get an iterator for map contents. This iterator should be order-preserving in most node implementations.</p> <ul> <li>We recommend this because you'll get the best compositional bang-for-buck if you take this approach when writing your library. (If you use maps that don't maintain order, and try to rely on, say, <a href="/docs/codecs/known/dag-cbor/">DAG-CBOR</a>'s sorting: you'll get some things to work, but later have problems correctly handling Selectors on non-canonical data in that codec; or handling the round-tripping or application of Selectors on other non-sorted codecs (like JSON!) correctly at all. Don't put yourself in that situation.)</li> </ul> </li> <li> <p>An IPLD library's "Node" interface and implementations may wish to include some feature which let a node say what, if any, sorted order it contains. This can allow other features based on sorting to work quickly and efficiently (they can just check if the data <em>is</em> sorted, rather than needing to verify it). Simply remembering whether data is already sorted can be a significant saving (for example, consider the impact when an application uses the same sorted codec for saving and loading data).</p> </li> <li> <p>When implementing a codec that specifies an opinionated sorted order, the implementation should error when given data in a different order by default; sorting automatically should be a configurable option, but not be the default.</p> <ul> <li>Sorting can imply a performance penalty, so it's best to make the user aware of this.</li> <li>Sorting data, if this isn't expected, can <em>change</em> it, which may lead to significant systemic bugs in a content-addressing-based system.</li> <li>NOTE: This is a recommendation. That doesn't mean it's widely followed. You will certainly find libraries in the wild with codec implementations which will sort automatically and silently. Beware.</li> </ul> </li> </ul> </div> </main> </body> </html>