From 8a48e8bb0b7de985a96b3a4cae389e3294a2c0e3 Mon Sep 17 00:00:00 2001 From: Jarred Sumner Date: Thu, 24 Aug 2023 19:39:00 -0700 Subject: Report extra memory more (#4289) * Report memory allocated in fetch * Memory size reporting to `Headers` * Fixup memory reporting allocator * Make these tests do more * cleanup some of this --------- Co-authored-by: Jarred Sumner <709451+Jarred-Sumner@users.noreply.github.com> --- src/bun.js/webcore/blob.zig | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) (limited to 'src/bun.js/webcore/blob.zig') diff --git a/src/bun.js/webcore/blob.zig b/src/bun.js/webcore/blob.zig index c5e893a5a..604726c1e 100644 --- a/src/bun.js/webcore/blob.zig +++ b/src/bun.js/webcore/blob.zig @@ -1233,6 +1233,34 @@ pub const Blob = struct { return blob_; } + fn estimatedByteSize(this: *Blob) usize { + // in-memory size. not the size on disk. + if (this.size != Blob.max_size) { + return this.size; + } + + var store = this.store orelse return 0; + if (store.data == .bytes) { + return store.data.bytes.len; + } + + return 0; + } + + pub fn estimatedSize(this: *Blob) callconv(.C) usize { + var size = this.estimatedByteSize() + @sizeOf(Blob); + + if (this.store) |store| { + size += @sizeOf(Blob.Store); + size += switch (store.data) { + .bytes => store.data.bytes.stored_name.len, + .file => store.data.file.pathlike.path.slice().len, + }; + } + + return size + (this.content_type.len * @as(usize, @intFromBool(this.content_type_allocated))); + } + comptime { if (!JSC.is_bindgen) { _ = JSDOMFile__hasInstance; -- cgit v1.2.3