Commit 9271a89c65

Linus Groh <mail@linusgroh.de>
2024-09-11 23:44:02
InternPool: Replace default values with a .empty declaration
1 parent 8588964
Changed files (2)
src/InternPool.zig
@@ -2,20 +2,20 @@
 //! This data structure is self-contained.
 
 /// One item per thread, indexed by `tid`, which is dense and unique per thread.
-locals: []Local = &.{},
+locals: []Local,
 /// Length must be a power of two and represents the number of simultaneous
 /// writers that can mutate any single sharded data structure.
-shards: []Shard = &.{},
+shards: []Shard,
 /// Key is the error name, index is the error tag value. Index 0 has a length-0 string.
-global_error_set: GlobalErrorSet = GlobalErrorSet.empty,
+global_error_set: GlobalErrorSet,
 /// Cached number of active bits in a `tid`.
-tid_width: if (single_threaded) u0 else std.math.Log2Int(u32) = 0,
+tid_width: if (single_threaded) u0 else std.math.Log2Int(u32),
 /// Cached shift amount to put a `tid` in the top bits of a 30-bit value.
-tid_shift_30: if (single_threaded) u0 else std.math.Log2Int(u32) = if (single_threaded) 0 else 31,
+tid_shift_30: if (single_threaded) u0 else std.math.Log2Int(u32),
 /// Cached shift amount to put a `tid` in the top bits of a 31-bit value.
-tid_shift_31: if (single_threaded) u0 else std.math.Log2Int(u32) = if (single_threaded) 0 else 31,
+tid_shift_31: if (single_threaded) u0 else std.math.Log2Int(u32),
 /// Cached shift amount to put a `tid` in the top bits of a 32-bit value.
-tid_shift_32: if (single_threaded) u0 else std.math.Log2Int(u32) = if (single_threaded) 0 else 31,
+tid_shift_32: if (single_threaded) u0 else std.math.Log2Int(u32),
 
 /// Dependencies on the source code hash associated with a ZIR instruction.
 /// * For a `declaration`, this is the entire declaration body.
@@ -23,36 +23,36 @@ tid_shift_32: if (single_threaded) u0 else std.math.Log2Int(u32) = if (single_th
 /// * For a `func`, this is the source of the full function signature.
 /// These are also invalidated if tracking fails for this instruction.
 /// Value is index into `dep_entries` of the first dependency on this hash.
-src_hash_deps: std.AutoArrayHashMapUnmanaged(TrackedInst.Index, DepEntry.Index) = .empty,
+src_hash_deps: std.AutoArrayHashMapUnmanaged(TrackedInst.Index, DepEntry.Index),
 /// Dependencies on the value of a Nav.
 /// Value is index into `dep_entries` of the first dependency on this Nav value.
-nav_val_deps: std.AutoArrayHashMapUnmanaged(Nav.Index, DepEntry.Index) = .empty,
+nav_val_deps: std.AutoArrayHashMapUnmanaged(Nav.Index, DepEntry.Index),
 /// Dependencies on an interned value, either:
 /// * a runtime function (invalidated when its IES changes)
 /// * a container type requiring resolution (invalidated when the type must be recreated at a new index)
 /// Value is index into `dep_entries` of the first dependency on this interned value.
-interned_deps: std.AutoArrayHashMapUnmanaged(Index, DepEntry.Index) = .empty,
+interned_deps: std.AutoArrayHashMapUnmanaged(Index, DepEntry.Index),
 /// Dependencies on the full set of names in a ZIR namespace.
 /// Key refers to a `struct_decl`, `union_decl`, etc.
 /// Value is index into `dep_entries` of the first dependency on this namespace.
-namespace_deps: std.AutoArrayHashMapUnmanaged(TrackedInst.Index, DepEntry.Index) = .empty,
+namespace_deps: std.AutoArrayHashMapUnmanaged(TrackedInst.Index, DepEntry.Index),
 /// Dependencies on the (non-)existence of some name in a namespace.
 /// Value is index into `dep_entries` of the first dependency on this name.
-namespace_name_deps: std.AutoArrayHashMapUnmanaged(NamespaceNameKey, DepEntry.Index) = .empty,
+namespace_name_deps: std.AutoArrayHashMapUnmanaged(NamespaceNameKey, DepEntry.Index),
 
 /// Given a `Depender`, points to an entry in `dep_entries` whose `depender`
 /// matches. The `next_dependee` field can be used to iterate all such entries
 /// and remove them from the corresponding lists.
-first_dependency: std.AutoArrayHashMapUnmanaged(AnalUnit, DepEntry.Index) = .empty,
+first_dependency: std.AutoArrayHashMapUnmanaged(AnalUnit, DepEntry.Index),
 
 /// Stores dependency information. The hashmaps declared above are used to look
 /// up entries in this list as required. This is not stored in `extra` so that
 /// we can use `free_dep_entries` to track free indices, since dependencies are
 /// removed frequently.
-dep_entries: std.ArrayListUnmanaged(DepEntry) = .empty,
+dep_entries: std.ArrayListUnmanaged(DepEntry),
 /// Stores unused indices in `dep_entries` which can be reused without a full
 /// garbage collection pass.
-free_dep_entries: std.ArrayListUnmanaged(DepEntry.Index) = .empty,
+free_dep_entries: std.ArrayListUnmanaged(DepEntry.Index),
 
 /// Whether a multi-threaded intern pool is useful.
 /// Currently `false` until the intern pool is actually accessed
@@ -62,6 +62,24 @@ const want_multi_threaded = true;
 /// Whether a single-threaded intern pool impl is in use.
 pub const single_threaded = builtin.single_threaded or !want_multi_threaded;
 
+pub const empty: InternPool = .{
+    .locals = &.{},
+    .shards = &.{},
+    .global_error_set = .empty,
+    .tid_width = 0,
+    .tid_shift_30 = if (single_threaded) 0 else 31,
+    .tid_shift_31 = if (single_threaded) 0 else 31,
+    .tid_shift_32 = if (single_threaded) 0 else 31,
+    .src_hash_deps = .empty,
+    .nav_val_deps = .empty,
+    .interned_deps = .empty,
+    .namespace_deps = .empty,
+    .namespace_name_deps = .empty,
+    .first_dependency = .empty,
+    .dep_entries = .empty,
+    .free_dep_entries = .empty,
+};
+
 /// A `TrackedInst.Index` provides a single, unchanging reference to a ZIR instruction across a whole
 /// compilation. From this index, you can acquire a `TrackedInst`, which containss a reference to both
 /// the file which the instruction lives in, and the instruction index itself, which is updated on
@@ -9858,7 +9876,7 @@ fn extraData(extra: Local.Extra, comptime T: type, index: u32) T {
 test "basic usage" {
     const gpa = std.testing.allocator;
 
-    var ip: InternPool = .{};
+    var ip: InternPool = .empty;
     defer ip.deinit(gpa);
 
     const i32_type = try ip.get(gpa, .main, .{ .int_type = .{
src/Zcu.zig
@@ -116,7 +116,7 @@ embed_table: std.StringArrayHashMapUnmanaged(*EmbedFile) = .empty,
 /// Stores all Type and Value objects.
 /// The idea is that this will be periodically garbage-collected, but such logic
 /// is not yet implemented.
-intern_pool: InternPool = .{},
+intern_pool: InternPool = .empty,
 
 analysis_in_progress: std.AutoArrayHashMapUnmanaged(AnalUnit, void) = .empty,
 /// The ErrorMsg memory is owned by the `AnalUnit`, using Module's general purpose allocator.