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

Remove vec! allocations #1

Closed
jabuwu opened this issue Dec 10, 2023 · 1 comment
Closed

Remove vec! allocations #1

jabuwu opened this issue Dec 10, 2023 · 1 comment

Comments

@jabuwu
Copy link
Owner

jabuwu commented Dec 10, 2023

For better integration with Rust, the C code was modified to allocate some vectors (search for the vec! macro in lib.rs). There's probably a better way to do this, avoiding the allocations.

@jabuwu
Copy link
Owner Author

jabuwu commented May 18, 2024

Fixed in 033fb1f

Allocations were removed using some slice trickery. Given that we know the max size, we can allocate a slice array on the stack instead of using a vector.

rusty_enet/src/c/protocol.rs

Lines 2213 to 2224 in 033fb1f

let mut in_buffers: [&[u8]; ENET_BUFFER_MAXIMUM as usize] =
std::array::from_fn(|_| {
from_raw_parts_or_empty::<u8>(std::ptr::null(), 0)
});
#[allow(clippy::needless_range_loop)]
for i in 0..((*host).buffer_count).wrapping_sub(1) {
let buffer = ((*host).buffers).as_mut_ptr().add(1 + i);
in_buffers[i] = super::from_raw_parts_or_empty(
(*buffer).data,
(*buffer).data_length,
);
}

The send and peer allocations still exist. The peer one exists in ENet, so that's fine. It was changed to at least reserve the peer count ahead of time before pushing the peers.

The send allocation is worth keeping since most socket implementations will likely want to do this, anyway. It was changed to reserve the buffer count in 6fe949c. It could avoid this allocation if compression is disabled, but one allocation during send seems pretty harmless.

@jabuwu jabuwu closed this as completed May 18, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant