-
Notifications
You must be signed in to change notification settings - Fork 2.8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat(bundler): implement enum inlining / more constant folding #12144
Conversation
❌ @Jarred-Sumner, your commit has failing tests :( 🐧💪 1 failing tests Linux AARCH64
🐧🖥 1 failing tests Linux x64 baseline
🪟💻 4 failing tests Windows x64 baseline
🪟💻 3 failing tests Windows x64
|
76e7420
to
3aec7f1
Compare
This comment was marked as outdated.
This comment was marked as outdated.
/// by calling out to the APIs in WebKit which are responsible for this operation. | ||
/// | ||
/// This can return `null` in wasm builds to avoid linking JSC | ||
pub fn toString(this: Number, allocator: std.mem.Allocator) ?string { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
note: this is renamed from toStringSafely
to toString
, not because it is now less safe, but because the original intent of "we are taking a safer approach" does not apply.
@@ -123,7 +123,7 @@ pub fn getOSGlibCVersion(os: OperatingSystem) ?Version { | |||
} | |||
|
|||
pub fn build(b: *Build) !void { | |||
std.debug.print("zig build v{s}\n", .{builtin.zig_version_string}); | |||
std.log.info("zig compiler v{s}", .{builtin.zig_version_string}); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
apologies for doing a ton of other random things in this pr. i fixed the banned.json linter which resulted in needing to apply a ton of small transformations. i did this because i was sick of std.debug.print
showing at all in my cmd+shift+f menu. this also resulted in the deletion of some old files, which some did not even past astgen.
additionally, i have been using a client side spell-checker, so it just becomes annoying to see a ton of yellow underlines. maybe i will upstream a ci check to keep spelling somewhat intact (i have been very impressed with the typos
cli and lsp, it is very low false positive).
@compileError("This function can only be called in comptime."); | ||
} | ||
var x = 0; // if you hit an error on this line, you are not in a comptime context | ||
_ = &x; | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
comptime @inComptime
is a compiler error in 0.14 and in 0.13 does not do what we want.
@@ -46,8 +46,6 @@ pub const allow_json_single_quotes = true; | |||
|
|||
pub const react_specific_warnings = true; | |||
|
|||
pub const log_allocations = false; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this behavior is now provided via BUN_DEBUG_mimalloc=1
src/js_ast.zig
Outdated
/// generated proxy symbols that represent the property access "x3.y". This | ||
/// map is unique per namespace block because "x3" is the argument symbol that | ||
/// is specific to that particular namespace block. | ||
property_accesses: std.StringArrayHashMapUnmanaged(Ref) = .{}, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
property_accesses: std.StringArrayHashMapUnmanaged(Ref) = .{}, | |
property_accesses: bun.StringArrayHashMapUnmanaged(Ref) = .{}, |
two bugs remain
|
src/js_parser.zig
Outdated
@@ -6819,7 +7200,7 @@ fn NewParser_( | |||
} | |||
} | |||
|
|||
if (comptime !Environment.isRelease) { | |||
if (comptime Environment.allow_assert) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
if (comptime Environment.allow_assert) { | |
if (comptime Environment.isDebug) { |
@@ -8286,11 +8650,7 @@ fn NewParser_( | |||
else => { | |||
if (comptime get_metadata) { | |||
const find_result = p.findSymbol(logger.Loc.Empty, p.lexer.identifier) catch unreachable; | |||
if (p.known_enum_values.contains(find_result.ref)) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What was this change?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
known_enum_values was removed in favor of real enum inlining.
.loc = ns.name.loc, | ||
}, | ||
); | ||
// try p.ref_to_ts_namespace_member.put( |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Was this deliberately commented out?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this second put is seen in esbuild's code but i cannot think of a situation where the value is not already in the map
Initially was a PR to fix an enum bug, but it turned out the bug was due to a lack of a feature: enum inlining. So I decided to do it, since it was a feature I personally have a big use for (For example, the
bun.report
project has a regex hack workaround to force enum inlining, because the codegen otherwise is an abomination).edit: As time went, this PR turned into not just that, but a lot of changes to constant folding. i am much happier with how the bundler handles this now.
Fixes #11764
Fixes #2945
Fixes #2889
Fixes #12285
Fixes
Draft TODO:
extras:
n + "baa"
, where n is any constant number. this uses webkit's code to properly adhere to JS spec+"1.2"
. this uses webkit's code to properly adhere to JS spec--minify-syntax
, where previously it seemed to always be applied. Though keep in mind the JS runtime transpiles all files with--minify-syntax
require
,import
, and macro calls; even if minify-syntax is disabled. This was previously not an issue because it was always enabled.Simple Sample
After:
Before:
If you reference the enum directly, you can get the two-way map. This even works with
const enum
, as it is TSC's job to prohibit such usage.The codegen of these enums have been improved. Projects with heavy enum usage should see smaller bundle sizes. Now:
Before:
It would be interesting as a future project to see if the size of these enums could be shrunk further, but
esbuild
doesn't do that, and I think personally it's fine to leaveenum
in the dust asconst enum
and inlining all values becomes the new norm.