From f899f03dbe7b2880570bb26c45ab2c33d84640f6 Mon Sep 17 00:00:00 2001 From: Max Bradbury Date: Tue, 28 Apr 2020 18:00:31 +0100 Subject: [PATCH] allow inlining (this did not provide a statistically significant performance boost - oh well) --- src/colour.rs | 1 + src/dialogue.rs | 1 + src/ending.rs | 1 + src/exit.rs | 4 ++++ src/game.rs | 9 +++++++++ src/item.rs | 4 ++++ src/lib.rs | 11 +++++++++-- src/mock.rs | 8 ++++++++ src/palette.rs | 1 + src/position.rs | 1 + src/room.rs | 5 +++++ src/sprite.rs | 6 ++++++ src/text.rs | 2 ++ src/tile.rs | 3 +++ src/variable.rs | 1 + 15 files changed, 56 insertions(+), 2 deletions(-) diff --git a/src/colour.rs b/src/colour.rs index 2bc716a..ae34a97 100644 --- a/src/colour.rs +++ b/src/colour.rs @@ -6,6 +6,7 @@ pub struct Colour { } impl From for Colour { + #[inline] fn from(string: String) -> Colour { let values: Vec<&str> = string.split(',').collect(); diff --git a/src/dialogue.rs b/src/dialogue.rs index 9a5cfef..ad282ac 100644 --- a/src/dialogue.rs +++ b/src/dialogue.rs @@ -5,6 +5,7 @@ pub struct Dialogue { } impl From for Dialogue { + #[inline] fn from(string: String) -> Dialogue { let lines: Vec<&str> = string.lines().collect(); let id = lines[0].replace("DLG ", "").to_string(); diff --git a/src/ending.rs b/src/ending.rs index 9ef6389..1969fec 100644 --- a/src/ending.rs +++ b/src/ending.rs @@ -6,6 +6,7 @@ pub struct Ending { } impl From for Ending { + #[inline] fn from(string: String) -> Ending { let lines: Vec<&str> = string.lines().collect(); let id = lines[0].replace("END ", "").to_string(); diff --git a/src/exit.rs b/src/exit.rs index 2d59bae..4c9f2ac 100644 --- a/src/exit.rs +++ b/src/exit.rs @@ -14,6 +14,7 @@ pub enum Transition { } impl From<&str> for Transition { + #[inline] fn from(str: &str) -> Transition { match str { "fade_w" => Transition::FadeToWhite, @@ -30,6 +31,7 @@ impl From<&str> for Transition { } impl ToString for Transition { + #[inline] fn to_string(&self) -> String { match &self { Transition::FadeToWhite => " FX fade_w", @@ -56,6 +58,7 @@ pub struct Exit { } impl From for Exit { + #[inline] fn from(string: String) -> Exit { // e.g. "EXT 6,4 0 10,12 FX fade_w" let room_position_effect: Vec<&str> = string.split_whitespace().collect(); @@ -77,6 +80,7 @@ impl From for Exit { } impl ToString for Exit { + #[inline] fn to_string(&self) -> String { format!( "{} {}{}", diff --git a/src/game.rs b/src/game.rs index 6878c15..7e43c19 100644 --- a/src/game.rs +++ b/src/game.rs @@ -274,6 +274,7 @@ impl ToString for Game { } impl Game { + #[inline] pub fn tile_ids(&self) -> Vec { self.tiles.iter().map(|tile| tile.id).collect() } @@ -281,6 +282,7 @@ impl Game { /// first available tile ID. /// e.g. if current tile IDs are [0, 2, 3] the result will be `1` /// if current tile IDs are [0, 1, 2] the result will be `3` + #[inline] pub fn new_tile_id(&self) -> u64 { let mut new_id = 0; @@ -299,6 +301,7 @@ impl Game { } /// adds a tile safely and returns the new tile ID + #[inline] pub fn add_tile(&mut self, mut tile: Tile) -> u64 { let new_id = self.new_tile_id(); tile.id = new_id; @@ -306,6 +309,7 @@ impl Game { new_id } + #[inline] fn version_line(&self) -> String { if self.version.is_some() { format!( @@ -317,6 +321,7 @@ impl Game { } } + #[inline] fn room_format_line(&self) -> String { if self.room_format.is_some() { format!("\n\n! ROOM_FORMAT {}", self.room_format.unwrap().to_string()) @@ -325,6 +330,7 @@ impl Game { } } + #[inline] fn font_line(&self) -> String { if self.font == Font::AsciiSmall { "".to_string() @@ -337,6 +343,7 @@ impl Game { } } + #[inline] fn text_direction_line(&self) -> &str { if self.text_direction == TextDirection::RightToLeft { "\n\nTEXT_DIRECTION RTL" @@ -346,11 +353,13 @@ impl Game { } /// older bitsy games do not specify a version, but we can infer 1.0 + #[inline] pub fn version(&self) -> Version { self.version.unwrap_or(Version { major: 1, minor: 0 }) } /// older bitsy games do not specify a room format, but we can infer 0 + #[inline] pub fn room_format(&self) -> RoomFormat { self.room_format.unwrap_or(RoomFormat::Contiguous) } diff --git a/src/item.rs b/src/item.rs index a26d7e3..09578ff 100644 --- a/src/item.rs +++ b/src/item.rs @@ -10,20 +10,24 @@ pub struct Item { } impl Item { + #[inline] fn name_line(&self) -> String { optional_data_line("NAME", self.name.as_ref()) } + #[inline] fn dialogue_line(&self) -> String { optional_data_line("DLG", self.dialogue_id.as_ref()) } + #[inline] fn colour_line(&self) -> String { optional_data_line("COL", self.colour_id.as_ref()) } } impl From for Item { + #[inline] fn from(string: String) -> Item { let mut lines: Vec<&str> = string.lines().collect(); diff --git a/src/lib.rs b/src/lib.rs index 533f421..72e16a5 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -72,11 +72,13 @@ impl AnimationFrames for Vec { } } +#[inline] fn from_base36(str: &str) -> u64 { u64::from_str_radix(str, 36).expect(&format!("Invalid base36 string: {}", str)) } /// this doesn't work inside ToBase36 for some reason +#[inline] fn to_base36(int: u64) -> String { format!("{}", radix_36(int)) } @@ -86,12 +88,14 @@ pub trait ToBase36 { } impl ToBase36 for u64 { + #[inline] fn to_base36(&self) -> String { to_base36(*self) } } /// e.g. `\nNAME DLG_0` +#[inline] fn optional_data_line(label: &str, item: Option) -> String { if item.is_some() { format!("\n{} {}", label, item.unwrap()) @@ -100,6 +104,7 @@ fn optional_data_line(label: &str, item: Option) -> String { } } +#[inline] fn transform_line_endings(input: String, mode: TransformMode) -> String { let mut input = Cursor::new(input); let mut output = Cursor::new(Vec::new()); @@ -108,6 +113,7 @@ fn transform_line_endings(input: String, mode: TransformMode) -> String { String::from_utf8(output.into_inner()).unwrap() } +#[inline] fn segments_from_string(string: String) -> Vec { let mut output:Vec = Vec::new(); // are we inside `"""\n...\n"""`? if so, ignore empty lines @@ -133,8 +139,9 @@ fn segments_from_string(string: String) -> Vec { output } -// for some reason the sprites with numeric IDs go first, -// then SPR A (avatar), then all the non-numeric IDs +/// for some reason the sprites with numeric IDs go first, +/// then SPR A (avatar), then all the non-numeric IDs +#[inline] fn is_string_numeric(str: String) -> bool { for c in str.chars() { if !c.is_numeric() { diff --git a/src/mock.rs b/src/mock.rs index ae0b5a5..86f244c 100644 --- a/src/mock.rs +++ b/src/mock.rs @@ -4,6 +4,7 @@ use crate::game::{RoomType, RoomFormat}; pub mod image { use crate::Image; + #[inline] pub(crate) fn chequers_1() -> Image { Image { pixels: vec![ @@ -14,6 +15,7 @@ pub mod image { } } + #[inline] pub fn chequers_2() -> Image { Image { pixels: vec![ @@ -25,6 +27,7 @@ pub mod image { } } +#[inline] pub fn avatar() -> Sprite { Sprite { id: 0, @@ -53,6 +56,7 @@ pub fn avatar() -> Sprite { } } +#[inline] pub fn tile_default() -> Tile { Tile { id: 10, @@ -69,6 +73,7 @@ pub fn tile_default() -> Tile { } } +#[inline] pub fn sprite() -> Sprite { Sprite { id: 10, @@ -88,6 +93,7 @@ pub fn sprite() -> Sprite { } } +#[inline] pub fn item() -> Item { Item { id: 6, @@ -104,6 +110,7 @@ pub fn item() -> Item { } } +#[inline] pub fn room() -> Room { Room { id: 10, @@ -407,6 +414,7 @@ pub fn room() -> Room { } } +#[inline] pub fn game_default() -> Game { Game { name: "Write your game's title here".to_string(), diff --git a/src/palette.rs b/src/palette.rs index 58f3c35..eae6dc0 100644 --- a/src/palette.rs +++ b/src/palette.rs @@ -9,6 +9,7 @@ pub struct Palette { } impl From for Palette { + #[inline] fn from(string: String) -> Palette { let lines: Vec<&str> = string.lines().collect(); diff --git a/src/position.rs b/src/position.rs index c24e190..e31ddd5 100644 --- a/src/position.rs +++ b/src/position.rs @@ -7,6 +7,7 @@ pub struct Position { } impl Position { + #[inline] pub(crate) fn from(string: String) -> Result { // e.g. "2,5" let xy: Vec<&str> = string.split(',').collect(); diff --git a/src/room.rs b/src/room.rs index 892e947..cd4ee0e 100644 --- a/src/room.rs +++ b/src/room.rs @@ -15,10 +15,12 @@ pub struct Room { } impl Room { + #[inline] fn name_line(&self) -> String { optional_data_line("NAME", self.name.as_ref()) } + #[inline] fn wall_line(&self) -> String { if self.walls.len() > 0 { let ids: Vec = self.walls.iter().map(|&id| id.to_base36()).collect(); @@ -28,6 +30,7 @@ impl Room { } } + #[inline] fn palette_line(&self) -> String { if self.palette_id.is_some() { optional_data_line("PAL", Some(self.palette_id.unwrap().to_base36())) @@ -38,6 +41,7 @@ impl Room { } impl From for Room { + #[inline] fn from(string: String) -> Room { let string = string.replace("ROOM ", ""); let string = string.replace("SET ", ""); @@ -141,6 +145,7 @@ impl From for Room { } impl Room { + #[inline] pub fn to_string(&self, room_format: RoomFormat, room_type: RoomType) -> String { let mut tiles = String::new(); let mut items = String::new(); diff --git a/src/sprite.rs b/src/sprite.rs index 7660446..bdd4a74 100644 --- a/src/sprite.rs +++ b/src/sprite.rs @@ -13,14 +13,17 @@ pub struct Sprite { } impl Sprite { + #[inline] fn name_line(&self) -> String { optional_data_line("NAME", self.name.as_ref()) } + #[inline] fn dialogue_line(&self) -> String { optional_data_line("DLG", self.dialogue_id.as_ref()) } + #[inline] fn room_position_line(&self) -> String { if self.room_id.is_some() && self.position.is_some() { format!( @@ -33,10 +36,12 @@ impl Sprite { } } + #[inline] fn colour_line(&self) -> String { optional_data_line("COL", self.colour_id.as_ref()) } + #[inline] fn item_lines(&self) -> String { if self.items.len() == 0 { "".to_string() @@ -48,6 +53,7 @@ impl Sprite { } impl From for Sprite { + #[inline] fn from(string: String) -> Sprite { let mut lines: Vec<&str> = string.lines().collect(); diff --git a/src/text.rs b/src/text.rs index 8755421..ee0c8f0 100644 --- a/src/text.rs +++ b/src/text.rs @@ -9,6 +9,7 @@ pub enum Font { } impl Font { + #[inline] pub(crate) fn from(str: &str) -> Font { match str { "unicode_european_small" => Font::UnicodeEuropeanSmall, @@ -19,6 +20,7 @@ impl Font { } } + #[inline] pub(crate) fn to_string(&self) -> Result { match &self { Font::UnicodeEuropeanSmall => Ok("unicode_european_small".to_string()), diff --git a/src/tile.rs b/src/tile.rs index 0d1333c..060c903 100644 --- a/src/tile.rs +++ b/src/tile.rs @@ -10,10 +10,12 @@ pub struct Tile { } impl Tile { + #[inline] fn name_line(&self) -> String { optional_data_line("NAME", self.name.as_ref()) } + #[inline] fn wall_line(&self) -> String { if self.wall.is_some() { format!("\nWAL {}", self.wall.unwrap()) @@ -22,6 +24,7 @@ impl Tile { } } + #[inline] fn colour_line(&self) -> String { if self.colour_id.is_some() { format!("\nCOL {}", self.colour_id.unwrap()) diff --git a/src/variable.rs b/src/variable.rs index 2473bf4..8f225ed 100644 --- a/src/variable.rs +++ b/src/variable.rs @@ -5,6 +5,7 @@ pub struct Variable { } impl From for Variable { + #[inline] fn from(string: String) -> Variable { let id_value: Vec<&str> = string.lines().collect(); let id = id_value[0].replace("VAR ", "").to_string();