Skip to content

Commit

Permalink
Make serialization format stable
Browse files Browse the repository at this point in the history
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
  • Loading branch information
cschwan committed Dec 8, 2023
1 parent e006c95 commit 1943937
Showing 1 changed file with 15 additions and 2 deletions.
17 changes: 15 additions & 2 deletions pineappl/src/grid.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -291,9 +291,22 @@ struct Mmv2 {
key_value_db: HashMap<String, String>,
}

fn ordered_map_serialize<S, K: Ord + Serialize, V: Serialize>(
value: &HashMap<K, V>,
serializer: S,
) -> Result<S::Ok, S::Error>
where
S: Serializer,
{
let ordered: BTreeMap<_, _> = value.iter().collect();
ordered.serialize(serializer)
}

#[derive(Clone, Deserialize, Serialize)]
struct Mmv3 {
remapper: Option<BinRemapper>,
// order the HashMap before serializing it to make the output stable
#[serde(serialize_with = "ordered_map_serialize")]
key_value_db: HashMap<String, String>,
subgrid_template: SubgridEnum,
}
Expand Down

0 comments on commit 1943937

Please sign in to comment.