From 055fbfba78de49be3f18df81ba749dae72e3e910 Mon Sep 17 00:00:00 2001 From: Deukhoofd Date: Sun, 25 Dec 2022 11:26:14 +0100 Subject: [PATCH] Update Wasmer, log load times, some minor performance tweaks --- .cargo/config.toml | 5 +- Cargo.toml | 2 +- .../wasm/script_resolver.rs | 6 +-- src/utils/string_key.rs | 5 +- tests/common/library_loader.rs | 50 +++++++++++++++++-- tests/main.rs | 27 ++++++++-- 6 files changed, 82 insertions(+), 13 deletions(-) diff --git a/.cargo/config.toml b/.cargo/config.toml index 327a62c..803e058 100755 --- a/.cargo/config.toml +++ b/.cargo/config.toml @@ -1,3 +1,6 @@ [target.x86_64-unknown-linux-gnu] linker = "/usr/bin/clang" -rustflags = ["-Clink-arg=-fuse-ld=/usr/bin/mold"] \ No newline at end of file +rustflags = ["-Clink-arg=-fuse-ld=/usr/bin/mold"] + +[rust] +debuginfo-level = 1 \ No newline at end of file diff --git a/Cargo.toml b/Cargo.toml index 9d9b3f6..b7335f0 100755 --- a/Cargo.toml +++ b/Cargo.toml @@ -53,7 +53,7 @@ hashbrown = "0.13.1" indexmap = "1.8.2" parking_lot = "0.12.1" serde = { version = "1.0.137", optional = true, features = ["derive"] } -wasmer = { version = "3.0.1", optional = true, default-features = true } +wasmer = { version = "3.1.0", optional = true, default-features = false, features = ["sys", "wat", "llvm"] } uuid = "1.2.2" paste = { version = "1.0.8" } arcstr = { version = "1.1.4", features = ["std"] } diff --git a/src/script_implementations/wasm/script_resolver.rs b/src/script_implementations/wasm/script_resolver.rs index 3444c07..8e8664a 100755 --- a/src/script_implementations/wasm/script_resolver.rs +++ b/src/script_implementations/wasm/script_resolver.rs @@ -7,8 +7,8 @@ use hashbrown::{HashMap, HashSet}; use parking_lot::lock_api::RwLockReadGuard; use parking_lot::{RawRwLock, RwLock}; use wasmer::{ - AsStoreMut, AsStoreRef, Cranelift, EngineBuilder, Extern, Features, Function, FunctionEnv, Imports, Instance, - Memory, Module, Store, StoreMut, StoreRef, TypedFunction, Value, + AsStoreMut, AsStoreRef, EngineBuilder, Extern, Features, Function, FunctionEnv, Imports, Instance, Memory, Module, + Store, StoreMut, StoreRef, TypedFunction, Value, }; use crate::dynamic_data::{ItemScript, Script, ScriptOwnerData, ScriptResolver}; @@ -52,7 +52,7 @@ struct ScriptCapabilitiesKey { impl WebAssemblyScriptResolver { /// Instantiates a new WebAssemblyScriptResolver. pub fn new() -> Box { - let compiler = Cranelift::default(); + let compiler = wasmer::LLVM::default(); let mut features = Features::new(); features.multi_value = true; features.reference_types = true; diff --git a/src/utils/string_key.rs b/src/utils/string_key.rs index 802eac4..4c3dd5d 100755 --- a/src/utils/string_key.rs +++ b/src/utils/string_key.rs @@ -129,10 +129,13 @@ impl ValueIdentifiable for StringKey { } } +/// The difference in ascii characters to translate from uppercase to lowercase. +const CAPITAL_DIFF: u8 = b'a' - b'A'; + /// Converts a character to lowercased in a const safe way. const fn to_lower(c: u8) -> u8 { if c >= b'A' && c <= b'Z' { - return c + (b'a' - b'A'); + return c + CAPITAL_DIFF; } c } diff --git a/tests/common/library_loader.rs b/tests/common/library_loader.rs index 31951c4..c207aac 100755 --- a/tests/common/library_loader.rs +++ b/tests/common/library_loader.rs @@ -1,3 +1,4 @@ +use chrono::Duration; use std::convert::TryFrom; use std::fmt::Debug; use std::fs::File; @@ -24,18 +25,45 @@ use pkmn_lib::static_data::{ }; use pkmn_lib::StringKey; -pub fn load_library() -> Arc { +pub struct LoadResult { + pub library: Arc, + pub types_load_time: Duration, + pub natures_load_time: Duration, + pub items_load_time: Duration, + pub growth_rate_load_time: Duration, + pub abilities_load_time: Duration, + pub moves_load_time: Duration, + pub species_load_time: Duration, + pub wasm_load_time: Duration, +} + +pub fn load_library() -> LoadResult { let mut path = get_project_root().unwrap(); path.push("tests/data/"); let path = path.to_str().unwrap().to_string(); + let t1 = chrono::Utc::now(); let types = load_types(&path); + let t2 = chrono::Utc::now(); + let types_load_time = t2 - t1; let natures = load_natures(&path); + let t1 = chrono::Utc::now(); + let natures_load_time = t1 - t2; let items = load_items(&path); + let t2 = chrono::Utc::now(); + let items_load_time = t2 - t1; let growth_rates = load_growth_rates(&path); + let t1 = chrono::Utc::now(); + let growth_rate_load_time = t1 - t2; let abilities = load_abilities(&path); + let t2 = chrono::Utc::now(); + let abilities_load_time = t2 - t1; let moves = load_moves(&path, &types); + let t1 = chrono::Utc::now(); + let moves_load_time = t1 - t2; let species = load_species(&path, &types, &moves); + let t2 = chrono::Utc::now(); + let species_load_time = t2 - t1; let data = StaticDataImpl::new( Box::new(LibrarySettingsImpl::new(100)), @@ -47,16 +75,32 @@ pub fn load_library() -> Arc { natures, abilities, ); + + let t1 = chrono::Utc::now(); let mut resolver = WebAssemblyScriptResolver::new(); load_wasm(&path, resolver.as_mut()); + let t2 = chrono::Utc::now(); + let wasm_load_time = t2 - t1; - Arc::new(DynamicLibraryImpl::new( + let library = Arc::new(DynamicLibraryImpl::new( Box::new(data), Box::new(Gen7BattleStatCalculator::new()), Box::new(Gen7DamageLibrary::new(false)), Box::new(Gen7MiscLibrary::new()), resolver, - )) + )); + + LoadResult { + library, + types_load_time, + natures_load_time, + items_load_time, + growth_rate_load_time, + abilities_load_time, + moves_load_time, + species_load_time, + wasm_load_time, + } } pub fn load_types(path: &String) -> Box { diff --git a/tests/main.rs b/tests/main.rs index 86479fa..eea94a1 100755 --- a/tests/main.rs +++ b/tests/main.rs @@ -19,16 +19,35 @@ use crate::common::{library_loader, TestCase}; pub mod common; fn get_library() -> Arc { - library_loader::load_library() + library_loader::load_library().library } #[test] -#[cfg_attr(miri, ignore)] fn validate_library_load() { let start_time = chrono::Utc::now(); - library_loader::load_library(); + let result = library_loader::load_library(); let end_time = chrono::Utc::now(); - println!("Built library in {} ms", (end_time - start_time).num_milliseconds()); + println!( + "Built library in {} ms\ + \n\t- Types load time: {} ms\ + \n\t- Natures load time: {} ms\ + \n\t- Items load time: {} ms\ + \n\t- Growth Rate load time: {} ms\ + \n\t- Abilities load time: {} ms\ + \n\t- Moves load time: {} ms\ + \n\t- Species load time: {} ms\ + \n\t- WASM load time: {} ms\ + ", + (end_time - start_time).num_milliseconds(), + result.types_load_time.num_milliseconds(), + result.natures_load_time.num_milliseconds(), + result.items_load_time.num_milliseconds(), + result.growth_rate_load_time.num_milliseconds(), + result.abilities_load_time.num_milliseconds(), + result.moves_load_time.num_milliseconds(), + result.species_load_time.num_milliseconds(), + result.wasm_load_time.num_milliseconds(), + ); } #[datatest::files("tests/test_cases", {