Skip to content

Commit

Permalink
Transfer buffers instead of copying them
Browse files Browse the repository at this point in the history
  • Loading branch information
cute-the-niini committed Dec 28, 2023
1 parent 4417268 commit f8eb02d
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 33 deletions.
32 changes: 10 additions & 22 deletions packages/kate-core/source/os/apis/processes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,8 @@ export class KateProcesses {
const cart = await Cart.parse_metadata(blob, this.os.kernel.version);
const has_proper_offset = cart.files.every((x) => x.offset !== null);
const file_map = new Map(cart.files.map((x) => [x.path, x]));
let data_map: Map<string, Cart.DataFile> | null = null;
if (!has_proper_offset) {
data_map = (await Cart.parse_whole(blob, this.os.kernel.version)).file_map;
throw new Error(`Running this cartridge is not supported without installation.`);
}

const storage = await this.os.object_store.cartridge(cart, false).get_local_storage();
Expand All @@ -56,28 +55,17 @@ export class KateProcesses {
filesystem: {
read: async (path) => {
const node = file_map.get(path);
if (node == null) {
if (node == null || node.offset == null) {
throw new Error(`File not found in ${cart.id}: ${path}`);
}
if (data_map != null) {
const data = data_map.get(path)!.data;
return {
path: node.path,
mime: node.mime,
data,
};
} else if (node.offset != null) {
const data = new Uint8Array(
await blob.slice(node.offset, node.offset + node.size).arrayBuffer()
);
return {
path: node.path,
mime: node.mime,
data,
};
} else {
throw new Error(`Cannot read file ${path} in ${cart.id}`);
}
const data = new Uint8Array(
await blob.slice(node.offset, node.offset + node.size).arrayBuffer()
);
return {
path: node.path,
mime: node.mime,
data,
};
},
},
});
Expand Down
4 changes: 2 additions & 2 deletions packages/kate-core/source/os/ipc/cart_fs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,14 @@
* file, You can obtain one at https://mozilla.org/MPL/2.0/.
*/

import { EMessageFailed, handler } from "./handlers";
import { EMessageFailed, WithTransfer, handler } from "./handlers";
import { TC } from "../../utils";

export default [
handler("kate:cart.read-file", TC.spec({ path: TC.str }), async (os, process, ipc, { path }) => {
try {
const file = await process.file_system.read(path);
return { mime: file.mime, bytes: file.data };
return new WithTransfer({ mime: file.mime, bytes: file.data }, [file.data.buffer]);
} catch (error) {
console.error(`[Kate] failed to read file ${path} from ${process.cartridge.id}`);
throw new EMessageFailed("kate.cart-fs.file-not-found", `Failed to read file ${path}`);
Expand Down
4 changes: 2 additions & 2 deletions packages/kate-core/source/os/ipc/device-file.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

import type { KateOS } from "../os";
import { TC, make_id } from "../../utils";
import { EMessageFailed, auth_handler, handler } from "./handlers";
import { EMessageFailed, WithTransfer, auth_handler, handler } from "./handlers";
import { DeviceFileHandle } from "../apis";
import * as UI from "../ui";
import { Process } from "../../kernel";
Expand Down Expand Up @@ -122,7 +122,7 @@ export default [
async (os, env, ipc, { id }) => {
const file = (await device_ipc.resolve(os, env, id)).handle;
const data = await file.arrayBuffer();
return new Uint8Array(data);
return new WithTransfer(new Uint8Array(data), [data]);
}
),
];
4 changes: 4 additions & 0 deletions packages/kate-core/source/os/ipc/handlers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,10 @@ export class EMessageFailed extends Error {
}
}

export class WithTransfer {
constructor(readonly value: unknown, readonly transfer: Transferable[]) {}
}

export function handler<A, B>(
type: string,
parser: Handler<A, B>["parser"],
Expand Down
26 changes: 19 additions & 7 deletions packages/kate-core/source/os/ipc/ipc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
*/

import type { KateOS } from "../os";
import { EMessageFailed, type Handler } from "./handlers";
import { EMessageFailed, WithTransfer, type Handler } from "./handlers";
import { TC, make_id } from "../../utils";
import CaptureMessages from "./capture";
import CartFSMessages from "./cart_fs";
Expand Down Expand Up @@ -110,12 +110,16 @@ export class KateIPCServer {
value: result,
});
}
process.send({
type: "kate:reply",
id: payload.id,
ok: true,
value: result,
});
const [value, transfer] = this.unpack_transfers(result);
process.send(
{
type: "kate:reply",
id: payload.id,
ok: true,
value: value,
},
transfer
);
} catch (error: unknown) {
console.error(
`[kate:ipc] Error handling ${payload.type} from ${process.id}`,
Expand All @@ -137,6 +141,14 @@ export class KateIPCServer {
}
};

private unpack_transfers(x: unknown): [unknown, Transferable[]] {
if (x instanceof WithTransfer) {
return [x.value, x.transfer];
} else {
return [x, []];
}
}

private async mark_suspicious_activity(ev: Message, process: Process) {
console.debug(`[Kate] suspicious IPC activity from ${process.id}`, ev);
this.remove_process(process);
Expand Down

0 comments on commit f8eb02d

Please sign in to comment.