From dd96d3b9d4d6a6999d3b198659c57991351605ac Mon Sep 17 00:00:00 2001 From: Ran Benita Date: Sat, 25 Mar 2017 18:01:38 +0300 Subject: [PATCH] Speed up apply_mask() This function is the most speed-critical in the library. In profiles, this optimization reduces it from ~75% of the profile to ~55%. I have tried several approaches, but didn't manage to improve on this one (LLVM already unrolls the loop here). Though I'm sure it is possible. --- src/protocol/frame/frame.rs | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/src/protocol/frame/frame.rs b/src/protocol/frame/frame.rs index 3719e25..de9ef59 100644 --- a/src/protocol/frame/frame.rs +++ b/src/protocol/frame/frame.rs @@ -14,9 +14,8 @@ use error::{Error, Result}; use super::coding::{OpCode, Control, Data, CloseCode}; fn apply_mask(buf: &mut [u8], mask: &[u8; 4]) { - let iter = buf.iter_mut().zip(mask.iter().cycle()); - for (byte, &key) in iter { - *byte ^= key + for (i, byte) in buf.iter_mut().enumerate() { + *byte ^= mask[i & 3]; } }