begin to incorporate actual blockfish library into webworker

This commit is contained in:
milo 2024-03-05 19:59:14 -05:00
parent c66cf45294
commit c6a20e2bcd
6 changed files with 202 additions and 5 deletions

View File

@ -2,6 +2,30 @@
# It is not intended for manual editing. # It is not intended for manual editing.
version = 3 version = 3
[[package]]
name = "ahash"
version = "0.8.11"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e89da841a80418a9b391ebaea17f5c112ffaaa96f621d2c285b5174da76b9011"
dependencies = [
"cfg-if 1.0.0",
"once_cell",
"version_check",
"zerocopy",
]
[[package]]
name = "allocator-api2"
version = "0.2.16"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0942ffc6dcaadf03badf6e6a2d0228460359d5e34b57ccdc720b7382dfbd5ec5"
[[package]]
name = "anyhow"
version = "1.0.80"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5ad32ce52e4161730f7098c077cd2ed6229b5804ccf99e5366be1ab72a98b4e1"
[[package]] [[package]]
name = "bumpalo" name = "bumpalo"
version = "3.15.3" version = "3.15.3"
@ -20,15 +44,44 @@ version = "1.0.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd" checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd"
[[package]]
name = "fish"
version = "0.1.0"
dependencies = [
"ahash",
"bumpalo",
"hashbrown",
"mino",
"smallvec",
"tracing",
]
[[package]] [[package]]
name = "fish-webworker" name = "fish-webworker"
version = "0.1.0" version = "0.1.0"
dependencies = [ dependencies = [
"fish",
"wasm-bindgen", "wasm-bindgen",
"wasm-bindgen-macro", "wasm-bindgen-macro",
"wee_alloc", "wee_alloc",
] ]
[[package]]
name = "hashbrown"
version = "0.14.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "290f1a1d9242c78d09ce40a5e87e7554ee637af1351968159f4952f028f75604"
dependencies = [
"ahash",
"allocator-api2",
]
[[package]]
name = "itoa"
version = "1.0.10"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b1a46d1a171d865aa5f83f92695765caa047a9b4cbae2cbf37dbd613a793fd4c"
[[package]] [[package]]
name = "libc" name = "libc"
version = "0.2.153" version = "0.2.153"
@ -47,12 +100,34 @@ version = "0.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8452105ba047068f40ff7093dd1d9da90898e63dd61736462e9cdda6a90ad3c3" checksum = "8452105ba047068f40ff7093dd1d9da90898e63dd61736462e9cdda6a90ad3c3"
[[package]]
name = "mino"
version = "0.1.0"
dependencies = [
"mino-code-gen",
]
[[package]]
name = "mino-code-gen"
version = "0.1.0"
dependencies = [
"anyhow",
"serde",
"serde_json",
]
[[package]] [[package]]
name = "once_cell" name = "once_cell"
version = "1.19.0" version = "1.19.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3fdb12b2476b595f9358c5161aa467c2438859caa136dec86c26fdd2efe17b92" checksum = "3fdb12b2476b595f9358c5161aa467c2438859caa136dec86c26fdd2efe17b92"
[[package]]
name = "pin-project-lite"
version = "0.2.13"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8afb450f006bf6385ca15ef45d71d2288452bc3683ce2e2cacc0d18e4be60b58"
[[package]] [[package]]
name = "proc-macro2" name = "proc-macro2"
version = "1.0.78" version = "1.0.78"
@ -71,6 +146,49 @@ dependencies = [
"proc-macro2", "proc-macro2",
] ]
[[package]]
name = "ryu"
version = "1.0.17"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e86697c916019a8588c99b5fac3cead74ec0b4b819707a682fd4d23fa0ce1ba1"
[[package]]
name = "serde"
version = "1.0.197"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3fb1c873e1b9b056a4dc4c0c198b24c3ffa059243875552b2bd0933b1aee4ce2"
dependencies = [
"serde_derive",
]
[[package]]
name = "serde_derive"
version = "1.0.197"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7eb0b34b42edc17f6b7cac84a52a1c5f0e1bb2227e997ca9011ea3dd34e8610b"
dependencies = [
"proc-macro2",
"quote",
"syn",
]
[[package]]
name = "serde_json"
version = "1.0.114"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c5f09b1bd632ef549eaa9f60a1f8de742bdbc698e6cee2095fc84dde5f549ae0"
dependencies = [
"itoa",
"ryu",
"serde",
]
[[package]]
name = "smallvec"
version = "1.13.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e6ecd384b10a64542d77071bd64bd7b231f4ed5940fba55e98c3de13824cf3d7"
[[package]] [[package]]
name = "syn" name = "syn"
version = "2.0.50" version = "2.0.50"
@ -82,12 +200,34 @@ dependencies = [
"unicode-ident", "unicode-ident",
] ]
[[package]]
name = "tracing"
version = "0.1.40"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c3523ab5a71916ccf420eebdf5521fcef02141234bbc0b8a49f2fdc4544364ef"
dependencies = [
"pin-project-lite",
"tracing-core",
]
[[package]]
name = "tracing-core"
version = "0.1.32"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c06d3da6113f116aaee68e4d601191614c9053067f9ab7f6edbcb161237daa54"
[[package]] [[package]]
name = "unicode-ident" name = "unicode-ident"
version = "1.0.12" version = "1.0.12"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3354b9ac3fae1ff6755cb6db53683adb661634f67557942dea4facebec0fee4b" checksum = "3354b9ac3fae1ff6755cb6db53683adb661634f67557942dea4facebec0fee4b"
[[package]]
name = "version_check"
version = "0.9.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "49874b5167b65d7193b8aba1567f5c7d93d001cafc34600cee003eda787e483f"
[[package]] [[package]]
name = "wasm-bindgen" name = "wasm-bindgen"
version = "0.2.91" version = "0.2.91"
@ -175,3 +315,23 @@ name = "winapi-x86_64-pc-windows-gnu"
version = "0.4.0" version = "0.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "712e227841d057c1ee1cd2fb22fa7e5a5461ae8e48fa2ca79ec42cfc1931183f" checksum = "712e227841d057c1ee1cd2fb22fa7e5a5461ae8e48fa2ca79ec42cfc1931183f"
[[package]]
name = "zerocopy"
version = "0.7.32"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "74d4d3961e53fa4c9a25a8637fc2bfaf2595b3d3ae34875568a5cf64787716be"
dependencies = [
"zerocopy-derive",
]
[[package]]
name = "zerocopy-derive"
version = "0.7.32"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9ce1b18ccd8e73a9321186f97e46f9f04b778851177567b1975109d26a08d2a6"
dependencies = [
"proc-macro2",
"quote",
"syn",
]

