diff options
author | Gusted <williamzijl7@hotmail.com> | 2023-11-12 16:36:57 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2023-11-12 16:36:57 +0100 |
commit | 1834cecd7e1ad98a4affda5ce6fbc4ae01995de7 (patch) | |
tree | 75b53766e1732bf68cd8134158eea83c0ca38b28 /compress.go | |
parent | Fix spelling mistake (diff) | |
download | fasthttp-1834cecd7e1ad98a4affda5ce6fbc4ae01995de7.tar.gz fasthttp-1834cecd7e1ad98a4affda5ce6fbc4ae01995de7.tar.bz2 fasthttp-1834cecd7e1ad98a4affda5ce6fbc4ae01995de7.zip |
Lazy load stackless functions (#1656)
- I noticed that fasthttp was taking up 1.8MB of heap memory, even
though it wasn't being used. This turned out to be the stackless
function: 1.80MB github.com/valyala/fasthttp/stackless.NewFunc
- Lazy load the stackless functions with sync.Once, given this a simple
atomic read, it shouldn't affect performance for the fast-path (I
haven't seen benchmarks with compression enabled).
Diffstat (limited to 'compress.go')
-rw-r--r-- | compress.go | 24 |
1 files changed, 22 insertions, 2 deletions
diff --git a/compress.go b/compress.go index 8494d56..1f44f1e 100644 --- a/compress.go +++ b/compress.go @@ -177,7 +177,17 @@ func WriteGzipLevel(w io.Writer, p []byte, level int) (int, error) { } } -var stacklessWriteGzip = stackless.NewFunc(nonblockingWriteGzip) +var ( + stacklessWriteGzipOnce sync.Once + stacklessWriteGzipFunc func(ctx interface{}) bool +) + +func stacklessWriteGzip(ctx interface{}) { + stacklessWriteGzipOnce.Do(func() { + stacklessWriteGzipFunc = stackless.NewFunc(nonblockingWriteGzip) + }) + stacklessWriteGzipFunc(ctx) +} func nonblockingWriteGzip(ctxv interface{}) { ctx := ctxv.(*compressCtx) @@ -270,7 +280,17 @@ func WriteDeflateLevel(w io.Writer, p []byte, level int) (int, error) { } } -var stacklessWriteDeflate = stackless.NewFunc(nonblockingWriteDeflate) +var ( + stacklessWriteDeflateOnce sync.Once + stacklessWriteDeflateFunc func(ctx interface{}) bool +) + +func stacklessWriteDeflate(ctx interface{}) { + stacklessWriteDeflateOnce.Do(func() { + stacklessWriteDeflateFunc = stackless.NewFunc(nonblockingWriteDeflate) + }) + stacklessWriteDeflateFunc(ctx) +} func nonblockingWriteDeflate(ctxv interface{}) { ctx := ctxv.(*compressCtx) |