From c497967b1cbbe3af0823a0857bca316c7c263f9c Mon Sep 17 00:00:00 2001 From: Emmanuel Gil Peyrot Date: Mon, 10 Feb 2025 15:30:08 +0100 Subject: [PATCH] xmpp-parsers: Reduce component handshake allocations MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This function always appends to an existing string, so by passing it a String directly we can avoid superfluous allocations. Also tokio-xmpp was doing a bunch of &str to String for no reason around there, let’s remove that too. --- parsers/ChangeLog | 4 ++++ parsers/src/component.rs | 8 +++++--- tokio-xmpp/src/component/login.rs | 11 +++++------ tokio-xmpp/src/component/mod.rs | 1 - 4 files changed, 14 insertions(+), 10 deletions(-) diff --git a/parsers/ChangeLog b/parsers/ChangeLog index bc5e5e2d3be2ebfbef54c11d665642ecb8889d0d..16ab61c7032acb15c41b1ba1b1160fb84d187ff2 100644 --- a/parsers/ChangeLog +++ b/parsers/ChangeLog @@ -54,6 +54,10 @@ XXXX-YY-ZZ RELEASER - pubsub::Event is now the wrapper for the pubsub::event::Payload enum, and the PublishedItems and RetractedItems have been merged into the Items sub-struct. These replace the previous PubSubEvent enum (!531) + - Handshake::from_password_and_stream_id() became + Handshake::from_stream_id_and_password(), with the two parameters + having been exchanged, and the stream_id is now a String instead of + &str. * New parsers/serialisers: - Stream Features (RFC 6120) (!400) - Spam Reporting (XEP-0377) (!506) diff --git a/parsers/src/component.rs b/parsers/src/component.rs index 5e344b628e273246117669c73ffa8a09fef65c26..f0da0a27c00034b7fc558f062d4b9a7ed70e7f93 100644 --- a/parsers/src/component.rs +++ b/parsers/src/component.rs @@ -31,8 +31,8 @@ impl Handshake { } /// Creates an authentication request from the component. - pub fn from_password_and_stream_id(password: &str, stream_id: &str) -> Handshake { - let input = String::from(stream_id) + password; + pub fn from_stream_id_and_password(stream_id: String, password: &str) -> Handshake { + let input = stream_id + password; let hash = Sha1::digest(input.as_bytes()); Handshake { data: Some(hash.into()), @@ -76,7 +76,9 @@ mod tests { let handshake = Handshake::new(); assert_eq!(handshake.data, None); - let handshake = Handshake::from_password_and_stream_id("123456", "sid"); + let stream_id = String::from("sid"); + let password = "123456"; + let handshake = Handshake::from_stream_id_and_password(stream_id, password); assert_eq!( handshake.data, Some([ diff --git a/tokio-xmpp/src/component/login.rs b/tokio-xmpp/src/component/login.rs index be8b3b4d35646de5cc3d1f8060ea1ecbf80ee667..08b93334ee8a15ee2d67893162cdadd2d8752bc6 100644 --- a/tokio-xmpp/src/component/login.rs +++ b/tokio-xmpp/src/component/login.rs @@ -12,15 +12,14 @@ use crate::xmlstream::{ReadError, Timeouts, XmppStream, XmppStreamElement}; pub async fn component_login( connector: C, jid: Jid, - password: String, + password: &str, timeouts: Timeouts, ) -> Result, Error> { - let password = password; let (mut stream, _) = connector.connect(&jid, ns::COMPONENT, timeouts).await?; let header = stream.take_header(); let mut stream = stream.skip_features(); let stream_id = match header.id { - Some(ref v) => &**v, + Some(id) => id.into_owned(), None => { return Err(io::Error::new( io::ErrorKind::InvalidData, @@ -29,16 +28,16 @@ pub async fn component_login( .into()) } }; - auth(&mut stream, stream_id, &password).await?; + auth(&mut stream, stream_id, password).await?; Ok(stream) } pub async fn auth( stream: &mut XmppStream, - stream_id: &str, + stream_id: String, password: &str, ) -> Result<(), Error> { - let nonza = Handshake::from_password_and_stream_id(password, stream_id); + let nonza = Handshake::from_stream_id_and_password(stream_id, password); stream .send(&XmppStreamElement::ComponentHandshake(nonza)) .await?; diff --git a/tokio-xmpp/src/component/mod.rs b/tokio-xmpp/src/component/mod.rs index d041fe4684e7f6a24825b36ae40d1f28c4201d87..27bb232e4e81300780680ad38a5ccf7fe2b8baab 100644 --- a/tokio-xmpp/src/component/mod.rs +++ b/tokio-xmpp/src/component/mod.rs @@ -87,7 +87,6 @@ impl Component { timeouts: Timeouts, ) -> Result { let jid = Jid::from_str(jid)?; - let password = password.to_owned(); let stream = component_login(connector, jid.clone(), password, timeouts).await?; Ok(Component { jid, stream }) }