mirror of
https://github.com/ethereum/go-ethereum.git
synced 2026-07-27 23:26:44 +00:00
feat(ccc): reuse a buffer for json encoding (#744)
This commit is contained in:
parent
070ec68aa5
commit
5d0d197ce4
2 changed files with 10 additions and 6 deletions
|
|
@ -24,7 +24,7 @@ import (
|
|||
const (
|
||||
VersionMajor = 5 // Major version component of the current release
|
||||
VersionMinor = 3 // Minor version component of the current release
|
||||
VersionPatch = 12 // Patch version component of the current release
|
||||
VersionPatch = 13 // Patch version component of the current release
|
||||
VersionMeta = "mainnet" // Version metadata to append to the version string
|
||||
)
|
||||
|
||||
|
|
|
|||
|
|
@ -10,6 +10,7 @@ package circuitcapacitychecker
|
|||
import "C" //nolint:typecheck
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"sync"
|
||||
|
|
@ -29,7 +30,8 @@ func init() {
|
|||
type CircuitCapacityChecker struct {
|
||||
// mutex for each CircuitCapacityChecker itself
|
||||
sync.Mutex
|
||||
ID uint64
|
||||
ID uint64
|
||||
jsonBuffer bytes.Buffer
|
||||
}
|
||||
|
||||
// NewCircuitCapacityChecker creates a new CircuitCapacityChecker
|
||||
|
|
@ -65,13 +67,14 @@ func (ccc *CircuitCapacityChecker) ApplyTransaction(traces *types.BlockTrace) (*
|
|||
return nil, ErrUnknown
|
||||
}
|
||||
|
||||
tracesByt, err := json.Marshal(traces)
|
||||
ccc.jsonBuffer.Reset()
|
||||
err := json.NewEncoder(&ccc.jsonBuffer).Encode(traces)
|
||||
if err != nil {
|
||||
log.Error("fail to json marshal traces in ApplyTransaction", "id", ccc.ID, "TxHash", traces.Transactions[0].TxHash, "err", err)
|
||||
return nil, ErrUnknown
|
||||
}
|
||||
|
||||
tracesStr := C.CString(string(tracesByt))
|
||||
tracesStr := C.CString(string(ccc.jsonBuffer.Bytes()))
|
||||
defer func() {
|
||||
C.free(unsafe.Pointer(tracesStr))
|
||||
}()
|
||||
|
|
@ -111,13 +114,14 @@ func (ccc *CircuitCapacityChecker) ApplyBlock(traces *types.BlockTrace) (*types.
|
|||
ccc.Lock()
|
||||
defer ccc.Unlock()
|
||||
|
||||
tracesByt, err := json.Marshal(traces)
|
||||
ccc.jsonBuffer.Reset()
|
||||
err := json.NewEncoder(&ccc.jsonBuffer).Encode(traces)
|
||||
if err != nil {
|
||||
log.Error("fail to json marshal traces in ApplyBlock", "id", ccc.ID, "blockNumber", traces.Header.Number, "blockHash", traces.Header.Hash(), "err", err)
|
||||
return nil, ErrUnknown
|
||||
}
|
||||
|
||||
tracesStr := C.CString(string(tracesByt))
|
||||
tracesStr := C.CString(string(ccc.jsonBuffer.Bytes()))
|
||||
defer func() {
|
||||
C.free(unsafe.Pointer(tracesStr))
|
||||
}()
|
||||
|
|
|
|||
Loading…
Reference in a new issue