Skip to content

Commit

Permalink
feat(ccc): reuse a buffer for json encoding
Browse files Browse the repository at this point in the history
  • Loading branch information
omerfirmak authored and 0xmountaintop committed Jun 25, 2024
1 parent 459324c commit c3f891f
Showing 1 changed file with 9 additions and 5 deletions.
14 changes: 9 additions & 5 deletions rollup/circuitcapacitychecker/impl.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ package circuitcapacitychecker
import "C" //nolint:typecheck

import (
"bytes"
"encoding/json"
"fmt"
"sync"
Expand All @@ -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
Expand Down Expand Up @@ -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))
}()
Expand Down Expand Up @@ -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))
}()
Expand Down

0 comments on commit c3f891f

Please sign in to comment.