From bcbbe84373e285842585fd945b6464c365d1c014 Mon Sep 17 00:00:00 2001 From: mennovf Date: Wed, 28 Aug 2024 13:49:23 +0200 Subject: [PATCH] perf: Remove unnecessary allocation and construction of a HashMap in Signature::parse --- src/resources/webhook_events.rs | 32 +++++++++++++++++--------------- 1 file changed, 17 insertions(+), 15 deletions(-) diff --git a/src/resources/webhook_events.rs b/src/resources/webhook_events.rs index 2397858f4..738791542 100644 --- a/src/resources/webhook_events.rs +++ b/src/resources/webhook_events.rs @@ -565,21 +565,23 @@ struct Signature<'r> { #[cfg(feature = "webhook-events")] impl<'r> Signature<'r> { fn parse(raw: &'r str) -> Result, WebhookError> { - let headers: HashMap<&str, &str> = raw - .split(',') - .map(|header| { - let mut key_and_value = header.split('='); - let key = key_and_value.next(); - let value = key_and_value.next(); - (key, value) - }) - .filter_map(|(key, value)| match (key, value) { - (Some(key), Some(value)) => Some((key, value)), - _ => None, - }) - .collect(); - let t = headers.get("t").ok_or(WebhookError::BadSignature)?; - let v1 = headers.get("v1").ok_or(WebhookError::BadSignature)?; + let mut t = None; + let mut v1 = None; + for header in raw.split(',') { + let Some((key, value)) = header.split_once('=') else { + continue; + }; + + match key { + "t" => t = Some(value), + "v1" => v1 = Some(value), + _ => (), + } + } + let (Some(t), Some(v1)) = (t, v1) else { + return Err(WebhookError::BadSignature); + }; + Ok(Signature { t: t.parse::().map_err(WebhookError::BadHeader)?, v1 }) } }