Skip to content

Commit

Permalink
schemas: compress schema files to reduce LS binary size
Browse files Browse the repository at this point in the history
vsfsgen used GZIP compression by default but we abandoned it in favour of the stdlib embed package in #1070. This alone resulted in 80M binary size (compared to 18M before). Breaking down schemas to individual files would further increase binary size to 160M. In either case this is well over the limit of what we can pack into VSIX (VS Code extension) - currently 30MB.

Using gzip compression can bring the size to 19M. Mentioned sizes reflect darwin/arm64, but differences on other platforms should be similar.
  • Loading branch information
radeksimko committed Oct 4, 2022
1 parent a2b6811 commit 2037051
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 10 deletions.
27 changes: 20 additions & 7 deletions internal/schemas/gen/gen.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
package main

import (
"bytes"
"compress/gzip"
"context"
"encoding/json"
"fmt"
Expand Down Expand Up @@ -200,9 +202,9 @@ func gen() error {
continue
}

log.Printf("%s: obtained schema for %s (%d bytes); terraform init: %s",
log.Printf("%s: obtained schema for %s (%db raw / %db compressed); terraform init: %s",
input.Provider.Addr.ForDisplay(), details.Version,
details.Size, details.InitElapsedTime)
details.RawSize, details.CompressedSize, details.InitElapsedTime)
}
}(i)
}
Expand All @@ -222,7 +224,8 @@ type Inputs struct {

type Outputs struct {
Version string
Size int64
RawSize int
CompressedSize int64
InitElapsedTime time.Duration
}

Expand Down Expand Up @@ -358,15 +361,24 @@ func schemaForProvider(ctx context.Context, client registry.Client, input Inputs
return nil, err
}

f, err := os.Create(filepath.Join(dataDir, "schema.json"))
f, err := os.Create(filepath.Join(dataDir, "schema.json.gz"))
if err != nil {
return nil, fmt.Errorf("failed to create schema file: %w", err)
}

err = json.NewEncoder(f).Encode(ps)
var rawJson bytes.Buffer
err = json.NewEncoder(&rawJson).Encode(ps)
if err != nil {
return nil, fmt.Errorf("failed to encode schema file: %w", err)
}
gzw := gzip.NewWriter(f)
_, err = gzw.Write(rawJson.Bytes())
if err != nil {
return nil, fmt.Errorf("failed to write compressed file: %w", err)
}
err = gzw.Close()
if err != nil {
return nil, fmt.Errorf("failed to close compressed file: %w", err)
}

fi, err := f.Stat()
if err != nil {
Expand All @@ -375,7 +387,8 @@ func schemaForProvider(ctx context.Context, client registry.Client, input Inputs

return &Outputs{
Version: pVersion.String(),
Size: fi.Size(),
RawSize: rawJson.Len(),
CompressedSize: fi.Size(),
InitElapsedTime: initElapsed,
}, nil
}
Expand Down
15 changes: 12 additions & 3 deletions internal/schemas/schemas.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
package schemas

import (
"compress/gzip"
"embed"
"errors"
"fmt"
"io"
"io/fs"
"log"
"path"

"github.com/hashicorp/go-version"
Expand All @@ -15,7 +18,7 @@ import (
var FS embed.FS

type ProviderSchema struct {
File fs.File
File io.Reader
Version *version.Version
}

Expand Down Expand Up @@ -44,7 +47,7 @@ func FindProviderSchemaFile(filesystem fs.ReadDirFS, pAddr tfaddr.Provider) (*Pr

rawVersion := entries[0].Name()

filePath := path.Join(providerPath, rawVersion, "schema.json")
filePath := path.Join(providerPath, rawVersion, "schema.json.gz")
file, err := filesystem.Open(filePath)
if err != nil {
return nil, err
Expand All @@ -55,8 +58,14 @@ func FindProviderSchemaFile(filesystem fs.ReadDirFS, pAddr tfaddr.Provider) (*Pr
return nil, err
}

gzipReader, err := gzip.NewReader(file)
if err != nil {
return nil, err
}
log.Printf("unzipped file at %s", filePath)

return &ProviderSchema{
File: file,
File: gzipReader,
Version: version,
}, nil
}

0 comments on commit 2037051

Please sign in to comment.