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

Terrible perfomance of the iterator str::bytes().zip(str::bytes()) #44424

Closed
IBUzPE9 opened this issue Sep 8, 2017 · 5 comments · Fixed by #44790
Closed

Terrible perfomance of the iterator str::bytes().zip(str::bytes()) #44424

IBUzPE9 opened this issue Sep 8, 2017 · 5 comments · Fixed by #44790
Labels
A-collections Area: `std::collection` C-enhancement Category: An issue proposing an enhancement or a PR with one. I-slow Issue: Problems and improvements with respect to performance of generated code. T-libs-api Relevant to the library API team, which will review and decide on the PR/issue.

Comments

@IBUzPE9
Copy link

IBUzPE9 commented Sep 8, 2017

I compared the performance of three equivalent functions:

pub fn chars_zip(x:&str, y:&str) -> usize {
  x.chars()
    .zip(y.chars())
    .filter(|&(xc, yc)| xc != yc)
    .count()
}

pub fn bytes_zip(x:&str, y:&str) -> usize {
  x.bytes()
    .zip(y.bytes())
    .filter(|&(xc, yc)| xc != yc)
    .count()
}

pub fn slice_zip(x:&str, y:&str) -> usize {
    let x = x.as_bytes();
    let y = y.as_bytes();
    x.iter()
        .zip(y)
        .filter(|&(xc, yc)| xc != yc)
        .count()
}

This is results (release mode):

chars_zip       0.1450s
bytes_zip       0.2599s
slice_zip       0.0643s

Playground

@IBUzPE9 IBUzPE9 changed the title Terrible perfomance of the interator str::bytes().zip(str::bytes()) Terrible perfomance of the iterator str::bytes().zip(str::bytes()) Sep 8, 2017
@sfackler
Copy link
Member

sfackler commented Sep 8, 2017

IIRC zip is specialized for slices which is why the last case is so much faster.

@leonardo-m
Copy link

Try also to compile with -C opt-level=3 -C target-cpu=native

@leonardo-m
Copy link

A question could be: is it possible for bytes() to return something similar to as_bytes().iter() ?

@IBUzPE9
Copy link
Author

IBUzPE9 commented Sep 8, 2017

@leonardo-m

Try also to compile with -C opt-level=3 -C target-cpu=native

It makes the slice_zip() two times faster, the speed of the bytes_zip() and the chars_zip() has stayed the same as before.

@alexcrichton alexcrichton added C-enhancement Category: An issue proposing an enhancement or a PR with one. T-libs-api Relevant to the library API team, which will review and decide on the PR/issue. labels Sep 8, 2017
@bluss
Copy link
Member

bluss commented Sep 9, 2017

You've discovered the "untweaked speed" of zip ☹️, which is what str::bytes has.

str::bytes can implement TrustedRandomAccess the same way slice iterators do, in fact str::bytes is just a newtype around an iterator that already implements that trait.

@Mark-Simulacrum Mark-Simulacrum added the I-slow Issue: Problems and improvements with respect to performance of generated code. label Sep 10, 2017
@TimNN TimNN added the A-collections Area: `std::collection` label Sep 17, 2017
@bors bors closed this as completed in f22b9da Sep 28, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-collections Area: `std::collection` C-enhancement Category: An issue proposing an enhancement or a PR with one. I-slow Issue: Problems and improvements with respect to performance of generated code. T-libs-api Relevant to the library API team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

7 participants