valyala / fastjson

Fast JSON parser and validator for Go. No custom structs, no code generation, no reflection
MIT License
2.28k stars 137 forks source link

Shouldn't ArenaPool call arena.Reset() on Put or on Get? #61

Open KromDaniel opened 3 years ago

KromDaniel commented 3 years ago

Hey, Not sure this is a bug or by design, I didn't find anything on the docs mention that and I think it made my app OOM

This is the implementation of arena pool:

// Get returns an Arena from ap.
//
// The Arena must be Put to ap after use.
func (ap *ArenaPool) Get() *Arena {
    v := ap.pool.Get()
    if v == nil {
        return &Arena{}
    }
    return v.(*Arena)
}

// Put returns a to ap.
//
// a and objects created by a cannot be used after a is put into ap.
func (ap *ArenaPool) Put(a *Arena) {
    ap.pool.Put(a)
}

However (AFAIK), pools usually should call reset on returned object so shouldn't Put implementation should be:

a.Reset()
ap.pool.Put(a)

I can call .Reset myself but since the pool is not doing that, I'm afraid that I might be missing something?

Would really appreciate if you have time to look, Thanks!

develar commented 3 years ago

Same question.

saartamir commented 2 years ago

Same here... I see that the 'b' field is keep growing if I'm not using Reset()