Skip to content

Commit

Permalink
Don't use arena in node:fs (#5863)
Browse files Browse the repository at this point in the history
Co-authored-by: Jarred Sumner <709451+Jarred-Sumner@users.noreply.github.com>
  • Loading branch information
Jarred-Sumner and Jarred-Sumner committed Sep 22, 2023
1 parent 8684a59 commit e34ff61
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 8 deletions.
22 changes: 22 additions & 0 deletions bench/snippets/rmdir.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import { tmpdir } from "node:os";
import { promises, existsSync, mkdirSync } from "node:fs";
const count = 1024 * 12;

var queue = new Array(count);
var paths = new Array(count);
for (let i = 0; i < count; i++) {
const path = `${tmpdir()}/${Date.now()}.rm.dir${i}`;
try {
mkdirSync(path);
} catch (e) {}
paths[i] = path;
queue[i] = promises.rmdir(path);
}

await Promise.all(queue);

for (let i = 0; i < count; i++) {
if (existsSync(paths[i])) {
throw new Error(`Path ${paths[i]} was not removed`);
}
}
12 changes: 6 additions & 6 deletions src/bun.js/node/node_fs.zig
Original file line number Diff line number Diff line change
Expand Up @@ -1074,7 +1074,7 @@ pub const Arguments = struct {
}

pub fn fromJS(ctx: JSC.C.JSContextRef, arguments: *ArgumentsSlice, exception: JSC.C.ExceptionRef) ?Stat {
const path = PathLike.fromJSWithAllocator(ctx, arguments, bun.default_allocator, exception) orelse {
const path = PathLike.fromJS(ctx, arguments, exception) orelse {
if (exception.* == null) {
JSC.throwInvalidArguments(
"path must be a string or TypedArray",
Expand Down Expand Up @@ -1649,7 +1649,7 @@ pub const Arguments = struct {
}

pub fn fromJS(ctx: JSC.C.JSContextRef, arguments: *ArgumentsSlice, exception: JSC.C.ExceptionRef) ?Readdir {
const path = PathLike.fromJSWithAllocator(ctx, arguments, bun.default_allocator, exception) orelse {
const path = PathLike.fromJS(ctx, arguments, exception) orelse {
if (exception.* == null) {
JSC.throwInvalidArguments(
"path must be a string or TypedArray",
Expand Down Expand Up @@ -2917,7 +2917,7 @@ pub const Arguments = struct {
}

pub fn fromJS(ctx: JSC.C.JSContextRef, arguments: *ArgumentsSlice, exception: JSC.C.ExceptionRef) ?CopyFile {
const src = PathLike.fromJSWithAllocator(ctx, arguments, bun.default_allocator, exception) orelse {
const src = PathLike.fromJS(ctx, arguments, exception) orelse {
if (exception.* == null) {
JSC.throwInvalidArguments(
"src must be a string or buffer",
Expand All @@ -2931,7 +2931,7 @@ pub const Arguments = struct {

if (exception.* != null) return null;

const dest = PathLike.fromJSWithAllocator(ctx, arguments, bun.default_allocator, exception) orelse {
const dest = PathLike.fromJS(ctx, arguments, exception) orelse {
src.deinit();

if (exception.* == null) {
Expand Down Expand Up @@ -2981,7 +2981,7 @@ pub const Arguments = struct {
}

pub fn fromJS(ctx: JSC.C.JSContextRef, arguments: *ArgumentsSlice, exception: JSC.C.ExceptionRef) ?Cp {
const src = PathLike.fromJSWithAllocator(ctx, arguments, bun.default_allocator, exception) orelse {
const src = PathLike.fromJS(ctx, arguments, exception) orelse {
if (exception.* == null) {
JSC.throwInvalidArguments(
"src must be a string or buffer",
Expand All @@ -2995,7 +2995,7 @@ pub const Arguments = struct {

if (exception.* != null) return null;

const dest = PathLike.fromJSWithAllocator(ctx, arguments, bun.default_allocator, exception) orelse {
const dest = PathLike.fromJS(ctx, arguments, exception) orelse {
defer src.deinit();
if (exception.* == null) {
JSC.throwInvalidArguments(
Expand Down
2 changes: 1 addition & 1 deletion src/bun.js/node/node_fs_stat_watcher.zig
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,7 @@ pub const StatWatcher = struct {

pub fn fromJS(ctx: JSC.C.JSContextRef, arguments: *ArgumentsSlice, exception: JSC.C.ExceptionRef) ?Arguments {
const vm = ctx.vm();
const path = PathLike.fromJS(ctx, arguments, exception) orelse {
const path = PathLike.fromJSWithAllocator(ctx, arguments, bun.default_allocator, exception) orelse {
if (exception.* == null) {
JSC.throwInvalidArguments(
"filename must be a string or TypedArray",
Expand Down
2 changes: 1 addition & 1 deletion src/bun.js/node/types.zig
Original file line number Diff line number Diff line change
Expand Up @@ -802,7 +802,7 @@ pub const PathLike = union(Tag) {
}

pub fn fromJS(ctx: JSC.C.JSContextRef, arguments: *ArgumentsSlice, exception: JSC.C.ExceptionRef) ?PathLike {
return fromJSWithAllocator(ctx, arguments, arguments.arena.allocator(), exception);
return fromJSWithAllocator(ctx, arguments, bun.default_allocator, exception);
}
pub fn fromJSWithAllocator(ctx: JSC.C.JSContextRef, arguments: *ArgumentsSlice, allocator: std.mem.Allocator, exception: JSC.C.ExceptionRef) ?PathLike {
const arg = arguments.next() orelse return null;
Expand Down
19 changes: 19 additions & 0 deletions test/js/node/fs/fs.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1062,6 +1062,25 @@ describe("rmdir", () => {
done();
});
});

it("removes a dir x 512", async () => {
var queue = new Array(512);
var paths = new Array(512);
for (let i = 0; i < 512; i++) {
const path = `${tmpdir()}/${Date.now()}.rm.dir${i}`;
try {
mkdirSync(path);
} catch (e) {}
paths[i] = path;
queue[i] = promises.rmdir(path);
}

await Promise.all(queue);

for (let i = 0; i < 512; i++) {
expect(existsSync(paths[i])).toBe(false);
}
});
it("does not remove a dir with a file in it", async () => {
const path = `${tmpdir()}/${Date.now()}.rm.dir`;
try {
Expand Down

0 comments on commit e34ff61

Please sign in to comment.