Skip to content

Commit f019530

Browse files
committed
add rnd pool
1 parent 3435278 commit f019530

File tree

4 files changed

+43
-30
lines changed

4 files changed

+43
-30
lines changed

.gitignore

100644100755
Lines changed: 12 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,19 @@
1-
# If you prefer the allow list template instead of the deny list, see community template:
2-
# https://github.com/github/gitignore/blob/main/community/Golang/Go.AllowList.gitignore
3-
#
4-
# Binaries for programs and plugins
1+
.tools/
2+
bin/
3+
vendor/
4+
build/
5+
.idea/
6+
.vscode/
7+
coverage.txt
8+
coverage.out
59
*.exe
610
*.exe~
711
*.dll
812
*.so
913
*.dylib
10-
11-
# Test binary, built with `go test -c`
14+
*.db
15+
*.db-journal
16+
*.mmdb
1217
*.test
13-
14-
# Output of the go coverage tool, specifically when used with LiteIDE
1518
*.out
16-
17-
# Dependency directories (remove the comment below to include it)
18-
# vendor/
19-
20-
# Go workspace file
21-
go.work
22-
go.work.sum
23-
.tools/
19+
.env

Makefile

100644100755
Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,28 @@
11

22
.PHONY: install
33
install:
4-
go install github.com/osspkg/devtool@latest
5-
6-
.PHONY: setup
7-
setup:
8-
devtool setup-lib
4+
go install go.osspkg.com/goppy/v2/cmd/goppy@latest
5+
goppy setup-lib
96

107
.PHONY: lint
118
lint:
12-
devtool lint
9+
goppy lint
1310

1411
.PHONY: license
1512
license:
16-
devtool license
13+
goppy license
1714

1815
.PHONY: build
1916
build:
20-
devtool build --arch=amd64
17+
goppy build --arch=amd64
2118

2219
.PHONY: tests
2320
tests:
24-
devtool test
21+
goppy test
2522

2623
.PHONY: pre-commite
27-
pre-commite: setup lint build tests
24+
pre-commite: install lint tests build
2825

2926
.PHONY: ci
30-
ci: install setup lint build tests
27+
ci: pre-commite
3128

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
module go.osspkg.com/random
22

3-
go 1.21
3+
go 1.22.5

random.go

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,21 +8,35 @@ package random
88
import (
99
crand "crypto/rand"
1010
"math/rand"
11+
"sync"
1112
"time"
1213
)
1314

14-
var rnd = rand.New(rand.NewSource(time.Now().UnixNano()))
15-
1615
var (
1716
digest = []byte("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789-+=~*@#$%&?!<>")
17+
pool = sync.Pool{New: func() any {
18+
return createRand()
19+
}}
1820
)
1921

22+
func createRand() *rand.Rand {
23+
return rand.New(rand.NewSource(time.Now().UnixNano()))
24+
}
25+
2026
func BytesOf(n int, src []byte) []byte {
27+
rnd, ok := pool.Get().(*rand.Rand)
28+
if !ok {
29+
rnd = createRand()
30+
}
31+
defer pool.Put(rnd)
32+
2133
tmp := make([]byte, len(src))
2234
copy(tmp, src)
35+
2336
rnd.Shuffle(len(tmp), func(i, j int) {
2437
tmp[i], tmp[j] = tmp[j], tmp[i]
2538
})
39+
2640
b := make([]byte, n)
2741
for i := range b {
2842
b[i] = tmp[rnd.Intn(len(tmp))]
@@ -43,6 +57,12 @@ func String(n int) string {
4357
}
4458

4559
func Shuffle(v []string) []string {
60+
rnd, ok := pool.Get().(*rand.Rand)
61+
if !ok {
62+
rnd = createRand()
63+
}
64+
defer pool.Put(rnd)
65+
4666
rnd.Shuffle(len(v), func(i, j int) { v[i], v[j] = v[j], v[i] })
4767
return v
4868
}

0 commit comments

Comments
 (0)