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>
This commit is contained in:
Mihai Parparita 2022-08-02 15:00:26 -07:00 committed by Mihai Parparita
parent 9a2171e4ea
commit f04bc31820
3 changed files with 19 additions and 10 deletions

View File

@ -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:

View File

@ -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())
}

View File

@ -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() {