json icon indicating copy to clipboard operation
json copied to clipboard

Make json macro pre-allocate capacity for maps

Open dtolnay opened this issue 4 years ago • 0 comments

Currently an invocation like json!({ "x": 0, "y": 'y' as i8, "z": [] }) expands into:

::serde_json::Value::Object({
    let mut object = ::serde_json::Map::new();
    let _ = object.insert(...);
    let _ = object.insert(...);
    let _ = object.insert(...);
    object
})

It would be better if it were able to allocate capacity up front, such as by:

-   let mut object = ::serde_json::Map::new();
+   let mut object = ::serde_json::Map::with_capacity(1 + 1 + 1);

dtolnay avatar Oct 09 '21 17:10 dtolnay