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

heavy revision on heap_breakdown's safety #12445

Merged
merged 2 commits into from
Jul 9, 2024
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
19 changes: 9 additions & 10 deletions src/StandaloneModuleGraph.zig
Original file line number Diff line number Diff line change
Expand Up @@ -74,31 +74,30 @@ pub const StandaloneModuleGraph = struct {
loader: bun.options.Loader,
contents: []const u8 = "",
sourcemap: LazySourceMap,
blob_: ?*bun.JSC.WebCore.Blob = null,
cached_blob: ?*bun.JSC.WebCore.Blob = null,

pub fn blob(this: *File, globalObject: *bun.JSC.JSGlobalObject) *bun.JSC.WebCore.Blob {
if (this.blob_ == null) {
if (this.cached_blob == null) {
var store = bun.JSC.WebCore.Blob.Store.init(@constCast(this.contents), bun.default_allocator);
// make it never free
store.ref();

var blob_ = bun.default_allocator.create(bun.JSC.WebCore.Blob) catch bun.outOfMemory();
blob_.* = bun.JSC.WebCore.Blob.initWithStore(store, globalObject);
blob_.allocator = bun.default_allocator;
const b = bun.JSC.WebCore.Blob.initWithStore(store, globalObject).new();
b.allocator = bun.default_allocator;

if (bun.http.MimeType.byExtensionNoDefault(bun.strings.trimLeadingChar(std.fs.path.extension(this.name), '.'))) |mime| {
store.mime_type = mime;
blob_.content_type = mime.value;
blob_.content_type_was_set = true;
blob_.content_type_allocated = false;
b.content_type = mime.value;
b.content_type_was_set = true;
b.content_type_allocated = false;
}

store.data.bytes.stored_name = bun.PathString.init(this.name);

this.blob_ = blob_;
this.cached_blob = b;
}

return this.blob_.?;
return this.cached_blob.?;
}
};

Expand Down
12 changes: 6 additions & 6 deletions src/brotli.zig
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,17 @@ const mimalloc = bun.Mimalloc;

const BrotliAllocator = struct {
pub fn alloc(_: ?*anyopaque, len: usize) callconv(.C) *anyopaque {
if (comptime bun.is_heap_breakdown_enabled) {
const zone = bun.HeapBreakdown.malloc_zone_t.get(BrotliAllocator);
return zone.malloc_zone_malloc(len).?;
if (bun.heap_breakdown.enabled) {
const zone = bun.heap_breakdown.getZone(BrotliAllocator);
return zone.malloc_zone_malloc(len) orelse bun.outOfMemory();
}

return mimalloc.mi_malloc(len) orelse unreachable;
return mimalloc.mi_malloc(len) orelse bun.outOfMemory();
}

pub fn free(_: ?*anyopaque, data: ?*anyopaque) callconv(.C) void {
if (comptime bun.is_heap_breakdown_enabled) {
const zone = bun.HeapBreakdown.malloc_zone_t.get(BrotliAllocator);
if (bun.heap_breakdown.enabled) {
const zone = bun.heap_breakdown.getZone(BrotliAllocator);
zone.malloc_zone_free(data);
return;
}
Expand Down
18 changes: 3 additions & 15 deletions src/bun.js/api/BunObject.zig
Original file line number Diff line number Diff line change
Expand Up @@ -1299,7 +1299,7 @@ extern fn dump_zone_malloc_stats() void;

fn dump_mimalloc(globalObject: *JSC.JSGlobalObject, _: *JSC.CallFrame) JSC.JSValue {
globalObject.bunVM().arena.dumpStats();
if (comptime bun.is_heap_breakdown_enabled) {
if (bun.heap_breakdown.enabled) {
dump_zone_malloc_stats();
}
return .undefined;
Expand Down Expand Up @@ -2246,13 +2246,7 @@ pub const Crypto = struct {
};
this.ref = .{};
this.promise.strong = .{};

const concurrent_task = bun.default_allocator.create(JSC.ConcurrentTask) catch bun.outOfMemory();
concurrent_task.* = JSC.ConcurrentTask{
.task = JSC.Task.init(&result.task),
.auto_delete = true,
};
this.event_loop.enqueueTaskConcurrent(concurrent_task);
this.event_loop.enqueueTaskConcurrent(JSC.ConcurrentTask.createFrom(&result.task));
this.deinit();
}
};
Expand Down Expand Up @@ -2488,13 +2482,7 @@ pub const Crypto = struct {
};
this.ref = .{};
this.promise.strong = .{};

const concurrent_task = bun.default_allocator.create(JSC.ConcurrentTask) catch bun.outOfMemory();
concurrent_task.* = JSC.ConcurrentTask{
.task = JSC.Task.init(&result.task),
.auto_delete = true,
};
this.event_loop.enqueueTaskConcurrent(concurrent_task);
this.event_loop.enqueueTaskConcurrent(JSC.ConcurrentTask.createFrom(&result.task));
this.deinit();
}
};
Expand Down
21 changes: 2 additions & 19 deletions src/bun.js/api/JSBundler.zig
Original file line number Diff line number Diff line change
Expand Up @@ -683,16 +683,7 @@ pub const JSBundler = struct {
completion.ref();

this.js_task = AnyTask.init(this);
const concurrent_task = bun.default_allocator.create(JSC.ConcurrentTask) catch {
completion.deref();
this.deinit();
return;
};
concurrent_task.* = JSC.ConcurrentTask{
.auto_delete = true,
.task = this.js_task.task(),
};
completion.jsc_event_loop.enqueueTaskConcurrent(concurrent_task);
completion.jsc_event_loop.enqueueTaskConcurrent(JSC.ConcurrentTask.create(this.js_task.task()));
}

pub fn runOnJSThread(this: *Resolve) void {
Expand Down Expand Up @@ -839,15 +830,7 @@ pub const JSBundler = struct {
completion.ref();

this.js_task = AnyTask.init(this);
const concurrent_task = bun.default_allocator.create(JSC.ConcurrentTask) catch {
completion.deref();
this.deinit();
return;
};
concurrent_task.* = JSC.ConcurrentTask{
.auto_delete = true,
.task = this.js_task.task(),
};
const concurrent_task = JSC.ConcurrentTask.createFrom(&this.js_task);
completion.jsc_event_loop.enqueueTaskConcurrent(concurrent_task);
}

Expand Down
12 changes: 5 additions & 7 deletions src/bun.js/event_loop.zig
Original file line number Diff line number Diff line change
Expand Up @@ -1933,9 +1933,8 @@ pub const MiniEventLoop = struct {

pub fn stderr(this: *MiniEventLoop) *JSC.WebCore.Blob.Store {
return this.stderr_store orelse brk: {
const store = bun.default_allocator.create(JSC.WebCore.Blob.Store) catch bun.outOfMemory();
var mode: bun.Mode = 0;
const fd = if (comptime Environment.isWindows) bun.FDImpl.fromUV(2).encode() else bun.STDERR_FD;
const fd = if (Environment.isWindows) bun.FDImpl.fromUV(2).encode() else bun.STDERR_FD;

switch (bun.sys.fstat(fd)) {
.result => |stat| {
Expand All @@ -1944,7 +1943,7 @@ pub const MiniEventLoop = struct {
.err => {},
}

store.* = JSC.WebCore.Blob.Store{
const store = JSC.WebCore.Blob.Store.new(.{
.ref_count = std.atomic.Value(u32).init(2),
.allocator = bun.default_allocator,
.data = .{
Expand All @@ -1956,7 +1955,7 @@ pub const MiniEventLoop = struct {
.mode = mode,
},
},
};
});

this.stderr_store = store;
break :brk store;
Expand All @@ -1965,7 +1964,6 @@ pub const MiniEventLoop = struct {

pub fn stdout(this: *MiniEventLoop) *JSC.WebCore.Blob.Store {
return this.stdout_store orelse brk: {
const store = bun.default_allocator.create(JSC.WebCore.Blob.Store) catch bun.outOfMemory();
var mode: bun.Mode = 0;
const fd = if (Environment.isWindows) bun.FDImpl.fromUV(1).encode() else bun.STDOUT_FD;

Expand All @@ -1976,7 +1974,7 @@ pub const MiniEventLoop = struct {
.err => {},
}

store.* = JSC.WebCore.Blob.Store{
const store = JSC.WebCore.Blob.Store.new(.{
.ref_count = std.atomic.Value(u32).init(2),
.allocator = bun.default_allocator,
.data = .{
Expand All @@ -1988,7 +1986,7 @@ pub const MiniEventLoop = struct {
.mode = mode,
},
},
};
});

this.stdout_store = store;
break :brk store;
Expand Down
9 changes: 3 additions & 6 deletions src/bun.js/javascript.zig
Original file line number Diff line number Diff line change
Expand Up @@ -460,12 +460,9 @@ pub export fn Bun__reportUnhandledError(globalObject: *JSGlobalObject, value: JS
pub export fn Bun__queueTaskConcurrently(global: *JSGlobalObject, task: *JSC.CppTask) void {
JSC.markBinding(@src());

const concurrent = bun.default_allocator.create(JSC.ConcurrentTask) catch unreachable;
concurrent.* = JSC.ConcurrentTask{
.task = Task.init(task),
.auto_delete = true,
};
global.bunVMConcurrently().eventLoop().enqueueTaskConcurrent(concurrent);
global.bunVMConcurrently().eventLoop().enqueueTaskConcurrent(
JSC.ConcurrentTask.create(Task.init(task)),
);
}

pub export fn Bun__handleRejectedPromise(global: *JSGlobalObject, promise: *JSC.JSPromise) void {
Expand Down
7 changes: 1 addition & 6 deletions src/bun.js/module_loader.zig
Original file line number Diff line number Diff line change
Expand Up @@ -895,12 +895,7 @@ pub const ModuleLoader = struct {
pub fn onWakeHandler(ctx: *anyopaque, _: *PackageManager) void {
debug("onWake", .{});
var this = bun.cast(*Queue, ctx);
const concurrent_task = bun.default_allocator.create(JSC.ConcurrentTask) catch bun.outOfMemory();
concurrent_task.* = .{
.task = JSC.Task.init(this),
.auto_delete = true,
};
this.vm().enqueueTaskConcurrent(concurrent_task);
this.vm().enqueueTaskConcurrent(JSC.ConcurrentTask.createFrom(this));
}

pub fn onPoll(this: *Queue) void {
Expand Down
3 changes: 1 addition & 2 deletions src/bun.js/node/node_fs_binding.zig
Original file line number Diff line number Diff line change
Expand Up @@ -260,8 +260,7 @@ pub const NodeJSFS = struct {
};

pub fn createBinding(globalObject: *JSC.JSGlobalObject) JSC.JSValue {
var module = globalObject.allocator().create(NodeJSFS) catch bun.outOfMemory();
module.* = .{};
const module = NodeJSFS.new(.{});

const vm = globalObject.bunVM();
if (vm.standalone_module_graph != null)
Expand Down
Loading
Loading