Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Vectors are deserialized with spare capacity #241

Closed
cswinter opened this issue Jul 28, 2018 · 4 comments
Closed

Vectors are deserialized with spare capacity #241

cswinter opened this issue Jul 28, 2018 · 4 comments

Comments

@cswinter
Copy link

@cswinter cswinter commented Jul 28, 2018

Thanks for a great library!

Serializing/deserializing a Vec<u8> with len > 4096 will give a Vec with capacity equal to a power of two. This is undesirably for use cases where the deserialized data is not modified. Is there a way to deserialize a Vec without allocating spare capacity?

Reproduce with https://github.com/cswinter/bincode_overalloc:

extern crate bincode;

use bincode::{serialize, deserialize};

fn main() {
    for i in 0..(1<<30) {
        let vec = vec![0u8; i];
        assert_eq!(vec.len(), vec.capacity());
        let serialized = serialize(&vec).unwrap();
        let deserialized: Vec<u8> = deserialize(&serialized).unwrap();
        if deserialized.len() != deserialized.capacity() {
            println!("{} {}", deserialized.len(), deserialized.capacity());
        }
    }
}
@ZoeyR
Copy link
Collaborator

@ZoeyR ZoeyR commented Jul 28, 2018

You could make deserialized be a &[u8] instead of a Vec<u8> if you aren't going to modify the data.

@cswinter
Copy link
Author

@cswinter cswinter commented Jul 28, 2018

I see. In this case, I want to return the deserialized value and keep it alive permanently/until eviction. I can't think of a good way of achieving that without having ownership of the value.

@JoshMcguigan
Copy link
Contributor

@JoshMcguigan JoshMcguigan commented May 2, 2019

This functionality actually lives in the root serde crate, not in bincode. It looks like they purposefully only pre-allocate up to len 4096, and after that rely on the standard allocation behavior of the vec type, which explains the behavior you are seeing.

https://github.com/serde-rs/serde/blob/97920be33aee4019bf7bd86bc3cf957a4de91413/serde/src/de/impls.rs#L862
https://github.com/serde-rs/serde/blob/97920be33aee4019bf7bd86bc3cf957a4de91413/serde/src/private/de.rs#L198

I found this issue on the serde repo discussing this behavior, with a possible work around.

@ZoeyR
Copy link
Collaborator

@ZoeyR ZoeyR commented Apr 16, 2020

Closing since this is behavior from serde itself.

@ZoeyR ZoeyR closed this Apr 16, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
3 participants
You can’t perform that action at this time.