From 060caf653daee1fca1d0007fc641ea7750c2279b Mon Sep 17 00:00:00 2001 From: itchyny Date: Mon, 30 Aug 2021 20:23:04 +0900 Subject: [PATCH] truncate values offset and stack limits to reduce allocations (fix #86) --- execute.go | 15 +++++++++++---- stack.go | 4 ++-- 2 files changed, 13 insertions(+), 6 deletions(-) diff --git a/execute.go b/execute.go index 6bba7401..2e1e0869 100644 --- a/execute.go +++ b/execute.go @@ -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) @@ -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 } @@ -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) diff --git a/stack.go b/stack.go index 11816aac..f629d28e 100644 --- a/stack.go +++ b/stack.go @@ -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) {