Skip to content

Commit f61de99

Browse files
committed
ceval: Avoid large MAXIMUM_MEMORY if it cannot grow anyway
1 parent 30e16de commit f61de99

File tree

2 files changed

+18
-12
lines changed

2 files changed

+18
-12
lines changed

ui/ceval/src/ctrl.ts

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,19 @@ import throttle from 'common/throttle';
77
import { povChances } from './winningChances';
88
import { sanIrreversible } from './util';
99

10-
function sharedWasmMemory(initial: number, maximum: number): WebAssembly.Memory | undefined {
10+
function sharedWasmMemory(initial: number, maximum: number): WebAssembly.Memory {
11+
return new WebAssembly.Memory({ shared: true, initial, maximum } as WebAssembly.MemoryDescriptor);
12+
}
13+
14+
function sendableSharedWasmMemory(initial: number, maximum: number): WebAssembly.Memory | undefined {
1115
// Atomics
1216
if (typeof Atomics !== 'object') return;
1317

1418
// SharedArrayBuffer
1519
if (typeof SharedArrayBuffer !== 'function') return;
1620

1721
// Shared memory
18-
const mem = new WebAssembly.Memory({ shared: true, initial, maximum } as WebAssembly.MemoryDescriptor);
22+
const mem = sharedWasmMemory(initial, maximum);
1923
if (!(mem.buffer instanceof SharedArrayBuffer)) return;
2024

2125
// Structured cloning
@@ -51,7 +55,7 @@ export default function (opts: CevalOpts): CevalCtrl {
5155
const source = Uint8Array.from([0, 97, 115, 109, 1, 0, 0, 0]);
5256
if (typeof WebAssembly === 'object' && typeof WebAssembly.validate === 'function' && WebAssembly.validate(source)) {
5357
technology = 'wasm'; // WebAssembly 1.0
54-
const sharedMem = sharedWasmMemory(8, 16);
58+
const sharedMem = sendableSharedWasmMemory(8, 16);
5559
if (sharedMem) {
5660
technology = 'hce';
5761

@@ -98,7 +102,7 @@ export default function (opts: CevalOpts): CevalCtrl {
98102
const hovering = prop<Hovering | null>(null);
99103
const isDeeper = prop(false);
100104

101-
const workerOpts = {
105+
const protocolOpts = {
102106
minDepth,
103107
variant: opts.variant.key,
104108
threads: (technology == 'hce' || technology == 'nnue') && (() => Math.min(parseInt(threads()), maxThreads)),
@@ -218,24 +222,24 @@ export default function (opts: CevalOpts): CevalCtrl {
218222

219223
if (!worker) {
220224
if (technology == 'nnue')
221-
worker = new ThreadedWasmWorker(workerOpts, {
225+
worker = new ThreadedWasmWorker(protocolOpts, {
222226
baseUrl: 'vendor/stockfish-nnue.wasm/',
223227
module: 'Stockfish',
224228
downloadProgress: throttle(200, mb => {
225229
downloadProgress(mb);
226230
opts.redraw();
227231
}),
228232
version: '2732f2',
233+
wasmMemory: sharedWasmMemory(2048, growableSharedMem ? 32768 : 2048),
229234
});
230235
else if (technology == 'hce')
231-
worker = new ThreadedWasmWorker(
232-
workerOpts,
233-
officialStockfish(opts.variant.key)
234-
? { baseUrl: 'vendor/stockfish.wasm/', module: 'Stockfish' }
235-
: { baseUrl: 'vendor/stockfish-mv.wasm/', module: 'StockfishMv' }
236-
);
236+
worker = new ThreadedWasmWorker(protocolOpts, {
237+
baseUrl: officialStockfish(opts.variant.key) ? 'vendor/stockfish.wasm/' : 'vendor/stockfish-mv.wasm/',
238+
module: officialStockfish(opts.variant.key) ? 'Stockfish' : 'StockfishMv',
239+
wasmMemory: sharedWasmMemory(1024, growableSharedMem ? 32768 : 1088),
240+
});
237241
else
238-
worker = new WebWorker(workerOpts, {
242+
worker = new WebWorker(protocolOpts, {
239243
url: technology == 'wasm' ? 'vendor/stockfish.js/stockfish.wasm.js' : 'vendor/stockfish.js/stockfish.js',
240244
});
241245
}

ui/ceval/src/worker.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ export interface ThreadedWasmWorkerOpts {
6565
module: 'Stockfish' | 'StockfishMv';
6666
version?: string;
6767
downloadProgress?: (mb: number) => void;
68+
wasmMemory: WebAssembly.Memory;
6869
}
6970

7071
export class ThreadedWasmWorker extends AbstractWorker<ThreadedWasmWorkerOpts> {
@@ -97,6 +98,7 @@ export class ThreadedWasmWorker extends AbstractWorker<ThreadedWasmWorkerOpts> {
9798
wasmBinary,
9899
locateFile: (path: string) =>
99100
lichess.assetUrl(this.opts.baseUrl + path, { version, sameDomain: path.endsWith('.worker.js') }),
101+
wasmMemory: this.opts.wasmMemory,
100102
})
101103
)
102104
.then((sf: any) => {

0 commit comments

Comments
 (0)