-
Notifications
You must be signed in to change notification settings - Fork 20.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
core/vm: Add method Stack.popptr and apply to a bunch of instructions #24859
Conversation
This is a nice optimization idea, but very dangerous. |
I still think we should pursue this optimization idea, just need to find an abstraction that makes it less likely to mess things up. |
|
It's actually not terribly dangerous (see #24860 (review)). When something is pushed to the stack, the item is copied, so the stack owns it.
As long as the caller only uses it in a very small scope, it is safe, just like the |
Hm, this can actually already be achieved, kind of, without popptr, but using
Using this diff diff --git a/core/vm/instructions.go b/core/vm/instructions.go
index 32b7e2ddb6..81d076d89d 100644
--- a/core/vm/instructions.go
+++ b/core/vm/instructions.go
@@ -27,8 +27,9 @@ import (
)
func opAdd(pc *uint64, interpreter *EVMInterpreter, scope *ScopeContext) ([]byte, error) {
- x, y := scope.Stack.popptr(), scope.Stack.peek()
+ x, y := scope.Stack.Back(0), scope.Stack.Back(1)
y.Add(x, y)
+ scope.Stack.Discard()
return nil, nil
}
diff --git a/core/vm/stack.go b/core/vm/stack.go
index d56c6a6cdf..0e00c2a612 100644
--- a/core/vm/stack.go
+++ b/core/vm/stack.go
@@ -64,6 +64,10 @@ func (st *Stack) popptr() (ret *uint256.Int) {
return
}
+func (st *Stack) Discard() {
+ st.data = st.data[:len(st.data)-1]
+}
+
func (st *Stack) len() int {
return len(st.data)
} But I don't know, it's hard to get really stable benchmarks. |
|
I don't think we want to merge this, since it doesn't markedly improve speed, but does make the stack interaction less simple |
It's safe if the popped ptr not escape the body of OP function.