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 unneeded clone #94

Closed

Conversation

vincentfretin
Copy link
Contributor

Small changes after understanding how Rust works :)
I'm using these changes in production.

@@ -267,9 +266,10 @@ extern "C" fn init(callbacks: *mut PluginCallbacks, config_path: *const c_char)
message_threads
};

let mut senders = Vec::with_capacity(num_threads);
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Creating Vec with Vec::with_capacity is a best practice when you know the size in advance.
From memory allocation point of view, I read there is no allocation when using Vec::new, there is an allocation for 4 items on the first push, then a new allocation with capacity x2 when the capacity is reached and so on.
Here nothing changes fundamentally, this code is executed when the plugin initialize, we may save an extra allocation if you have more than 4 cpus. :)

for i in 0..num_threads {
let (messages_tx, messages_rx) = mpsc::sync_channel(0);
senders.push(messages_tx.clone());
senders.push(messages_tx);
Copy link
Contributor Author

Choose a reason for hiding this comment

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

messages_tx is added to senders Vec, then the Vec is set on the static variable.
There is no need to clone messages_tx, this is not moved to the created thread. Only messages_rx is moved to the created thread.

@@ -479,7 +479,7 @@ fn process_join(from: &Arc<Session>, room_id: RoomId, user_id: UserId, subscribe
switchboard.join_publisher(Arc::clone(from), user_id.clone(), room_id.clone());
notify_except(&notification, &user_id, switchboard.publishers_occupying(&room_id));
} else {
switchboard.join_subscriber(Arc::clone(from), user_id.clone(), room_id.clone());
switchboard.join_subscriber(Arc::clone(from), user_id, room_id);
Copy link
Contributor Author

Choose a reason for hiding this comment

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

If found this one with cargo clippy.
user_id and room_id are owned String from the MessageKind::Join message (serde DeserializeOwned) that are already moved to process_join. Those two String are not used after this, so no need to clone, join_subscriber can own them.

Note that in process_block, the whom String is not cloned, the String is moved to switchboard.establish_block without issue.

@vincentfretin
Copy link
Contributor Author

@mqp did you see this PR and the other one?

@vincentfretin
Copy link
Contributor Author

I merged this in master in networked-aframe fork https://github.com/networked-aframe/janus-plugin-sfu

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

Successfully merging this pull request may close these issues.

1 participant