gltf icon indicating copy to clipboard operation
gltf copied to clipboard

Fix Primitive::attributes serialization determinism issue

Open kirdaybov opened this issue 3 years ago • 1 comments

HashMap uses a hash function that behaves differently based on the moment of the initialization of the program. That leads to the different iteration orders during serialization. Replacing HashMap with BTreeMap fixes the issue

kirdaybov avatar Mar 03 '22 19:03 kirdaybov

#336

kirdaybov avatar Mar 03 '22 19:03 kirdaybov

Hi, thanks for your PR. I have the exact same problem but I'm concerned replacing HashMap with BTreeMap might affect performance. Perhaps we could implement this as a feature option instead, something along the lines of deterministic_serialization.

alteous avatar Apr 20 '23 09:04 alteous

I'm concerned replacing HashMap with BTreeMap might affect performance.

The notable cost of a BTreeMap vs. HashMap is that it requires more pointer indirection to follow the tree nodes. But the value of the “B” constant actually used in Rust's BTreeMap is 6, meaning that the tree contains only one node unless there are more than 2*B - 1 = 11 entries (attributes) stored in it — which is the same amount of indirection a HashMap's storage needs too. So, this is unlikely to be a problem except for (1) very complex attribute sets (2) which are repeatedly accessed directly from the gltf data structure instead of iterated once and copied to the application's own data structure.

kpreid avatar Apr 20 '23 19:04 kpreid

@kpreid, thanks for your input, the pointer indirection was indeed my concern. I assumed the B in BTreeMap stood for binary without looking any further into it. I stand corrected. 😄

It's unlikely the number of vertex attributes would exceed 11 so I see no issues merging as-is.

@kirdaybov, I apologise if my misunderstanding caused any frustration.

alteous avatar Apr 21 '23 09:04 alteous

No hard feelings ;)

kirdaybov avatar Apr 21 '23 11:04 kirdaybov