aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/bun.js/bindings/CommonJSModuleRecord.cpp2
-rw-r--r--src/bun.js/bindings/ModuleLoader.cpp2
-rw-r--r--src/bun.js/bindings/ZigSourceProvider.cpp14
-rw-r--r--src/bun.js/bindings/ZigSourceProvider.h8
-rw-r--r--src/bun.js/bindings/exports.zig3
-rw-r--r--src/bun.js/bindings/headers-handwritten.h3
-rw-r--r--src/bun.js/javascript.zig7
-rw-r--r--src/bun.js/module_loader.zig87
-rw-r--r--src/js_parser.zig4
-rw-r--r--src/string.zig4
10 files changed, 90 insertions, 44 deletions
diff --git a/src/bun.js/bindings/CommonJSModuleRecord.cpp b/src/bun.js/bindings/CommonJSModuleRecord.cpp
index cc42c5684..da9f89fc8 100644
--- a/src/bun.js/bindings/CommonJSModuleRecord.cpp
+++ b/src/bun.js/bindings/CommonJSModuleRecord.cpp
@@ -936,7 +936,7 @@ std::optional<JSC::SourceCode> createCommonJSModule(
bool isBuiltIn)
{
JSCommonJSModule* moduleObject;
- WTF::String sourceURL = toStringCopy(source.source_url);
+ WTF::String sourceURL = Bun::toWTFString(source.source_url);
JSValue specifierValue = Bun::toJS(globalObject, source.specifier);
JSValue entry = globalObject->requireMap()->get(globalObject, specifierValue);
diff --git a/src/bun.js/bindings/ModuleLoader.cpp b/src/bun.js/bindings/ModuleLoader.cpp
index 209add950..94c852e35 100644
--- a/src/bun.js/bindings/ModuleLoader.cpp
+++ b/src/bun.js/bindings/ModuleLoader.cpp
@@ -561,7 +561,7 @@ JSValue fetchCommonJSModule(
Bun__transpileFile(bunVM, globalObject, specifier, referrer, res, false);
if (res->success && res->result.value.commonJSExportsLen) {
- target->evaluate(globalObject, Bun::toWTFString(*specifier).isolatedCopy(), res->result.value);
+ target->evaluate(globalObject, Bun::toWTFString(*specifier), res->result.value);
RETURN_IF_EXCEPTION(scope, {});
RELEASE_AND_RETURN(scope, target);
}
diff --git a/src/bun.js/bindings/ZigSourceProvider.cpp b/src/bun.js/bindings/ZigSourceProvider.cpp
index 2c82d2ea7..df1f8cc29 100644
--- a/src/bun.js/bindings/ZigSourceProvider.cpp
+++ b/src/bun.js/bindings/ZigSourceProvider.cpp
@@ -96,10 +96,17 @@ Ref<SourceProvider> SourceProvider::create(Zig::GlobalObject* globalObject, Reso
{
auto stringImpl = Bun::toWTFString(resolvedSource.source_code);
- auto sourceURLString = toStringCopy(resolvedSource.source_url);
+ auto sourceURLString = Bun::toWTFString(resolvedSource.source_url);
+
bool isCodeCoverageEnabled = !!globalObject->vm().controlFlowProfiler();
- bool shouldGenerateCodeCoverage = isCodeCoverageEnabled && !isBuiltin && BunTest__shouldGenerateCodeCoverage(Bun::toString(sourceURLString));
+ bool shouldGenerateCodeCoverage = isCodeCoverageEnabled && !isBuiltin && BunTest__shouldGenerateCodeCoverage(resolvedSource.source_url);
+
+ if (resolvedSource.needsDeref) {
+ resolvedSource.source_code.deref();
+ resolvedSource.specifier.deref();
+ // source_url gets deref'd by the WTF::String above.
+ }
auto provider = adoptRef(*new SourceProvider(
globalObject->isThreadLocalDefaultGlobalObject ? globalObject : nullptr,
@@ -153,6 +160,9 @@ void SourceProvider::cacheBytecode(const BytecodeCacheGenerator& generator)
if (update)
m_cachedBytecode->addGlobalUpdate(*update);
}
+SourceProvider::~SourceProvider()
+{
+}
void SourceProvider::commitCachedBytecode()
{
// if (!m_resolvedSource.bytecodecache_fd || !m_cachedBytecode || !m_cachedBytecode->hasUpdates())
diff --git a/src/bun.js/bindings/ZigSourceProvider.h b/src/bun.js/bindings/ZigSourceProvider.h
index 418490e45..76b850f7f 100644
--- a/src/bun.js/bindings/ZigSourceProvider.h
+++ b/src/bun.js/bindings/ZigSourceProvider.h
@@ -39,13 +39,7 @@ class SourceProvider final : public JSC::SourceProvider {
public:
static Ref<SourceProvider> create(Zig::GlobalObject*, ResolvedSource resolvedSource, JSC::SourceProviderSourceType sourceType = JSC::SourceProviderSourceType::Module, bool isBuiltIn = false);
- ~SourceProvider()
- {
- freeSourceCode();
-
- commitCachedBytecode();
- }
-
+ ~SourceProvider();
unsigned hash() const override;
StringView source() const override { return StringView(m_source.get()); }
RefPtr<JSC::CachedBytecode> cachedBytecode()
diff --git a/src/bun.js/bindings/exports.zig b/src/bun.js/bindings/exports.zig
index 3bbe7976d..ff831b06b 100644
--- a/src/bun.js/bindings/exports.zig
+++ b/src/bun.js/bindings/exports.zig
@@ -196,7 +196,7 @@ pub const ResolvedSource = extern struct {
specifier: bun.String = bun.String.empty,
source_code: bun.String = bun.String.empty,
- source_url: ZigString = ZigString.Empty,
+ source_url: bun.String = bun.String.empty,
commonjs_exports: ?[*]ZigString = null,
commonjs_exports_len: u32 = 0,
@@ -205,6 +205,7 @@ pub const ResolvedSource = extern struct {
allocator: ?*anyopaque = null,
tag: Tag = Tag.javascript,
+ needs_deref: bool = true,
pub const Tag = @import("generated/ResolvedSourceTag.zig").ResolvedSourceTag;
};
diff --git a/src/bun.js/bindings/headers-handwritten.h b/src/bun.js/bindings/headers-handwritten.h
index 9dd0febb1..83517db4c 100644
--- a/src/bun.js/bindings/headers-handwritten.h
+++ b/src/bun.js/bindings/headers-handwritten.h
@@ -69,12 +69,13 @@ typedef struct ErrorableString {
typedef struct ResolvedSource {
BunString specifier;
BunString source_code;
- ZigString source_url;
+ BunString source_url;
ZigString* commonJSExports;
uint32_t commonJSExportsLen;
uint32_t hash;
void* allocator;
uint32_t tag;
+ bool needsDeref;
} ResolvedSource;
static const uint32_t ResolvedSourceTagPackageJSONTypeModule = 1;
typedef union ErrorableResolvedSourceResult {
diff --git a/src/bun.js/javascript.zig b/src/bun.js/javascript.zig
index 5bf37152b..0d9e03aca 100644
--- a/src/bun.js/javascript.zig
+++ b/src/bun.js/javascript.zig
@@ -492,6 +492,7 @@ pub const VirtualMachine = struct {
preload: []const string = &[_][]const u8{},
unhandled_pending_rejection_to_capture: ?*JSC.JSValue = null,
standalone_module_graph: ?*bun.StandaloneModuleGraph = null,
+ smol: bool = false,
hot_reload: bun.CLI.Command.HotReload = .none,
jsc: *JSC.VM = undefined,
@@ -1312,6 +1313,7 @@ pub const VirtualMachine = struct {
vm.regular_event_loop.global = vm.global;
vm.regular_event_loop.virtual_machine = vm;
vm.jsc = vm.global.vm();
+ vm.smol = opts.smol;
if (source_code_printer == null) {
var writer = try js_printer.BufferWriter.init(allocator);
@@ -1433,7 +1435,7 @@ pub const VirtualMachine = struct {
vm.bundler.configureLinker();
try vm.bundler.configureFramework(false);
-
+ vm.smol = opts.smol;
vm.bundler.macro_context = js_ast.Macro.MacroContext.init(&vm.bundler);
if (opts.args.serve orelse false) {
@@ -1476,9 +1478,10 @@ pub const VirtualMachine = struct {
return ResolvedSource{
.source_code = bun.String.init(source.impl),
.specifier = specifier,
- .source_url = ZigString.init(source_url),
+ .source_url = bun.String.init(source_url),
.hash = source.hash,
.allocator = source,
+ .needs_deref = false,
};
}
diff --git a/src/bun.js/module_loader.zig b/src/bun.js/module_loader.zig
index 209f86ca1..7e11eac9b 100644
--- a/src/bun.js/module_loader.zig
+++ b/src/bun.js/module_loader.zig
@@ -143,9 +143,10 @@ inline fn jsSyntheticModule(comptime name: ResolvedSource.Tag, specifier: String
.allocator = null,
.source_code = bun.String.empty,
.specifier = specifier,
- .source_url = ZigString.init(@tagName(name)),
+ .source_url = bun.String.init(@tagName(name)),
.hash = 0,
.tag = name,
+ .needs_deref = false,
};
}
@@ -288,7 +289,7 @@ pub const RuntimeTranspilerStore = struct {
var log = this.log;
this.log = logger.Log.init(bun.default_allocator);
var resolved_source = this.resolved_source;
- resolved_source.source_url = specifier.toZigString();
+ resolved_source.source_url = specifier.dupeRef();
resolved_source.tag = brk: {
if (resolved_source.commonjs_exports_len > 0) {
@@ -331,6 +332,7 @@ pub const RuntimeTranspilerStore = struct {
pub fn run(this: *TranspilerJob) void {
var arena = bun.ArenaAllocator.init(bun.default_allocator);
defer arena.deinit();
+ const allocator = arena.allocator();
defer this.dispatchToMainThread();
if (this.generation_number != this.vm.transpiler_store.generation_number.load(.Monotonic)) {
@@ -341,13 +343,13 @@ pub const RuntimeTranspilerStore = struct {
if (ast_memory_store == null) {
ast_memory_store = bun.default_allocator.create(js_ast.ASTMemoryAllocator) catch @panic("out of memory!");
ast_memory_store.?.* = js_ast.ASTMemoryAllocator{
- .allocator = arena.allocator(),
+ .allocator = allocator,
.previous = null,
};
}
+ ast_memory_store.?.allocator = allocator;
ast_memory_store.?.reset();
- ast_memory_store.?.allocator = arena.allocator();
ast_memory_store.?.push();
const path = this.path;
@@ -358,7 +360,6 @@ pub const RuntimeTranspilerStore = struct {
var vm = this.vm;
var bundler: bun.Bundler = undefined;
bundler = vm.bundler;
- var allocator = arena.allocator();
bundler.setAllocator(allocator);
bundler.setLog(&this.log);
bundler.resolver.opts = bundler.options;
@@ -481,11 +482,12 @@ pub const RuntimeTranspilerStore = struct {
}
if (parse_result.already_bundled) {
+ const duped = String.create(specifier);
this.resolved_source = ResolvedSource{
.allocator = null,
.source_code = bun.String.createLatin1(parse_result.source.contents),
- .specifier = String.create(specifier),
- .source_url = ZigString.init(path.text),
+ .specifier = duped,
+ .source_url = if (duped.eqlUTF8(path.text)) duped.dupeRef() else String.init(path.text),
.hash = 0,
};
return;
@@ -554,11 +556,22 @@ pub const RuntimeTranspilerStore = struct {
dumpSource(specifier, &printer);
}
+ const duped = String.create(specifier);
this.resolved_source = ResolvedSource{
.allocator = null,
- .source_code = bun.String.createLatin1(printer.ctx.getWritten()),
- .specifier = String.create(specifier),
- .source_url = ZigString.init(path.text),
+ .source_code = brk: {
+ const written = printer.ctx.getWritten();
+ const result = bun.String.createLatin1(written);
+
+ if (written.len > 1024 * 1024 * 2 or vm.smol) {
+ printer.ctx.buffer.deinit();
+ source_code_printer.?.* = printer;
+ }
+
+ break :brk result;
+ },
+ .specifier = duped,
+ .source_url = if (duped.eqlUTF8(path.text)) duped.dupeRef() else String.create(path.text),
.commonjs_exports = null,
.commonjs_exports_len = if (parse_result.ast.exports_kind == .cjs)
std.math.maxInt(u32)
@@ -1281,7 +1294,7 @@ pub const ModuleLoader = struct {
.allocator = null,
.source_code = bun.String.createLatin1(printer.ctx.getWritten()),
.specifier = String.init(specifier),
- .source_url = ZigString.init(path.text),
+ .source_url = String.init(path.text),
.commonjs_exports = if (commonjs_exports.len > 0)
commonjs_exports.ptr
else
@@ -1364,14 +1377,19 @@ pub const ModuleLoader = struct {
if (jsc_vm.parser_arena) |shared| {
arena = shared;
jsc_vm.parser_arena = null;
- _ = arena.reset(.retain_capacity);
} else {
- arena = bun.ArenaAllocator.init(jsc_vm.allocator);
+ arena = bun.ArenaAllocator.init(bun.default_allocator);
}
var give_back_arena = true;
defer {
if (give_back_arena) {
if (jsc_vm.parser_arena == null) {
+ if (jsc_vm.smol) {
+ _ = arena.reset(.free_all);
+ } else {
+ _ = arena.reset(.{ .retain_with_limit = 8 * 1024 * 1024 });
+ }
+
jsc_vm.parser_arena = arena;
} else {
arena.deinit();
@@ -1379,8 +1397,7 @@ pub const ModuleLoader = struct {
}
}
- // var allocator = arena.allocator();
- var allocator = bun.default_allocator;
+ var allocator = arena.allocator();
var fd: ?StoredFileDescriptorType = null;
var package_json: ?*PackageJSON = null;
@@ -1537,7 +1554,7 @@ pub const ModuleLoader = struct {
.allocator = null,
.source_code = bun.String.create(parse_result.source.contents),
.specifier = input_specifier,
- .source_url = ZigString.init(path.text),
+ .source_url = if (input_specifier.eqlUTF8(path.text)) input_specifier.dupeRef() else String.init(path.text),
.hash = 0,
.tag = ResolvedSource.Tag.json_for_object_loader,
@@ -1553,7 +1570,7 @@ pub const ModuleLoader = struct {
else => unreachable,
},
.specifier = input_specifier,
- .source_url = ZigString.init(path.text),
+ .source_url = if (input_specifier.eqlUTF8(path.text)) input_specifier.dupeRef() else String.init(path.text),
.hash = 0,
};
}
@@ -1563,7 +1580,7 @@ pub const ModuleLoader = struct {
.allocator = null,
.source_code = bun.String.createLatin1(parse_result.source.contents),
.specifier = input_specifier,
- .source_url = ZigString.init(path.text),
+ .source_url = if (input_specifier.eqlUTF8(path.text)) input_specifier.dupeRef() else String.init(path.text),
.hash = 0,
};
@@ -1685,9 +1702,19 @@ pub const ModuleLoader = struct {
return .{
.allocator = null,
- .source_code = bun.String.createLatin1(printer.ctx.getWritten()),
+ .source_code = brk: {
+ const written = printer.ctx.getWritten();
+ const result = bun.String.createLatin1(written);
+
+ if (written.len > 1024 * 1024 * 2 or jsc_vm.smol) {
+ printer.ctx.buffer.deinit();
+ source_code_printer.* = printer;
+ }
+
+ break :brk result;
+ },
.specifier = input_specifier,
- .source_url = ZigString.init(path.text),
+ .source_url = if (input_specifier.eqlUTF8(path.text)) input_specifier.dupeRef() else String.init(path.text),
.commonjs_exports = if (commonjs_exports.len > 0)
commonjs_exports.ptr
else
@@ -1741,7 +1768,7 @@ pub const ModuleLoader = struct {
// .allocator = if (jsc_vm.has_loaded) &jsc_vm.allocator else null,
// .source_code = ZigString.init(jsc_vm.allocator.dupe(u8, parse_result.source.contents) catch unreachable),
// .specifier = ZigString.init(specifier),
- // .source_url = ZigString.init(path.text),
+ // .source_url = if (input_specifier.eqlUTF8(path.text)) input_specifier.dupeRef() else String.init(path.text),
// .hash = 0,
// .tag = ResolvedSource.Tag.wasm,
// };
@@ -1766,7 +1793,7 @@ pub const ModuleLoader = struct {
.allocator = null,
.source_code = bun.String.static(@embedFile("../js/wasi-runner.js")),
.specifier = input_specifier,
- .source_url = ZigString.init(path.text),
+ .source_url = if (input_specifier.eqlUTF8(path.text)) input_specifier.dupeRef() else String.init(path.text),
.tag = .esm,
.hash = 0,
};
@@ -1813,7 +1840,7 @@ pub const ModuleLoader = struct {
.allocator = &jsc_vm.allocator,
.source_code = public_url,
.specifier = input_specifier,
- .source_url = ZigString.init(path.text),
+ .source_url = if (input_specifier.eqlUTF8(path.text)) input_specifier.dupeRef() else String.init(path.text),
.hash = 0,
};
},
@@ -2020,9 +2047,9 @@ pub const ModuleLoader = struct {
if (specifier.eqlComptime(Runtime.Runtime.Imports.Name)) {
return ResolvedSource{
.allocator = null,
- .source_code = bun.String.init(Runtime.Runtime.sourceContentBun()),
- .specifier = bun.String.init(Runtime.Runtime.Imports.Name),
- .source_url = ZigString.init(Runtime.Runtime.Imports.Name),
+ .source_code = String.init(Runtime.Runtime.sourceContentBun()),
+ .specifier = specifier,
+ .source_url = String.init(Runtime.Runtime.Imports.Name),
.hash = Runtime.Runtime.versionHash(),
};
} else if (HardcodedModule.Map.getWithEql(specifier, bun.String.eqlComptime)) |hardcoded| {
@@ -2032,9 +2059,10 @@ pub const ModuleLoader = struct {
.allocator = null,
.source_code = bun.String.create(jsc_vm.entry_point.source.contents),
.specifier = specifier,
- .source_url = ZigString.init(bun.asByteSlice(JSC.VirtualMachine.main_file_name)),
+ .source_url = String.init(bun.asByteSlice(JSC.VirtualMachine.main_file_name)),
.hash = 0,
.tag = .esm,
+ .needs_deref = true,
};
},
@@ -2113,7 +2141,7 @@ pub const ModuleLoader = struct {
.allocator = null,
.source_code = bun.String.create(entry.source.contents),
.specifier = specifier,
- .source_url = specifier.toZigString(),
+ .source_url = specifier.dupeRef(),
.hash = 0,
};
}
@@ -2125,8 +2153,9 @@ pub const ModuleLoader = struct {
.allocator = null,
.source_code = bun.String.static(file.contents),
.specifier = specifier,
- .source_url = specifier.toZigString(),
+ .source_url = specifier.dupeRef(),
.hash = 0,
+ .needs_deref = false,
};
}
}
diff --git a/src/js_parser.zig b/src/js_parser.zig
index f569f515f..3c734bf13 100644
--- a/src/js_parser.zig
+++ b/src/js_parser.zig
@@ -21037,6 +21037,10 @@ fn NewParser_(
// operations, so they should all be child scopes and should all be popped
// by the time we get here.
p.scopes_in_order.items[scope_index] = null;
+ // Decrement the length so that in code with lots of scopes, we use
+ // less memory and do less work
+ p.scopes_in_order.items.len -= @as(usize, @intFromBool(p.scopes_in_order.items.len == scope_index + 1));
+
// Remove the last child from the parent scope
const last = parent.children.len - 1;
if (comptime Environment.allow_assert) assert(parent.children.ptr[last] == to_flatten);
diff --git a/src/string.zig b/src/string.zig
index 6fe819013..c093953d9 100644
--- a/src/string.zig
+++ b/src/string.zig
@@ -846,6 +846,10 @@ pub const String = extern struct {
}
}
+ pub fn eqlUTF8(this: String, other: []const u8) bool {
+ return this.toZigString().eql(ZigString.initUTF8(other));
+ }
+
pub fn eql(this: String, other: String) bool {
return this.toZigString().eql(other.toZigString());
}