diff --git a/Cargo.lock b/Cargo.lock index b90a975..db6f9b1 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2,6 +2,40 @@ # It is not intended for manual editing. version = 4 +[[package]] +name = "cc" +version = "1.11.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "02f0e023adb368345ce0ca6cdfc0f24a06554468d0e8503e14a0841de79bfa69" +dependencies = [ + "find-msvc-tools", + "shlex", +] + +[[package]] +name = "find-msvc-tools" +version = "0.1.15" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1c66915cc5391097de7a2b0b972e8505aa33c0df401fd3a6811dbdb32881ba48" + +[[package]] +name = "libmimalloc-sys" +version = "0.1.49" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6a45a52f43e1c16f667ccfe4dd8c85b7f7c204fd5e3bf46c5b0db9a5c3c0b8e9" +dependencies = [ + "cc", +] + +[[package]] +name = "mimalloc" +version = "0.1.52" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2d4139bb28d14ad1facf21d5eb8825051b326e172d216b39f6d31df53cc97862" +dependencies = [ + "libmimalloc-sys", +] + [[package]] name = "ootrr" version = "0.8.2" @@ -15,6 +49,7 @@ dependencies = [ name = "ootrr-core" version = "0.8.2" dependencies = [ + "mimalloc", "ootrr-cosmetics", "ootrr-game", "ootrr-logic", @@ -24,6 +59,7 @@ dependencies = [ name = "ootrr-cosmetics" version = "0.8.2" dependencies = [ + "mimalloc", "ootrr-game", "ruzstd", ] @@ -72,6 +108,12 @@ dependencies = [ "twox-hash", ] +[[package]] +name = "shlex" +version = "2.0.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f8fadd59c855ef2080decdef8ff161eb6661b86933c9d82e5ba29dc602a55aba" + [[package]] name = "twox-hash" version = "2.1.5" diff --git a/randomizer/core/Cargo.toml b/randomizer/core/Cargo.toml index f51bf78..a12fbb1 100644 --- a/randomizer/core/Cargo.toml +++ b/randomizer/core/Cargo.toml @@ -15,3 +15,4 @@ ootrr-cosmetics = { path = "../cosmetics" } [dev-dependencies] ootrr-game = { path = "../game", features = ["fixtures"] } +mimalloc = { version = "0.1", default-features = false } diff --git a/randomizer/core/src/lib.rs b/randomizer/core/src/lib.rs index c9e4b1d..19d2163 100644 --- a/randomizer/core/src/lib.rs +++ b/randomizer/core/src/lib.rs @@ -105,3 +105,7 @@ pub mod teleport_sim; pub mod teleport_start; pub const TITLE_ID: &str = "0400486024DEC000"; + +#[cfg(test)] +#[global_allocator] +static GLOBAL: mimalloc::MiMalloc = mimalloc::MiMalloc; diff --git a/randomizer/cosmetics/Cargo.toml b/randomizer/cosmetics/Cargo.toml index 8150eb5..346531f 100644 --- a/randomizer/cosmetics/Cargo.toml +++ b/randomizer/cosmetics/Cargo.toml @@ -14,3 +14,4 @@ ruzstd = "0.9.1" [dev-dependencies] ootrr-game = { path = "../game", features = ["fixtures"] } +mimalloc = { version = "0.1", default-features = false } diff --git a/randomizer/cosmetics/src/lib.rs b/randomizer/cosmetics/src/lib.rs index b37707b..7f2f546 100644 --- a/randomizer/cosmetics/src/lib.rs +++ b/randomizer/cosmetics/src/lib.rs @@ -17,3 +17,7 @@ pub mod cosmetic; pub mod sfx; pub mod slink; pub mod title_logo; + +#[cfg(test)] +#[global_allocator] +static GLOBAL: mimalloc::MiMalloc = mimalloc::MiMalloc; diff --git a/randomizer/cosmetics/src/sfx.rs b/randomizer/cosmetics/src/sfx.rs index 0faedc1..6c7ea58 100644 --- a/randomizer/cosmetics/src/sfx.rs +++ b/randomizer/cosmetics/src/sfx.rs @@ -571,9 +571,8 @@ mod tests { let Some(root) = std::env::var("OOTRR_ROMFS").map(PB::from).ok().filter(|p| p.join("Pack").join("ZsDic.pack.zs").is_file()) else { return; }; - let mut rf: Romfs = Romfs::open(&root).unwrap(); - let mut maps: Vec> = Vec::new(); - for seed in ["first", "second"] { + let check = |seed: &str| -> BM { + let mut rf: Romfs = Romfs::open(&root).unwrap(); let out: PB = std::env::temp_dir().join(format!("ootrr_sfx_{}_{seed}", std::process::id())); let _ = fs::remove_dir_all(&out); let notes: Vec = apply(&out, &root, Sfx::Grouped, seed).unwrap(); @@ -615,9 +614,14 @@ mod tests { assert_eq!(checked.len(), streams.len()); println!("{seed}: {} streamed and {shorts} short bank entries changed", streams.len()); assert!(shorts > 2000, "{shorts}"); - maps.push(streams); let _ = fs::remove_dir_all(&out); - } + streams + }; + let check = ✓ + let maps: Vec> = std::thread::scope(|sc| { + let hs: Vec>> = ["first", "second"].into_iter().map(|seed| sc.spawn(move || check(seed))).collect(); + hs.into_iter().map(|h| h.join().unwrap()).collect() + }); assert!(maps[0].len() > 1000); assert_ne!(maps[0], maps[1]); } diff --git a/randomizer/game/src/byml.rs b/randomizer/game/src/byml.rs index 7455761..5abd3e2 100644 --- a/randomizer/game/src/byml.rs +++ b/randomizer/game/src/byml.rs @@ -214,37 +214,32 @@ impl<'a> Rd<'a> { } } -struct Wr { +struct Wr<'a> { out: Vec, be: bool, - keys: Vec, - strs: Vec, + keys: Vec<&'a str>, + strs: Vec<&'a str>, wides: HashMap<(u8, u64), u32>, - seen: HashMap, u32>, + ids: Ids, + seen: HashMap, } -fn fold(s: &str) -> Vec<(u8, char)> { - s.chars() - .map(|c| { - let cls: u8 = if c.is_alphabetic() { - 2 - } else if c.is_ascii_digit() { - 1 - } else { - 0 - }; - (cls, c.to_lowercase().next().unwrap_or(c)) - }) - .collect() +fn fold(s: &str) -> impl Iterator + '_ { + s.chars().map(|c| { + let cls: u8 = if c.is_alphabetic() { + 2 + } else if c.is_ascii_digit() { + 1 + } else { + 0 + }; + (cls, c.to_lowercase().next().unwrap_or(c)) + }) } fn order(m: &[(String, Node)]) -> Vec { let mut ix: Vec = (0..m.len()).collect(); - ix.sort_by(|a, b| { - fold(&m[*a].0) - .cmp(&fold(&m[*b].0)) - .then_with(|| m[*b].0.cmp(&m[*a].0)) - }); + ix.sort_by(|a, b| fold(&m[*a].0).cmp(fold(&m[*b].0)).then_with(|| m[*b].0.cmp(&m[*a].0))); ix } @@ -267,7 +262,7 @@ fn gather(n: &Node, out: &mut Vec<(u8, u64)>) { } } -fn ident(n: &Node, out: &mut Vec) { +fn ident(n: &Node, out: &mut Vec, ids: &mut Ids) { match n { Node::Null => out.push(0xFF), Node::Bool(x) => out.extend_from_slice(&[0xD0, *x as u8]), @@ -306,28 +301,46 @@ fn ident(n: &Node, out: &mut Vec) { out.extend_from_slice(d); } Node::Arr(a) => { - out.push(0xC0); - out.extend_from_slice(&(a.len() as u32).to_le_bytes()); + let mut key: Vec = vec![0xC0]; + key.extend_from_slice(&(a.len() as u32).to_le_bytes()); for x in a { - ident(x, out); + ident(x, &mut key, ids); } + ids.close(n, key, 0xC0, out); } Node::Map(m) => { - out.push(0xC1); - out.extend_from_slice(&(m.len() as u32).to_le_bytes()); + let mut key: Vec = vec![0xC1]; + key.extend_from_slice(&(m.len() as u32).to_le_bytes()); for e in m { - out.extend_from_slice(&(e.0.len() as u32).to_le_bytes()); - out.extend_from_slice(e.0.as_bytes()); - ident(&e.1, out); + key.extend_from_slice(&(e.0.len() as u32).to_le_bytes()); + key.extend_from_slice(e.0.as_bytes()); + ident(&e.1, &mut key, ids); } + ids.close(n, key, 0xC1, out); } } } -fn collect(n: &Node, keys: &mut BTreeSet, strs: &mut BTreeSet) { +#[derive(Default)] +struct Ids { + of: HashMap<*const Node, u32>, + shapes: HashMap, u32>, +} + +impl Ids { + fn close(&mut self, n: &Node, key: Vec, tag: u8, out: &mut Vec) { + let next: u32 = self.shapes.len() as u32; + let id: u32 = *self.shapes.entry(key).or_insert(next); + self.of.insert(n as *const Node, id); + out.push(tag); + out.extend_from_slice(&id.to_le_bytes()); + } +} + +fn collect<'a>(n: &'a Node, keys: &mut BTreeSet<&'a str>, strs: &mut BTreeSet<&'a str>) { match n { Node::Str(s) => { - strs.insert(s.clone()); + strs.insert(s); } Node::Arr(a) => { for x in a { @@ -336,7 +349,7 @@ fn collect(n: &Node, keys: &mut BTreeSet, strs: &mut BTreeSet) { } Node::Map(m) => { for (k, v) in m { - keys.insert(k.clone()); + keys.insert(k); collect(v, keys, strs); } } @@ -344,7 +357,7 @@ fn collect(n: &Node, keys: &mut BTreeSet, strs: &mut BTreeSet) { } } -impl Wr { +impl Wr<'_> { fn p32(&mut self, v: u32) { let b: [u8; 4] = if self.be { v.to_be_bytes() } else { v.to_le_bytes() }; self.out.extend_from_slice(&b); @@ -384,6 +397,14 @@ impl Wr { } } + fn id(&self, n: &Node) -> Res { + self.ids + .of + .get(&(n as *const Node)) + .copied() + .ok_or(Error::Format("byml: container missing from the shape table")) + } + fn wide(&self, t: u8, v: u64) -> Res { self.wides .get(&(t, v)) @@ -391,7 +412,7 @@ impl Wr { .ok_or(Error::Format("byml: wide value missing from block")) } - fn table(&mut self, v: &[String]) { + fn table(&mut self, v: &[&str]) { let at: usize = self.out.len(); self.out.push(0xC2); self.p24(v.len() as u32); @@ -423,7 +444,7 @@ impl Wr { Node::Str(s) => { let i: usize = self .strs - .binary_search(s) + .binary_search(&s.as_str()) .map_err(|_| Error::Format("byml: string missing from table"))?; Ok((0xA0, i as u32)) } @@ -436,8 +457,7 @@ impl Wr { Ok((0xA1, at)) } Node::Arr(a) => { - let mut id: Vec = Vec::new(); - ident(n, &mut id); + let id: u32 = self.id(n)?; if let Some(at) = self.seen.get(&id) { return Ok((0xC0, *at)); } @@ -466,8 +486,7 @@ impl Wr { if !es.iter().all(|e| names.insert(e.0.as_str())) { return Err(Error::Format("byml: duplicate map key")); } - let mut id: Vec = Vec::new(); - ident(n, &mut id); + let id: u32 = self.id(n)?; if let Some(at) = self.seen.get(&id) { return Ok((0xC1, *at)); } @@ -485,7 +504,7 @@ impl Wr { let e: &(String, Node) = es[i]; let k: usize = self .keys - .binary_search(&e.0) + .binary_search(&e.0.as_str()) .map_err(|_| Error::Format("byml: key missing from table"))?; let (t, v): (u8, u32) = self.emit(&e.1)?; let p: usize = slots + 8 * i; @@ -540,8 +559,8 @@ impl Byml { } pub fn write(&self) -> Res> { - let mut ks: BTreeSet = BTreeSet::new(); - let mut ss: BTreeSet = BTreeSet::new(); + let mut ks: BTreeSet<&str> = BTreeSet::new(); + let mut ss: BTreeSet<&str> = BTreeSet::new(); collect(&self.root, &mut ks, &mut ss); let mut w: Wr = Wr { out: Vec::new(), @@ -549,8 +568,10 @@ impl Byml { keys: ks.into_iter().collect(), strs: ss.into_iter().collect(), wides: HashMap::new(), + ids: Ids::default(), seen: HashMap::new(), }; + ident(&self.root, &mut Vec::new(), &mut w.ids); w.out.extend_from_slice(if self.big { b"BY" } else { b"YB" }); let v: [u8; 2] = if self.big { self.version.to_be_bytes() } else { self.version.to_le_bytes() }; w.out.extend_from_slice(&v); @@ -561,12 +582,12 @@ impl Byml { let mut so: u32 = 0; if !w.keys.is_empty() { ko = w.out.len() as u32; - let k: Vec = w.keys.clone(); + let k: Vec<&str> = w.keys.clone(); w.table(&k); } if !w.strs.is_empty() { so = w.out.len() as u32; - let s: Vec = w.strs.clone(); + let s: Vec<&str> = w.strs.clone(); w.table(&s); } w.put_wides(&self.root);