Skip to content

Make json macro pre-allocate capacity for maps #810

@dtolnay

Description

@dtolnay

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);

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions