Skip to content

Commit 1834cec

Browse files
author
Gusted
authored
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).
1 parent 3267649 commit 1834cec

File tree

3 files changed

+45
-4
lines changed

3 files changed

+45
-4
lines changed

brotli.go

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,17 @@ func WriteBrotliLevel(w io.Writer, p []byte, level int) (int, error) {
132132
}
133133
}
134134

135-
var stacklessWriteBrotli = stackless.NewFunc(nonblockingWriteBrotli)
135+
var (
136+
stacklessWriteBrotliOnce sync.Once
137+
stacklessWriteBrotliFunc func(ctx interface{}) bool
138+
)
139+
140+
func stacklessWriteBrotli(ctx interface{}) {
141+
stacklessWriteBrotliOnce.Do(func() {
142+
stacklessWriteBrotliFunc = stackless.NewFunc(nonblockingWriteBrotli)
143+
})
144+
stacklessWriteBrotliFunc(ctx)
145+
}
136146

137147
func nonblockingWriteBrotli(ctxv interface{}) {
138148
ctx := ctxv.(*compressCtx)

compress.go

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -177,7 +177,17 @@ func WriteGzipLevel(w io.Writer, p []byte, level int) (int, error) {
177177
}
178178
}
179179

180-
var stacklessWriteGzip = stackless.NewFunc(nonblockingWriteGzip)
180+
var (
181+
stacklessWriteGzipOnce sync.Once
182+
stacklessWriteGzipFunc func(ctx interface{}) bool
183+
)
184+
185+
func stacklessWriteGzip(ctx interface{}) {
186+
stacklessWriteGzipOnce.Do(func() {
187+
stacklessWriteGzipFunc = stackless.NewFunc(nonblockingWriteGzip)
188+
})
189+
stacklessWriteGzipFunc(ctx)
190+
}
181191

182192
func nonblockingWriteGzip(ctxv interface{}) {
183193
ctx := ctxv.(*compressCtx)
@@ -270,7 +280,17 @@ func WriteDeflateLevel(w io.Writer, p []byte, level int) (int, error) {
270280
}
271281
}
272282

273-
var stacklessWriteDeflate = stackless.NewFunc(nonblockingWriteDeflate)
283+
var (
284+
stacklessWriteDeflateOnce sync.Once
285+
stacklessWriteDeflateFunc func(ctx interface{}) bool
286+
)
287+
288+
func stacklessWriteDeflate(ctx interface{}) {
289+
stacklessWriteDeflateOnce.Do(func() {
290+
stacklessWriteDeflateFunc = stackless.NewFunc(nonblockingWriteDeflate)
291+
})
292+
stacklessWriteDeflateFunc(ctx)
293+
}
274294

275295
func nonblockingWriteDeflate(ctxv interface{}) {
276296
ctx := ctxv.(*compressCtx)

stackless/writer.go

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"errors"
55
"fmt"
66
"io"
7+
"sync"
78

89
"github.com/valyala/bytebufferpool"
910
)
@@ -98,7 +99,17 @@ func (w *writer) do(op op) error {
9899

99100
var errHighLoad = errors.New("cannot compress data due to high load")
100101

101-
var stacklessWriterFunc = NewFunc(writerFunc)
102+
var (
103+
stacklessWriterFuncOnce sync.Once
104+
stacklessWriterFuncFunc func(ctx interface{}) bool
105+
)
106+
107+
func stacklessWriterFunc(ctx interface{}) bool {
108+
stacklessWriterFuncOnce.Do(func() {
109+
stacklessWriterFuncFunc = NewFunc(writerFunc)
110+
})
111+
return stacklessWriterFuncFunc(ctx)
112+
}
102113

103114
func writerFunc(ctx interface{}) {
104115
w := ctx.(*writer)

0 commit comments

Comments
 (0)