Repository navigation
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.
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.
|
|
||
| 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.
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).
| } | ||
| 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.
comptime @inComptime is a compiler error in 0.14 and in 0.13 does not do what we want.
|
|
||
| pub const react_specific_warnings = true; | ||
|
|
||
| pub const log_allocations = false; |
There was a problem hiding this comment.
this behavior is now provided via BUN_DEBUG_mimalloc=1
| /// 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.
| property_accesses: std.StringArrayHashMapUnmanaged(Ref) = .{}, | |
| property_accesses: bun.StringArrayHashMapUnmanaged(Ref) = .{}, |
|
two bugs remain
|
| @@ -6819,7 +7200,7 @@ fn NewParser_( | |||
| } | |||
| } | |||
|
|
|||
| if (comptime !Environment.isRelease) { | |||
| if (comptime Environment.allow_assert) { | |||
There was a problem hiding this comment.
| 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.
What was this change?
There was a problem hiding this comment.
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.
Was this deliberately commented out?
There was a problem hiding this comment.
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.reportproject 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-syntaxrequire,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
esbuilddoesn't do that, and I think personally it's fine to leaveenumin the dust asconst enumand inlining all values becomes the new norm.