Skip to content

Commit

Permalink
fix memory bug caused by misusing fastjson.Parser (#41)
Browse files Browse the repository at this point in the history
* fix memory bug caused by misusing fastjson.Parser

* Apply suggestions from code review
  • Loading branch information
laizy authored May 26, 2022
1 parent 1f575c8 commit 70d8494
Showing 1 changed file with 9 additions and 4 deletions.
13 changes: 9 additions & 4 deletions structs_marshal.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,9 @@ func (l *Log) MarshalJSON() ([]byte, error) {
o.Set("blockNumber", a.NewString(fmt.Sprintf("0x%x", l.BlockNumber)))
o.Set("address", a.NewString(l.Address.String()))
if l.Event != nil {
// p can not be put back to pool, because The returned value of p.Parse
// is valid until the next call to Parse*.
p := defaultPool.Get()
defer defaultPool.Put(p)
buf, _ := json.Marshal(l.Event)
v, err := p.Parse(string(buf))
if err == nil {
Expand Down Expand Up @@ -141,17 +142,21 @@ func (t *Receipt) MarshalJSON() ([]byte, error) {
}
o.Set("logsBloom", a.NewString(hexutil.Bytes(t.LogsBloom).String()))
logs := a.NewArray()
p := defaultPool.Get()
defer defaultPool.Put(p)
for i, log := range t.Logs {
// p can not be put back to pool, because The returned value of p.Parse
// is valid until the next call to Parse*.
p := defaultPool.Get()
buf, _ := json.Marshal(log)
v, err := p.Parse(string(buf))
utils.Ensure(err)
logs.SetArrayItem(i, v)
}
o.Set("logs", logs)

return o.MarshalTo(nil), nil
data := o.MarshalTo(nil)
defaultArena.Put(a)

return data, nil
}

// MarshalJSON implements the Marshal interface.
Expand Down

0 comments on commit 70d8494

Please sign in to comment.