Skip to content

Commit

Permalink
Multiple small improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
Namaneo committed Aug 6, 2023
1 parent 1741947 commit ecc36a0
Show file tree
Hide file tree
Showing 7 changed files with 41 additions and 54 deletions.
3 changes: 2 additions & 1 deletion app/GNUmakefile
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@ LDFLAGS := \
-lwasi-emulated-mman \
-mexec-model=reactor -Wl,--no-entry \
$(EXPORTS) -Wl,--export=calloc,--export=free \
-Wl,--initial-memory=$$((450 * 1024 * 1024)) \
-Wl,--initial-memory=$$((100 * 1024 * 1024)) \
-Wl,--max-memory=$$((600 * 1024 * 1024)) \
-Wl,--import-memory,--export-memory

ifeq ($(DEBUG), 1)
Expand Down
6 changes: 3 additions & 3 deletions ui/sources/services/core.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,12 @@ const fs = `

export default class Core {
/** @type {number} */
static #INITIAL_MEMORY = 450 * 1024 * 1024;
static #INITIAL_MEMORY = 100 * 1024 * 1024;

/** @type {WebAssembly.Memory} */
static #memory = new WebAssembly.Memory({
initial: this.#INITIAL_MEMORY / 65536,
maximum: this.#INITIAL_MEMORY / 65536,
initial: (this.#INITIAL_MEMORY * 1) / 65536,
maximum: (this.#INITIAL_MEMORY * 6) / 65536,
shared: true,
});

Expand Down
2 changes: 1 addition & 1 deletion ui/sources/services/files.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ export default class Files {

static async #fs() {
if (!this.#filesystem)
this.#filesystem = await Parallel.create(Filesystem, false);
this.#filesystem = await Parallel.create('Filesystem', Filesystem, false);
return this.#filesystem;
}

Expand Down
26 changes: 0 additions & 26 deletions ui/sources/services/interop.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,6 @@ export default class Interop {
/** @type {Worker} */
static #worker = null;

/** @type {Worker[]} */
static #threads = [];

/** @type {Worker} */
static get worker() { return this.#worker; }

Expand All @@ -26,29 +23,9 @@ export default class Interop {
this.#memory = memory;
this.#worker = new Worker('worker.js', { name, type: 'module' });

Caller.receive(Interop.worker, Interop);
await Caller.call(Interop.worker, 'init', memory);
}

/**
* @param {number} start_arg
* @param {Int32Array} sync
* @returns {number}
*/
static async spawn(start_arg) {
const id = this.#threads.length + 1;

const name = `${this.#name}-${id}`;
const worker = new Worker('worker.js', { name, type: 'module' });

Caller.receive(worker, Interop);
Caller.call(worker, 'init', this.#memory, start_arg);

this.#threads.push(worker);

return id;
}

/**
* @returns {Promise<void>}
*/
Expand All @@ -57,9 +34,6 @@ export default class Interop {

this.#worker.terminate();
this.#worker = null;

this.#threads.forEach(thread => thread.terminate());
this.#threads = [];
}

static Core = class {
Expand Down
23 changes: 12 additions & 11 deletions ui/sources/services/parallel.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,34 +51,37 @@ export default class Parallel {

/**
* @template T
* @param {string} name
* @param {new() => T} cls
* @param {boolean} sync
* @returns {Promise<T>}
*/
static async create(cls, sync) {
const parallel = new Parallel();

static async create(name, cls, sync) {
const script = `
const TYPE_NUMBER = ${TYPE_NUMBER};
const TYPE_STRING = ${TYPE_STRING};
const TYPE_OBJECT = ${TYPE_OBJECT};
const context = new (${cls});
onmessage = ${thread};
`;

const parallel = new Parallel();
const blob = new Blob([script], { type: 'text/javascript' });
parallel.#worker = new Worker(URL.createObjectURL(blob), { name: 'Parallel' });
parallel.#worker = new Worker(URL.createObjectURL(blob), { name });

await parallel.#call('-ready-', [], false);

const instance = new cls();
parallel.#proxy = new Proxy(instance, {
get: (_, name) => {
if (instance[name])
return function() { return parallel.#call(name, arguments, [], sync); };
return function() { return parallel.#call(name, arguments, sync); };
return Reflect.get(instance, ...arguments);
}
});

Parallel.#instances.push(parallel);
await parallel.#call('-ready-', [], [], false);

return parallel.#proxy;
}

Expand Down Expand Up @@ -110,17 +113,15 @@ export default class Parallel {
/**
* @param {string} name
* @param {Array} args
* @param {Array} transfer
* @returns {number | Promise<number>}
* @returns {any | Promise<any>}
*/
#call(name, args, transfer, sync) {
#call(name, args, sync) {
if (!args) args = [];
if (!transfer) transfer = [];

const sab = new Int32Array(new SharedArrayBuffer(12, { maxByteLength: 4096 }));

const message = { name, args: [sab, ...args] };
this.#worker.postMessage(message, transfer);
this.#worker.postMessage(message);

if (sync) {
Atomics.wait(sab, 0, 0);
Expand Down
18 changes: 9 additions & 9 deletions ui/sources/services/wasi.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ const WHENCE_END = 2;
const O_CREAT = 9;

export const JUN = {
/** @type {SharedArrayBuffer} */
/** @type {WebAssembly.Memory} */
memory: null,

/** @type {Filesystem} */
Expand All @@ -35,39 +35,39 @@ export const JUN = {
* @returns {number}
*/
get_uint32: (ptr) => {
return new DataView(JUN.memory).getUint32(ptr, true);
return new DataView(JUN.memory.buffer).getUint32(ptr, true);
},

/**
* @param {number} ptr
* @param {number} value
*/
set_uint32: (ptr, value) => {
new DataView(JUN.memory).setUint32(ptr, value, true);
new DataView(JUN.memory.buffer).setUint32(ptr, value, true);
},

/**
* @param {number} ptr
* @returns {number}
*/
get_uint64: (ptr) => {
return new DataView(JUN.memory).getBigUint64(ptr, true);
return new DataView(JUN.memory.buffer).getBigUint64(ptr, true);
},

/**
* @param {number} ptr
* @param {number} value
*/
set_uint64: (ptr, value) => {
new DataView(JUN.memory).setBigUint64(ptr, BigInt(value), true);
new DataView(JUN.memory.buffer).setBigUint64(ptr, BigInt(value), true);
},

/**
* @param {number} ptr
* @returns {string}
*/
str_to_js: (ptr) => {
const buf = new Uint8Array(JUN.memory, ptr);
const buf = new Uint8Array(JUN.memory.buffer, ptr);
let length = 0; for (; buf[length] != 0; length++);
return new TextDecoder().decode(buf.slice(0, length));
},
Expand All @@ -78,7 +78,7 @@ export const JUN = {
*/
str_to_c: (ptr, str) => {
const buf = new TextEncoder().encode(str);
new Uint8Array(JUN.memory, ptr).set([...buf, 0]);
new Uint8Array(JUN.memory.buffer, ptr).set([...buf, 0]);
},
}

Expand Down Expand Up @@ -141,7 +141,7 @@ export const WASI_ENV = {
const buf_len = JUN.get_uint32(ptr + 4);
const len = buf_len < size - offset ? buf_len : size - offset;

const sab = new Uint8Array(JUN.memory, buf_ptr, len);
const sab = new Uint8Array(JUN.memory.buffer, buf_ptr, len);
JUN.filesystem.read(JUN.fds[fd].path, sab, offset);

offset += len;
Expand Down Expand Up @@ -195,7 +195,7 @@ export const WASI_ENV = {
const buf_ptr = JUN.get_uint32(ptr);
const buf_len = JUN.get_uint32(ptr + 4);

write(new Uint8Array(JUN.memory, buf_ptr, buf_len), offset);
write(new Uint8Array(JUN.memory.buffer, buf_ptr, buf_len), offset);

offset += buf_len;
}
Expand Down
17 changes: 14 additions & 3 deletions ui/sources/worker.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@ class Core {
/** @type {WebAssembly.Instance} */
#instance = null;

/** @type {Worker[]} */
#threads = [];

/**
* @param {string} name
*/
Expand Down Expand Up @@ -99,15 +102,23 @@ class Core {
* @returns {Promise<void>}
*/
async init(memory, start_arg) {
JUN.memory = memory.buffer;
JUN.filesystem = await Parallel.create(Filesystem, true);
JUN.memory = memory;
JUN.filesystem = await Parallel.create('Filesystem', Filesystem, true);

const origin = location.origin + location.pathname.substring(0, location.pathname.lastIndexOf('/'));
const source = await WebAssembly.instantiateStreaming(fetch(`${origin}/modules/lib${this.#name}.wasm`), {
env: { memory },
wasi_snapshot_preview1: WASI_ENV,
wasi: { 'thread-spawn': (start_arg) => {
return Caller.callSync(self, 'spawn', start_arg);
const id = this.#threads.length + 1;
const name = `${this.#name}-${id}`;
const worker = new Worker('worker.js', { name, type: 'module' });

Caller.call(worker, 'init', memory, start_arg);

this.#threads.push(worker);

return id;
}},
});

Expand Down

0 comments on commit ecc36a0

Please sign in to comment.