Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Lazy load stackless functions #1656

Merged
merged 1 commit into from
Nov 12, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 11 additions & 1 deletion brotli.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
24 changes: 22 additions & 2 deletions compress.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down
13 changes: 12 additions & 1 deletion stackless/writer.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"errors"
"fmt"
"io"
"sync"

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

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

var stacklessWriterFunc = NewFunc(writerFunc)
var (
stacklessWriterFuncOnce sync.Once
stacklessWriterFuncFunc func(ctx interface{}) bool
)

func stacklessWriterFunc(ctx interface{}) bool {
stacklessWriterFuncOnce.Do(func() {
stacklessWriterFuncFunc = NewFunc(writerFunc)
})
return stacklessWriterFuncFunc(ctx)
}

func writerFunc(ctx interface{}) {
w := ctx.(*writer)
Expand Down
Loading