From f04bc3182005d874a105592b201404448bdaf05d Mon Sep 17 00:00:00 2001 From: Mihai Parparita Date: Tue, 2 Aug 2022 15:00:26 -0700 Subject: [PATCH] cmd/tsconnect: add -fast-compression option Changes Gzip and Brotli to optimize for speed instead of size. This signficantly speeds up Brotli, and is useful when iterating locally or running the build during a CI job (where we just care that it can successfully build). Signed-off-by: Mihai Parparita --- .github/workflows/cross-wasm.yml | 2 +- cmd/tsconnect/build.go | 20 ++++++++++++++------ cmd/tsconnect/tsconnect.go | 7 ++++--- 3 files changed, 19 insertions(+), 10 deletions(-) diff --git a/.github/workflows/cross-wasm.yml b/.github/workflows/cross-wasm.yml index 9d729a7f7..b188940fb 100644 --- a/.github/workflows/cross-wasm.yml +++ b/.github/workflows/cross-wasm.yml @@ -34,7 +34,7 @@ jobs: - name: tsconnect static build # Use our custom Go toolchain, we set build tags (to control binary size) # that depend on it. - run: ./tool/go run ./cmd/tsconnect build + run: ./tool/go run ./cmd/tsconnect --fast-compression build - uses: k0kubun/action-slack@v2.0.0 with: diff --git a/cmd/tsconnect/build.go b/cmd/tsconnect/build.go index 570f3038a..933c953cb 100644 --- a/cmd/tsconnect/build.go +++ b/cmd/tsconnect/build.go @@ -71,7 +71,7 @@ func runBuild() { log.Fatalf("Cannot write metadata: %v", err) } - if er := precompressDist(); err != nil { + if er := precompressDist(*fastCompression); err != nil { log.Fatalf("Cannot precompress resources: %v", er) } } @@ -125,7 +125,7 @@ func cleanDist() error { return nil } -func precompressDist() error { +func precompressDist(fastCompression bool) error { log.Printf("Pre-compressing files in %s/...\n", *distDir) var eg errgroup.Group err := fs.WalkDir(os.DirFS(*distDir), ".", func(p string, d fs.DirEntry, err error) error { @@ -142,7 +142,7 @@ func precompressDist() error { log.Printf("Pre-compressing %v\n", p) eg.Go(func() error { - return precompress(p) + return precompress(p, fastCompression) }) return nil }) @@ -158,7 +158,7 @@ func precompressDist() error { ".wasm": true, } -func precompress(path string) error { +func precompress(path string, fastCompression bool) error { contents, err := os.ReadFile(path) if err != nil { return err @@ -168,14 +168,22 @@ func precompress(path string) error { return err } + gzipLevel := gzip.BestCompression + if fastCompression { + gzipLevel = gzip.BestSpeed + } err = writeCompressed(contents, func(w io.Writer) (io.WriteCloser, error) { - return gzip.NewWriterLevel(w, gzip.BestCompression) + return gzip.NewWriterLevel(w, gzipLevel) }, path+".gz", fi.Mode()) if err != nil { return err } + brotliLevel := brotli.BestCompression + if fastCompression { + brotliLevel = brotli.BestSpeed + } return writeCompressed(contents, func(w io.Writer) (io.WriteCloser, error) { - return brotli.NewWriterLevel(w, brotli.BestCompression), nil + return brotli.NewWriterLevel(w, brotliLevel), nil }, path+".br", fi.Mode()) } diff --git a/cmd/tsconnect/tsconnect.go b/cmd/tsconnect/tsconnect.go index 1fb183deb..099cc973c 100644 --- a/cmd/tsconnect/tsconnect.go +++ b/cmd/tsconnect/tsconnect.go @@ -18,9 +18,10 @@ ) var ( - addr = flag.String("addr", ":9090", "address to listen on") - distDir = flag.String("distdir", "./dist", "path of directory to place build output in") - yarnPath = flag.String("yarnpath", "../../tool/yarn", "path yarn executable used to install JavaScript dependencies") + addr = flag.String("addr", ":9090", "address to listen on") + distDir = flag.String("distdir", "./dist", "path of directory to place build output in") + yarnPath = flag.String("yarnpath", "../../tool/yarn", "path yarn executable used to install JavaScript dependencies") + fastCompression = flag.Bool("fast-compression", false, "Use faster compression when building, to speed up build time. Meant to iterative/debugging use only.") ) func main() {