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

prealloc size of Lower::lower_into_rust_buffer #2420

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion uniffi_core/src/ffi_converter_traits.rs
Original file line number Diff line number Diff line change
@@ -245,7 +245,7 @@ pub unsafe trait Lower<UT>: Sized {

/// Convenience method
fn lower_into_rust_buffer(obj: Self) -> RustBuffer {
let mut buf = ::std::vec::Vec::new();
let mut buf = ::std::vec::Vec::with_capacity(size_of::<Self>());
Copy link
Contributor

@bendk bendk Jan 29, 2025

Choose a reason for hiding this comment

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

This would be a great optimization, but is size_of<Self> the right value? Our serialization code and Rust's type layout are very different. Still, maybe size_of<Self> is a good estimate -- I'm not really sure.

Can you do some tests? Like create a set of Records, Enums, strings, vecs and hashmaps, some with few fields/variants/items, some with lots of them, then compare size_of::<Self> against lower_into_rust_buffer(self).len(). I'd love to see the results of that.

Self::write(obj, &mut buf);
RustBuffer::from_vec(buf)
}