Skip to content

Complex iterator chain is very slow to compile #31953

Closed
@Nercury

Description

@Nercury

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions