Skip to content

Commit

Permalink
truncate values offset and stack limits to reduce allocations (fix #86)
Browse files Browse the repository at this point in the history
  • Loading branch information
itchyny committed Aug 30, 2021
1 parent 9cca018 commit 060caf6
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 6 deletions.
15 changes: 11 additions & 4 deletions execute.go
Original file line number Diff line number Diff line change
Expand Up @@ -205,8 +205,7 @@ loop:
if callpc >= 0 {
saveindex = index
} else {
s := env.scopes.pop().(scope)
callpc, saveindex = s.pc, s.saveindex
callpc, saveindex = env.popscope()
}
} else {
env.scopes.save(&saveindex, &limit)
Expand All @@ -228,8 +227,7 @@ loop:
if backtrack {
break loop
}
s := env.scopes.pop().(scope)
pc, env.scopes.index = s.pc, s.saveindex
pc, env.scopes.index = env.popscope()
if env.scopes.empty() {
return env.pop(), true
}
Expand Down Expand Up @@ -349,6 +347,15 @@ func (env *env) pop() interface{} {
return env.stack.pop()
}

func (env *env) popscope() (int, int) {
free := env.scopes.index > env.scopes.limit
s := env.scopes.pop().(scope)
if free {
env.offset = s.offset
}
return s.pc, s.saveindex
}

func (env *env) pushfork(pc int) {
f := fork{pc: pc, expdepth: env.expdepth}
env.stack.save(&f.stackindex, &f.stacklimit)
Expand Down
4 changes: 2 additions & 2 deletions stack.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,10 +44,10 @@ func (s *stack) empty() bool {
}

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

func (s *stack) restore(index, limit int) {
Expand Down

0 comments on commit 060caf6

Please sign in to comment.