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 {...@@ -80,6 +80,7 @@ pub fn main() !void {
80 .query = .{},80 .query = .{},
81 .result = try std.zig.system.resolveTargetQuery(.{}),81 .result = try std.zig.system.resolveTargetQuery(.{}),
82 },82 },
83 .dependency_cache = std.Build.InitializedDepMap.initContext(arena, .{ .allocator = arena }),
83 };84 };
8485
85 graph.cache.addPrefix(.{ .path = null, .handle = std.fs.cwd() });86 graph.cache.addPrefix(.{ .path = null, .handle = std.fs.cwd() });
lib/std/Build.zig+4-10
...@@ -90,9 +90,6 @@ modules: std.StringArrayHashMap(*Module),...@@ -90,9 +90,6 @@ modules: std.StringArrayHashMap(*Module),
9090
91named_writefiles: std.StringArrayHashMap(*Step.WriteFile),91named_writefiles: std.StringArrayHashMap(*Step.WriteFile),
92named_lazy_paths: std.StringArrayHashMap(LazyPath),92named_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,
96/// The hash of this instance's package. `""` means that this is the root package.93/// The hash of this instance's package. `""` means that this is the root package.
97pkg_hash: []const u8,94pkg_hash: []const u8,
98/// A mapping from dependency names to package hashes.95/// A mapping from dependency names to package hashes.
...@@ -125,6 +122,7 @@ pub const Graph = struct {...@@ -125,6 +122,7 @@ pub const Graph = struct {
125 host: ResolvedTarget,122 host: ResolvedTarget,
126 incremental: ?bool = null,123 incremental: ?bool = null,
127 random_seed: u32 = 0,124 random_seed: u32 = 0,
125 dependency_cache: InitializedDepMap,
128};126};
129127
130const AvailableDeps = []const struct { []const u8, []const u8 };128const AvailableDeps = []const struct { []const u8, []const u8 };
...@@ -144,7 +142,7 @@ const SystemLibraryMode = enum {...@@ -144,7 +142,7 @@ const SystemLibraryMode = enum {
144 declared_enabled,142 declared_enabled,
145};143};
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);
148const InitializedDepKey = struct {146const InitializedDepKey = struct {
149 build_root_string: []const u8,147 build_root_string: []const u8,
150 user_input_options: UserInputOptionsMap,148 user_input_options: UserInputOptionsMap,
...@@ -252,8 +250,6 @@ pub fn create(...@@ -252,8 +250,6 @@ pub fn create(
252 available_deps: AvailableDeps,250 available_deps: AvailableDeps,
253) !*Build {251) !*Build {
254 const arena = graph.arena;252 const arena = graph.arena;
255 const initialized_deps = try arena.create(InitializedDepMap);
256 initialized_deps.* = InitializedDepMap.initContext(arena, .{ .allocator = arena });
257253
258 const b = try arena.create(Build);254 const b = try arena.create(Build);
259 b.* = .{255 b.* = .{
...@@ -304,7 +300,6 @@ pub fn create(...@@ -304,7 +300,6 @@ pub fn create(
304 .modules = .init(arena),300 .modules = .init(arena),
305 .named_writefiles = .init(arena),301 .named_writefiles = .init(arena),
306 .named_lazy_paths = .init(arena),302 .named_lazy_paths = .init(arena),
307 .initialized_deps = initialized_deps,
308 .pkg_hash = "",303 .pkg_hash = "",
309 .available_deps = available_deps,304 .available_deps = available_deps,
310 .release_mode = .off,305 .release_mode = .off,
...@@ -398,7 +393,6 @@ fn createChildOnly(...@@ -398,7 +393,6 @@ fn createChildOnly(
398 .modules = .init(allocator),393 .modules = .init(allocator),
399 .named_writefiles = .init(allocator),394 .named_writefiles = .init(allocator),
400 .named_lazy_paths = .init(allocator),395 .named_lazy_paths = .init(allocator),
401 .initialized_deps = parent.initialized_deps,
402 .pkg_hash = pkg_hash,396 .pkg_hash = pkg_hash,
403 .available_deps = pkg_deps,397 .available_deps = pkg_deps,
404 .release_mode = parent.release_mode,398 .release_mode = parent.release_mode,
...@@ -2127,7 +2121,7 @@ fn dependencyInner(...@@ -2127,7 +2121,7 @@ fn dependencyInner(
2127 args: anytype,2121 args: anytype,
2128) *Dependency {2122) *Dependency {
2129 const user_input_options = userInputOptionsFromArgs(b.allocator, args);2123 const user_input_options = userInputOptionsFromArgs(b.allocator, args);
2130 if (b.initialized_deps.get(.{2124 if (b.graph.dependency_cache.get(.{
2131 .build_root_string = build_root_string,2125 .build_root_string = build_root_string,
2132 .user_input_options = user_input_options,2126 .user_input_options = user_input_options,
2133 })) |dep|2127 })) |dep|
...@@ -2155,7 +2149,7 @@ fn dependencyInner(...@@ -2155,7 +2149,7 @@ fn dependencyInner(
2155 const dep = b.allocator.create(Dependency) catch @panic("OOM");2149 const dep = b.allocator.create(Dependency) catch @panic("OOM");
2156 dep.* = .{ .builder = sub_builder };2150 dep.* = .{ .builder = sub_builder };
21572151
2158 b.initialized_deps.put(.{2152 b.graph.dependency_cache.put(.{
2159 .build_root_string = build_root_string,2153 .build_root_string = build_root_string,
2160 .user_input_options = user_input_options,2154 .user_input_options = user_input_options,
2161 }, dep) catch @panic("OOM");2155 }, dep) catch @panic("OOM");