What is the encoding order of Map and Set elements? #253
Replies: 2 comments 1 reply
-
The stability of the container encoding problemTo be precise, object codec to container encoding, are converted to an array after encoding, so after conversion to an array involves the issue of element order BTreeMap/BTreeSet itself is traversed in order, sorting depends on the corresponding Ord trait design, custom Object can design their own Ord trait impls, as long as the algorithm to ensure compliance with the container Ord The elements inside the HashMap/HashSet do not need to support Ord trait, but for the need of stable coding, the default implementation of RawCodec restricts the need to support Ord this trait: CYFS/src/component/cyfs-base/src/codec/raw/raw_types.rs Lines 784 to 792 in c7a777d So the internal sorting based on the elements Ord, now it seems a little strange, if the user's HashMap element type does not support Ord trait, then it will not be able to use the default sort and RawCodec impls, and the user may not need a stable sort itself, and there is a performance waste here itself HashSet this one should be missed, according to the principle of consistency, should be consistent with the HashMap Design principlesI think this we should have the following design principles
Introduce the labelTherefore, there should be a label "stable encoding or not" to identify the default data type supported by cyfs-base, so that you can choose the stable or non-stable version of the data structure according to your needs when designing the data structure for object selection. |
Beta Was this translation helpful? Give feedback.
-
I think we should only define a definite arrangement order, we just need to ensure that the encoded content of the same content is exactly the same. For the user, each element is stored in the container after decoding, and how the elements are arranged in it is not important. |
Beta Was this translation helpful? Give feedback.
-
I am reviewing the encoding format for types. But I find that our encoding process only explicitly sorts the HashMap. Other types(as HashSet/BTreeMap/BTreeSet) depends on the iter in
Rust
. I have read the documents for those types, The iters for BTreeMap and BTreeSet return elements in ascending order. but I have not found how it is sorted in HashSet.I am reading the code in raw_types.rs.
I think we have to explicitly define the encoding order of the fields/elements of the composite type, otherwise different results may be encoded.
Beta Was this translation helpful? Give feedback.
All reactions