From 1943937918727555b36009f8021325a845255972 Mon Sep 17 00:00:00 2001 From: Christopher Schwan Date: Fri, 8 Dec 2023 10:36:09 +0100 Subject: [PATCH] Make serialization format stable Metadata is saved in a `HashMap`, in which the key-value pairs are stored, but their ordering is random. We now sort the pairs using their key name first and then serialize the sorted pairs, removing the randomness --- pineappl/src/grid.rs | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-) diff --git a/pineappl/src/grid.rs b/pineappl/src/grid.rs index 259a7167..2d7db108 100644 --- a/pineappl/src/grid.rs +++ b/pineappl/src/grid.rs @@ -19,10 +19,10 @@ use indicatif::{ProgressBar, ProgressStyle}; use itertools::Itertools; use lz4_flex::frame::{FrameDecoder, FrameEncoder}; use ndarray::{s, Array3, Array5, ArrayView5, Axis, Dimension}; -use serde::{Deserialize, Serialize}; +use serde::{Deserialize, Serialize, Serializer}; use std::borrow::Cow; use std::cmp::Ordering; -use std::collections::HashMap; +use std::collections::{BTreeMap, HashMap}; use std::convert::{TryFrom, TryInto}; use std::io::{self, BufRead, BufReader, BufWriter, Read, Write}; use std::iter; @@ -291,9 +291,22 @@ struct Mmv2 { key_value_db: HashMap, } +fn ordered_map_serialize( + value: &HashMap, + serializer: S, +) -> Result +where + S: Serializer, +{ + let ordered: BTreeMap<_, _> = value.iter().collect(); + ordered.serialize(serializer) +} + #[derive(Clone, Deserialize, Serialize)] struct Mmv3 { remapper: Option, + // order the HashMap before serializing it to make the output stable + #[serde(serialize_with = "ordered_map_serialize")] key_value_db: HashMap, subgrid_template: SubgridEnum, }