Skip to content

Introduce bytecode caching, polish "cjs" bundler output format - #14232

Merged
Jarred-Sumner merged 29 commits into
mainfrom
jarred/bytecode
Oct 1, 2024
Merged

Jarred-Sumner merged 29 commits into
mainfrom
jarred/bytecode

Conversation

@Jarred-Sumner

@Jarred-Sumner Jarred-Sumner commented Sep 28, 2024 •

Copy link
Copy Markdown
Collaborator

What does this PR do?

This adds a new --bytecode flag, and // @bun @bun-cjs comment that marks a file as already transpiled with CommonJS.

bun build --compile --bytecode <script>
bun build --target=bun --bytecode --outdir=out <script>

2x faster tsc --help

image

When you use --bytecode without --compile, it adds .jsc files which contain the compiled bytecode. When the bytecode is unable to be loaded, it uses the source code. Bytecode compilation currently has no impact on async functions and no impact on ESM.

How did you verify your code works?

Added tests.

@robobun

robobun commented Sep 28, 2024 •

Copy link
Copy Markdown
Collaborator

@Jarred-Sumner Jarred-Sumner changed the title Introduce bytecode caching Introduce bytecode caching and implement polish "cjs" bundler output format Sep 30, 2024
@Jarred-Sumner Jarred-Sumner changed the title Introduce bytecode caching and implement polish "cjs" bundler output format Introduce bytecode caching and polish "cjs" bundler output format Sep 30, 2024
target: "node",
run: {
stdout: "undefined undefined",
stdout: "true true",

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ooh i like this. import.meta should just work, aliasing to __dirname and __filename.

this test however should

  • run in node to ensure import.meta is not actually emitted. you can do this by setting runtime: "node" in the run object.
  • fs.existsSync is not a good enough behavior test. what if the path is /. it should be a string that is checked for exactly. if you turn the config object passed to itBundled into a function with signature (({ root }) => ({ files: ... you can use that path to write exact expectations for the absolute paths. see tsconfig/Path for more details on this api.

though after irl mention of this, it seems we are emitting import.meta, so maybe we just make this behavior error since that is likely easier.

},
target: "node",
format: "cjs",
bundleErrors: {

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

amazing

// format: "cjs",
// platform: "node",
// });
itBundled("default/RequireMainCacheCommonJS", {

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Paperdave we fixed this since that test was commented out, pretty sure

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

great

format: "iife",
});
itBundled("importstar/ExportSelfIIFEWithName", {
todo: true,

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i'm scoping out support for IIFE in this PR

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

reasonable.

@Jarred-Sumner Jarred-Sumner changed the title Introduce bytecode caching and polish "cjs" bundler output format Introduce bytecode caching, polish "cjs" bundler output format Sep 30, 2024
Comment thread src/js/builtins/Module.ts
// If we can pull out a ModuleNamespaceObject, let's do it.
if (esm?.evaluated && (esm.state ?? 0) >= $ModuleReady) {
const namespace = Loader.getModuleNamespaceObject(esm!.module);
return (mod.exports =

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

cc @Paperdave @dylan-conway please take a look at this

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what is the reasoning behind this change. it is breaking.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Discussed IRL. The goal of this, originally, was to let you mutate values on the ES module namespace object. But, that doesn't work and has not worked.

Instead, this has the effect of making console.log and (...) not work as well

@Jarred-Sumner
Jarred-Sumner marked this pull request as ready for review September 30, 2024 11:17
Comment thread src/bun.js/javascript.zig
return promise;
} else {
const promise = JSModuleLoader.loadAndEvaluateModule(this.global, &String.init(this.main)) orelse return error.JSError;
const promise = JSModuleLoader.loadAndEvaluateModule(this.global, &String.fromBytes(this.main)) orelse return error.JSError;

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This change looks identical to the original

.already_bundled = true,
.hash = 0,
.bytecode_cache = if (bytecode_slice.len > 0) bytecode_slice.ptr else null,
.bytecode_cache_size = if (bytecode_slice.len > 0) bytecode_slice.len else 0,

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
.bytecode_cache_size = if (bytecode_slice.len > 0) bytecode_slice.len else 0,
.bytecode_cache_size = bytecode_slice.len,

Comment thread src/bun.js/module_loader.zig Resolved
Comment thread src/bun.js/api/JSBundler.zig Resolved
Comment thread src/js/builtins/Module.ts
// If we can pull out a ModuleNamespaceObject, let's do it.
if (esm?.evaluated && (esm.state ?? 0) >= $ModuleReady) {
const namespace = Loader.getModuleNamespaceObject(esm!.module);
return (mod.exports =

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what is the reasoning behind this change. it is breaking.

Comment thread src/bundler/bundle_v2.zig Resolved
target: "node",
run: {
stdout: "undefined undefined",
stdout: "true true",

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ooh i like this. import.meta should just work, aliasing to __dirname and __filename.

this test however should

  • run in node to ensure import.meta is not actually emitted. you can do this by setting runtime: "node" in the run object.
  • fs.existsSync is not a good enough behavior test. what if the path is /. it should be a string that is checked for exactly. if you turn the config object passed to itBundled into a function with signature (({ root }) => ({ files: ... you can use that path to write exact expectations for the absolute paths. see tsconfig/Path for more details on this api.

though after irl mention of this, it seems we are emitting import.meta, so maybe we just make this behavior error since that is likely easier.

},
target: "node",
format: "cjs",
bundleErrors: {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

amazing

// format: "cjs",
// platform: "node",
// });
itBundled("default/RequireMainCacheCommonJS", {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

great

format: "iife",
});
itBundled("importstar/ExportSelfIIFEWithName", {
todo: true,

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

reasonable.

Comment thread test/bundler/expectBundled.ts Resolved
Comment thread src/runtime.zig Resolved
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants