Skip to content

Commit c07b2f7

Browse files
committed
compression: fix nocgo build
Also add `make testnocgo` target and update the nightly workflow (note: we were running this without invariants before, that was not intentional). We also add this build to the CI builds that run on each PR.
1 parent e15bfef commit c07b2f7

File tree

4 files changed

+40
-7
lines changed

4 files changed

+40
-7
lines changed

.github/workflows/ci.yaml

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,19 @@ jobs:
6363

6464
- run: GOTRACEBACK=all make test TAGS=
6565

66+
linux-no-cgo:
67+
name: go-linux-no-cgo
68+
runs-on: ubuntu-latest
69+
steps:
70+
- uses: actions/checkout@v4
71+
72+
- name: Set up Go
73+
uses: actions/setup-go@v4
74+
with:
75+
go-version: '1.24'
76+
77+
- run: GOTRACEBACK=all make testnocgo
78+
6679
linux-race:
6780
name: linux-race
6881
runs-on: ubuntu-latest

.github/workflows/tests.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -171,7 +171,7 @@ jobs:
171171
with:
172172
go-version: ${{ inputs.go_version }}
173173

174-
- run: GOTRACEBACK=all CGO_ENABLED=0 make test TAGS=
174+
- run: GOTRACEBACK=all make testnocgo
175175

176176
- name: Post issue on failure
177177
if: failure() && inputs.file_issue_branch != ''

Makefile

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,10 @@ testmsan: testflags += -msan -timeout 20m
4444
testmsan: TAGS += slowbuild
4545
testmsan: test
4646

47+
.PHONY: testnocgo
48+
testnocgo:
49+
CGO_ENABLED=0 ${GO} test -tags '$(TAGS)' ${testflags} -run ${TESTS} ${PKG}
50+
4751
.PHONY: testobjiotracing
4852
testobjiotracing:
4953
${GO} test -tags '$(TAGS) pebble_obj_io_tracing' ${testflags} -run ${TESTS} ./objstorage/objstorageprovider/objiotracing

internal/compression/zstd_nocgo.go

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,19 +8,22 @@ package compression
88

99
import (
1010
"encoding/binary"
11+
"sync"
1112

1213
"github.com/cockroachdb/errors"
1314
"github.com/cockroachdb/pebble/internal/base"
1415
"github.com/klauspost/compress/zstd"
1516
)
1617

17-
type zstdCompressor zstd.Encoder
18+
type zstdCompressor struct {
19+
level int
20+
encoder *zstd.Encoder
21+
}
1822

1923
var _ Compressor = (*zstdCompressor)(nil)
2024

21-
func getZstdCompressor(level int) *zstdCompressor {
22-
writer, _ := zstd.NewWriter(nil, zstd.WithEncoderLevel(zstd.EncoderLevelFromZstd(level)))
23-
return (*zstdCompressor)(writer)
25+
var zstdCompressorPool = sync.Pool{
26+
New: func() any { return &zstdCompressor{} },
2427
}
2528

2629
// UseStandardZstdLib indicates whether the zstd implementation is a port of the
@@ -39,14 +42,27 @@ func (z *zstdCompressor) Compress(compressedBuf, b []byte) ([]byte, Setting) {
3942
compressedBuf = append(compressedBuf, make([]byte, binary.MaxVarintLen64-len(compressedBuf))...)
4043
}
4144
varIntLen := binary.PutUvarint(compressedBuf, uint64(len(b)))
42-
res := (*zstd.Encoder)(z).EncodeAll(b, compressedBuf[:varIntLen])
45+
res := z.encoder.EncodeAll(b, compressedBuf[:varIntLen])
4346
return res, Setting{Algorithm: Zstd, Level: uint8(z.level)}
4447
}
4548

4649
func (z *zstdCompressor) Close() {
47-
if err := (*zstd.Encoder)(z).Close(); err != nil {
50+
if err := z.encoder.Close(); err != nil {
51+
panic(err)
52+
}
53+
z.encoder = nil
54+
zstdCompressorPool.Put(z)
55+
}
56+
57+
func getZstdCompressor(level int) *zstdCompressor {
58+
encoder, err := zstd.NewWriter(nil, zstd.WithEncoderLevel(zstd.EncoderLevelFromZstd(level)))
59+
if err != nil {
4860
panic(err)
4961
}
62+
z := zstdCompressorPool.Get().(*zstdCompressor)
63+
z.level = level
64+
z.encoder = encoder
65+
return z
5066
}
5167

5268
type zstdDecompressor struct{}

0 commit comments

Comments
 (0)