allow inlining (this did not provide a statistically significant performance boost - oh well)

This commit is contained in:
Max Bradbury 2020-04-28 18:00:31 +01:00
parent dd00686de0
commit f899f03dbe
15 changed files with 56 additions and 2 deletions

View File

@ -6,6 +6,7 @@ pub struct Colour {
} }
impl From<String> for Colour { impl From<String> for Colour {
#[inline]
fn from(string: String) -> Colour { fn from(string: String) -> Colour {
let values: Vec<&str> = string.split(',').collect(); let values: Vec<&str> = string.split(',').collect();

View File

@ -5,6 +5,7 @@ pub struct Dialogue {
} }
impl From<String> for Dialogue { impl From<String> for Dialogue {
#[inline]
fn from(string: String) -> Dialogue { fn from(string: String) -> Dialogue {
let lines: Vec<&str> = string.lines().collect(); let lines: Vec<&str> = string.lines().collect();
let id = lines[0].replace("DLG ", "").to_string(); let id = lines[0].replace("DLG ", "").to_string();

View File

@ -6,6 +6,7 @@ pub struct Ending {
} }
impl From<String> for Ending { impl From<String> for Ending {
#[inline]
fn from(string: String) -> Ending { fn from(string: String) -> Ending {
let lines: Vec<&str> = string.lines().collect(); let lines: Vec<&str> = string.lines().collect();
let id = lines[0].replace("END ", "").to_string(); let id = lines[0].replace("END ", "").to_string();

View File

@ -14,6 +14,7 @@ pub enum Transition {
} }
impl From<&str> for Transition { impl From<&str> for Transition {
#[inline]
fn from(str: &str) -> Transition { fn from(str: &str) -> Transition {
match str { match str {
"fade_w" => Transition::FadeToWhite, "fade_w" => Transition::FadeToWhite,
@ -30,6 +31,7 @@ impl From<&str> for Transition {
} }
impl ToString for Transition { impl ToString for Transition {
#[inline]
fn to_string(&self) -> String { fn to_string(&self) -> String {
match &self { match &self {
Transition::FadeToWhite => " FX fade_w", Transition::FadeToWhite => " FX fade_w",
@ -56,6 +58,7 @@ pub struct Exit {
} }
impl From<String> for Exit { impl From<String> for Exit {
#[inline]
fn from(string: String) -> Exit { fn from(string: String) -> Exit {
// e.g. "EXT 6,4 0 10,12 FX fade_w" // e.g. "EXT 6,4 0 10,12 FX fade_w"
let room_position_effect: Vec<&str> = string.split_whitespace().collect(); let room_position_effect: Vec<&str> = string.split_whitespace().collect();
@ -77,6 +80,7 @@ impl From<String> for Exit {
} }
impl ToString for Exit { impl ToString for Exit {
#[inline]
fn to_string(&self) -> String { fn to_string(&self) -> String {
format!( format!(
"{} {}{}", "{} {}{}",

View File

@ -274,6 +274,7 @@ impl ToString for Game {
} }
impl Game { impl Game {
#[inline]
pub fn tile_ids(&self) -> Vec<u64> { pub fn tile_ids(&self) -> Vec<u64> {
self.tiles.iter().map(|tile| tile.id).collect() self.tiles.iter().map(|tile| tile.id).collect()
} }
@ -281,6 +282,7 @@ impl Game {
/// first available tile ID. /// first available tile ID.
/// e.g. if current tile IDs are [0, 2, 3] the result will be `1` /// 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` /// if current tile IDs are [0, 1, 2] the result will be `3`
#[inline]
pub fn new_tile_id(&self) -> u64 { pub fn new_tile_id(&self) -> u64 {
let mut new_id = 0; let mut new_id = 0;
@ -299,6 +301,7 @@ impl Game {
} }
/// adds a tile safely and returns the new tile ID /// adds a tile safely and returns the new tile ID
#[inline]
pub fn add_tile(&mut self, mut tile: Tile) -> u64 { pub fn add_tile(&mut self, mut tile: Tile) -> u64 {
let new_id = self.new_tile_id(); let new_id = self.new_tile_id();
tile.id = new_id; tile.id = new_id;
@ -306,6 +309,7 @@ impl Game {
new_id new_id
} }
#[inline]
fn version_line(&self) -> String { fn version_line(&self) -> String {
if self.version.is_some() { if self.version.is_some() {
format!( format!(
@ -317,6 +321,7 @@ impl Game {
} }
} }
#[inline]
fn room_format_line(&self) -> String { fn room_format_line(&self) -> String {
if self.room_format.is_some() { if self.room_format.is_some() {
format!("\n\n! ROOM_FORMAT {}", self.room_format.unwrap().to_string()) format!("\n\n! ROOM_FORMAT {}", self.room_format.unwrap().to_string())
@ -325,6 +330,7 @@ impl Game {
} }
} }
#[inline]
fn font_line(&self) -> String { fn font_line(&self) -> String {
if self.font == Font::AsciiSmall { if self.font == Font::AsciiSmall {
"".to_string() "".to_string()
@ -337,6 +343,7 @@ impl Game {
} }
} }
#[inline]
fn text_direction_line(&self) -> &str { fn text_direction_line(&self) -> &str {
if self.text_direction == TextDirection::RightToLeft { if self.text_direction == TextDirection::RightToLeft {
"\n\nTEXT_DIRECTION RTL" "\n\nTEXT_DIRECTION RTL"
@ -346,11 +353,13 @@ impl Game {
} }
/// older bitsy games do not specify a version, but we can infer 1.0 /// older bitsy games do not specify a version, but we can infer 1.0
#[inline]
pub fn version(&self) -> Version { pub fn version(&self) -> Version {
self.version.unwrap_or(Version { major: 1, minor: 0 }) self.version.unwrap_or(Version { major: 1, minor: 0 })
} }
/// older bitsy games do not specify a room format, but we can infer 0 /// older bitsy games do not specify a room format, but we can infer 0
#[inline]
pub fn room_format(&self) -> RoomFormat { pub fn room_format(&self) -> RoomFormat {
self.room_format.unwrap_or(RoomFormat::Contiguous) self.room_format.unwrap_or(RoomFormat::Contiguous)
} }

View File

@ -10,20 +10,24 @@ pub struct Item {
} }
impl Item { impl Item {
#[inline]
fn name_line(&self) -> String { fn name_line(&self) -> String {
optional_data_line("NAME", self.name.as_ref()) optional_data_line("NAME", self.name.as_ref())
} }
#[inline]
fn dialogue_line(&self) -> String { fn dialogue_line(&self) -> String {
optional_data_line("DLG", self.dialogue_id.as_ref()) optional_data_line("DLG", self.dialogue_id.as_ref())
} }
#[inline]
fn colour_line(&self) -> String { fn colour_line(&self) -> String {
optional_data_line("COL", self.colour_id.as_ref()) optional_data_line("COL", self.colour_id.as_ref())
} }
} }
impl From<String> for Item { impl From<String> for Item {
#[inline]
fn from(string: String) -> Item { fn from(string: String) -> Item {
let mut lines: Vec<&str> = string.lines().collect(); let mut lines: Vec<&str> = string.lines().collect();

View File

@ -72,11 +72,13 @@ impl AnimationFrames for Vec<Image> {
} }
} }
#[inline]
fn from_base36(str: &str) -> u64 { fn from_base36(str: &str) -> u64 {
u64::from_str_radix(str, 36).expect(&format!("Invalid base36 string: {}", str)) u64::from_str_radix(str, 36).expect(&format!("Invalid base36 string: {}", str))
} }
/// this doesn't work inside ToBase36 for some reason /// this doesn't work inside ToBase36 for some reason
#[inline]
fn to_base36(int: u64) -> String { fn to_base36(int: u64) -> String {
format!("{}", radix_36(int)) format!("{}", radix_36(int))
} }
@ -86,12 +88,14 @@ pub trait ToBase36 {
} }
impl ToBase36 for u64 { impl ToBase36 for u64 {
#[inline]
fn to_base36(&self) -> String { fn to_base36(&self) -> String {
to_base36(*self) to_base36(*self)
} }
} }
/// e.g. `\nNAME DLG_0` /// e.g. `\nNAME DLG_0`
#[inline]
fn optional_data_line<T: Display>(label: &str, item: Option<T>) -> String { fn optional_data_line<T: Display>(label: &str, item: Option<T>) -> String {
if item.is_some() { if item.is_some() {
format!("\n{} {}", label, item.unwrap()) format!("\n{} {}", label, item.unwrap())
@ -100,6 +104,7 @@ fn optional_data_line<T: Display>(label: &str, item: Option<T>) -> String {
} }
} }
#[inline]
fn transform_line_endings(input: String, mode: TransformMode) -> String { fn transform_line_endings(input: String, mode: TransformMode) -> String {
let mut input = Cursor::new(input); let mut input = Cursor::new(input);
let mut output = Cursor::new(Vec::new()); 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() String::from_utf8(output.into_inner()).unwrap()
} }
#[inline]
fn segments_from_string(string: String) -> Vec<String> { fn segments_from_string(string: String) -> Vec<String> {
let mut output:Vec<String> = Vec::new(); let mut output:Vec<String> = Vec::new();
// are we inside `"""\n...\n"""`? if so, ignore empty lines // are we inside `"""\n...\n"""`? if so, ignore empty lines
@ -133,8 +139,9 @@ fn segments_from_string(string: String) -> Vec<String> {
output output
} }
// for some reason the sprites with numeric IDs go first, /// for some reason the sprites with numeric IDs go first,
// then SPR A (avatar), then all the non-numeric IDs /// then SPR A (avatar), then all the non-numeric IDs
#[inline]
fn is_string_numeric(str: String) -> bool { fn is_string_numeric(str: String) -> bool {
for c in str.chars() { for c in str.chars() {
if !c.is_numeric() { if !c.is_numeric() {

View File

@ -4,6 +4,7 @@ use crate::game::{RoomType, RoomFormat};
pub mod image { pub mod image {
use crate::Image; use crate::Image;
#[inline]
pub(crate) fn chequers_1() -> Image { pub(crate) fn chequers_1() -> Image {
Image { Image {
pixels: vec![ pixels: vec![
@ -14,6 +15,7 @@ pub mod image {
} }
} }
#[inline]
pub fn chequers_2() -> Image { pub fn chequers_2() -> Image {
Image { Image {
pixels: vec![ pixels: vec![
@ -25,6 +27,7 @@ pub mod image {
} }
} }
#[inline]
pub fn avatar() -> Sprite { pub fn avatar() -> Sprite {
Sprite { Sprite {
id: 0, id: 0,
@ -53,6 +56,7 @@ pub fn avatar() -> Sprite {
} }
} }
#[inline]
pub fn tile_default() -> Tile { pub fn tile_default() -> Tile {
Tile { Tile {
id: 10, id: 10,
@ -69,6 +73,7 @@ pub fn tile_default() -> Tile {
} }
} }
#[inline]
pub fn sprite() -> Sprite { pub fn sprite() -> Sprite {
Sprite { Sprite {
id: 10, id: 10,
@ -88,6 +93,7 @@ pub fn sprite() -> Sprite {
} }
} }
#[inline]
pub fn item() -> Item { pub fn item() -> Item {
Item { Item {
id: 6, id: 6,
@ -104,6 +110,7 @@ pub fn item() -> Item {
} }
} }
#[inline]
pub fn room() -> Room { pub fn room() -> Room {
Room { Room {
id: 10, id: 10,
@ -407,6 +414,7 @@ pub fn room() -> Room {
} }
} }
#[inline]
pub fn game_default() -> Game { pub fn game_default() -> Game {
Game { Game {
name: "Write your game's title here".to_string(), name: "Write your game's title here".to_string(),

View File

@ -9,6 +9,7 @@ pub struct Palette {
} }
impl From<String> for Palette { impl From<String> for Palette {
#[inline]
fn from(string: String) -> Palette { fn from(string: String) -> Palette {
let lines: Vec<&str> = string.lines().collect(); let lines: Vec<&str> = string.lines().collect();

View File

@ -7,6 +7,7 @@ pub struct Position {
} }
impl Position { impl Position {
#[inline]
pub(crate) fn from(string: String) -> Result<Position, &'static dyn Error> { pub(crate) fn from(string: String) -> Result<Position, &'static dyn Error> {
// e.g. "2,5" // e.g. "2,5"
let xy: Vec<&str> = string.split(',').collect(); let xy: Vec<&str> = string.split(',').collect();

View File

@ -15,10 +15,12 @@ pub struct Room {
} }
impl Room { impl Room {
#[inline]
fn name_line(&self) -> String { fn name_line(&self) -> String {
optional_data_line("NAME", self.name.as_ref()) optional_data_line("NAME", self.name.as_ref())
} }
#[inline]
fn wall_line(&self) -> String { fn wall_line(&self) -> String {
if self.walls.len() > 0 { if self.walls.len() > 0 {
let ids: Vec<String> = self.walls.iter().map(|&id| id.to_base36()).collect(); let ids: Vec<String> = self.walls.iter().map(|&id| id.to_base36()).collect();
@ -28,6 +30,7 @@ impl Room {
} }
} }
#[inline]
fn palette_line(&self) -> String { fn palette_line(&self) -> String {
if self.palette_id.is_some() { if self.palette_id.is_some() {
optional_data_line("PAL", Some(self.palette_id.unwrap().to_base36())) optional_data_line("PAL", Some(self.palette_id.unwrap().to_base36()))
@ -38,6 +41,7 @@ impl Room {
} }
impl From<String> for Room { impl From<String> for Room {
#[inline]
fn from(string: String) -> Room { fn from(string: String) -> Room {
let string = string.replace("ROOM ", ""); let string = string.replace("ROOM ", "");
let string = string.replace("SET ", ""); let string = string.replace("SET ", "");
@ -141,6 +145,7 @@ impl From<String> for Room {
} }
impl Room { impl Room {
#[inline]
pub fn to_string(&self, room_format: RoomFormat, room_type: RoomType) -> String { pub fn to_string(&self, room_format: RoomFormat, room_type: RoomType) -> String {
let mut tiles = String::new(); let mut tiles = String::new();
let mut items = String::new(); let mut items = String::new();

View File

@ -13,14 +13,17 @@ pub struct Sprite {
} }
impl Sprite { impl Sprite {
#[inline]
fn name_line(&self) -> String { fn name_line(&self) -> String {
optional_data_line("NAME", self.name.as_ref()) optional_data_line("NAME", self.name.as_ref())
} }
#[inline]
fn dialogue_line(&self) -> String { fn dialogue_line(&self) -> String {
optional_data_line("DLG", self.dialogue_id.as_ref()) optional_data_line("DLG", self.dialogue_id.as_ref())
} }
#[inline]
fn room_position_line(&self) -> String { fn room_position_line(&self) -> String {
if self.room_id.is_some() && self.position.is_some() { if self.room_id.is_some() && self.position.is_some() {
format!( format!(
@ -33,10 +36,12 @@ impl Sprite {
} }
} }
#[inline]
fn colour_line(&self) -> String { fn colour_line(&self) -> String {
optional_data_line("COL", self.colour_id.as_ref()) optional_data_line("COL", self.colour_id.as_ref())
} }
#[inline]
fn item_lines(&self) -> String { fn item_lines(&self) -> String {
if self.items.len() == 0 { if self.items.len() == 0 {
"".to_string() "".to_string()
@ -48,6 +53,7 @@ impl Sprite {
} }
impl From<String> for Sprite { impl From<String> for Sprite {
#[inline]
fn from(string: String) -> Sprite { fn from(string: String) -> Sprite {
let mut lines: Vec<&str> = string.lines().collect(); let mut lines: Vec<&str> = string.lines().collect();

View File

@ -9,6 +9,7 @@ pub enum Font {
} }
impl Font { impl Font {
#[inline]
pub(crate) fn from(str: &str) -> Font { pub(crate) fn from(str: &str) -> Font {
match str { match str {
"unicode_european_small" => Font::UnicodeEuropeanSmall, "unicode_european_small" => Font::UnicodeEuropeanSmall,
@ -19,6 +20,7 @@ impl Font {
} }
} }
#[inline]
pub(crate) fn to_string(&self) -> Result<String, &'static str> { pub(crate) fn to_string(&self) -> Result<String, &'static str> {
match &self { match &self {
Font::UnicodeEuropeanSmall => Ok("unicode_european_small".to_string()), Font::UnicodeEuropeanSmall => Ok("unicode_european_small".to_string()),

View File

@ -10,10 +10,12 @@ pub struct Tile {
} }
impl Tile { impl Tile {
#[inline]
fn name_line(&self) -> String { fn name_line(&self) -> String {
optional_data_line("NAME", self.name.as_ref()) optional_data_line("NAME", self.name.as_ref())
} }
#[inline]
fn wall_line(&self) -> String { fn wall_line(&self) -> String {
if self.wall.is_some() { if self.wall.is_some() {
format!("\nWAL {}", self.wall.unwrap()) format!("\nWAL {}", self.wall.unwrap())
@ -22,6 +24,7 @@ impl Tile {
} }
} }
#[inline]
fn colour_line(&self) -> String { fn colour_line(&self) -> String {
if self.colour_id.is_some() { if self.colour_id.is_some() {
format!("\nCOL {}", self.colour_id.unwrap()) format!("\nCOL {}", self.colour_id.unwrap())

View File

@ -5,6 +5,7 @@ pub struct Variable {
} }
impl From<String> for Variable { impl From<String> for Variable {
#[inline]
fn from(string: String) -> Variable { fn from(string: String) -> Variable {
let id_value: Vec<&str> = string.lines().collect(); let id_value: Vec<&str> = string.lines().collect();
let id = id_value[0].replace("VAR ", "").to_string(); let id = id_value[0].replace("VAR ", "").to_string();