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

Allow larger preallocated capacity for smaller elements #2495

Merged
merged 1 commit into from Jul 9, 2023

Conversation

dtolnay
Copy link
Member

@dtolnay dtolnay commented Jul 9, 2023

Closes #2494.

@dtolnay dtolnay merged commit a622b8a into serde-rs:master Jul 9, 2023
20 checks passed
@dtolnay dtolnay deleted the cautious branch July 9, 2023 00:51
pub fn cautious<Element>(hint: Option<usize>) -> usize {
const MAX_PREALLOC_BYTES: usize = 1024 * 1024;

if mem::size_of::<Element>() == 0 {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It would be good to cache the result of mem::size_of::<Element>() and reuse it at line 19 below

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is the const function since 1.24 so it will be computed by the compiler

jonasbb added a commit to jonasbb/serde_with that referenced this pull request Jul 31, 2023
Upstream serde has a new implementation of the cautious size hint
function, which takes into consideration the size of the elements.
Use the same implementation in serde_with.

serde-rs/serde#2495
bors bot added a commit to jonasbb/serde_with that referenced this pull request Jul 31, 2023
629: Sync the size_hint changes from serde r=jonasbb a=jonasbb

Upstream serde has a new implementation of the cautious size hint
function, which takes into consideration the size of the elements.
Use the same implementation in serde_with.

serde-rs/serde#2495

bors r+

Co-authored-by: Jonas Bushart <jonas@bushart.org>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

How can serde impls take better advantage of length hints?
3 participants