Skip to content

Commit 3b10030

Browse files
committed
encoding/jsonschema: parallelize vendor_external.go a bit
Fetching the zip file from upstream takes ~150ms, and reading and updating the test data takes ~400ms. Given that all `go generate ./...` steps happen sequentially, cutting down the runtime cost even by 150ms is noticeable. │ old │ new │ │ sec/op │ sec/op vs base │ VendorExternal 900.5m ± 8% 751.2m ± 3% -16.58% (p=0.000 n=8) Signed-off-by: Daniel Martí <[email protected]> Change-Id: If6e23e1fcde00b4ace8e0a18e71826f952ed2b18 Reviewed-on: https://review.gerrithub.io/c/cue-lang/cue/+/1217835 Unity-Result: CUE porcuepine <[email protected]> Reviewed-by: Roger Peppe <[email protected]> TryBot-Result: CUEcueckoo <[email protected]>
1 parent 206e824 commit 3b10030

File tree

1 file changed

+42
-30
lines changed

1 file changed

+42
-30
lines changed

encoding/jsonschema/vendor_external.go

Lines changed: 42 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,8 @@ import (
3535
"path/filepath"
3636
"strings"
3737

38+
"golang.org/x/sync/errgroup"
39+
3840
"cuelang.org/go/encoding/jsonschema/internal/externaltest"
3941
)
4042

@@ -59,24 +61,45 @@ func main() {
5961
}
6062

6163
func doVendor(commit string) error {
62-
// Fetch a commit from GitHub via their archive ZIP endpoint, which is a lot faster
63-
// than git cloning just to retrieve a single commit's files.
64-
// See: https://docs.github.com/en/rest/repos/contents?apiVersion=2022-11-28#download-a-repository-archive-zip
65-
zipURL := fmt.Sprintf("https://github.com/json-schema-org/JSON-Schema-Test-Suite/archive/%s.zip", commit)
66-
log.Printf("fetching %s", zipURL)
67-
resp, err := http.Get(zipURL)
68-
if err != nil {
69-
return err
70-
}
71-
defer resp.Body.Close()
72-
zipBytes, err := io.ReadAll(resp.Body)
73-
if err != nil {
64+
// Reading the old test data and fetching a zip file for the upstream data can be done in parallel.
65+
// This is useful as each operation takes hundreds of milliseconds.
66+
g := new(errgroup.Group)
67+
var oldTests map[string][]*externaltest.Schema
68+
g.Go(func() error {
69+
log.Printf("reading old test data")
70+
var err error
71+
oldTests, err = externaltest.ReadTestDir(testDir)
72+
if err != nil && !errors.Is(err, externaltest.ErrNotFound) {
73+
return err
74+
}
75+
return nil
76+
})
77+
var fsys fs.FS
78+
g.Go(func() error {
79+
// Fetch a commit from GitHub via their archive ZIP endpoint, which is a lot faster
80+
// than git cloning just to retrieve a single commit's files.
81+
// See: https://docs.github.com/en/rest/repos/contents?apiVersion=2022-11-28#download-a-repository-archive-zip
82+
zipURL := fmt.Sprintf("https://github.com/json-schema-org/JSON-Schema-Test-Suite/archive/%s.zip", commit)
83+
log.Printf("fetching %s", zipURL)
84+
resp, err := http.Get(zipURL)
85+
if err != nil {
86+
return err
87+
}
88+
defer resp.Body.Close()
89+
zipBytes, err := io.ReadAll(resp.Body)
90+
if err != nil {
91+
return err
92+
}
93+
zipr, err := zip.NewReader(bytes.NewReader(zipBytes), int64(len(zipBytes)))
94+
if err != nil {
95+
return err
96+
}
97+
// Note that GitHub produces archives with a top-level directory representing
98+
// the name of the repository and the version which was retrieved.
99+
fsys, err = fs.Sub(zipr, fmt.Sprintf("JSON-Schema-Test-Suite-%s/tests", commit))
74100
return err
75-
}
76-
77-
log.Printf("reading old test data")
78-
oldTests, err := externaltest.ReadTestDir(testDir)
79-
if err != nil && !errors.Is(err, externaltest.ErrNotFound) {
101+
})
102+
if err := g.Wait(); err != nil {
80103
return err
81104
}
82105

@@ -85,17 +108,7 @@ func doVendor(commit string) error {
85108
if err := os.RemoveAll(testSubdir); err != nil {
86109
return err
87110
}
88-
zipr, err := zip.NewReader(bytes.NewReader(zipBytes), int64(len(zipBytes)))
89-
if err != nil {
90-
return err
91-
}
92-
// Note that GitHub produces archives with a top-level directory representing
93-
// the name of the repository and the version which was retrieved.
94-
fsys, err := fs.Sub(zipr, fmt.Sprintf("JSON-Schema-Test-Suite-%s/tests", commit))
95-
if err != nil {
96-
return err
97-
}
98-
err = fs.WalkDir(fsys, ".", func(filename string, d fs.DirEntry, err error) error {
111+
if err := fs.WalkDir(fsys, ".", func(filename string, d fs.DirEntry, err error) error {
99112
if err != nil {
100113
return err
101114
}
@@ -121,8 +134,7 @@ func doVendor(commit string) error {
121134
return err
122135
}
123136
return nil
124-
})
125-
if err != nil {
137+
}); err != nil {
126138
return err
127139
}
128140

0 commit comments

Comments
 (0)