|
| 1 | +use crate::platform::prelude::*; |
| 2 | +use crate::Time; |
| 3 | + |
| 4 | +// We use a Vec here because a HashMap would require hashing the comparison and |
| 5 | +// then comparing the comparison with the string at the index calculated from |
| 6 | +// the hash. This means at least two full iterations over the string are |
| 7 | +// necessary, with one of them being somewhat expensive due to the hashing. Most |
| 8 | +// of the time it is faster to just iterate the few comparisons we have and |
| 9 | +// compare them directly. Most will be rejected right away as the first byte |
| 10 | +// doesn't even match, so in the end you'll end up with less than two full |
| 11 | +// iterations over the string. In fact most of the time Personal Best will be |
| 12 | +// the first in the list and that's the one we most often want to look up |
| 13 | +// anyway. |
| 14 | +// |
| 15 | +// One additional reason for doing this is that the ahash that was calculated |
| 16 | +// for the HashMap uses 128-bit multiplications which regressed a lot in Rust |
| 17 | +// 1.44 for targets where the `compiler-builtins` helpers were used. |
| 18 | +// https://github.com/rust-lang/rust/issues/73135 |
| 19 | +// |
| 20 | +// We could potentially look into interning our comparisons in the future which |
| 21 | +// could yield even better performance. |
| 22 | + |
| 23 | +/// A collection of a segment's comparison times. |
| 24 | +#[derive(Clone, Default, Debug, PartialEq)] |
| 25 | +pub struct Comparisons(Vec<(Box<str>, Time)>); |
| 26 | + |
| 27 | +impl Comparisons { |
| 28 | + fn index_of(&self, comparison: &str) -> Option<usize> { |
| 29 | + Some( |
| 30 | + self.0 |
| 31 | + .iter() |
| 32 | + .enumerate() |
| 33 | + .find(|(_, (c, _))| &**c == comparison)? |
| 34 | + .0, |
| 35 | + ) |
| 36 | + } |
| 37 | + |
| 38 | + /// Accesses the time for the comparison specified. |
| 39 | + pub fn get(&self, comparison: &str) -> Option<Time> { |
| 40 | + Some(self.0[self.index_of(comparison)?].1) |
| 41 | + } |
| 42 | + |
| 43 | + /// Accesses the time for the comparison specified, or inserts a new empty |
| 44 | + /// one if there is none. |
| 45 | + pub fn get_or_insert_default(&mut self, comparison: &str) -> &mut Time { |
| 46 | + if let Some(index) = self.index_of(comparison) { |
| 47 | + &mut self.0[index].1 |
| 48 | + } else { |
| 49 | + self.0.push((comparison.into(), Time::default())); |
| 50 | + &mut self.0.last_mut().unwrap().1 |
| 51 | + } |
| 52 | + } |
| 53 | + |
| 54 | + /// Sets the time for the comparison specified. |
| 55 | + pub fn set(&mut self, comparison: &str, time: Time) { |
| 56 | + *self.get_or_insert_default(comparison) = time; |
| 57 | + } |
| 58 | + |
| 59 | + /// Removes the time for the comparison specified and returns it if there |
| 60 | + /// was one. |
| 61 | + pub fn remove(&mut self, comparison: &str) -> Option<Time> { |
| 62 | + let index = self.index_of(comparison)?; |
| 63 | + let (_, time) = self.0.remove(index); |
| 64 | + Some(time) |
| 65 | + } |
| 66 | + |
| 67 | + /// Clears all the comparisons and their times. |
| 68 | + pub fn clear(&mut self) { |
| 69 | + self.0.clear(); |
| 70 | + } |
| 71 | + |
| 72 | + /// Iterates over all the comparisons and their times. |
| 73 | + pub fn iter(&self) -> impl Iterator<Item = &(Box<str>, Time)> + '_ { |
| 74 | + self.0.iter() |
| 75 | + } |
| 76 | + |
| 77 | + /// Mutably iterates over all the comparisons and their times. Be careful |
| 78 | + /// when modifying the comparison name. Having duplicates will likely cause |
| 79 | + /// problems. |
| 80 | + pub fn iter_mut(&mut self) -> impl Iterator<Item = &mut (Box<str>, Time)> + '_ { |
| 81 | + self.0.iter_mut() |
| 82 | + } |
| 83 | +} |
0 commit comments