internal/filetypes: replace encoding/gocode with go:embed
This package uses types.cue to describe the file types that we support,
with logic such as what extensions they match and how they behave.
We used encoding/gocode to generate a types.go file to build
a cue.Value which can then be used elsewhere in the package.
gocode is pretty advanced in terms of encoding a gzip-compressed blob
containing all the CUE files making up an instance.
Fortunately, here we have a single file, so we can use go:embed
alongside cue.Context.CompileString to build a cue.Value.
Note that we no longer compress types.cue, but that seems perfectly fine
given that it weighs 7.3KiB and is unlikely to grow much larger.
Besides no longer having to maintain gen.go, one benefit is that
the package init cost, as measured by benchinit, goes down slightly:
│ old │ new │
│ sec/op │ sec/op vs base │
CuelangOrgGoInternalFiletypes 2.350m ± 1% 2.312m ± 0% -1.60% (p=0.000 n=8)
│ old │ new │
│ B/op │ B/op vs base │
CuelangOrgGoInternalFiletypes 1.234Mi ± 0% 1.203Mi ± 0% -2.54% (p=0.000 n=8)
│ old │ new │
│ allocs/op │ allocs/op vs base │
CuelangOrgGoInternalFiletypes 17.83k ± 0% 18.34k ± 0% +2.87% (p=0.000 n=8)
It's unclear why the number of allocations goes up slightly,
but what matters is the wall clock time, as that affects how long
cmd/cue spends initializing packages before it starts doing useful work.
Another, and perhaps more relevant, advantage is a drop in the size
of cmd/cue, as compress/gzip and encoding/gob are no longer bundled.
A vanilla "go build" binary drops by 200KiB from 26.2MiB to 26.0MiB.
This also means that nowhere in the cue repo do we use encoding/gocode
alongside the cue.Value.Marshal and cue.Value.Unmarshal methods anymore.
They are still part of the public API and remain in place for now.
Finally, we rename cuegenValue as there is no longer code generation.
Note that internal/filetypes using CompileString rather than Unmarshal
means that its evaluator work now counted towards $CUE_STATS,
which we don't want to occur as it can be confusing for the end user.
Add a function to reset the global stats and call it from cmd/cue/cmd
once all init work has finished, akin to testing.B.ResetTimer.
Signed-off-by: Daniel Martí <mvdan@mvdan.cc>
Change-Id: Ic7ff13d445b46190b4809dee470dba07da8cadc9
Reviewed-on: https://review.gerrithub.io/c/cue-lang/cue/+/1190095
TryBot-Result: CUEcueckoo <cueckoo@cuelang.org>
Unity-Result: CUE porcuepine <cue.porcuepine@gmail.com>
Reviewed-by: Marcel van Lohuizen <mpvl@gmail.com>