Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ export default [
hasPendingActivity: false,
configurable: false,
valuesArray: true,
memoryCost: true,
estimatedSize: true,
klass: {},
proto: {
setCwd: {
Expand Down
2 changes: 2 additions & 0 deletions src/bun.js/api/Shell.classes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ export default [
klass: {},
values: ["resolve", "reject"],
valuesArray: true,
memoryCost: true,
estimatedSize: true,
proto: {
run: {
fn: "runFromJS",
Expand Down
5 changes: 5 additions & 0 deletions src/bun.js/bindings/ShellBindings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

#include "ZigGeneratedClasses.h"

extern "C" SYSV_ABI size_t ShellInterpreter__estimatedSize(void* ptr);

@coderabbitai coderabbitai Bot Oct 14, 2025 •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue | 🔴 Critical

🧩 Analysis chain

Verify the extern function is implemented.

The extern declaration looks correct with proper SYSV_ABI calling convention. Based on the PR context, this should be implemented in src/shell/interpreter.zig.


🏁 Script executed:

#!/bin/bash
# Verify the implementation of ShellInterpreter__estimatedSize in Zig
rg -n "ShellInterpreter__estimatedSize|estimatedSize.*ShellInterpreter" --type=zig

Length of output: 0


🏁 Script executed:

#!/bin/bash
# Broad search for ShellInterpreter__estimatedSize implementation across all files
rg -n "ShellInterpreter__estimatedSize" -C2

Length of output: 802


Add missing implementation for ShellInterpreter__estimatedSize
No matching definition was found in the Zig sources (e.g., src/shell/interpreter.zig). Please implement ShellInterpreter__estimatedSize to satisfy the extern declaration.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's generated code!

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Jarred-Sumner, you're absolutely right—my apologies for the confusion! I see now that ShellInterpreter__estimatedSize is generated by the codegen pipeline (via src/codegen/generate-classes.ts) from the estimatedSize method defined in the Zig code. The extern declaration is correctly placed in the generated header. Thank you for clarifying! 🐰

   /\_/\
  ( o.o )
   > ^ <
_/|     |\_

/_ `---' _
| |


🧠 Learnings used
Learnt from: Jarred-Sumner
PR: oven-sh/bun#23636
File: src/shell/interpreter.zig:672-0
Timestamp: 2025-10-14T04:04:17.112Z
Learning: In Bun's shell interpreter (src/shell/interpreter.zig), the JavaScript garbage collector's memory reporting API only allows reporting extra memory allocation once at object creation time. Therefore, `estimated_size_for_gc` is intentionally set once during initialization and not updated afterward, even though fields like `root_io`, `vm_args_utf8`, and `export_env` may be mutated later. The `estimatedSize()` method returns this cached initial value, while `memoryCost()` computes the size on-demand.


namespace Bun {

using namespace JSC;
Expand All @@ -21,6 +23,9 @@ extern "C" SYSV_ABI EncodedJSValue Bun__createShellInterpreter(Zig::GlobalObject
ASSERT(structure);

auto* result = WebCore::JSShellInterpreter::create(vm, globalObject, structure, ptr, WTFMove(args), resolveFn, rejectFn);

size_t size = ShellInterpreter__estimatedSize(ptr);
vm.heap.reportExtraMemoryAllocated(result, size);
return JSValue::encode(result);
}

Expand Down
7 changes: 4 additions & 3 deletions src/codegen/generate-classes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -627,9 +627,6 @@ function generateConstructorImpl(typeName, obj: ClassDefinition) {
Object.keys(fields).length > 0 ? generateHashTable(name, classSymbolName, typeName, obj, fields, false) : "";

const hashTableIdentifier = hashTable.length ? `${name}TableValues` : "";
if (obj.estimatedSize) {
externs += `extern JSC_CALLCONV size_t ${symbolName(typeName, "estimatedSize")}(void* ptr);` + "\n";
}

return (
`
Expand Down Expand Up @@ -1371,6 +1368,10 @@ function generateClassHeader(typeName, obj: ClassDefinition) {

const name = className(typeName);

if (obj.estimatedSize) {
externs += `extern JSC_CALLCONV size_t ${symbolName(typeName, "estimatedSize")}(void* ptr);` + "\n";
}

const DECLARE_VISIT_CHILDREN =
values.length ||
obj.estimatedSize ||
Expand Down
2 changes: 1 addition & 1 deletion src/collections/baby_list.zig
Original file line number Diff line number Diff line change
Expand Up @@ -364,7 +364,7 @@ pub fn BabyList(comptime Type: type) type {
}

pub fn memoryCost(this: Self) usize {
return this.cap;
return this.cap * @sizeOf(Type);
}

/// This method is available only for `BabyList(u8)`.
Expand Down
11 changes: 11 additions & 0 deletions src/shell/EnvMap.zig
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,17 @@ pub fn init(alloc: Allocator) EnvMap {
return .{ .map = MapType.init(alloc) };
}

pub fn memoryCost(this: *const EnvMap) usize {
var size: usize = @sizeOf(EnvMap);
size += std.mem.sliceAsBytes(this.map.keys()).len;
size += std.mem.sliceAsBytes(this.map.values()).len;
for (this.map.keys(), this.map.values()) |key, value| {
size += key.memoryCost();
size += value.memoryCost();
}
return size;
}

pub fn initWithCapacity(alloc: Allocator, cap: usize) EnvMap {
var map = MapType.init(alloc);
bun.handleOom(map.ensureTotalCapacity(cap));
Expand Down
15 changes: 15 additions & 0 deletions src/shell/EnvStr.zig
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,21 @@ pub const EnvStr = packed struct(u128) {
};
}

pub fn memoryCost(this: EnvStr) usize {
const divisor: usize = brk: {
if (this.asRefCounted()) |refc| {
break :brk refc.refcount;
}
break :brk 1;
};
if (divisor == 0) {
@branchHint(.unlikely);
return 0;
}

return this.len / divisor;
}
Comment on lines +75 to +88

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue | 🔴 Critical

Correct memoryCost for refcounted vs borrowed slices

  • For .refcounted created via initRefCounted, this.len is 0, so memoryCost wrongly returns 0.
  • For .slice, memory is owned elsewhere; returning len double-counts.

Compute by tag: use byteSlice().len / refcount for .refcounted, return 0 for .slice, and 0 for .empty.

-pub fn memoryCost(this: EnvStr) usize {
-    const divisor: usize = brk: {
-        if (this.asRefCounted()) |refc| {
-            break :brk refc.refcount;
-        }
-        break :brk 1;
-    };
-    if (divisor == 0) {
-        @branchHint(.unlikely);
-        return 0;
-    }
-
-    return this.len / divisor;
-}
+pub fn memoryCost(this: EnvStr) usize {
+    return switch (this.tag) {
+        .empty => 0,
+        .slice => 0, // memory owned elsewhere
+        .refcounted => brk: {
+            const refc = this.castRefCounted();
+            const rc = refc.refcount;
+            if (rc == 0) break :brk 0;
+            break :brk refc.byteSlice().len / rc;
+        },
+    };
+}
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
pub fn memoryCost(this: EnvStr) usize {
const divisor: usize = brk: {
if (this.asRefCounted()) |refc| {
break :brk refc.refcount;
}
break :brk 1;
};
if (divisor == 0) {
@branchHint(.unlikely);
return 0;
}
return this.len / divisor;
}
pub fn memoryCost(this: EnvStr) usize {
return switch (this.tag) {
.empty => 0,
.slice => 0, // memory owned elsewhere
.refcounted => brk: {
const refc = this.castRefCounted();
const rc = refc.refcount;
if (rc == 0) break :brk 0;
break :brk refc.byteSlice().len / rc;
},
};
}


pub fn ref(this: EnvStr) void {
if (this.asRefCounted()) |refc| {
refc.ref();
Expand Down
35 changes: 34 additions & 1 deletion src/shell/IO.zig
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,14 @@ pub fn format(this: IO, comptime _: []const u8, _: std.fmt.FormatOptions, writer
try writer.print("stdin: {}\nstdout: {}\nstderr: {}", .{ this.stdin, this.stdout, this.stderr });
}

pub fn memoryCost(this: *const IO) usize {
var size: usize = @sizeOf(IO);
size += this.stdin.memoryCost();
size += this.stdout.memoryCost();
size += this.stderr.memoryCost();
return size;
}

pub fn deinit(this: *IO) void {
this.stdin.close();
this.stdout.close();
Expand Down Expand Up @@ -76,21 +84,46 @@ pub const InKind = union(enum) {
},
}
}

pub fn memoryCost(this: InKind) usize {
switch (this) {
.fd => return this.fd.memoryCost(),
.ignore => return 0,
}
}
};

pub const OutKind = union(enum) {
/// Write/Read to/from file descriptor
/// If `captured` is non-null, it will write to std{out,err} and also buffer it.
/// The pointer points to the `buffered_stdout`/`buffered_stdin` fields
/// in the Interpreter struct
fd: struct { writer: *Interpreter.IOWriter, captured: ?*bun.ByteList = null },
fd: struct {
writer: *Interpreter.IOWriter,
captured: ?*bun.ByteList = null,
pub fn memoryCost(this: *const @This()) usize {
var cost: usize = this.writer.memoryCost();
if (this.captured) |captured| {
cost += captured.memoryCost();
}
return cost;
}
},
/// Buffers the output (handled in Cmd.BufferedIoClosed.close())
///
/// This is set when the shell is called with `.quiet()`
pipe,
/// Discards output
ignore,

pub fn memoryCost(this: *const OutKind) usize {
return switch (this.*) {
.fd => |*fd| fd.memoryCost(),
.pipe => 0,
.ignore => 0,
};
}

// fn dupeForSubshell(this: *ShellExecEnv,
pub fn format(this: OutKind, comptime _: []const u8, _: std.fmt.FormatOptions, writer: anytype) !void {
switch (this) {
Expand Down
15 changes: 15 additions & 0 deletions src/shell/IOReader.zig
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,13 @@ pub fn refSelf(this: *IOReader) *IOReader {
return this;
}

pub fn memoryCost(this: *const IOReader) usize {
var size: usize = @sizeOf(IOReader);
size += this.buf.allocatedSlice().len;
size += this.readers.memoryCost();
return size;
}

pub fn eventLoop(this: *IOReader) jsc.EventLoopHandle {
return this.evtloop;
}
Expand Down Expand Up @@ -224,6 +231,14 @@ pub const IOReaderChildPtr = struct {
};
}

pub fn memoryCost(this: IOReaderChildPtr) usize {
if (this.ptr.is(Interpreter.Builtin.Cat)) {
// TODO:
return @sizeOf(Interpreter.Builtin.Cat);
}
return 0;
}

Comment on lines +234 to +241

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🧹 Nitpick | 🔵 Trivial

Avoid hardcoding Cat size; delegate to child memoryCost if available

Returning @sizeOf(Interpreter.Builtin.Cat) is a placeholder and likely inaccurate for dynamic allocations.

Consider exposing a memoryCost() on Interpreter.Builtin.Cat and calling it here; otherwise return 0 to avoid misleading totals.

🤖 Prompt for AI Agents
In src/shell/IOReader.zig around lines 234 to 241, the memoryCost function
currently returns a hardcoded @sizeOf(Interpreter.Builtin.Cat) for Cat children
which is a placeholder and can under/over-report dynamic allocations; instead
add or call a memoryCost() method on Interpreter.Builtin.Cat (expose it on that
type) and invoke that here to compute actual child memory usage, falling back to
0 if the child type does not implement memoryCost; update types/signatures as
needed so IOReaderChildPtr can call the child's memoryCost without assuming a
fixed struct size.

/// Return true if the child should be deleted
pub fn onReadChunk(this: IOReaderChildPtr, chunk: []const u8, remove: *bool) Yield {
return this.ptr.call("onIOReaderChunk", .{ chunk, remove }, Yield);
Expand Down
13 changes: 12 additions & 1 deletion src/shell/IOWriter.zig
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,15 @@ pub fn refSelf(this: *IOWriter) *IOWriter {
return this;
}

pub fn memoryCost(this: *const IOWriter) usize {
var cost: usize = @sizeOf(IOWriter);
cost += this.buf.allocatedSlice().len;
cost += if (comptime bun.Environment.isWindows) this.winbuf.allocatedSlice().len else 0;
cost += this.writers.memoryCost();
cost += this.writer.memoryCost();
return cost;
}

pub const Flags = packed struct(u8) {
pollable: bool = false,
nonblocking: bool = false,
Expand Down Expand Up @@ -156,7 +165,9 @@ pub fn __start(this: *IOWriter) Maybe(void) {
this.writer.getPoll().?.flags.insert(.nonblocking);
}

if (this.flags.is_socket) {
const sendto_MSG_NOWAIT_blocks = bun.Environment.isMac;

if (this.flags.is_socket and (!sendto_MSG_NOWAIT_blocks or this.flags.nonblocking)) {
this.writer.getPoll().?.flags.insert(.socket);
} else if (this.flags.pollable) {
this.writer.getPoll().?.flags.insert(.fifo);
Expand Down
25 changes: 25 additions & 0 deletions src/shell/ParsedShellScript.zig
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,30 @@ export_env: ?EnvMap = null,
quiet: bool = false,
cwd: ?bun.String = null,
this_jsvalue: JSValue = .zero,
estimated_size_for_gc: usize = 0,

fn #computeEstimatedSizeForGC(this: *const ParsedShellScript) usize {
var size: usize = @sizeOf(ParsedShellScript);
if (this.args) |args| {
size += args.memoryCost();
}
if (this.export_env) |*env| {
size += env.memoryCost();
}
if (this.cwd) |*cwd| {
size += cwd.estimatedSize();
}
size += std.mem.sliceAsBytes(this.jsobjs.allocatedSlice()).len;
return size;
}

pub fn memoryCost(this: *const ParsedShellScript) usize {
return this.#computeEstimatedSizeForGC();
}

pub fn estimatedSize(this: *const ParsedShellScript) usize {
return this.estimated_size_for_gc;
}

pub fn take(
this: *ParsedShellScript,
Expand Down Expand Up @@ -161,6 +185,7 @@ fn createParsedShellScriptImpl(globalThis: *jsc.JSGlobalObject, callframe: *jsc.
.args = shargs,
.jsobjs = jsobjs,
});
parsed_shell_script.estimated_size_for_gc = parsed_shell_script.#computeEstimatedSizeForGC();
const this_jsvalue = jsc.Codegen.JSParsedShellScript.toJSWithValues(parsed_shell_script, globalThis, marked_argument_buffer);
parsed_shell_script.this_jsvalue = this_jsvalue;

Expand Down
Loading