Skip to content

Commit

Permalink
avoid interfaces in scope stack to reduce allocations (ref #86)
Browse files Browse the repository at this point in the history
  • Loading branch information
itchyny committed Aug 30, 2021
1 parent 060caf6 commit 79e3f4b
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 5 deletions.
4 changes: 2 additions & 2 deletions env.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ import "context"
type env struct {
pc int
stack *stack
scopes *stack
paths *stack
scopes *scopeStack
values []interface{}
codes []*code
codeinfos []codeinfo
Expand All @@ -22,8 +22,8 @@ type env struct {
func newEnv(ctx context.Context) *env {
return &env{
stack: newStack(),
scopes: newStack(),
paths: newStack(),
scopes: newScopeStack(),
ctx: ctx,
}
}
Expand Down
6 changes: 3 additions & 3 deletions execute.go
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,7 @@ loop:
env.scopes.index = index
}
if outerindex = index; outerindex >= 0 {
if s := env.scopes.data[outerindex].value.(scope); s.id == xs[0] {
if s := env.scopes.data[outerindex].value; s.id == xs[0] {
outerindex = s.outerindex
}
}
Expand Down Expand Up @@ -349,7 +349,7 @@ func (env *env) pop() interface{} {

func (env *env) popscope() (int, int) {
free := env.scopes.index > env.scopes.limit
s := env.scopes.pop().(scope)
s := env.scopes.pop()
if free {
env.offset = s.offset
}
Expand Down Expand Up @@ -377,7 +377,7 @@ func (env *env) popfork() int {

func (env *env) index(v [2]int) int {
for id, i := v[0], env.scopes.index; i >= 0; {
s := env.scopes.data[i].value.(scope)
s := env.scopes.data[i].value
if s.id == id {
return s.offset + v[1]
}
Expand Down
51 changes: 51 additions & 0 deletions scope_stack.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package gojq

type scopeStack struct {
data []scopeBlock
index int
limit int
}

type scopeBlock struct {
value scope
next int
}

func newScopeStack() *scopeStack {
return &scopeStack{index: -1, limit: -1}
}

func (s *scopeStack) push(v scope) {
b := scopeBlock{v, s.index}
i := s.index + 1
if i <= s.limit {
i = s.limit + 1
}
s.index = i
if i < len(s.data) {
s.data[i] = b
} else {
s.data = append(s.data, b)
}
}

func (s *scopeStack) pop() scope {
b := s.data[s.index]
s.index = b.next
return b.value
}

func (s *scopeStack) empty() bool {
return s.index < 0
}

func (s *scopeStack) save(index, limit *int) {
*index, *limit = s.index, s.limit
if s.index > s.limit {
s.limit = s.index
}
}

func (s *scopeStack) restore(index, limit int) {
s.index, s.limit = index, limit
}

0 comments on commit 79e3f4b

Please sign in to comment.