aboutsummaryrefslogtreecommitdiff
path: root/brotli.go
diff options
context:
space:
mode:
authorGravatar Gusted <williamzijl7@hotmail.com> 2023-11-12 16:36:57 +0100
committerGravatar GitHub <noreply@github.com> 2023-11-12 16:36:57 +0100
commit1834cecd7e1ad98a4affda5ce6fbc4ae01995de7 (patch)
tree75b53766e1732bf68cd8134158eea83c0ca38b28 /brotli.go
parentFix spelling mistake (diff)
downloadfasthttp-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 'brotli.go')
-rw-r--r--brotli.go12
1 files changed, 11 insertions, 1 deletions
diff --git a/brotli.go b/brotli.go
index c829c39..032bd95 100644
--- a/brotli.go
+++ b/brotli.go
@@ -132,7 +132,17 @@ func WriteBrotliLevel(w io.Writer, p []byte, level int) (int, error) {
}
}
-var stacklessWriteBrotli = stackless.NewFunc(nonblockingWriteBrotli)
+var (
+ stacklessWriteBrotliOnce sync.Once
+ stacklessWriteBrotliFunc func(ctx interface{}) bool
+)
+
+func stacklessWriteBrotli(ctx interface{}) {
+ stacklessWriteBrotliOnce.Do(func() {
+ stacklessWriteBrotliFunc = stackless.NewFunc(nonblockingWriteBrotli)
+ })
+ stacklessWriteBrotliFunc(ctx)
+}
func nonblockingWriteBrotli(ctxv interface{}) {
ctx := ctxv.(*compressCtx)