aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
Diffstat (limited to '')
-rw-r--r--src/bun.js/node/node_fs.zig6
-rw-r--r--src/darwin_c.zig71
-rw-r--r--src/js_printer.zig5
-rw-r--r--src/libarchive/libarchive.zig2
-rw-r--r--src/linux_c.zig1
5 files changed, 45 insertions, 40 deletions
diff --git a/src/bun.js/node/node_fs.zig b/src/bun.js/node/node_fs.zig
index 3a275287f..fe62e77ec 100644
--- a/src/bun.js/node/node_fs.zig
+++ b/src/bun.js/node/node_fs.zig
@@ -3453,11 +3453,11 @@ pub const NodeFS = struct {
var written: usize = 0;
// Attempt to pre-allocate large files
- if (comptime Environment.isMac or Environment.isLinux) {
+ if (Environment.isLinux) {
preallocate: {
// Worthwhile after 6 MB at least on ext4 linux
- if (buf.len >= 2_000_000) {
- const offset: usize = if (Environment.isMac or args.file == .path)
+ if (buf.len >= bun.C.preallocate_length) {
+ const offset: usize = if (args.file == .path)
// on mac, it's relatively positioned
0
else brk: {
diff --git a/src/darwin_c.zig b/src/darwin_c.zig
index 205fd5a0a..ab5fb13f7 100644
--- a/src/darwin_c.zig
+++ b/src/darwin_c.zig
@@ -140,39 +140,40 @@ pub extern "c" fn clonefile([*c]const u8, [*c]const u8, uint32_t: c_int) c_int;
// };
// }
-pub const struct_fstore = extern struct {
- fst_flags: c_uint,
- fst_posmode: c_int,
- fst_offset: off_t,
- fst_length: off_t,
- fst_bytesalloc: off_t,
-};
-pub const fstore_t = struct_fstore;
-
-pub const F_ALLOCATECONTIG = @as(c_int, 0x00000002);
-pub const F_ALLOCATEALL = @as(c_int, 0x00000004);
-pub const F_PEOFPOSMODE = @as(c_int, 3);
-pub const F_VOLPOSMODE = @as(c_int, 4);
-
-pub fn preallocate_file(fd: os.fd_t, offset: off_t, len: off_t) !void {
- var fstore = zeroes(fstore_t);
- fstore.fst_flags = F_ALLOCATECONTIG;
- fstore.fst_posmode = F_PEOFPOSMODE;
- fstore.fst_offset = 0;
- fstore.fst_length = len + offset;
-
- // Based on https://api.kde.org/frameworks/kcoreaddons/html/posix__fallocate__mac_8h_source.html
- var rc = os.system.fcntl(fd, os.F.PREALLOCATE, &fstore);
-
- switch (rc) {
- 0 => return,
- else => {
- fstore.fst_flags = F_ALLOCATEALL;
- rc = os.system.fcntl(fd, os.F.PREALLOCATE, &fstore);
- },
- }
-
- std.mem.doNotOptimizeAway(&fstore);
+// benchmarking this did nothing on macOS
+// i verified it wasn't returning -1
+pub fn preallocate_file(_: os.fd_t, _: off_t, _: off_t) !void {
+ // pub const struct_fstore = extern struct {
+ // fst_flags: c_uint,
+ // fst_posmode: c_int,
+ // fst_offset: off_t,
+ // fst_length: off_t,
+ // fst_bytesalloc: off_t,
+ // };
+ // pub const fstore_t = struct_fstore;
+
+ // pub const F_ALLOCATECONTIG = @as(c_int, 0x00000002);
+ // pub const F_ALLOCATEALL = @as(c_int, 0x00000004);
+ // pub const F_PEOFPOSMODE = @as(c_int, 3);
+ // pub const F_VOLPOSMODE = @as(c_int, 4);
+ // var fstore = zeroes(fstore_t);
+ // fstore.fst_flags = F_ALLOCATECONTIG;
+ // fstore.fst_posmode = F_PEOFPOSMODE;
+ // fstore.fst_offset = 0;
+ // fstore.fst_length = len + offset;
+
+ // // Based on https://api.kde.org/frameworks/kcoreaddons/html/posix__fallocate__mac_8h_source.html
+ // var rc = os.system.fcntl(fd, os.F.PREALLOCATE, &fstore);
+
+ // switch (rc) {
+ // 0 => return,
+ // else => {
+ // fstore.fst_flags = F_ALLOCATEALL;
+ // rc = os.system.fcntl(fd, os.F.PREALLOCATE, &fstore);
+ // },
+ // }
+
+ // std.mem.doNotOptimizeAway(&fstore);
}
pub const SystemErrno = enum(u8) {
@@ -788,3 +789,7 @@ pub const sockaddr_dl = extern struct {
pub usingnamespace @cImport({
@cInclude("sys/spawn.h");
});
+
+// it turns out preallocating on APFS on an M1 is slower.
+// so this is a linux-only optimization for now.
+pub const preallocate_length = std.math.maxInt(u51);
diff --git a/src/js_printer.zig b/src/js_printer.zig
index 7ca4f7f79..2dc1b7b74 100644
--- a/src/js_printer.zig
+++ b/src/js_printer.zig
@@ -5989,9 +5989,8 @@ pub fn printCommonJSThreaded(
defer lock.unlock();
lock.lock();
result.off = @truncate(u32, try getPos(getter));
- if (comptime Environment.isMac or Environment.isLinux) {
- // Don't bother preallocate the file if it's less than 1 KB. Preallocating is potentially two syscalls
- if (printer.writer.written > 1024) {
+ if (comptime Environment.isLinux) {
+ if (printer.writer.written > C.preallocate_length) {
// on mac, it's relative to current position in file handle
// on linux, it's absolute
try C.preallocate_file(
diff --git a/src/libarchive/libarchive.zig b/src/libarchive/libarchive.zig
index a88f3e328..ebaec7dd7 100644
--- a/src/libarchive/libarchive.zig
+++ b/src/libarchive/libarchive.zig
@@ -608,7 +608,7 @@ pub const Archive = struct {
}
// archive_read_data_into_fd reads in chunks of 1 MB
// #define MAX_WRITE (1024 * 1024)
- if (size > 4096) {
+ if (size > 1_000_000) {
C.preallocate_file(
file.handle,
0,
diff --git a/src/linux_c.zig b/src/linux_c.zig
index 0b39d1987..dade43a32 100644
--- a/src/linux_c.zig
+++ b/src/linux_c.zig
@@ -292,6 +292,7 @@ pub const SystemErrno = enum(u8) {
};
};
+pub const preallocate_length = 2048 * 1024;
pub fn preallocate_file(fd: std.os.fd_t, offset: std.os.off_t, len: std.os.off_t) anyerror!void {
// https://gist.github.com/Jarred-Sumner/b37b93399b63cbfd86e908c59a0a37df
// ext4 NVME Linux kernel 5.17.0-1016-oem x86_64