mirror of
https://github.com/ethereum/go-ethereum.git
synced 2026-08-11 14:33:52 +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 (
|
const (
|
||||||
VersionMajor = 5 // Major version component of the current release
|
VersionMajor = 5 // Major version component of the current release
|
||||||
VersionMinor = 3 // Minor 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
|
VersionMeta = "mainnet" // Version metadata to append to the version string
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -10,6 +10,7 @@ package circuitcapacitychecker
|
||||||
import "C" //nolint:typecheck
|
import "C" //nolint:typecheck
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"bytes"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
"sync"
|
"sync"
|
||||||
|
|
@ -29,7 +30,8 @@ func init() {
|
||||||
type CircuitCapacityChecker struct {
|
type CircuitCapacityChecker struct {
|
||||||
// mutex for each CircuitCapacityChecker itself
|
// mutex for each CircuitCapacityChecker itself
|
||||||
sync.Mutex
|
sync.Mutex
|
||||||
ID uint64
|
ID uint64
|
||||||
|
jsonBuffer bytes.Buffer
|
||||||
}
|
}
|
||||||
|
|
||||||
// NewCircuitCapacityChecker creates a new CircuitCapacityChecker
|
// NewCircuitCapacityChecker creates a new CircuitCapacityChecker
|
||||||
|
|
@ -65,13 +67,14 @@ func (ccc *CircuitCapacityChecker) ApplyTransaction(traces *types.BlockTrace) (*
|
||||||
return nil, ErrUnknown
|
return nil, ErrUnknown
|
||||||
}
|
}
|
||||||
|
|
||||||
tracesByt, err := json.Marshal(traces)
|
ccc.jsonBuffer.Reset()
|
||||||
|
err := json.NewEncoder(&ccc.jsonBuffer).Encode(traces)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Error("fail to json marshal traces in ApplyTransaction", "id", ccc.ID, "TxHash", traces.Transactions[0].TxHash, "err", err)
|
log.Error("fail to json marshal traces in ApplyTransaction", "id", ccc.ID, "TxHash", traces.Transactions[0].TxHash, "err", err)
|
||||||
return nil, ErrUnknown
|
return nil, ErrUnknown
|
||||||
}
|
}
|
||||||
|
|
||||||
tracesStr := C.CString(string(tracesByt))
|
tracesStr := C.CString(string(ccc.jsonBuffer.Bytes()))
|
||||||
defer func() {
|
defer func() {
|
||||||
C.free(unsafe.Pointer(tracesStr))
|
C.free(unsafe.Pointer(tracesStr))
|
||||||
}()
|
}()
|
||||||
|
|
@ -111,13 +114,14 @@ func (ccc *CircuitCapacityChecker) ApplyBlock(traces *types.BlockTrace) (*types.
|
||||||
ccc.Lock()
|
ccc.Lock()
|
||||||
defer ccc.Unlock()
|
defer ccc.Unlock()
|
||||||
|
|
||||||
tracesByt, err := json.Marshal(traces)
|
ccc.jsonBuffer.Reset()
|
||||||
|
err := json.NewEncoder(&ccc.jsonBuffer).Encode(traces)
|
||||||
if err != nil {
|
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)
|
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
|
return nil, ErrUnknown
|
||||||
}
|
}
|
||||||
|
|
||||||
tracesStr := C.CString(string(tracesByt))
|
tracesStr := C.CString(string(ccc.jsonBuffer.Bytes()))
|
||||||
defer func() {
|
defer func() {
|
||||||
C.free(unsafe.Pointer(tracesStr))
|
C.free(unsafe.Pointer(tracesStr))
|
||||||
}()
|
}()
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue