Skip to content

Commit

Permalink
Internal refactor to improve performance (google#225)
Browse files Browse the repository at this point in the history
* Internal refactor to improve performance
  • Loading branch information
sparkprime authored Jun 1, 2018
1 parent 530cf7b commit 2973e24
Show file tree
Hide file tree
Showing 24 changed files with 776 additions and 912 deletions.
537 changes: 249 additions & 288 deletions builtins.go

Large diffs are not rendered by default.

227 changes: 0 additions & 227 deletions evaluator.go

This file was deleted.

23 changes: 14 additions & 9 deletions imports.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,42 +98,47 @@ func (cache *ImportCache) importData(importedFrom, importedPath string) (content
}

// ImportString imports a string, caches it and then returns it.
func (cache *ImportCache) ImportString(importedFrom, importedPath string, e *evaluator) (*valueString, error) {
func (cache *ImportCache) ImportString(importedFrom, importedPath string, i *interpreter, trace TraceElement) (*valueString, error) {
data, _, err := cache.importData(importedFrom, importedPath)
if err != nil {
return nil, e.Error(err.Error())
return nil, i.Error(err.Error(), trace)
}
return makeValueString(data.String()), nil
}

func codeToPV(e *evaluator, filename string, code string) potentialValue {
func codeToPV(i *interpreter, filename string, code string) *cachedThunk {
node, err := snippetToAST(filename, code)
if err != nil {
// TODO(sbarzowski) we should wrap (static) error here
// within a RuntimeError. Because whether we get this error or not
// actually depends on what happens in Runtime (whether import gets
// evaluated).
// The same thinking applies to external variables.
return makeErrorThunk(err)
return &cachedThunk{err: err}
}
env := makeInitialEnv(filename, i.baseStd)
return &cachedThunk{
env: &env,
body: node,
content: nil,
}
return makeThunk(makeInitialEnv(filename, e.i.baseStd), node)
}

// ImportCode imports code from a path.
func (cache *ImportCache) ImportCode(importedFrom, importedPath string, e *evaluator) (value, error) {
func (cache *ImportCache) ImportCode(importedFrom, importedPath string, i *interpreter, trace TraceElement) (value, error) {
contents, foundAt, err := cache.importData(importedFrom, importedPath)
if err != nil {
return nil, e.Error(err.Error())
return nil, i.Error(err.Error(), trace)
}
var pv potentialValue
if cachedPV, isCached := cache.codeCache[foundAt]; !isCached {
// File hasn't been parsed and analyzed before, update the cache record.
pv = codeToPV(e, foundAt, contents.String())
pv = codeToPV(i, foundAt, contents.String())
cache.codeCache[foundAt] = pv
} else {
pv = cachedPV
}
return e.evaluate(pv)
return i.evaluatePV(pv, trace)
}

// Concrete importers
Expand Down
Loading

0 comments on commit 2973e24

Please sign in to comment.