View File

@ -8,7 +8,7 @@ edition = "2021"
crate-type = ["cdylib"] crate-type = ["cdylib"]
[dependencies] [dependencies]
#fish = { path = "../fish" } fish = { path = "../fish" }
wasm-bindgen = "0.2" wasm-bindgen = "0.2"
wasm-bindgen-macro = "0.2" wasm-bindgen-macro = "0.2"

View File

@ -5,7 +5,7 @@
"main": "blockfish.js", "main": "blockfish.js",
"scripts": { "scripts": {
"prebuild": "cargo build --release && wasm-bindgen target/wasm32-unknown-unknown/release/fish_webworker.wasm --out-dir build --target web --no-typescript", "prebuild": "cargo build --release && wasm-bindgen target/wasm32-unknown-unknown/release/fish_webworker.wasm --out-dir build --target web --no-typescript",
"build": "npx esbuild src/worker.js --bundle --outdir=build", "build": "npx esbuild src/worker.js --bundle --minify --outdir=build",
"postbuild": "cp build/fish_webworker_bg.wasm ./blockfish.wasm && cp build/worker.js ./blockfish.js" "postbuild": "cp build/fish_webworker_bg.wasm ./blockfish.wasm && cp build/worker.js ./blockfish.js"
}, },
"author": "iitalics", "author": "iitalics",

View File

@ -1,13 +1,41 @@
use std::{cell::RefCell, mem::MaybeUninit};
use wasm_bindgen_macro::wasm_bindgen; use wasm_bindgen_macro::wasm_bindgen;
use fish::Weights;
#[global_allocator] #[global_allocator]
static ALLOC: wee_alloc::WeeAlloc = wee_alloc::WeeAlloc::INIT; static ALLOC: wee_alloc::WeeAlloc = wee_alloc::WeeAlloc::INIT;
struct State {
weights: Weights,
max_iters: u32,
}
static mut STATE: MaybeUninit<RefCell<State>> = MaybeUninit::uninit();
fn state() -> &'static RefCell<State> {
unsafe { STATE.assume_init_ref() }
}
#[wasm_bindgen(start)] #[wasm_bindgen(start)]
fn start() { fn start() {
let init_state = State {
weights: Weights::default(),
max_iters: 10_000,
};
unsafe { STATE.write(RefCell::new(init_state)) };
} }
#[wasm_bindgen] #[wasm_bindgen]
pub fn get_version() -> Result<String, String> { pub fn get_version() -> String {
Ok("0.0.x".to_owned()) "0.0.x".to_owned()
}
#[wasm_bindgen]
pub fn get_config() -> Vec<i32> {
let state = state().borrow();
let mut config = Vec::from(state.weights.0);
config.push(state.max_iters as i32);
config
} }

View File

@ -1,6 +1,7 @@
import { import {
default as init, default as init,
get_version as getVersion, get_version as getVersion,
get_config as getConfig,
} from '../build/fish_webworker.js' } from '../build/fish_webworker.js'
let initPromise = init('/blockfish.wasm'); let initPromise = init('/blockfish.wasm');
@ -8,7 +9,12 @@ let initPromise = init('/blockfish.wasm');
async function handle(msg) { async function handle(msg) {
switch (msg.type) { switch (msg.type) {
case 'init': case 'init':
return { version: getVersion() }; {
let version = getVersion();
let weights = Array.from(getConfig());
let maxIters = weights.pop();
return { version, config: { weights, maxIters } };
}
default: default:
throw `Unrecognized message "${msg.type}"`; throw `Unrecognized message "${msg.type}"`;

View File

@ -10,3 +10,6 @@ type HashMap<K, V> = hashbrown::HashMap<K, V, HashBuilder>;
type HashSet<T> = hashbrown::HashSet<T, HashBuilder>; type HashSet<T> = hashbrown::HashSet<T, HashBuilder>;
type HashBuilder = core::hash::BuildHasherDefault<ahash::AHasher>; type HashBuilder = core::hash::BuildHasherDefault<ahash::AHasher>;
type Arena = bumpalo::Bump; type Arena = bumpalo::Bump;
pub use bot::{Bot, Metrics};
pub use eval::Weights;