Skip to content

Complex iterator chain is very slow to compile #31953

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

Closed
Nercury opened this issue Feb 28, 2016 · 2 comments
Closed

Complex iterator chain is very slow to compile #31953

Nercury opened this issue Feb 28, 2016 · 2 comments

Comments

@Nercury
Copy link
Contributor

Nercury commented Feb 28, 2016

I thought this was a cool way to implement collection of values from a builder into an array, and stumbled upon this case:

use std::iter;

pub struct ConfigFilterRef {
    alpha_mask_size: Option<[i32; 2]>,
    alpha_size: Option<[i32; 2]>,
    bind_to_texture_rgb: Option<[i32; 2]>,
    bind_to_texture_rgba: Option<[i32; 2]>,
    blue_size: Option<[i32; 2]>,
    buffer_size: Option<[i32; 2]>,
    color_buffer_type: Option<[i32; 2]>,
    config_caveat: Option<[i32; 2]>,
    config_id: Option<[i32; 2]>,
    conformant: Option<[i32; 2]>,
}

impl ConfigFilterRef {
    pub fn new() -> ConfigFilterRef {
        ConfigFilterRef {
            alpha_mask_size: None,
            alpha_size: None,
            bind_to_texture_rgb: None,
            bind_to_texture_rgba: None,
            blue_size: None,
            buffer_size: None,
            color_buffer_type: None,
            config_caveat: None,
            config_id: None,
            conformant: None,
        }
    }

    pub fn attrs(self) -> Vec<i32> {
        iter::empty()
            // slow part start
            .chain(self.alpha_mask_size.iter().flat_map(|i| i))
            .chain(self.alpha_size.iter().flat_map(|i| i))
            .chain(self.bind_to_texture_rgb.iter().flat_map(|i| i))
            .chain(self.bind_to_texture_rgba.iter().flat_map(|i| i))
            .chain(self.blue_size.iter().flat_map(|i| i))
            .chain(self.buffer_size.iter().flat_map(|i| i))
            .chain(self.color_buffer_type.iter().flat_map(|i| i))
            .chain(self.config_caveat.iter().flat_map(|i| i))
            .chain(self.config_id.iter().flat_map(|i| i))
            .chain(self.conformant.iter().flat_map(|i| i))
            // slow part end
            .chain(&[0])
            .cloned()
            .collect()
    }
}

fn main() {
    ConfigFilterRef::new().attrs();
}

On stable:

   Compiling test v0.1.0 (file:///Users/nercury/webs/test)
cargo build  40.86s user 0.74s system 99% cpu 41.930 total

On nightly:

   Compiling test v0.1.0 (file:///Users/nercury/webs/test)
cargo build  46.43s user 0.73s system 99% cpu 47.174 total

On nightly-2016-02-28:

   Compiling test v0.1.0 (file:///Users/nercury/webs/test)
cargo build  44.31s user 0.77s system 99% cpu 45.082 total

Without "slow part":

   Compiling test v0.1.0 (file:///Users/nercury/webs/test)
cargo build  0.63s user 0.33s system 99% cpu 0.959 total

Feel free to close if this is a duplicate/won't fix.

@Nercury Nercury changed the title Complex iterator chain is very slow Complex iterator chain is very slow to compile Feb 28, 2016
@Nercury
Copy link
Contributor Author

Nercury commented Feb 28, 2016

For the record, reducing type hierarchy helps, i.e.:

    pub fn attrs(self) -> Vec<i32> {
        [
            self.alpha_mask_size,
            self.alpha_size,
            self.bind_to_texture_rgb,
            self.bind_to_texture_rgba,
            self.blue_size,
            self.buffer_size,
            self.color_buffer_type,
            self.config_caveat,
            self.config_id,
            self.conformant,
        ]
            .into_iter()
            .flat_map(|i| i)
            .flat_map(|i| i)
            .chain(&[0])
            .cloned()
            .collect()
    }
   Compiling test v0.1.0 (file:///Users/nercury/webs/test)
cargo build  0.61s user 0.33s system 99% cpu 0.947 total

@bluss
Copy link
Member

bluss commented Feb 29, 2016

This is a bad bug, and there is already an issue open for it, see #22204.

@bluss bluss closed this as completed Feb 29, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants