authorgravatar for johnnymarler@gmail.comJonathan Marler <johnnymarler@gmail.com> 2024-09-27 09:35:16-06:00
committergravatar for johnnymarler@gmail.comJonathan Marler <johnnymarler@gmail.com> 2024-09-27 09:38:59-06:00
log28189b0fa5be6b56d1d9961b4e432ac7c16a80de
treea5bbba1435339ca84b9a2d065838eb401f0fe6ae
parent085cc54aadb327b9910be2c72b31ea046e7e8f52

build: move dependency cache into Graph

The dependency cache is shared amongst all Build objects. This is currently done by allocating a single instance and storing a reference to it in each Build object. However, the Graph object already exists to host shared state so by moving it there we reuse the same pattern for shared state and avoid an extra object on the heap.

2 files changed, 5 insertions(+), 10 deletions(-)

lib/compiler/build_runner.zig+1
......@@ -80,6 +80,7 @@ pub fn main() !void {
8080 .query = .{},
8181 .result = try std.zig.system.resolveTargetQuery(.{}),
8282 },
83 .dependency_cache = std.Build.InitializedDepMap.initContext(arena, .{ .allocator = arena }),
8384 };
8485
8586 graph.cache.addPrefix(.{ .path = null, .handle = std.fs.cwd() });
lib/std/Build.zig+4-10
......@@ -90,9 +90,6 @@ modules: std.StringArrayHashMap(*Module),
9090
9191named_writefiles: std.StringArrayHashMap(*Step.WriteFile),
9292named_lazy_paths: std.StringArrayHashMap(LazyPath),
93/// A map from build root dirs to the corresponding `*Dependency`. This is shared with all child
94/// `Build`s.
95initialized_deps: *InitializedDepMap,
9693/// The hash of this instance's package. `""` means that this is the root package.
9794pkg_hash: []const u8,
9895/// A mapping from dependency names to package hashes.
......@@ -125,6 +122,7 @@ pub const Graph = struct {
125122 host: ResolvedTarget,
126123 incremental: ?bool = null,
127124 random_seed: u32 = 0,
125 dependency_cache: InitializedDepMap,
128126};
129127
130128const AvailableDeps = []const struct { []const u8, []const u8 };
......@@ -144,7 +142,7 @@ const SystemLibraryMode = enum {
144142 declared_enabled,
145143};
146144
147const InitializedDepMap = std.HashMap(InitializedDepKey, *Dependency, InitializedDepContext, std.hash_map.default_max_load_percentage);
145pub const InitializedDepMap = std.HashMap(InitializedDepKey, *Dependency, InitializedDepContext, std.hash_map.default_max_load_percentage);
148146const InitializedDepKey = struct {
149147 build_root_string: []const u8,
150148 user_input_options: UserInputOptionsMap,
......@@ -252,8 +250,6 @@ pub fn create(
252250 available_deps: AvailableDeps,
253251) !*Build {
254252 const arena = graph.arena;
255 const initialized_deps = try arena.create(InitializedDepMap);
256 initialized_deps.* = InitializedDepMap.initContext(arena, .{ .allocator = arena });
257253
258254 const b = try arena.create(Build);
259255 b.* = .{
......@@ -304,7 +300,6 @@ pub fn create(
304300 .modules = .init(arena),
305301 .named_writefiles = .init(arena),
306302 .named_lazy_paths = .init(arena),
307 .initialized_deps = initialized_deps,
308303 .pkg_hash = "",
309304 .available_deps = available_deps,
310305 .release_mode = .off,
......@@ -398,7 +393,6 @@ fn createChildOnly(
398393 .modules = .init(allocator),
399394 .named_writefiles = .init(allocator),
400395 .named_lazy_paths = .init(allocator),
401 .initialized_deps = parent.initialized_deps,
402396 .pkg_hash = pkg_hash,
403397 .available_deps = pkg_deps,
404398 .release_mode = parent.release_mode,
......@@ -2127,7 +2121,7 @@ fn dependencyInner(
21272121 args: anytype,
21282122) *Dependency {
21292123 const user_input_options = userInputOptionsFromArgs(b.allocator, args);
2130 if (b.initialized_deps.get(.{
2124 if (b.graph.dependency_cache.get(.{
21312125 .build_root_string = build_root_string,
21322126 .user_input_options = user_input_options,
21332127 })) |dep|
......@@ -2155,7 +2149,7 @@ fn dependencyInner(
21552149 const dep = b.allocator.create(Dependency) catch @panic("OOM");
21562150 dep.* = .{ .builder = sub_builder };
21572151
2158 b.initialized_deps.put(.{
2152 b.graph.dependency_cache.put(.{
21592153 .build_root_string = build_root_string,
21602154 .user_input_options = user_input_options,
21612155 }, dep) catch @panic("OOM");