bus: Change named ranges from hashmap to array

Improves performance by 0.4 seconds in unit tests
This commit is contained in:
John 2023-03-28 07:35:18 -05:00
parent fbc0a0b2ea
commit f2f47e13d4
3 changed files with 11 additions and 6 deletions

View File

@ -4,7 +4,7 @@ cover:
cargo llvm-cov --open --doctests cargo llvm-cov --open --doctests
test: test:
cargo test --all cargo test --doc && cargo nextest run
tokei: tokei:
tokei --exclude tests/chip8-test-suite tokei --exclude tests/chip8-test-suite

View File

@ -7,7 +7,6 @@
use crate::error::Result; use crate::error::Result;
use std::{ use std::{
collections::HashMap,
fmt::{Debug, Display, Formatter}, fmt::{Debug, Display, Formatter},
ops::Range, ops::Range,
slice::SliceIndex, slice::SliceIndex,
@ -49,12 +48,14 @@ pub trait Write<T> {
} }
/// Represents a named region in memory /// Represents a named region in memory
#[non_exhaustive]
#[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)] #[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub enum Region { pub enum Region {
Charset, Charset,
Program, Program,
Screen, Screen,
Stack, Stack,
Count,
} }
impl Display for Region { impl Display for Region {
@ -67,6 +68,7 @@ impl Display for Region {
Region::Program => "program", Region::Program => "program",
Region::Screen => "screen", Region::Screen => "screen",
Region::Stack => "stack", Region::Stack => "stack",
_ => "",
} }
) )
} }
@ -76,7 +78,7 @@ impl Display for Region {
#[derive(Clone, Debug, Default, PartialEq)] #[derive(Clone, Debug, Default, PartialEq)]
pub struct Bus { pub struct Bus {
memory: Vec<u8>, memory: Vec<u8>,
region: HashMap<Region, Range<usize>>, region: [Option<Range<usize>>; Region::Count as usize],
} }
impl Bus { impl Bus {
@ -153,7 +155,9 @@ impl Bus {
/// ``` /// ```
pub fn add_region(mut self, name: Region, range: Range<usize>) -> Self { pub fn add_region(mut self, name: Region, range: Range<usize>) -> Self {
self.with_size(range.end); self.with_size(range.end);
self.region.insert(name, range); if let Some(region) = self.region.get_mut(name as usize) {
*region = Some(range);
}
self self
} }
/// Loads data into a named region /// Loads data into a named region
@ -242,7 +246,7 @@ impl Bus {
///# } ///# }
/// ``` /// ```
pub fn get_region(&self, name: Region) -> Option<&[u8]> { pub fn get_region(&self, name: Region) -> Option<&[u8]> {
self.get(self.region.get(&name)?.clone()) self.get(self.region.get(name as usize)?.clone()?)
} }
/// Gets a mutable slice of a named region of memory /// Gets a mutable slice of a named region of memory
@ -257,7 +261,7 @@ impl Bus {
///# } ///# }
/// ``` /// ```
pub fn get_region_mut(&mut self, name: Region) -> Option<&mut [u8]> { pub fn get_region_mut(&mut self, name: Region) -> Option<&mut [u8]> {
self.get_mut(self.region.get(&name)?.clone()) self.get_mut(self.region.get(name as usize)?.clone()?)
} }
/// Prints the region of memory called `Screen` at 1bpp using box characters /// Prints the region of memory called `Screen` at 1bpp using box characters

View File

@ -6,6 +6,7 @@
use thiserror::Error; use thiserror::Error;
pub type Result<T> = std::result::Result<T, Error>; pub type Result<T> = std::result::Result<T, Error>;
#[derive(Debug, Error)] #[derive(Debug, Error)]
pub enum Error { pub enum Error {
#[error("Unrecognized opcode {word}")] #[error("Unrecognized opcode {word}")]