Skip to content

Commit

Permalink
Add spans
Browse files Browse the repository at this point in the history
  • Loading branch information
antonmedv committed Apr 13, 2024
1 parent eb8fb13 commit 355fb28
Show file tree
Hide file tree
Showing 6 changed files with 63 additions and 0 deletions.
29 changes: 29 additions & 0 deletions compiler/compiler.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,11 @@ func Compile(tree *parser.Tree, config *conf.Config) (program *Program, err erro
}
}

var span *Span
if len(c.spans) > 0 {
span = c.spans[0]
}

program = NewProgram(
tree.Source,
tree.Node,
Expand All @@ -60,6 +65,7 @@ func Compile(tree *parser.Tree, config *conf.Config) (program *Program, err erro
c.arguments,
c.functions,
c.debugInfo,
span,
)
return
}
Expand All @@ -76,6 +82,7 @@ type compiler struct {
functionsIndex map[string]int
debugInfo map[string]string
nodes []ast.Node
spans []*Span
chains [][]int
arguments []int
}
Expand Down Expand Up @@ -193,6 +200,28 @@ func (c *compiler) compile(node ast.Node) {
c.nodes = c.nodes[:len(c.nodes)-1]
}()

if c.config != nil && c.config.Profile {
span := &Span{
Name: reflect.TypeOf(node).String(),
Expression: node.String(),
}
if len(c.spans) > 0 {
prev := c.spans[len(c.spans)-1]
prev.Children = append(prev.Children, span)
}
c.spans = append(c.spans, span)
defer func() {
if len(c.spans) > 1 {
c.spans = c.spans[:len(c.spans)-1]
}
}()

c.emit(OpProfileStart, c.addConstant(span))
defer func() {
c.emit(OpProfileEnd, c.addConstant(span))
}()
}

switch n := node.(type) {
case *ast.NilNode:
c.NilNode(n)
Expand Down
1 change: 1 addition & 0 deletions conf/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ type Config struct {
ExpectAny bool
Optimize bool
Strict bool
Profile bool
ConstFns map[string]reflect.Value
Visitors []ast.Visitor
Functions FunctionsTable
Expand Down
2 changes: 2 additions & 0 deletions vm/opcodes.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,8 @@ const (
OpGroupBy
OpSortBy
OpSort
OpProfileStart
OpProfileEnd
OpBegin
OpEnd // This opcode must be at the end of this list.
)
9 changes: 9 additions & 0 deletions vm/program.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ type Program struct {
variables int
functions []Function
debugInfo map[string]string
span *Span
}

// NewProgram returns a new Program. It's used by the compiler.
Expand All @@ -40,6 +41,7 @@ func NewProgram(
arguments []int,
functions []Function,
debugInfo map[string]string,
span *Span,
) *Program {
return &Program{
source: source,
Expand All @@ -51,6 +53,7 @@ func NewProgram(
Arguments: arguments,
functions: functions,
debugInfo: debugInfo,
span: span,
}
}

Expand Down Expand Up @@ -360,6 +363,12 @@ func (program *Program) DisassembleWriter(w io.Writer) {
case OpSort:
code("OpSort")

case OpProfileStart:
code("OpProfileStart")

case OpProfileEnd:
code("OpProfileEnd")

case OpBegin:
code("OpBegin")

Expand Down
13 changes: 13 additions & 0 deletions vm/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package vm

import (
"reflect"
"time"
)

type (
Expand All @@ -25,3 +26,15 @@ type Scope struct {
}

type groupBy = map[any][]any

type Span struct {
Name string `json:"name"`
Expression string `json:"expression"`
Duration int64 `json:"duration"`
Children []*Span `json:"children"`
start time.Time
}

func GetSpan(program *Program) *Span {
return program.span
}
9 changes: 9 additions & 0 deletions vm/vm.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"regexp"
"sort"
"strings"
"time"

"github.com/expr-lang/expr/builtin"
"github.com/expr-lang/expr/file"
Expand Down Expand Up @@ -523,6 +524,14 @@ func (vm *VM) Run(program *Program, env any) (_ any, err error) {
vm.memGrow(uint(scope.Len))
vm.push(sortable.Array)

case OpProfileStart:
span := program.Constants[arg].(*Span)
span.start = time.Now()

case OpProfileEnd:
span := program.Constants[arg].(*Span)
span.Duration += time.Since(span.start).Nanoseconds()

case OpBegin:
a := vm.pop()
array := reflect.ValueOf(a)
Expand Down

0 comments on commit 355fb28

Please sign in to comment.