Skip to content

Commit

Permalink
Optimized runtime DataSet usage
Browse files Browse the repository at this point in the history
  • Loading branch information
ziflex committed Apr 1, 2024
1 parent 03af1c7 commit 70268ba
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 30 deletions.
32 changes: 10 additions & 22 deletions pkg/compiler/compiler_func_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,27 +42,15 @@ func TestFunctionCall(t *testing.T) {
[]int{2, 4, 6, 8},
ShouldEqualJSON,
},
//{
// `RETURN FIRST((FOR i IN 1..10 RETURN i * 2))`,
// 2,
// nil,
//},
{
`RETURN FIRST((FOR i IN 1..10 RETURN i * 2))`,
2,
nil,
},
{
`RETURN UNION((FOR i IN 0..5 RETURN i), (FOR i IN 6..10 RETURN i))`,
[]int{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10},
ShouldEqualJSON,
},
})

//
//Convey("Should be able to use FOR as arguments", t, func() {
// c := compiler.New()
//
// p, err := c.Compile(`
// RETURN UNION((FOR i IN 0..5 RETURN i), (FOR i IN 6..10 RETURN i))
// `)
//
// So(err, ShouldBeNil)
//
// out, err := p.Run(context.Background())
//
// So(err, ShouldBeNil)
//
// So(string(out), ShouldEqual, `[0,1,2,3,4,5,6,7,8,9,10]`)
//})
}
15 changes: 14 additions & 1 deletion pkg/compiler/visitor.go
Original file line number Diff line number Diff line change
Expand Up @@ -231,7 +231,6 @@ func (v *visitor) VisitForExpression(ctx *fql.ForExpressionContext) interface{}
v.emitLoop(loopJump)
v.patchJump(exitJump)
v.endScope()
v.endLoop()
// pop the boolean value from the stack
v.emitPop()

Expand All @@ -243,6 +242,8 @@ func (v *visitor) VisitForExpression(ctx *fql.ForExpressionContext) interface{}
v.emitPop()
}

v.endLoop()

return nil
}

Expand Down Expand Up @@ -850,6 +851,18 @@ func (v *visitor) resolveLoopResult() int {

func (v *visitor) endLoop() {
v.loops = v.loops[:len(v.loops)-1]

var unwrap bool

if len(v.loops) == 0 {
unwrap = true
} else if !v.loops[len(v.loops)-1].passThrough {
unwrap = true
}

if unwrap {
v.emit(runtime.OpLoopUnwrapOutput)
}
}

func (v *visitor) resolveLocalVariable(name string) int {
Expand Down
24 changes: 20 additions & 4 deletions pkg/runtime/dataset.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,26 @@ func NewDataSet(distinct bool) *DataSet {
}
}

func (ds *DataSet) String() string {
return ds.values.String()
}

func (ds *DataSet) Unwrap() interface{} {
return ds.values
}

func (ds *DataSet) Hash() uint64 {
return ds.values.Hash()
}

func (ds *DataSet) Copy() core.Value {
return ds.values.Copy()
}

func (ds *DataSet) MarshalJSON() ([]byte, error) {
return ds.values.MarshalJSON()
}

func (ds *DataSet) Push(item core.Value) {
if ds.hashmap != nil {
hash := item.Hash()
Expand All @@ -39,10 +59,6 @@ func (ds *DataSet) Push(item core.Value) {
ds.values.Push(item)
}

func (ds *DataSet) MarshalJSON() ([]byte, error) {
return ds.values.MarshalJSON()
}

func (ds *DataSet) ToArray() *values.Array {
return ds.values
}
1 change: 1 addition & 0 deletions pkg/runtime/opcodes.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ const (
OpJump
OpJumpBackward
OpLoopInitOutput
OpLoopUnwrapOutput
OpForLoopInitInput
OpForLoopHasNext
OpForLoopNext
Expand Down
8 changes: 5 additions & 3 deletions pkg/runtime/vm.go
Original file line number Diff line number Diff line change
Expand Up @@ -382,9 +382,11 @@ loop:
return nil, err
}
case OpLoopInitOutput:
output := NewDataSet(arg == 1)
stack.Push(NewDataSet(arg == 1))

stack.Push(values.NewBoxedValue(output))
case OpLoopUnwrapOutput:
ds := stack.Pop().(*DataSet)
stack.Push(ds.ToArray())

case OpForLoopInitInput:
// start a new iteration
Expand Down Expand Up @@ -466,7 +468,7 @@ loop:
// pop the return value from the stack
res := stack.Pop()
pos := stack.Len() - arg
ds := stack.Get(pos).(*values.Boxed).Unwrap().(*DataSet)
ds := stack.Get(pos).(*DataSet)
ds.Push(res)

case OpReturn:
Expand Down

0 comments on commit 70268ba

Please sign in to comment.