Skip to content

Commit

Permalink
internal/filetypes: lazily load types.cue
Browse files Browse the repository at this point in the history
Don't parse and compile the CUE code until the first time it's needed.
This helps with developing or debugging the evaluator, as one can
run unit tests or the debugger on a slightly broken or instrumented
version of the evaluator without necessarily having to evaluate
internal/filetypes/types.cue before anything else.

On top of that, this shaves off about 6ms from the init time cost
of cmd/cue, which should help any command that makes no use of filetypes
to interpret arguments or files such as `cue help` or `cue login`.

                         │     old     │                 new                 │
                         │   sec/op    │   sec/op     vs base                │
    CuelangOrgGoCmdCue     7.302m ± 0%   1.425m ± 1%  -80.48% (p=0.000 n=10)

                         │      old      │                 new                  │
                         │     B/op      │     B/op      vs base                │
    CuelangOrgGoCmdCue      3.580Mi ± 0%   1.257Mi ± 0%  -64.90% (p=0.000 n=10)

                         │     old      │                 new                 │
                         │  allocs/op   │  allocs/op   vs base                │
    CuelangOrgGoCmdCue      45.50k ± 0%   10.19k ± 0%  -77.60% (p=0.000 n=10)

In the future we might try to redesign internal/filetypes so that its
logic does not depend on the evaluator, as that is a form of issue
for bootstrapping cmd/cue's basic commands. However, for now the
speed-ups in previous CLs plus the init time saving in this CL
are enough to stop filetypes from being an immediate issue.

Fixes #3024.

Signed-off-by: Daniel Martí <mvdan@mvdan.cc>
Change-Id: I24d96501a673c49e4cc59960307923fbdacbf3f6
Reviewed-on: https://review.gerrithub.io/c/cue-lang/cue/+/1193639
Unity-Result: CUE porcuepine <cue.porcuepine@gmail.com>
Reviewed-by: Roger Peppe <rogpeppe@gmail.com>
TryBot-Result: CUEcueckoo <cueckoo@cuelang.org>
  • Loading branch information
mvdan committed Apr 23, 2024
1 parent f891103 commit 2cc7b65
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 3 deletions.
3 changes: 3 additions & 0 deletions internal/filetypes/filetypes.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@ func FromFile(b *build.File, mode Mode) (*FileInfo, error) {
}, nil
}

typesInit()
modeVal := typesValue.LookupPath(cue.MakePath(cue.Str("modes"), cue.Str(mode.String())))
fileVal := modeVal.LookupPath(cue.MakePath(cue.Str("FileInfo")))
fileVal = fileVal.Fill(b)
Expand Down Expand Up @@ -161,6 +162,7 @@ func unifyWith(errs errors.Error, v1, v2 cue.Value, field, value string) (cue.Va
//
// json: foo.data bar.data json+schema: bar.schema
func ParseArgs(args []string) (files []*build.File, err error) {
typesInit()
var modeVal, fileVal cue.Value

qualifier := ""
Expand Down Expand Up @@ -254,6 +256,7 @@ func ParseFile(s string, mode Mode) (*build.File, error) {
}
// Quickly discard files which we aren't interested in.
// These cases are very common when loading `./...` in a large repository.
typesInit()
if scope == "" {
ext := fileExt(file)
if file == "-" {
Expand Down
6 changes: 3 additions & 3 deletions internal/filetypes/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ package filetypes

import (
_ "embed"
"sync"

"cuelang.org/go/cue"
"cuelang.org/go/cue/cuecontext"
Expand All @@ -27,8 +28,7 @@ var typesCUE string
var typesValue cue.Value
var knownExtensions map[string]bool

// TODO(mvdan): consider delaying this init work until it's needed
func init() {
var typesInit = sync.OnceFunc(func() {
ctx := cuecontext.New()
typesValue = ctx.CompileString(typesCUE, cue.Filename("types.cue"))
if err := typesValue.Err(); err != nil {
Expand All @@ -37,4 +37,4 @@ func init() {
if err := typesValue.LookupPath(cue.MakePath(cue.Str("knownExtensions"))).Decode(&knownExtensions); err != nil {
panic(err)
}
}
})

0 comments on commit 2cc7b65

Please sign in to comment.