mirror of
https://github.com/oven-sh/bun
synced 2026-02-10 19:08:50 +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>
352 lines
12 KiB
Zig
352 lines
12 KiB
Zig
pub const GitResolver = struct {
|
|
resolved: string,
|
|
resolution: *const Resolution,
|
|
dep_id: DependencyID,
|
|
new_name: []u8 = "",
|
|
|
|
pub fn count(this: @This(), comptime Builder: type, builder: Builder, _: JSAst.Expr) void {
|
|
builder.count(this.resolved);
|
|
}
|
|
|
|
pub fn resolve(this: @This(), comptime Builder: type, builder: Builder, _: JSAst.Expr) !Resolution {
|
|
var resolution = this.resolution.*;
|
|
resolution.value.github.resolved = builder.append(String, this.resolved);
|
|
return resolution;
|
|
}
|
|
|
|
pub fn checkBundledDependencies() bool {
|
|
return true;
|
|
}
|
|
};
|
|
|
|
const TarballResolver = struct {
|
|
url: string,
|
|
resolution: *const Resolution,
|
|
|
|
pub fn count(this: @This(), comptime Builder: type, builder: Builder, _: JSAst.Expr) void {
|
|
builder.count(this.url);
|
|
}
|
|
|
|
pub fn resolve(this: @This(), comptime Builder: type, builder: Builder, _: JSAst.Expr) !Resolution {
|
|
var resolution = this.resolution.*;
|
|
switch (resolution.tag) {
|
|
.local_tarball => {
|
|
resolution.value.local_tarball = builder.append(String, this.url);
|
|
},
|
|
.remote_tarball => {
|
|
resolution.value.remote_tarball = builder.append(String, this.url);
|
|
},
|
|
else => unreachable,
|
|
}
|
|
return resolution;
|
|
}
|
|
|
|
pub fn checkBundledDependencies() bool {
|
|
return true;
|
|
}
|
|
};
|
|
|
|
/// Returns true if we need to drain dependencies
|
|
pub fn processExtractedTarballPackage(
|
|
manager: *PackageManager,
|
|
package_id: *PackageID,
|
|
dep_id: DependencyID,
|
|
resolution: *const Resolution,
|
|
data: *const ExtractData,
|
|
log_level: Options.LogLevel,
|
|
) ?Lockfile.Package {
|
|
switch (resolution.tag) {
|
|
.git, .github => {
|
|
var package = package: {
|
|
var resolver = GitResolver{
|
|
.resolved = data.resolved,
|
|
.resolution = resolution,
|
|
.dep_id = dep_id,
|
|
};
|
|
|
|
var pkg = Lockfile.Package{};
|
|
if (data.json) |json| {
|
|
const package_json_source = &logger.Source.initPathString(
|
|
json.path,
|
|
json.buf,
|
|
);
|
|
|
|
pkg.parse(
|
|
manager.lockfile,
|
|
manager,
|
|
manager.allocator,
|
|
manager.log,
|
|
package_json_source,
|
|
GitResolver,
|
|
&resolver,
|
|
Features.npm,
|
|
) catch |err| {
|
|
if (log_level != .silent) {
|
|
const string_buf = manager.lockfile.buffers.string_bytes.items;
|
|
Output.err(err, "failed to parse package.json for <b>{f}<r>", .{
|
|
resolution.fmtURL(string_buf),
|
|
});
|
|
}
|
|
Global.crash();
|
|
};
|
|
|
|
const has_scripts = pkg.scripts.hasAny() or brk: {
|
|
const dir = std.fs.path.dirname(json.path) orelse "";
|
|
const binding_dot_gyp_path = Path.joinAbsStringZ(
|
|
dir,
|
|
&[_]string{"binding.gyp"},
|
|
.auto,
|
|
);
|
|
|
|
break :brk Syscall.exists(binding_dot_gyp_path);
|
|
};
|
|
|
|
pkg.meta.setHasInstallScript(has_scripts);
|
|
break :package pkg;
|
|
}
|
|
|
|
// package.json doesn't exist, no dependencies to worry about but we need to decide on a name for the dependency
|
|
var repo = switch (resolution.tag) {
|
|
.git => resolution.value.git,
|
|
.github => resolution.value.github,
|
|
else => unreachable,
|
|
};
|
|
|
|
const new_name = Repository.createDependencyNameFromVersionLiteral(manager.allocator, &repo, manager.lockfile, dep_id);
|
|
defer manager.allocator.free(new_name);
|
|
|
|
{
|
|
var builder = manager.lockfile.stringBuilder();
|
|
|
|
builder.count(new_name);
|
|
resolver.count(*Lockfile.StringBuilder, &builder, undefined);
|
|
|
|
bun.handleOom(builder.allocate());
|
|
|
|
const name = builder.append(ExternalString, new_name);
|
|
pkg.name = name.value;
|
|
pkg.name_hash = name.hash;
|
|
|
|
pkg.resolution = resolver.resolve(*Lockfile.StringBuilder, &builder, undefined) catch unreachable;
|
|
}
|
|
|
|
break :package pkg;
|
|
};
|
|
|
|
package = manager.lockfile.appendPackage(package) catch unreachable;
|
|
package_id.* = package.meta.id;
|
|
|
|
if (package.dependencies.len > 0) {
|
|
bun.handleOom(manager.lockfile.scratch.dependency_list_queue.writeItem(package.dependencies));
|
|
}
|
|
|
|
return package;
|
|
},
|
|
.local_tarball, .remote_tarball => {
|
|
const json = data.json.?;
|
|
const package_json_source = &logger.Source.initPathString(
|
|
json.path,
|
|
json.buf,
|
|
);
|
|
var package = Lockfile.Package{};
|
|
|
|
var resolver: TarballResolver = .{
|
|
.url = data.url,
|
|
.resolution = resolution,
|
|
};
|
|
|
|
package.parse(
|
|
manager.lockfile,
|
|
manager,
|
|
manager.allocator,
|
|
manager.log,
|
|
package_json_source,
|
|
TarballResolver,
|
|
&resolver,
|
|
Features.npm,
|
|
) catch |err| {
|
|
if (log_level != .silent) {
|
|
const string_buf = manager.lockfile.buffers.string_bytes.items;
|
|
Output.prettyErrorln("<r><red>error:<r> expected package.json in <b>{f}<r> to be a JSON file: {s}\n", .{
|
|
resolution.fmtURL(string_buf),
|
|
@errorName(err),
|
|
});
|
|
}
|
|
Global.crash();
|
|
};
|
|
|
|
const has_scripts = package.scripts.hasAny() or brk: {
|
|
const dir = std.fs.path.dirname(json.path) orelse "";
|
|
const binding_dot_gyp_path = Path.joinAbsStringZ(
|
|
dir,
|
|
&[_]string{"binding.gyp"},
|
|
.auto,
|
|
);
|
|
|
|
break :brk Syscall.exists(binding_dot_gyp_path);
|
|
};
|
|
|
|
package.meta.setHasInstallScript(has_scripts);
|
|
|
|
package = manager.lockfile.appendPackage(package) catch unreachable;
|
|
package_id.* = package.meta.id;
|
|
|
|
if (package.dependencies.len > 0) {
|
|
bun.handleOom(manager.lockfile.scratch.dependency_list_queue.writeItem(package.dependencies));
|
|
}
|
|
|
|
return package;
|
|
},
|
|
else => if (data.json.?.buf.len > 0) {
|
|
const json = data.json.?;
|
|
const package_json_source = &logger.Source.initPathString(
|
|
json.path,
|
|
json.buf,
|
|
);
|
|
initializeStore();
|
|
const json_root = JSON.parsePackageJSONUTF8(
|
|
package_json_source,
|
|
manager.log,
|
|
manager.allocator,
|
|
) catch |err| {
|
|
if (log_level != .silent) {
|
|
const string_buf = manager.lockfile.buffers.string_bytes.items;
|
|
Output.prettyErrorln("<r><red>error:<r> expected package.json in <b>{f}<r> to be a JSON file: {s}\n", .{
|
|
resolution.fmtURL(string_buf),
|
|
@errorName(err),
|
|
});
|
|
}
|
|
Global.crash();
|
|
};
|
|
var builder = manager.lockfile.stringBuilder();
|
|
Lockfile.Package.Scripts.parseCount(manager.allocator, &builder, json_root);
|
|
builder.allocate() catch unreachable;
|
|
if (comptime Environment.allow_assert) bun.assert(package_id.* != invalid_package_id);
|
|
var scripts = manager.lockfile.packages.items(.scripts)[package_id.*];
|
|
scripts.parseAlloc(manager.allocator, &builder, json_root);
|
|
scripts.filled = true;
|
|
},
|
|
}
|
|
|
|
return null;
|
|
}
|
|
|
|
pub fn processDependencyListItem(
|
|
this: *PackageManager,
|
|
item: TaskCallbackContext,
|
|
any_root: ?*bool,
|
|
install_peer: bool,
|
|
) !void {
|
|
switch (item) {
|
|
.dependency => |dependency_id| {
|
|
const dependency = this.lockfile.buffers.dependencies.items[dependency_id];
|
|
const resolution = this.lockfile.buffers.resolutions.items[dependency_id];
|
|
|
|
try this.enqueueDependencyWithMain(
|
|
dependency_id,
|
|
&dependency,
|
|
resolution,
|
|
install_peer,
|
|
);
|
|
},
|
|
.root_dependency => |dependency_id| {
|
|
const dependency = this.lockfile.buffers.dependencies.items[dependency_id];
|
|
const resolution = this.lockfile.buffers.resolutions.items[dependency_id];
|
|
|
|
try this.enqueueDependencyWithMainAndSuccessFn(
|
|
dependency_id,
|
|
&dependency,
|
|
resolution,
|
|
install_peer,
|
|
assignRootResolution,
|
|
failRootResolution,
|
|
);
|
|
if (any_root) |ptr| {
|
|
const new_resolution_id = this.lockfile.buffers.resolutions.items[dependency_id];
|
|
if (new_resolution_id != resolution) {
|
|
ptr.* = true;
|
|
}
|
|
}
|
|
},
|
|
else => {},
|
|
}
|
|
}
|
|
|
|
pub fn processPeerDependencyList(
|
|
this: *PackageManager,
|
|
) !void {
|
|
while (this.peer_dependencies.readItem()) |peer_dependency_id| {
|
|
const dependency = this.lockfile.buffers.dependencies.items[peer_dependency_id];
|
|
const resolution = this.lockfile.buffers.resolutions.items[peer_dependency_id];
|
|
|
|
try this.enqueueDependencyWithMain(
|
|
peer_dependency_id,
|
|
&dependency,
|
|
resolution,
|
|
true,
|
|
);
|
|
}
|
|
}
|
|
|
|
pub fn processDependencyList(
|
|
this: *PackageManager,
|
|
dep_list: TaskCallbackList,
|
|
comptime Ctx: type,
|
|
ctx: Ctx,
|
|
comptime callbacks: anytype,
|
|
install_peer: bool,
|
|
) !void {
|
|
if (dep_list.items.len > 0) {
|
|
var dependency_list = dep_list;
|
|
var any_root = false;
|
|
for (dependency_list.items) |item| {
|
|
try this.processDependencyListItem(item, &any_root, install_peer);
|
|
}
|
|
|
|
if (comptime @TypeOf(callbacks) != void and @TypeOf(callbacks.onResolve) != void) {
|
|
if (any_root) {
|
|
callbacks.onResolve(ctx);
|
|
}
|
|
}
|
|
|
|
dependency_list.deinit(this.allocator);
|
|
}
|
|
}
|
|
|
|
const string = []const u8;
|
|
|
|
const std = @import("std");
|
|
|
|
const bun = @import("bun");
|
|
const Environment = bun.Environment;
|
|
const Global = bun.Global;
|
|
const JSAst = bun.ast;
|
|
const JSON = bun.json;
|
|
const Output = bun.Output;
|
|
const Path = bun.path;
|
|
const Syscall = bun.sys;
|
|
const logger = bun.logger;
|
|
|
|
const Semver = bun.Semver;
|
|
const ExternalString = Semver.ExternalString;
|
|
const String = Semver.String;
|
|
|
|
const DependencyID = bun.install.DependencyID;
|
|
const ExtractData = bun.install.ExtractData;
|
|
const Features = bun.install.Features;
|
|
const PackageID = bun.install.PackageID;
|
|
const Repository = bun.install.Repository;
|
|
const Resolution = bun.install.Resolution;
|
|
const TaskCallbackContext = bun.install.TaskCallbackContext;
|
|
const initializeStore = bun.install.initializeStore;
|
|
const invalid_package_id = bun.install.invalid_package_id;
|
|
|
|
const Lockfile = bun.install.Lockfile;
|
|
const Package = Lockfile.Package;
|
|
|
|
const PackageManager = bun.install.PackageManager;
|
|
const Options = PackageManager.Options;
|
|
const TaskCallbackList = PackageManager.TaskCallbackList;
|
|
const assignRootResolution = PackageManager.assignRootResolution;
|
|
const failRootResolution = PackageManager.failRootResolution;
|