aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGravatar Jarred Sumner <jarred@jarredsumner.com> 2022-01-05 13:11:56 -0800
committerGravatar Jarred Sumner <jarred@jarredsumner.com> 2022-01-05 13:12:11 -0800
commit3cd129544ea9bb9dd17d30aa26572b90568e1e42 (patch)
tree2077920a4c7294911c52a904da2dbb03c6349965
parent1004e924ff2d53b14cb2e3a5c32ed0e4135fd89a (diff)
downloadbun-3cd129544ea9bb9dd17d30aa26572b90568e1e42.tar.gz
bun-3cd129544ea9bb9dd17d30aa26572b90568e1e42.tar.zst
bun-3cd129544ea9bb9dd17d30aa26572b90568e1e42.zip
[JS Parser] Reduce memory usage by ~8%
m---------src/deps/mimalloc0
-rw-r--r--src/js_ast.zig18
-rw-r--r--src/js_parser/js_parser.zig16
-rw-r--r--src/js_printer.zig11
-rw-r--r--src/options.zig2
-rw-r--r--src/pool.zig2
6 files changed, 42 insertions, 7 deletions
diff --git a/src/deps/mimalloc b/src/deps/mimalloc
-Subproject 0560fc27c08d28d523b7f741a42deb26cd01c0c
+Subproject f412df7a2b64421e1f1d61fde6055a6ea288e8f
diff --git a/src/js_ast.zig b/src/js_ast.zig
index e4d130f8d..369326de4 100644
--- a/src/js_ast.zig
+++ b/src/js_ast.zig
@@ -14,7 +14,7 @@ const default_allocator = _global.default_allocator;
const C = _global.C;
const Ref = @import("ast/base.zig").Ref;
const RefHashCtx = @import("ast/base.zig").RefHashCtx;
-
+const ObjectPool = @import("./pool.zig").ObjectPool;
const ImportRecord = @import("import_record.zig").ImportRecord;
const allocators = @import("allocators.zig");
@@ -3376,6 +3376,16 @@ pub const ArrayBinding = struct {
default_value: ?ExprNodeIndex = null,
};
+pub const SymbolPool = ObjectPool(
+ std.ArrayList(Symbol),
+ struct {
+ pub fn init(allocator: std.mem.Allocator) anyerror!std.ArrayList(Symbol) {
+ return std.ArrayList(Symbol).init(allocator);
+ }
+ }.init,
+ true,
+);
+
pub const Ast = struct {
approximate_newline_count: usize = 0,
has_lazy_export: bool = false,
@@ -3410,6 +3420,7 @@ pub const Ast = struct {
url_for_css: ?string = null,
parts: []Part,
symbols: []Symbol = &([_]Symbol{}),
+ symbol_pool: ?*SymbolPool.Node = null,
module_scope: ?Scope = null,
// char_freq: *CharFreq,
exports_ref: ?Ref = null,
@@ -6768,6 +6779,11 @@ pub const Macro = struct {
const value = promise.result(macro.vm.global.vm());
+ if (value.isError() or value.isAggregateError(macro.vm.global) or value.isException(macro.vm.global.vm())) {
+ macro.vm.defaultErrorHandler(value, null);
+ return error.MacroFailed;
+ }
+
if (JSCBase.GetJSPrivateData(JSNode, value.asObjectRef())) |node| {
node.updateSymbolsMap(Visitor, visitor);
return node.toExpr();
diff --git a/src/js_parser/js_parser.zig b/src/js_parser/js_parser.zig
index 3a64fd79b..7294bbe4d 100644
--- a/src/js_parser/js_parser.zig
+++ b/src/js_parser/js_parser.zig
@@ -60,7 +60,7 @@ pub const StringHashMap = _hash_map.StringHashMap;
pub const AutoHashMap = _hash_map.AutoHashMap;
const StringHashMapUnamanged = _hash_map.StringHashMapUnamanged;
const ObjectPool = @import("../pool.zig").ObjectPool;
-
+const SymbolPool = js_ast.SymbolPool;
const NodeFallbackModules = @import("../node_fallbacks.zig");
// Dear reader,
// There are some things you should know about this file to make it easier for humans to read
@@ -2933,7 +2933,8 @@ pub fn NewParser(
forbid_suffix_after_as_loc: logger.Loc = logger.Loc.Empty,
current_scope: *js_ast.Scope = undefined,
scopes_for_current_part: List(*js_ast.Scope) = .{},
- symbols: List(js_ast.Symbol) = .{},
+ symbols: ListManaged(js_ast.Symbol) = undefined,
+ symbol_pool_node: *SymbolPool.Node = undefined,
ts_use_counts: List(u32) = .{},
exports_ref: Ref = Ref.None,
require_ref: Ref = Ref.None,
@@ -5035,7 +5036,7 @@ pub fn NewParser(
pub fn newSymbol(p: *P, kind: Symbol.Kind, identifier: string) !Ref {
const inner_index = Ref.toInt(p.symbols.items.len);
- try p.symbols.append(p.allocator, Symbol{
+ try p.symbols.append(Symbol{
.kind = kind,
.original_name = identifier,
.link = null,
@@ -12197,7 +12198,7 @@ pub fn NewParser(
MacroVisitor{ .p = p, .loc = expr.loc },
) catch |err| {
if (err == error.MacroFailed) {
- p.log.addError(p.source, expr.loc, "error in macro") catch unreachable;
+ p.log.addError(p.source, expr.loc, "macro threw exception") catch unreachable;
} else {
p.log.addErrorFmt(p.source, expr.loc, p.allocator, "{s} error in macro", .{@errorName(err)}) catch unreachable;
}
@@ -15143,6 +15144,8 @@ pub fn NewParser(
// }
}
+ p.symbol_pool_node.data = p.symbols;
+
return js_ast.Ast{
.runtime_imports = p.runtime_imports,
.parts = parts,
@@ -15170,6 +15173,7 @@ pub fn NewParser(
(p.symbols.items[p.runtime_imports.__require.?.ref.inner_index].use_count_estimate > 0)
else
false,
+ .symbol_pool = p.symbol_pool_node,
// .top_Level_await_keyword = p.top_level_await_keyword,
};
}
@@ -15227,6 +15231,10 @@ pub fn NewParser(
.lexer = lexer,
};
+ this.symbol_pool_node = SymbolPool.get(_global.default_allocator);
+ this.symbols = this.symbol_pool_node.data;
+ this.symbols.clearRetainingCapacity();
+
if (comptime !only_scan_imports_and_do_not_visit) {
this.import_records = @TypeOf(this.import_records).init(allocator);
this.named_imports = NamedImportsType.init(allocator);
diff --git a/src/js_printer.zig b/src/js_printer.zig
index db9ceb601..0c3be9bac 100644
--- a/src/js_printer.zig
+++ b/src/js_printer.zig
@@ -4350,6 +4350,10 @@ pub fn printAst(
try printer.writer.done();
+ if (tree.symbol_pool) |symbol_pool| {
+ js_ast.SymbolPool.release(symbol_pool);
+ }
+
return @intCast(usize, std.math.max(printer.writer.written, 0));
}
@@ -4430,6 +4434,10 @@ pub fn printCommonJS(
try printer.writer.done();
+ if (tree.symbol_pool) |symbol_pool| {
+ js_ast.SymbolPool.release(symbol_pool);
+ }
+
return @intCast(usize, std.math.max(printer.writer.written, 0));
}
@@ -4515,5 +4523,8 @@ pub fn printCommonJSThreaded(
}
result.len = @intCast(usize, std.math.max(printer.writer.written, 0));
+ if (tree.symbol_pool) |symbol_pool| {
+ js_ast.SymbolPool.release(symbol_pool);
+ }
return result;
}
diff --git a/src/options.zig b/src/options.zig
index 46db4c152..20b2d02ae 100644
--- a/src/options.zig
+++ b/src/options.zig
@@ -398,7 +398,7 @@ pub const Platform = enum {
pub inline fn supportsBrowserField(this: Platform) bool {
return switch (this) {
- .neutral, .browser => true,
+ .bun, .bun_macro, .neutral, .browser => true,
else => false,
};
}
diff --git a/src/pool.zig b/src/pool.zig
index bfb026c3f..4f3396d30 100644
--- a/src/pool.zig
+++ b/src/pool.zig
@@ -20,7 +20,7 @@ pub fn ObjectPool(comptime Type: type, comptime Init: (fn (allocator: std.mem.Al
pub fn get(allocator: std.mem.Allocator) *LinkedList.Node {
if (data.loaded) {
if (data.list.popFirst()) |node| {
- node.data.reset();
+ if (comptime @hasDecl(Type, "reset")) node.data.reset();
return node;
}
}