Skip to content

Commit

Permalink
fix(runtime): move semantic analysis to the finalize step
Browse files Browse the repository at this point in the history
This moves the semantic analysis to the finalize step like it was
previously. For applications that avoid finalizing unless they are using
the interpreter, this should restore the quicker startup time because
package registration will store the AST and move on.

In the longer term, we probably want to stop using `init()` so much and
switch to a system where the application explicitly pulls packages into
the runtime. For now, this is more simple and doesn't involve any
changes to existing applications and restores the previous speed for
commands that do not use flux but import the flux runtime.
  • Loading branch information
jsternberg committed Jun 29, 2020
1 parent d2dde4a commit 6812212
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 17 deletions.
1 change: 1 addition & 0 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ jobs:
test-race:
docker:
- image: quay.io/influxdb/flux-build:latest
resource_class: large
environment:
GOPATH: /tmp/go
GOFLAGS: -p=8
Expand Down
49 changes: 32 additions & 17 deletions runtime/runtime.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ var Default = &runtime{}
// runtime contains the flux runtime for interpreting and
// executing queries.
type runtime struct {
astPkgs map[string]*ast.Package
pkgs map[string]*semantic.Package
builtins map[string]map[string]values.Value
finalized bool
Expand Down Expand Up @@ -56,32 +57,19 @@ func (r *runtime) RegisterPackage(pkg *ast.Package) error {
return errors.New(codes.Internal, "already finalized, cannot register builtin package")
}

if r.pkgs == nil {
r.pkgs = make(map[string]*semantic.Package)
if r.astPkgs == nil {
r.astPkgs = make(map[string]*ast.Package)
}

if _, ok := r.pkgs[pkg.Path]; ok {
if _, ok := r.astPkgs[pkg.Path]; ok {
return errors.Newf(codes.Internal, "duplicate builtin package %q", pkg.Path)
}

if ast.Check(pkg) > 0 {
err := ast.GetError(pkg)
return errors.Wrapf(err, codes.Inherit, "failed to parse builtin package %q", pkg.Path)
}

bs, err := json.Marshal(pkg)
if err != nil {
return err
}
hdl, err := r.JSONToHandle(bs)
if err != nil {
return err
}
root, err := AnalyzePackage(hdl)
if err != nil {
return err
}
r.pkgs[pkg.Path] = root
r.astPkgs[pkg.Path] = pkg
return nil
}

Expand Down Expand Up @@ -192,11 +180,38 @@ func (r *runtime) Stdlib() interpreter.Importer {
return &importer{r: r}
}

func (r *runtime) compilePackages() error {
pkgs := make(map[string]*semantic.Package)
for _, pkg := range r.astPkgs {
bs, err := json.Marshal(pkg)
if err != nil {
return err
}
hdl, err := r.JSONToHandle(bs)
if err != nil {
return err
}
root, err := AnalyzePackage(hdl)
if err != nil {
return err
}
pkgs[pkg.Path] = root
}
r.pkgs = pkgs
r.astPkgs = nil
return nil
}

func (r *runtime) Finalize() error {
if r.finalized {
return errors.New(codes.Internal, "already finalized")
}
r.finalized = true

if err := r.compilePackages(); err != nil {
return err
}

for path, pkg := range r.builtins {
semPkg, ok := r.pkgs[path]
if !ok {
Expand Down

0 comments on commit 6812212

Please sign in to comment.