Serializing and deserializing to or from msg pack more efficient

Hello, I would like to ask you if there is a way to serialize this hash map to and from a file in a better way? I would need to make it so that it can work with the example I have at the end, but so that it is fast and that it takes up less space in the file than it does now

use {
ahash::AHashMap,
serde::{Deserialize, Serialize},
};
fn main() {
let mut map: AHashMap<&str, Value> = AHashMap::new();
map.insert("hello", Value::Bool(true));
map.insert("john", Value::Vector(b"doe".to_vec()));
let vec = rmp_serde::to_vec(&map).unwrap();
let _map2: AHashMap<String, Value> = rmp_serde::from_slice(&vec).unwrap();
std::fs::write("map.txt", vec).unwrap();
}

#[derive(Deserialize, Serialize)]
pub enum Value {
Int(i32),
Float(f64),
Vector(Vec<u8>),
Bool(bool),
}

(Playground)

I mean MessagePack claims to be "compact and fast", so I guess that's pretty good?
In any case, you don't need to bother unless it impacts your performance, which you would either realize form benchmarking or users yelling.

Premature optimization is the root of all evil

1 Like

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.