Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

perf: fs optimizations - part 1 #15873

Merged
merged 24 commits into from
Sep 22, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
works
  • Loading branch information
littledivy committed Sep 12, 2022
commit 6e297af6b43a7aebf953c6e8cd46e982479a8e88
164 changes: 104 additions & 60 deletions runtime/js/30_fs.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@
ObjectPrototypeIsPrototypeOf,
SymbolAsyncIterator,
SymbolIterator,
Function,
ObjectEntries,
Uint32Array,
} = window.__bootstrap.primordials;
const { pathFromURL } = window.__bootstrap.util;
const build = window.__bootstrap.build.build;
Expand Down Expand Up @@ -161,7 +164,8 @@
path,
options = {},
) {
await core.opAsync("op_remove_async",
await core.opAsync(
"op_remove_async",
pathFromURL(path),
!!options.recursive,
);
Expand All @@ -175,53 +179,64 @@
}

async function rename(oldpath, newpath) {
await core.opAsync("op_rename_async",
await core.opAsync(
"op_rename_async",
pathFromURL(oldpath),
pathFromURL(newpath),
);
}

const infoBuf = new Uint32Array(24);
function parseFileInfoFromBuf() {
const unix = build.os === "darwin" || build.os === "linux";
const [
isFile,
isDirectory,
isSymlink,
size,
mtime,
atime,
birthtime,
dev,
ino,
mode,
nlink,
uid,
gid,
rdev,
blksize,
blocks,
] = infoBuf;
return {
isFile: isFile === 1,
isDirectory: isDirectory === 1,
isSymlink: isSymlink === 1,
size: size,
mtime: mtime !== 0 ? new Date(mtime) : null,
atime: atime !== 0 ? new Date(atime) : null,
birthtime: birthtime !== 0 ? new Date(birthtime) : null,
// Only non-null if on Unix
dev: unix ? dev : null,
ino: unix ? ino : null,
mode: unix ? mode : null,
nlink: unix ? nlink : null,
uid: unix ? uid : null,
gid: unix ? gid : null,
rdev: unix ? rdev : null,
blksize: unix ? blksize : null,
blocks: unix ? blocks : null,
function createByteStruct(types) {
littledivy marked this conversation as resolved.
Show resolved Hide resolved
// types can be "bool", "u32" or "u64"
// "?u64" is an optional Date.
const typeSizes = {
u32: 1,
u64: 2,
bool: 1,
};
}
let offset = 0;
let str = "return {";
for (let [name, type] of ObjectEntries(types)) {
const optional = type.startsWith("?");
if (optional) type = type.slice(1);

if (type == "u64") {
if (!optional) {
str += `${name}: view[${offset}] + view[${offset + 1}] * 2**32 ,`;
} else {
str += `${name}: view[${offset}] === 0 && view[${
offset + 1
}] === 0 ? null : new Date(view[${offset}] + view[${
offset + 1
}] * 2**32),`;
}
} else {
str += `${name}: ${type === "bool" ? "!!" : ""}view[${offset}],`;
}
offset += typeSizes[type];
}
str += "};";
return [new Function("view", str), new Uint32Array(offset)];
littledivy marked this conversation as resolved.
Show resolved Hide resolved
}

const [statStruct, statBuf] = createByteStruct({
isFile: "u64",
isDirectory: "u64",
isSymlink: "u64",
size: "u64",
mtime: "?u64",
atime: "?u64",
birthtime: "?u64",
dev: "u64",
ino: "u64",
mode: "u64",
nlink: "u64",
uid: "u64",
gid: "u64",
rdev: "u64",
blksize: "u64",
blocks: "u64",
});

function parseFileInfo(response) {
const unix = build.os === "darwin" || build.os === "linux";
littledivy marked this conversation as resolved.
Show resolved Hide resolved
Expand All @@ -230,11 +245,9 @@
isDirectory: response.isDirectory,
isSymlink: response.isSymlink,
size: response.size,
mtime: response.mtime != null ? new Date(response.mtime) : null,
atime: response.atime != null ? new Date(response.atime) : null,
birthtime: response.birthtime != null
? new Date(response.birthtime)
: null,
mtime: response.mtime != 0 ? new Date(response.mtime) : null,
littledivy marked this conversation as resolved.
Show resolved Hide resolved
atime: response.atime != 0 ? new Date(response.atime) : null,
birthtime: response.birthtime != 0 ? new Date(response.birthtime) : null,
// Only non-null if on Unix
dev: unix ? response.dev : null,
ino: unix ? response.ino : null,
Expand All @@ -249,7 +262,8 @@
}

function fstatSync(rid) {
return parseFileInfo(ops.op_fstat_sync(rid));
ops.op_fstat_sync(rid, statBuf.buffer);
return statStruct(statBuf);
}

async function fstat(rid) {
Expand All @@ -268,9 +282,9 @@
ops.op_stat_sync(
pathFromURL(path),
true,
infoBuf.buffer,
statBuf.buffer,
);
return parseFileInfoFromBuf();
return statStruct(statBuf);
}

async function stat(path) {
Expand All @@ -282,11 +296,12 @@
}

function statSync(path) {
const res = ops.op_stat_sync({
path: pathFromURL(path),
lstat: false,
});
return parseFileInfo(res);
ops.op_stat_sync(
pathFromURL(path),
false,
statBuf.buffer,
);
return statStruct(statBuf);
}

function coerceLen(len) {
Expand Down Expand Up @@ -363,7 +378,14 @@
) {
const [atimeSec, atimeNsec] = toUnixTimeFromEpoch(atime);
const [mtimeSec, mtimeNsec] = toUnixTimeFromEpoch(mtime);
await core.opAsync("op_futime_async", rid, atimeSec, atimeNsec, mtimeSec, mtimeNsec);
await core.opAsync(
"op_futime_async",
rid,
atimeSec,
atimeNsec,
mtimeSec,
mtimeNsec,
);
}

function utimeSync(
Expand All @@ -373,7 +395,13 @@
) {
const [atimeSec, atimeNsec] = toUnixTimeFromEpoch(atime);
const [mtimeSec, mtimeNsec] = toUnixTimeFromEpoch(mtime);
ops.op_utime_sync(pathFromURL(path), atimeSec, atimeNsec, mtimeSec, mtimeNsec);
ops.op_utime_sync(
pathFromURL(path),
atimeSec,
atimeNsec,
mtimeSec,
mtimeNsec,
);
}

async function utime(
Expand All @@ -383,23 +411,39 @@
) {
const [atimeSec, atimeNsec] = toUnixTimeFromEpoch(atime);
const [mtimeSec, mtimeNsec] = toUnixTimeFromEpoch(mtime);
await core.opAsync("op_utime_async", pathFromURL(path), atimeSec, atimeNsec, mtimeSec, mtimeNsec);
await core.opAsync(
"op_utime_async",
pathFromURL(path),
atimeSec,
atimeNsec,
mtimeSec,
mtimeNsec,
);
}

function symlinkSync(
oldpath,
newpath,
options,
) {
ops.op_symlink_sync(pathFromURL(oldpath), pathFromURL(newpath), options?.type);
ops.op_symlink_sync(
pathFromURL(oldpath),
pathFromURL(newpath),
options?.type,
);
}

async function symlink(
oldpath,
newpath,
options,
) {
await core.opAsync("op_symlink_async", pathFromURL(oldpath), pathFromURL(newpath), options?.type);
await core.opAsync(
"op_symlink_async",
pathFromURL(oldpath),
pathFromURL(newpath),
options?.type,
);
}

function fdatasyncSync(rid) {
Expand Down
Loading