Repository: servo/servo
File: components/profile/time.rs (Rust)

Code before:
                        let trace = file_path.as_ref().and_then(|p| TraceDump::new(p).ok());
                        let mut profiler = Profiler::new(port, trace, Some(outputoption));
                        profiler.start();
                    })
                    .expect("Thread spawning failed");
                // decide if we need to spawn the timer thread
                match *option {
                    OutputOptions::FileName(_) => { /* no timer thread needed */ },
                    OutputOptions::Stdout(period) => {
                        // Spawn a timer thread
                        let chan = chan.clone();
                        thread::Builder::new()
                            .name("TimeProfTimer".to_owned())
                            .spawn(move || {
                                loop {
                                    thread::sleep(std::time::Duration::from_secs_f64(period));
                                    if chan.send(ProfilerMsg::Print).is_err() {
                                        break;
                                    }
                                }
                            })
                            .expect("Thread spawning failed");
                    },
                }
                ProfilerChan(Some(chan))
            },
            None => ProfilerChan(None),
        }
    }

    pub fn new(
        port: IpcReceiver<ProfilerMsg>,
        trace: Option<TraceDump>,
        output: Option<OutputOptions>,
    ) -> Profiler {
        Profiler {
            port,
            buckets: BTreeMap::new(),
            output,
            last_msg: None,
            trace,
            blocked_layout_queries: HashMap::new(),
        }
    }

    pub fn start(&mut self) {
        while let Ok(msg) = self.port.recv() {
            if !self.handle_msg(msg) {
                break;
            }
        }

Review comment (suggestion):
It's also legal to use `--profiler-trace-path path.html` without using `-p` so we can't just get rid of this code here.

Code after:
                        let trace = file_path.as_ref().and_then(|p| TraceDump::new(p).ok());
                        let mut profiler = Profiler::new(port, trace, Some(outputoption));
                        profiler.start();
                    })
                    .expect("Thread spawning failed");
                // decide if we need to spawn the timer thread
                match *option {
                    OutputOptions::FileName(_) => { /* no timer thread needed */ },
                    OutputOptions::Stdout(period) => {
                        // Spawn a timer thread
                        let chan = chan.clone();
                        thread::Builder::new()
                            .name("TimeProfTimer".to_owned())
                            .spawn(move || {
                                loop {
                                    thread::sleep(std::time::Duration::from_secs_f64(period));
                                    if chan.send(ProfilerMsg::Print).is_err() {
                                        break;
                                    }
                                }
                            })
                            .expect("Thread spawning failed");
                    },
                }

                ProfilerChan(Some(chan))
            },
            None => {
                match file_path {
                    Some(path) => {
                        let (chan, port) = ipc::channel().unwrap();
                        // Spawn the time profiler
                        thread::Builder::new()
                            .name("TimeProfiler".to_owned())
                            .spawn(move || {
                                let trace = TraceDump::new(path).ok();
                                let mut profiler = Profiler::new(port, trace, None);
                                profiler.start();
                            })
                            .expect("Thread spawning failed");

                        ProfilerChan(Some(chan))
                    },
                    None => ProfilerChan(None),
                }
            },
        }
    }

    pub fn new(
        port: IpcReceiver<ProfilerMsg>,