fasttemplate
fasttemplate copied to clipboard
global lock contention when the generated Template object not reused by user
We are using this lib in our online system, and in a load test, we found that if the Template generated is not reused(eg.by sync.Map), there will be strong contention when the load is high:
goroutine profile: total 18910
18903 @ 0x102f20b 0x102f2b3 0x103fa4c 0x103f77d 0x10714df 0x1071d8f 0x1071d26 0x1071a5f 0x12feeb8 0x13005f0 0x13007c3 0x130107b 0x105c931
# 0x103f77c sync.runtime_SemacquireMutex+0x3c /usr/local/go/src/runtime/sema.go:71
# 0x10714de sync.(*Mutex).Lock+0xfe /usr/local/go/src/sync/mutex.go:134
# 0x1071d8e sync.(*Pool).pinSlow+0x3e /usr/local/go/src/sync/pool.go:198
# 0x1071d25 sync.(*Pool).pin+0x55 /usr/local/go/src/sync/pool.go:191
# 0x1071a5e sync.(*Pool).Get+0x2e /usr/local/go/src/sync/pool.go:128
# 0x12feeb7 github.com/valyala/fasttemplate/vendor/github.com/valyala/bytebufferpool.(*Pool).Get+0x37 /Users/xargin/go/src/github.com/valyala/fasttemplate/vendor/github.com/valyala/bytebufferpool/pool.go:49
# 0x13005ef github.com/valyala/fasttemplate.(*Template).ExecuteFuncString+0x3f /Users/xargin/go/src/github.com/valyala/fasttemplate/template.go:278
# 0x13007c2 github.com/valyala/fasttemplate.(*Template).ExecuteString+0x52 /Users/xargin/go/src/github.com/valyala/fasttemplate/template.go:299
# 0x130107a main.loop.func1+0x3a /Users/xargin/test/go/http/httptest.go:22
and after we use sync.Map to store the generated template objects, the piled goroutines disappear.
Maybe you need to warn users about this?