Repository: alacritty/alacritty
File: alacritty/src/display/mod.rs (Rust)

Code before:
#[derive(Default)]
pub struct FrameTimerInfo {
    /// The optimal delay for the renderer.
    swap_delay: Option<Duration>,

    /// The time stamp of the previous contiguos frame.
    frame_timestamp: Option<Instant>,

    /// The refresh rate we've used to compute the `swap_delay`.
    refresh_interval: Duration,
}

impl FrameTimerInfo {
    /// Compute the delay that we should use to achieve the target frame
    /// rate.
    pub fn compute_delay(&mut self, refresh_interval: Duration) -> Duration {
        // If the refresh rate has changed, reset the timings.
        if self.refresh_interval != refresh_interval || self.swap_delay.is_none() {
            self.refresh_interval = refresh_interval;
            self.frame_timestamp = Some(Instant::now());
            self.swap_delay = Some(refresh_interval);
            return refresh_interval;
        }

        // At this point we know that `swap_delay` has some value.
        let swap_delay = self.swap_delay.unwrap();

        let new_swap_delay = match self.frame_timestamp {
            Some(last_frame_timestamp) => {
                // Compute the error.
                let error = self.refresh_interval.as_micros() as i64
                    - last_frame_timestamp.elapsed().as_micros() as i64;
                let new_swap_delay = swap_delay.as_micros() as i64 + error;
                Duration::from_micros(u64::try_from(new_swap_delay).unwrap_or_default())
            },
            // If we don't have any information about the last frame, just return
            // previous target.
            None => swap_delay,
        };

        self.swap_delay = Some(new_swap_delay);
        self.frame_timestamp = Some(Instant::now());

        new_swap_delay
    }
}

/// Calculate the cell dimensions based on font metrics.
///
/// This will return a tuple of the cell width and height.
#[inline]

Review comment (performance):
Why not use this:

```rust
let swap_delay = match self.sway_delay.filter(|_| self.refresh_interval == refresh_interval) {
    Some(swap_delay) => swap_delay,
    None => {
        // Reset stuff
    },
};
```

Maybe a little worse for performance, but should basically be irrelevant.

Code after:
    }
}

impl<T> DerefMut for Replaceable<T> {
    fn deref_mut(&mut self) -> &mut Self::Target {
        self.get_mut()
    }
}

/// The frame timer state.
pub struct FrameTimer {
    /// Base timestamp used to compute sync points.
    base: Instant,

    /// The last timestamp we synced to.
    last_synced_timestamp: Instant,

    /// The refresh rate we've used to compute sync timestamps.
    refresh_interval: Duration,
}

impl FrameTimer {
    pub fn new() -> Self {
        let now = Instant::now();
        Self { base: now, last_synced_timestamp: now, refresh_interval: Duration::ZERO }
    }

    /// Compute the delay that we should use to achieve the target frame
    /// rate.
    pub fn compute_timeout(&mut self, refresh_interval: Duration) -> Duration {
        let now = Instant::now();

        // Handle refresh rate change.
        if self.refresh_interval != refresh_interval {
            self.base = now;
            self.last_synced_timestamp = now;
            self.refresh_interval = refresh_interval;
            return refresh_interval;
        }

        let next_frame = self.last_synced_timestamp + self.refresh_interval;

        if next_frame < now {
            // Redraw immediately if we haven't drawn in over `refresh_interval` microseconds.
            let elapsed_micros = (now - self.base).as_micros() as u64;
            let refresh_micros = self.refresh_interval.as_micros() as u64;
            self.last_synced_timestamp =
                now - Duration::from_micros(elapsed_micros % refresh_micros);
            Duration::ZERO
        } else {
            // Redraw on the next `refresh_interval` clock tick.