mindustry logic execution, map- and schematic- parsing and rendering
use a perfect hash map
bendn 2023-07-29
parent c6f4975 · commit 1a86c06
-rw-r--r--Cargo.toml4
-rw-r--r--build.rs5
-rw-r--r--src/data/renderer.rs37
3 files changed, 22 insertions, 24 deletions
diff --git a/Cargo.toml b/Cargo.toml
index ebf6044..4c62287 100644
--- a/Cargo.toml
+++ b/Cargo.toml
@@ -1,6 +1,6 @@
[package]
name = "mindus"
-version = "1.4.5"
+version = "1.4.6"
edition = "2021"
description = "A library for working with mindustry data formats (eg schematics and maps) (fork of plandustry)"
authors = [
@@ -25,7 +25,7 @@ fast_image_resize = "2.7"
thiserror = "1.0"
bobbin-bits = "0.1"
blurslice = { version = "0.1", optional = true }
-dashmap = { version = "5.5", features = ["inline"] }
+phf = { version = "0.11", features = ["macros"] }
[features]
schem_shadow = ["dep:blurslice"]
diff --git a/build.rs b/build.rs
index ab1cc61..f772a82 100644
--- a/build.rs
+++ b/build.rs
@@ -15,8 +15,7 @@ fn main() {
let o = std::env::var("OUT_DIR").unwrap();
let mut f = File::create(Path::new(&o).join("asset")).unwrap();
let mut n = 1usize;
- f.write_all(b"fn put(map: &DashMap<String, RgbaImage>) {")
- .unwrap();
+ f.write_all(b"phf::phf_map! {").unwrap();
let mut s = String::new(); // idk write_all / write wasnt working
for e in walkdir.into_iter().filter_map(|e| e.ok()) {
let path = e.path();
@@ -29,7 +28,7 @@ fn main() {
let mut f = File::create(Path::new(&o).join(n.to_string())).unwrap();
f.write_all(&p.into_raw()).unwrap();
println!("writing {path:?}");
- s+= &format!("\tmap.insert(String::from(\"{path}\"), RgbaImage::from_vec({x}, {y}, include_bytes!(concat!(env!(\"OUT_DIR\"), \"/{n}\")).to_vec()).unwrap());\n");
+ s += &format!("\t\"{path}\" => r!(LazyLock::new(|| RgbaImage::from_vec({x}, {y}, include_bytes!(concat!(env!(\"OUT_DIR\"), \"/{n}\")).to_vec()).unwrap())),\n");
n += 1;
}
}
diff --git a/src/data/renderer.rs b/src/data/renderer.rs
index cfd9c9b..b1b2e0a 100644
--- a/src/data/renderer.rs
+++ b/src/data/renderer.rs
@@ -1,31 +1,30 @@
//! schematic drawing
-use dashmap::mapref::one::Ref;
-use dashmap::DashMap;
-pub(crate) use image::{DynamicImage, RgbaImage};
-use std::ops::{Deref, DerefMut};
-use std::sync::LazyLock;
-
pub(crate) use super::autotile::*;
use crate::block::environment::METAL_FLOOR;
use crate::block::Rotation;
use crate::team::SHARDED;
pub(crate) use crate::utils::ImageUtils;
use crate::Map;
+pub(crate) use image::{DynamicImage, RgbaImage};
pub(crate) use std::borrow::{Borrow, BorrowMut};
+use std::ops::{Deref, DerefMut};
+use std::sync::LazyLock;
use super::schematic::Schematic;
use super::GridPos;
-type Cache = DashMap<String, RgbaImage>;
-include!(concat!(env!("OUT_DIR"), "/asset")); // put function from here
-static CACHE: LazyLock<Cache> = LazyLock::new(|| {
- let mut map = Cache::new();
- put(&mut map);
- map
-});
+macro_rules! r {
+ ($v:expr) => {{
+ static TMP: LazyLock<RgbaImage> = $v;
+ &TMP
+ }};
+}
+
+type Cache = phf::Map<&'static str, &'static LazyLock<RgbaImage>>;
+static CACHE: Cache = include!(concat!(env!("OUT_DIR"), "/asset"));
pub enum ImageHolder {
- Borrow(Ref<'static, String, RgbaImage>),
+ Borrow(&'static RgbaImage),
Own(RgbaImage),
}
@@ -50,7 +49,7 @@ impl Borrow<RgbaImage> for ImageHolder {
fn borrow(&self) -> &RgbaImage {
match self {
Self::Own(x) => x,
- Self::Borrow(x) => x.value(),
+ Self::Borrow(x) => x,
}
}
}
@@ -80,8 +79,8 @@ impl DerefMut for ImageHolder {
}
}
-impl From<Ref<'static, String, RgbaImage>> for ImageHolder {
- fn from(value: Ref<'static, String, RgbaImage>) -> Self {
+impl From<&'static RgbaImage> for ImageHolder {
+ fn from(value: &'static RgbaImage) -> Self {
Self::Borrow(value)
}
}
@@ -92,9 +91,9 @@ impl From<RgbaImage> for ImageHolder {
}
}
-pub(crate) fn try_load(name: &str) -> Option<Ref<'static, String, RgbaImage>> {
+pub(crate) fn try_load(name: &str) -> Option<&'static RgbaImage> {
let key = name.to_string();
- CACHE.get(&key)
+ CACHE.get(&key).map(|v| LazyLock::force(v))
}
pub(crate) fn load(name: &str) -> ImageHolder {