winglang / wing

A programming language for the cloud ☁️ A unified programming model, combining infrastructure and runtime code into one language ⚡
https://winglang.io
Other
5.02k stars 198 forks source link

Cache inflight code bundles #5524

Open Chriscbr opened 9 months ago

Chriscbr commented 9 months ago

Use Case

Improve the time it takes to compile Wing programs for resources / platforms that require inflight code bundles (e.g. AWS Lambda)

Proposed Solution

Improve bundling performance by caching repeat bundling operations with the same inputs. That is, suppose the JavaScript string we want to bundle is the same, e.g.

exports.handler = function(event) {
  const client = new (require("@aws-sdk/client-s3").S3Client)();
  console.log("hello!");
}

Then instead of performing an expensive bundling operation every compilation (in order to eventually produce a zip file or docker image with a minimal dependency closure included), we could retrieve the bundle artifact from a cache on compilations after the first.

Implementation Notes

Caching artifacts is a well established technique as demonstrated by its use in https://nx.dev/ and https://turbo.build/.

On the other hand, producing the fingerprints / hashes for caching based on files on system can be fairly expensive, and cache invalidation can be a source of new bugs. For example, in the example above, if any file inside the user's installed @aws-sdk/client-s3 dependency (or its transitive dependencies) were changed, they would want the change to be reflected in their bundle upon their next compilation.

Component

Compiler

Community Notes

skyrpex commented 8 months ago

I'd say that for npm dependencies, there's some kind of content hash in their package.json?

skyrpex commented 8 months ago

I think it's worth investigating the esbuild watch mode.

If Wing only changed files that need to be recompiled, the esbuild watcher would be quite fast recompiling the application. Of course, that would only work for wing it and would require some structural changes in how we run it from the console.