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 <mihai@tailscale.com>
pull/5278/head
Mihai Parparita 2 years ago committed by Mihai Parparita
parent 9a2171e4ea
commit f04bc31820

@ -34,7 +34,7 @@ jobs:
- name: tsconnect static build - name: tsconnect static build
# Use our custom Go toolchain, we set build tags (to control binary size) # Use our custom Go toolchain, we set build tags (to control binary size)
# that depend on it. # 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 - uses: k0kubun/action-slack@v2.0.0
with: with:

@ -71,7 +71,7 @@ func runBuild() {
log.Fatalf("Cannot write metadata: %v", err) 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) log.Fatalf("Cannot precompress resources: %v", er)
} }
} }
@ -125,7 +125,7 @@ func cleanDist() error {
return nil return nil
} }
func precompressDist() error { func precompressDist(fastCompression bool) error {
log.Printf("Pre-compressing files in %s/...\n", *distDir) log.Printf("Pre-compressing files in %s/...\n", *distDir)
var eg errgroup.Group var eg errgroup.Group
err := fs.WalkDir(os.DirFS(*distDir), ".", func(p string, d fs.DirEntry, err error) error { 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) log.Printf("Pre-compressing %v\n", p)
eg.Go(func() error { eg.Go(func() error {
return precompress(p) return precompress(p, fastCompression)
}) })
return nil return nil
}) })
@ -158,7 +158,7 @@ var compressibleExtensions = map[string]bool{
".wasm": true, ".wasm": true,
} }
func precompress(path string) error { func precompress(path string, fastCompression bool) error {
contents, err := os.ReadFile(path) contents, err := os.ReadFile(path)
if err != nil { if err != nil {
return err return err
@ -168,14 +168,22 @@ func precompress(path string) error {
return err return err
} }
gzipLevel := gzip.BestCompression
if fastCompression {
gzipLevel = gzip.BestSpeed
}
err = writeCompressed(contents, func(w io.Writer) (io.WriteCloser, error) { 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()) }, path+".gz", fi.Mode())
if err != nil { if err != nil {
return err return err
} }
brotliLevel := brotli.BestCompression
if fastCompression {
brotliLevel = brotli.BestSpeed
}
return writeCompressed(contents, func(w io.Writer) (io.WriteCloser, error) { 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()) }, path+".br", fi.Mode())
} }

@ -18,9 +18,10 @@ import (
) )
var ( var (
addr = flag.String("addr", ":9090", "address to listen on") addr = flag.String("addr", ":9090", "address to listen on")
distDir = flag.String("distdir", "./dist", "path of directory to place build output in") 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") 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() { func main() {

Loading…
Cancel
Save