authorgravatar for jagt@live.comjagt <jagt@live.com> 2022-03-25 22:53:58+08:00
committergravatar for git@vexu.euVeikka Tuominen <git@vexu.eu> 2022-03-27 11:23:11+03:00
log29c32b3dc50218d15f779201e154d425fffcefeb
treeb16349f3069554879e6bb060d29357b3d93cc98f
parent23507f20b943e7923978b293cb03bee886f913ed

`Namespace.decls` use context to save memory

change `Module.Namespace.decls` from `AutoArrayHashMapUnmanaged` to `ArrayHashMapUnmanaged(*Decl, void)` with custom context to eliminate duplicated decl name strings. Also see: https://zig.news/andrewrk/how-to-use-hash-map-contexts-to-save-memory-when-doing-a-string-table-3l33

2 files changed, 40 insertions(+), 16 deletions(-)

src/Module.zig+36-11
......@@ -1591,6 +1591,19 @@ pub const Var = struct {
15911591 }
15921592};
15931593
1594pub const DeclAdapter = struct {
1595 pub fn hash(self: @This(), s: []const u8) u32 {
1596 _ = self;
1597 return @truncate(u32, std.hash.Wyhash.hash(0, s));
1598 }
1599
1600 pub fn eql(self: @This(), a: []const u8, b_decl: *Decl, b_index: usize) bool {
1601 _ = self;
1602 _ = b_index;
1603 return mem.eql(u8, a, mem.sliceTo(b_decl.name, 0));
1604 }
1605};
1606
15941607/// The container that structs, enums, unions, and opaques have.
15951608pub const Namespace = struct {
15961609 parent: ?*Namespace,
......@@ -1601,9 +1614,8 @@ pub const Namespace = struct {
16011614 /// which decls have been added/removed from source.
16021615 /// Declaration order is preserved via entry order.
16031616 /// Key memory is owned by `decl.name`.
1604 /// TODO save memory with https://github.com/ziglang/zig/issues/8619.
16051617 /// Anonymous decls are not stored here; they are kept in `anon_decls` instead.
1606 decls: std.StringArrayHashMapUnmanaged(*Decl) = .{},
1618 decls: std.ArrayHashMapUnmanaged(*Decl, void, DeclContext, true) = .{},
16071619
16081620 anon_decls: std.AutoArrayHashMapUnmanaged(*Decl, void) = .{},
16091621
......@@ -1612,6 +1624,19 @@ pub const Namespace = struct {
16121624 /// Value is whether the usingnamespace decl is marked `pub`.
16131625 usingnamespace_set: std.AutoHashMapUnmanaged(*Decl, bool) = .{},
16141626
1627 const DeclContext = struct {
1628 pub fn hash(self: @This(), decl: *Decl) u32 {
1629 _ = self;
1630 return @truncate(u32, std.hash.Wyhash.hash(0, mem.sliceTo(decl.name, 0)));
1631 }
1632
1633 pub fn eql(self: @This(), a: *Decl, b: *Decl, b_index: usize) bool {
1634 _ = self;
1635 _ = b_index;
1636 return mem.eql(u8, mem.sliceTo(a.name, 0), mem.sliceTo(b.name, 0));
1637 }
1638 };
1639
16151640 pub fn deinit(ns: *Namespace, mod: *Module) void {
16161641 ns.destroyDecls(mod);
16171642 ns.* = undefined;
......@@ -1628,8 +1653,8 @@ pub const Namespace = struct {
16281653 var anon_decls = ns.anon_decls;
16291654 ns.anon_decls = .{};
16301655
1631 for (decls.values()) |value| {
1632 value.destroy(mod);
1656 for (decls.keys()) |decl| {
1657 decl.destroy(mod);
16331658 }
16341659 decls.deinit(gpa);
16351660
......@@ -1658,7 +1683,7 @@ pub const Namespace = struct {
16581683 // TODO rework this code to not panic on OOM.
16591684 // (might want to coordinate with the clearDecl function)
16601685
1661 for (decls.values()) |child_decl| {
1686 for (decls.keys()) |child_decl| {
16621687 mod.clearDecl(child_decl, outdated_decls) catch @panic("out of memory");
16631688 child_decl.destroy(mod);
16641689 }
......@@ -3325,7 +3350,7 @@ fn updateZirRefs(gpa: Allocator, file: *File, old_zir: Zir) !void {
33253350 }
33263351
33273352 if (decl.getInnerNamespace()) |namespace| {
3328 for (namespace.decls.values()) |sub_decl| {
3353 for (namespace.decls.keys()) |sub_decl| {
33293354 try decl_stack.append(gpa, sub_decl);
33303355 }
33313356 for (namespace.anon_decls.keys()) |sub_decl| {
......@@ -4420,7 +4445,7 @@ fn scanDecl(iter: *ScanDeclIter, decl_sub_index: usize, flags: u4) SemaError!voi
44204445 if (is_usingnamespace) try namespace.usingnamespace_set.ensureUnusedCapacity(gpa, 1);
44214446
44224447 // We create a Decl for it regardless of analysis status.
4423 const gop = try namespace.decls.getOrPut(gpa, decl_name);
4448 const gop = try namespace.decls.getOrPutAdapted(gpa, @as([]const u8, mem.sliceTo(decl_name, 0)), DeclAdapter{});
44244449 if (!gop.found_existing) {
44254450 const new_decl = try mod.allocateNewDecl(decl_name, namespace, decl_node, iter.parent_decl.src_scope);
44264451 if (is_usingnamespace) {
......@@ -4428,7 +4453,7 @@ fn scanDecl(iter: *ScanDeclIter, decl_sub_index: usize, flags: u4) SemaError!voi
44284453 }
44294454 log.debug("scan new {*} ({s}) into {*}", .{ new_decl, decl_name, namespace });
44304455 new_decl.src_line = line;
4431 gop.value_ptr.* = new_decl;
4456 gop.key_ptr.* = new_decl;
44324457 // Exported decls, comptime decls, usingnamespace decls, and
44334458 // test decls if in test mode, get analyzed.
44344459 const decl_pkg = namespace.file_scope.pkg;
......@@ -4464,7 +4489,7 @@ fn scanDecl(iter: *ScanDeclIter, decl_sub_index: usize, flags: u4) SemaError!voi
44644489 return;
44654490 }
44664491 gpa.free(decl_name);
4467 const decl = gop.value_ptr.*;
4492 const decl = gop.key_ptr.*;
44684493 log.debug("scan existing {*} ({s}) of {*}", .{ decl, decl.name, namespace });
44694494 // Update the AST node of the decl; even if its contents are unchanged, it may
44704495 // have been re-ordered.
......@@ -5333,7 +5358,7 @@ pub fn processOutdatedAndDeletedDecls(mod: *Module) !void {
53335358
53345359 // Remove from the namespace it resides in, preserving declaration order.
53355360 assert(decl.zir_decl_index != 0);
5336 _ = decl.src_namespace.decls.orderedRemove(mem.sliceTo(decl.name, 0));
5361 _ = decl.src_namespace.decls.orderedRemoveAdapted(@as([]const u8, mem.sliceTo(decl.name, 0)), DeclAdapter{});
53375362
53385363 try mod.clearDecl(decl, &outdated_decls);
53395364 decl.destroy(mod);
......@@ -5400,7 +5425,7 @@ pub fn populateTestFunctions(mod: *Module) !void {
54005425 const builtin_pkg = mod.main_pkg.table.get("builtin").?;
54015426 const builtin_file = (mod.importPkg(builtin_pkg) catch unreachable).file;
54025427 const builtin_namespace = builtin_file.root_decl.?.src_namespace;
5403 const decl = builtin_namespace.decls.get("test_functions").?;
5428 const decl = builtin_namespace.decls.getKeyAdapted(@as([]const u8, "test_functions"), DeclAdapter{}).?;
54045429 var buf: Type.SlicePtrFieldTypeBuffer = undefined;
54055430 const tmp_test_fn_ty = decl.ty.slicePtrFieldType(&buf).elemType();
54065431
src/Sema.zig+4-5
......@@ -4516,7 +4516,7 @@ fn lookupInNamespace(
45164516
45174517 while (check_i < checked_namespaces.count()) : (check_i += 1) {
45184518 const check_ns = checked_namespaces.keys()[check_i];
4519 if (check_ns.decls.get(ident_name)) |decl| {
4519 if (check_ns.decls.getKeyAdapted(ident_name, Module.DeclAdapter{})) |decl| {
45204520 // Skip decls which are not marked pub, which are in a different
45214521 // file than the `a.b`/`@hasDecl` syntax.
45224522 if (decl.is_pub or src_file == decl.getFileScope()) {
......@@ -4559,7 +4559,7 @@ fn lookupInNamespace(
45594559 return sema.failWithOwnedErrorMsg(block, msg);
45604560 },
45614561 }
4562 } else if (namespace.decls.get(ident_name)) |decl| {
4562 } else if (namespace.decls.getKeyAdapted(ident_name, Module.DeclAdapter{})) |decl| {
45634563 try mod.declareDeclDependency(sema.owner_decl, decl);
45644564 return decl;
45654565 }
......@@ -11502,12 +11502,11 @@ fn typeInfoDecls(
1150211502 const decls_len = if (opt_namespace) |ns| ns.decls.count() else 0;
1150311503 const decls_vals = try decls_anon_decl.arena().alloc(Value, decls_len);
1150411504 for (decls_vals) |*decls_val, i| {
11505 const decl = opt_namespace.?.decls.values()[i];
11506 const name = opt_namespace.?.decls.keys()[i];
11505 const decl = opt_namespace.?.decls.keys()[i];
1150711506 const name_val = v: {
1150811507 var anon_decl = try block.startAnonDecl(src);
1150911508 defer anon_decl.deinit();
11510 const bytes = try anon_decl.arena().dupeZ(u8, name);
11509 const bytes = try anon_decl.arena().dupeZ(u8, mem.sliceTo(decl.name, 0));
1151111510 const new_decl = try anon_decl.finish(
1151211511 try Type.Tag.array_u8_sentinel_0.create(anon_decl.arena(), bytes.len),
1151311512 try Value.Tag.bytes.create(anon_decl.arena(), bytes[0 .. bytes.len + 1]),