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

Inneficient code generation, unless splitting part of the code into #[inline(always)] sub-function #55458

Open
newpavlov opened this Issue Oct 29, 2018 · 1 comment

Comments

Projects
None yet
2 participants
@newpavlov
Contributor

newpavlov commented Oct 29, 2018

(from #43702)

Both examples are completely equivalent after inlining, but the second one gets compiled into a more efficient assembly:

pub fn square(mut buf: &mut [u8], self_buf: &[u8; 16*8]) {
  while buf.len() >= self_buf.len() {
      let (l, r) = {buf}.split_at_mut(self_buf.len());
      buf = r;
      for i in 0..16*8 { l[i] ^= self_buf[i]; }
  }
}

https://rust.godbolt.org/z/mXrwhv

#[inline(always)]
fn xor(l: &mut [u8], self_buf: &[u8; 16*8]) {
  for i in 0..16*8 { l[i] ^= self_buf[i]; }
}

pub fn square(mut buf: &mut [u8], self_buf: &[u8; 16*8]) {
  while buf.len() >= self_buf.len() {
      let (l, r) = {buf}.split_at_mut(self_buf.len());
      buf = r;
      xor(l, self_buf);
  }
}

https://rust.godbolt.org/z/Uk3X3Z

@newpavlov

This comment has been minimized.

Contributor

newpavlov commented Oct 29, 2018

BTW using zip breaks auto-vectorization:

pub fn square(mut buf: &mut [u8], self_buf: &[u8; 128]) {
  while buf.len() >= self_buf.len() {
      let (l, r) = {buf}.split_at_mut(self_buf.len());
      buf = r;
      for (a, b) in l.iter_mut().zip(self_buf.iter()) {
        *a ^= *b;
      }
  }
}

https://rust.godbolt.org/z/SLIi9D

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment