From a00b06bff512de2f0ca34c85a65c8e088d25ed39 Mon Sep 17 00:00:00 2001 From: codchen Date: Sat, 14 Sep 2024 22:02:43 +0800 Subject: [PATCH] use array for host contexts --- bindings/go/evmc/evmc.go | 34 +++++++++++++++++----------------- bindings/go/evmc/host.go | 1 + 2 files changed, 18 insertions(+), 17 deletions(-) diff --git a/bindings/go/evmc/evmc.go b/bindings/go/evmc/evmc.go index bf6ac8912..ae202ddc5 100644 --- a/bindings/go/evmc/evmc.go +++ b/bindings/go/evmc/evmc.go @@ -56,7 +56,7 @@ import "C" import ( "fmt" - "sync" + "reflect" "unsafe" ) @@ -207,7 +207,7 @@ type Result struct { func (vm *VM) Execute(ctx HostContext, rev Revision, kind CallKind, static bool, depth int, gas int64, recipient Address, sender Address, input []byte, value Hash, - code []byte) (res Result, err error) { + code []byte, hcSlot int) (res Result, err error) { flags := C.uint32_t(0) if static { @@ -241,30 +241,30 @@ func (vm *VM) Execute(ctx HostContext, rev Revision, var ( hostContextCounter uintptr - hostContextMap = map[uintptr]HostContext{} - hostContextMapMu sync.Mutex + + histContextSlots = make([]HostContext, 5000) ) func addHostContext(ctx HostContext) uintptr { - hostContextMapMu.Lock() - id := hostContextCounter - hostContextCounter++ - hostContextMap[id] = ctx - hostContextMapMu.Unlock() - return id + idx := ctx.GetTransactionIndex() + if idx >= len(histContextSlots) { + panic("received more than 5000 transactions in a block") + } + histContextSlots[idx] = ctx + return uintptr(unsafe.Pointer(&histContextSlots[idx])) } func removeHostContext(id uintptr) { - hostContextMapMu.Lock() - delete(hostContextMap, id) - hostContextMapMu.Unlock() } func getHostContext(idx uintptr) HostContext { - hostContextMapMu.Lock() - ctx := hostContextMap[idx] - hostContextMapMu.Unlock() - return ctx + sh := &reflect.SliceHeader{ + Data: idx, + Len: 1, + Cap: 1, + } + data := *(*[]HostContext)(unsafe.Pointer(sh)) + return data[0] } func evmcBytes32(in Hash) C.evmc_bytes32 { diff --git a/bindings/go/evmc/host.go b/bindings/go/evmc/host.go index 47e2efd28..9c58eb035 100644 --- a/bindings/go/evmc/host.go +++ b/bindings/go/evmc/host.go @@ -105,6 +105,7 @@ type HostContext interface { AccessStorage(addr Address, key Hash) AccessStatus GetTransientStorage(addr Address, key Hash) Hash SetTransientStorage(addr Address, key Hash, value Hash) + GetTransactionIndex() int } //export accountExists