Shrink the webassembly binary down to a reasonable size; TODO actually test it out
This commit is contained in:
parent
55b04efa85
commit
febec0cf10
|
@ -8,8 +8,8 @@ crate-type = ["cdylib"]
|
||||||
bench = false
|
bench = false
|
||||||
|
|
||||||
[profile.release]
|
[profile.release]
|
||||||
debug = true
|
|
||||||
lto = true
|
lto = true
|
||||||
|
opt-level = 's'
|
||||||
|
|
||||||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||||
|
|
||||||
|
|
|
@ -1,22 +1,52 @@
|
||||||
|
use std::alloc::{alloc, dealloc, Layout};
|
||||||
|
|
||||||
extern crate wee_alloc;
|
extern crate wee_alloc;
|
||||||
|
|
||||||
#[global_allocator]
|
#[global_allocator]
|
||||||
static ALLOC: wee_alloc::WeeAlloc = wee_alloc::WeeAlloc::INIT;
|
static ALLOC: wee_alloc::WeeAlloc = wee_alloc::WeeAlloc::INIT;
|
||||||
|
|
||||||
|
|
||||||
// webassembly version of all the wordle logic
|
// webassembly version of all the wordle logic
|
||||||
// because the javascript version is slow as hell on firefox
|
// because the javascript version is slow as hell on firefox
|
||||||
use std::os::raw::c_char;
|
use std::os::raw::c_char;
|
||||||
|
|
||||||
static mut LOOKUP: Option<Vec<u8>> = None;
|
static mut LOOKUP: Option<&'static mut [u8]> = None;
|
||||||
static mut STRINGS: Option<Vec<u8>> = None;
|
static mut STRINGS: Option<&'static mut [u8]> = None;
|
||||||
static mut STR_IDXS: Option<Vec<*const c_char>> = None;
|
static mut STR_IDXS: Option<&'static mut [*const c_char]> = None;
|
||||||
|
|
||||||
|
fn unwrap_or_abort<T>(v: Option<T>) -> T {
|
||||||
|
match v {
|
||||||
|
Some(v) => v,
|
||||||
|
None => std::process::abort(),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
unsafe fn alloc_ary<T>(sz: usize) -> &'static mut [T] {
|
||||||
|
let layout = unwrap_or_abort(Layout::from_size_align(
|
||||||
|
sz*std::mem::size_of::<T>(),
|
||||||
|
std::mem::align_of::<T>()
|
||||||
|
).ok());
|
||||||
|
let ptr = alloc(layout) as *mut T;
|
||||||
|
std::slice::from_raw_parts_mut(ptr, sz)
|
||||||
|
}
|
||||||
|
|
||||||
|
unsafe fn dealloc_ary<T>(v: &'static mut [T]) {
|
||||||
|
let layout = unwrap_or_abort(Layout::from_size_align(
|
||||||
|
v.len()*std::mem::size_of::<T>(),
|
||||||
|
std::mem::align_of::<T>()
|
||||||
|
).ok());
|
||||||
|
dealloc(v.as_ptr() as *mut u8, layout);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
#[no_mangle]
|
#[no_mangle]
|
||||||
pub extern fn init_strings(sz: usize) -> *mut c_char {
|
pub extern fn init_strings(sz: usize) -> *mut c_char {
|
||||||
unsafe {
|
unsafe {
|
||||||
STRINGS = Some(vec![0u8; sz]);
|
if let Some(v) = &mut STRINGS {
|
||||||
STRINGS.as_deref_mut().map(|v| v.as_mut_ptr() as *mut c_char).unwrap()
|
dealloc_ary(v);
|
||||||
|
STRINGS = None;
|
||||||
|
}
|
||||||
|
STRINGS = Some(alloc_ary(sz));
|
||||||
|
unwrap_or_abort(STRINGS.as_deref_mut().map(|v| v.as_mut_ptr() as *mut c_char))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -30,9 +60,14 @@ pub extern fn init_idx() -> usize {
|
||||||
pub extern fn init_lookup() -> *mut c_char {
|
pub extern fn init_lookup() -> *mut c_char {
|
||||||
unsafe {
|
unsafe {
|
||||||
let num_strings = STR_IDXS.as_ref().map(|idxs| idxs.len()).unwrap_or(0);
|
let num_strings = STR_IDXS.as_ref().map(|idxs| idxs.len()).unwrap_or(0);
|
||||||
|
if let Some(v) = &mut LOOKUP {
|
||||||
|
dealloc_ary(v);
|
||||||
|
STRINGS = None;
|
||||||
|
}
|
||||||
|
|
||||||
LOOKUP = Some(vec![0u8; num_strings*num_strings]);
|
LOOKUP = Some(alloc_ary(num_strings*num_strings));
|
||||||
LOOKUP.as_deref_mut().map(|v| v.as_mut_ptr() as *mut c_char).unwrap()
|
unwrap_or_abort(
|
||||||
|
LOOKUP.as_deref_mut().map(|v| v.as_mut_ptr() as *mut c_char))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue