mirror of
https://github.com/oven-sh/bun
synced 2026-02-12 11:59:00 +00:00
Fixes ENG-21287
Build times, from `bun run build && echo '//' >> src/main.zig && time
bun run build`
|Platform|0.14.1|0.15.2|Speedup|
|-|-|-|-|
|macos debug asan|126.90s|106.27s|1.19x|
|macos debug noasan|60.62s|50.85s|1.19x|
|linux debug asan|292.77s|241.45s|1.21x|
|linux debug noasan|146.58s|130.94s|1.12x|
|linux debug use_llvm=false|n/a|78.27s|1.87x|
|windows debug asan|177.13s|142.55s|1.24x|
Runtime performance:
- next build memory usage may have gone up by 5%. Otherwise seems the
same. Some code with writers may have gotten slower, especially one
instance of a counting writer and a few instances of unbuffered writers
that now have vtable overhead.
- File size reduced by 800kb (from 100.2mb to 99.4mb)
Improvements:
- `@export` hack is no longer needed for watch
- native x86_64 backend for linux builds faster. to use it, set use_llvm
false and no_link_obj false. also set `ASAN_OPTIONS=detect_leaks=0`
otherwise it will spam the output with tens of thousands of lines of
debug info errors. may need to use the zig lldb fork for debugging.
- zig test-obj, which we will be able to use for zig unit tests
Still an issue:
- false 'dependency loop' errors remain in watch mode
- watch mode crashes observed
Follow-up:
- [ ] search `comptime Writer: type` and `comptime W: type` and remove
- [ ] remove format_mode in our zig fork
- [ ] remove deprecated.zig autoFormatLabelFallback
- [ ] remove deprecated.zig autoFormatLabel
- [ ] remove deprecated.BufferedWriter and BufferedReader
- [ ] remove override_no_export_cpp_apis as it is no longer needed
- [ ] css Parser(W) -> Parser, and remove all the comptime writer: type
params
- [ ] remove deprecated writer fully
Files that add lines:
```
649 src/deprecated.zig
167 scripts/pack-codegen-for-zig-team.ts
54 scripts/cleartrace-impl.js
46 scripts/cleartrace.ts
43 src/windows.zig
18 src/fs.zig
17 src/bun.js/ConsoleObject.zig
16 src/output.zig
12 src/bun.js/test/debug.zig
12 src/bun.js/node/node_fs.zig
8 src/env_loader.zig
7 src/css/printer.zig
7 src/cli/init_command.zig
7 src/bun.js/node.zig
6 src/string/escapeRegExp.zig
6 src/install/PnpmMatcher.zig
5 src/bun.js/webcore/Blob.zig
4 src/crash_handler.zig
4 src/bun.zig
3 src/install/lockfile/bun.lock.zig
3 src/cli/update_interactive_command.zig
3 src/cli/pack_command.zig
3 build.zig
2 src/Progress.zig
2 src/install/lockfile/lockfile_json_stringify_for_debugging.zig
2 src/css/small_list.zig
2 src/bun.js/webcore/prompt.zig
1 test/internal/ban-words.test.ts
1 test/internal/ban-limits.json
1 src/watcher/WatcherTrace.zig
1 src/transpiler.zig
1 src/shell/builtin/cp.zig
1 src/js_printer.zig
1 src/io/PipeReader.zig
1 src/install/bin.zig
1 src/css/selectors/selector.zig
1 src/cli/run_command.zig
1 src/bun.js/RuntimeTranspilerStore.zig
1 src/bun.js/bindings/JSRef.zig
1 src/bake/DevServer.zig
```
Files that remove lines:
```
-1 src/test/recover.zig
-1 src/sql/postgres/SocketMonitor.zig
-1 src/sql/mysql/MySQLRequestQueue.zig
-1 src/sourcemap/CodeCoverage.zig
-1 src/css/values/color_js.zig
-1 src/compile_target.zig
-1 src/bundler/linker_context/convertStmtsForChunk.zig
-1 src/bundler/bundle_v2.zig
-1 src/bun.js/webcore/blob/read_file.zig
-1 src/ast/base.zig
-2 src/sql/postgres/protocol/ArrayList.zig
-2 src/shell/builtin/mkdir.zig
-2 src/install/PackageManager/patchPackage.zig
-2 src/install/PackageManager/PackageManagerDirectories.zig
-2 src/fmt.zig
-2 src/css/declaration.zig
-2 src/css/css_parser.zig
-2 src/collections/baby_list.zig
-2 src/bun.js/bindings/ZigStackFrame.zig
-2 src/ast/E.zig
-3 src/StandaloneModuleGraph.zig
-3 src/deps/picohttp.zig
-3 src/deps/libuv.zig
-3 src/btjs.zig
-4 src/threading/Futex.zig
-4 src/shell/builtin/touch.zig
-4 src/meta.zig
-4 src/install/lockfile.zig
-4 src/css/selectors/parser.zig
-5 src/shell/interpreter.zig
-5 src/css/error.zig
-5 src/bun.js/web_worker.zig
-5 src/bun.js.zig
-6 src/cli/test_command.zig
-6 src/bun.js/VirtualMachine.zig
-6 src/bun.js/uuid.zig
-6 src/bun.js/bindings/JSValue.zig
-9 src/bun.js/test/pretty_format.zig
-9 src/bun.js/api/BunObject.zig
-14 src/install/install_binding.zig
-14 src/fd.zig
-14 src/bun.js/node/path.zig
-14 scripts/pack-codegen-for-zig-team.sh
-17 src/bun.js/test/diff_format.zig
```
`git diff --numstat origin/main...HEAD | awk '{ print ($1-$2)"\t"$3 }' |
sort -rn`
---------
Co-authored-by: autofix-ci[bot] <114827586+autofix-ci[bot]@users.noreply.github.com>
Co-authored-by: Dylan Conway <dylan.conway567@gmail.com>
Co-authored-by: Meghan Denny <meghan@bun.com>
Co-authored-by: tayor.fish <contact@taylor.fish>
254 lines
9.1 KiB
Zig
254 lines
9.1 KiB
Zig
/// This task informs the DevServer's thread about new files to be bundled.
|
|
pub const HotReloadEvent = @This();
|
|
|
|
/// Align to cache lines to eliminate false sharing.
|
|
_: u0 align(std.atomic.cache_line) = 0,
|
|
|
|
owner: *DevServer,
|
|
/// Initialized in WatcherAtomics.watcherReleaseAndSubmitEvent
|
|
concurrent_task: jsc.ConcurrentTask,
|
|
/// The watcher is not able to peek into IncrementalGraph to know what files
|
|
/// to invalidate, so the watch events are de-duplicated and passed along.
|
|
/// The keys are owned by the file watcher.
|
|
files: bun.StringArrayHashMapUnmanaged(void),
|
|
/// Directories are watched so that resolution failures can be solved.
|
|
/// The keys are owned by the file watcher.
|
|
dirs: bun.StringArrayHashMapUnmanaged(void),
|
|
/// Same purpose as `files` but keys do not have an owner.
|
|
extra_files: std.ArrayListUnmanaged(u8),
|
|
/// Initialized by the WatcherAtomics.watcherAcquireEvent
|
|
timer: std.time.Timer,
|
|
/// This event may be referenced by either DevServer or Watcher thread.
|
|
/// 1 if referenced, 0 if unreferenced; see WatcherAtomics
|
|
contention_indicator: std.atomic.Value(u32),
|
|
|
|
debug_mutex: if (Environment.isDebug) bun.Mutex else void,
|
|
|
|
pub fn initEmpty(owner: *DevServer) HotReloadEvent {
|
|
return .{
|
|
.owner = owner,
|
|
.concurrent_task = undefined,
|
|
.files = .empty,
|
|
.dirs = .empty,
|
|
.timer = undefined,
|
|
.contention_indicator = .init(0),
|
|
.debug_mutex = if (Environment.isDebug) .{} else {},
|
|
.extra_files = .empty,
|
|
};
|
|
}
|
|
|
|
pub fn reset(ev: *HotReloadEvent) void {
|
|
if (Environment.isDebug)
|
|
ev.debug_mutex.unlock();
|
|
|
|
ev.files.clearRetainingCapacity();
|
|
ev.dirs.clearRetainingCapacity();
|
|
ev.extra_files.clearRetainingCapacity();
|
|
ev.timer = undefined;
|
|
}
|
|
|
|
pub fn isEmpty(ev: *const HotReloadEvent) bool {
|
|
return (ev.files.count() + ev.dirs.count()) == 0;
|
|
}
|
|
|
|
pub fn appendFile(event: *HotReloadEvent, allocator: Allocator, file_path: []const u8) void {
|
|
_ = bun.handleOom(event.files.getOrPut(allocator, file_path));
|
|
}
|
|
|
|
pub fn appendDir(event: *HotReloadEvent, allocator: Allocator, dir_path: []const u8, maybe_sub_path: ?[]const u8) void {
|
|
if (dir_path.len == 0) return;
|
|
_ = bun.handleOom(event.dirs.getOrPut(allocator, dir_path));
|
|
|
|
const sub_path = maybe_sub_path orelse return;
|
|
if (sub_path.len == 0) return;
|
|
|
|
const platform = bun.path.Platform.auto;
|
|
const ends_with_sep = platform.isSeparator(dir_path[dir_path.len - 1]);
|
|
const starts_with_sep = platform.isSeparator(sub_path[0]);
|
|
const sep_offset: i32 = if (ends_with_sep and starts_with_sep) -1 else 1;
|
|
|
|
bun.handleOom(event.extra_files.ensureUnusedCapacity(allocator, @intCast(@as(i32, @intCast(dir_path.len + sub_path.len)) + sep_offset + 1)));
|
|
event.extra_files.appendSliceAssumeCapacity(if (ends_with_sep) dir_path[0 .. dir_path.len - 1] else dir_path);
|
|
event.extra_files.appendAssumeCapacity(platform.separator());
|
|
event.extra_files.appendSliceAssumeCapacity(sub_path);
|
|
event.extra_files.appendAssumeCapacity(0);
|
|
}
|
|
|
|
/// Invalidates items in IncrementalGraph, appending all new items to `entry_points`
|
|
pub fn processFileList(
|
|
event: *HotReloadEvent,
|
|
dev: *DevServer,
|
|
entry_points: *EntryPointList,
|
|
temp_alloc: Allocator,
|
|
) void {
|
|
dev.graph_safety_lock.lock();
|
|
defer dev.graph_safety_lock.unlock();
|
|
|
|
// First handle directories, because this may mutate `event.files`
|
|
if (dev.directory_watchers.watches.count() > 0) for (event.dirs.keys()) |changed_dir_with_slash| {
|
|
const changed_dir = bun.strings.withoutTrailingSlashWindowsPath(changed_dir_with_slash);
|
|
|
|
// Bust resolution cache, but since Bun does not watch all
|
|
// directories in a codebase, this only targets the following resolutions
|
|
_ = dev.server_transpiler.resolver.bustDirCache(changed_dir);
|
|
|
|
// if a directory watch exists for resolution failures, check those now.
|
|
if (dev.directory_watchers.watches.getIndex(changed_dir)) |watcher_index| {
|
|
const entry = &dev.directory_watchers.watches.values()[watcher_index];
|
|
var new_chain: DirectoryWatchStore.Dep.Index.Optional = .none;
|
|
var it: ?DirectoryWatchStore.Dep.Index = entry.first_dep;
|
|
|
|
while (it) |index| {
|
|
const dep = &dev.directory_watchers.dependencies.items[index.get()];
|
|
it = dep.next.unwrap();
|
|
|
|
if ((dev.server_transpiler.resolver.resolve(
|
|
bun.path.dirname(dep.source_file_path, .auto),
|
|
dep.specifier,
|
|
.stmt,
|
|
) catch null) != null) {
|
|
// this resolution result is not preserved as passing it
|
|
// into BundleV2 is too complicated. the resolution is
|
|
// cached, anyways.
|
|
event.appendFile(dev.allocator(), dep.source_file_path);
|
|
bun.handleOom(dev.directory_watchers.freeDependencyIndex(dev.allocator(), index));
|
|
} else {
|
|
// rebuild a new linked list for unaffected files
|
|
dep.next = new_chain;
|
|
new_chain = index.toOptional();
|
|
}
|
|
}
|
|
|
|
if (new_chain.unwrap()) |new_first_dep| {
|
|
entry.first_dep = new_first_dep;
|
|
} else {
|
|
// without any files to depend on this watcher is freed
|
|
dev.directory_watchers.freeEntry(dev.allocator(), watcher_index);
|
|
}
|
|
}
|
|
};
|
|
|
|
var rest_extra = event.extra_files.items;
|
|
while (bun.strings.indexOfChar(rest_extra, 0)) |str| {
|
|
bun.handleOom(event.files.put(dev.allocator(), rest_extra[0..str], {}));
|
|
rest_extra = rest_extra[str + 1 ..];
|
|
}
|
|
if (rest_extra.len > 0) {
|
|
bun.handleOom(event.files.put(dev.allocator(), rest_extra, {}));
|
|
}
|
|
|
|
const changed_file_paths = event.files.keys();
|
|
inline for (.{ &dev.server_graph, &dev.client_graph }) |g| {
|
|
bun.handleOom(g.invalidate(changed_file_paths, entry_points, temp_alloc));
|
|
}
|
|
|
|
if (entry_points.set.count() == 0) {
|
|
Output.debugWarn("nothing to bundle", .{});
|
|
if (changed_file_paths.len > 0)
|
|
Output.debugWarn("modified files: {f}", .{
|
|
bun.fmt.fmtSlice(changed_file_paths, ", "),
|
|
});
|
|
|
|
if (event.dirs.count() > 0)
|
|
Output.debugWarn("modified dirs: {f}", .{
|
|
bun.fmt.fmtSlice(event.dirs.keys(), ", "),
|
|
});
|
|
|
|
dev.publish(.testing_watch_synchronization, &.{
|
|
MessageId.testing_watch_synchronization.char(),
|
|
1,
|
|
}, .binary);
|
|
return;
|
|
}
|
|
|
|
if (dev.has_tailwind_plugin_hack) |*map| {
|
|
for (map.keys()) |abs_path| {
|
|
const file = (dev.client_graph.bundled_files.get(abs_path) orelse continue).unpack();
|
|
if (file.kind() == .css)
|
|
bun.handleOom(entry_points.appendCss(temp_alloc, abs_path));
|
|
}
|
|
}
|
|
}
|
|
|
|
pub fn run(first: *HotReloadEvent) void {
|
|
assert(first.owner.magic == .valid);
|
|
debug.log("HMR Task start", .{});
|
|
defer debug.log("HMR Task end", .{});
|
|
|
|
const dev = first.owner;
|
|
|
|
if (comptime Environment.isDebug) {
|
|
assert(first.debug_mutex.tryLock());
|
|
assert(first.contention_indicator.load(.seq_cst) == 0);
|
|
}
|
|
|
|
if (dev.current_bundle != null) {
|
|
dev.next_bundle.reload_event = first;
|
|
return;
|
|
}
|
|
|
|
var sfb = std.heap.stackFallback(4096, dev.allocator());
|
|
const temp_alloc = sfb.get();
|
|
var entry_points: EntryPointList = .empty;
|
|
defer entry_points.deinit(temp_alloc);
|
|
|
|
first.processFileList(dev, &entry_points, temp_alloc);
|
|
|
|
const timer = first.timer;
|
|
|
|
var current = first;
|
|
while (true) {
|
|
current.processFileList(dev, &entry_points, temp_alloc);
|
|
current = dev.watcher_atomics.recycleEventFromDevServer(current) orelse break;
|
|
if (comptime Environment.isDebug) {
|
|
assert(current.debug_mutex.tryLock());
|
|
}
|
|
}
|
|
|
|
if (entry_points.set.count() == 0) {
|
|
return;
|
|
}
|
|
|
|
switch (dev.testing_batch_events) {
|
|
.disabled => {},
|
|
.enabled => |*ev| {
|
|
bun.handleOom(ev.append(dev, entry_points));
|
|
dev.publish(.testing_watch_synchronization, &.{
|
|
MessageId.testing_watch_synchronization.char(),
|
|
1,
|
|
}, .binary);
|
|
return;
|
|
},
|
|
.enable_after_bundle => bun.debugAssert(false),
|
|
}
|
|
|
|
dev.startAsyncBundle(
|
|
entry_points,
|
|
true,
|
|
timer,
|
|
) catch |err| {
|
|
bun.handleErrorReturnTrace(err, @errorReturnTrace());
|
|
return;
|
|
};
|
|
}
|
|
|
|
const bun = @import("bun");
|
|
const Environment = bun.Environment;
|
|
const Mutex = bun.Mutex;
|
|
const Output = bun.Output;
|
|
const Watcher = bun.Watcher;
|
|
const assert = bun.assert;
|
|
const bake = bun.bake;
|
|
const jsc = bun.jsc;
|
|
const BundleV2 = bun.bundle_v2.BundleV2;
|
|
|
|
const DevServer = bake.DevServer;
|
|
const DirectoryWatchStore = DevServer.DirectoryWatchStore;
|
|
const EntryPointList = DevServer.EntryPointList;
|
|
const MessageId = DevServer.MessageId;
|
|
const debug = DevServer.debug;
|
|
|
|
const std = @import("std");
|
|
const ArrayListUnmanaged = std.ArrayListUnmanaged;
|
|
const Allocator = std.mem.Allocator;
|