Skip to content

Commit bd08b4a

Browse files
committed
chore(queue): don't allocate new message (#102)
```sh Run go test -v -run=^$ -count 5 -benchmem -bench . ./... goos: darwin goarch: amd64 pkg: github.com/golang-queue/queue cpu: Intel(R) Xeon(R) CPU E5-2697 v2 @ 2.70GHz BenchmarkQueue BenchmarkQueue-3 2442654 498.4 ns/op 270 B/op 4 allocs/op BenchmarkQueue-3 2472348 504.0 ns/op 270 B/op 4 allocs/op BenchmarkQueue-3 2351935 514.6 ns/op 273 B/op 4 allocs/op BenchmarkQueue-3 2384654 517.1 ns/op 272 B/op 4 allocs/op BenchmarkQueue-3 2268847 527.1 ns/op 275 B/op 4 allocs/op ``` ```sh 54s Run go test -v -run=^$ -count 5 -benchmem -bench . ./... goos: darwin goarch: amd64 pkg: github.com/golang-queue/queue cpu: Intel(R) Xeon(R) CPU E5-1650 v2 @ 3.50GHz BenchmarkQueue BenchmarkQueue-3 3307142 390.1 ns/op 192 B/op 3 allocs/op BenchmarkQueue-3 3353919 393.2 ns/op 192 B/op 3 allocs/op BenchmarkQueue-3 3142076 397.9 ns/op 194 B/op 3 allocs/op BenchmarkQueue-3 3212948 390.7 ns/op 193 B/op 3 allocs/op BenchmarkQueue-3 3259310 367.6 ns/op 193 B/op 3 allocs/op ``` ```sh $ benchstat a.txt b.txt name old time/op new time/op delta Queue-3 512ns ± 3% 388ns ± 5% -24.27% (p=0.008 n=5+5) name old alloc/op new alloc/op delta Queue-3 272B ± 1% 193B ± 1% -29.12% (p=0.008 n=5+5) name old allocs/op new allocs/op delta Queue-3 4.00 ± 0% 3.00 ± 0% -25.00% (p=0.008 n=5+5) ``` Signed-off-by: Bo-Yi.Wu <appleboy.tw@gmail.com>
1 parent 2190832 commit bd08b4a

File tree

2 files changed

+13
-3
lines changed

2 files changed

+13
-3
lines changed

job/job.go

+9
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,15 @@ func (m *Message) Encode() []byte {
4747
return b
4848
}
4949

50+
// Rest for reset default value
51+
func (m *Message) Rest() {
52+
m.Task = nil
53+
m.Payload = nil
54+
m.RetryCount = 0
55+
m.Timeout = 0
56+
m.RetryDelay = 0
57+
}
58+
5059
func NewMessage(m core.QueuedMessage, opts ...AllowOption) *Message {
5160
o := NewOptions(opts...)
5261

queue.go

+4-3
Original file line numberDiff line numberDiff line change
@@ -120,10 +120,11 @@ func (q *Queue) Queue(m core.QueuedMessage, opts ...job.AllowOption) error {
120120
}
121121

122122
message := job.NewMessage(m, opts...)
123+
payload := message.Encode()
124+
message.Rest()
125+
message.Payload = payload
123126

124-
if err := q.worker.Queue(&job.Message{
125-
Payload: message.Encode(),
126-
}); err != nil {
127+
if err := q.worker.Queue(message); err != nil {
127128
return err
128129
}
129130

0 commit comments

Comments
 (0)