Skip to content

Commit e079b22

Browse files
authored
avoid allocation from timeout queue iterator (#710)
1 parent 3842342 commit e079b22

File tree

3 files changed

+16
-12
lines changed

3 files changed

+16
-12
lines changed

utils/event_emitter.go

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,11 @@ func (e *EventEmitter[K, V]) Observe(k K) *EventObserver[V] {
7575
o := l.Observe()
7676
e.mu.Unlock()
7777

78-
o.stop = append(o.stop, func() { e.cleanUpObserverList(k) })
78+
stop := o.stop
79+
o.stop = func() {
80+
stop()
81+
e.cleanUpObserverList(k)
82+
}
7983

8084
return o
8185
}
@@ -129,7 +133,7 @@ func (l *EventObserverList[V]) Observe() *EventObserver[V] {
129133
le := l.observers.PushBack(o)
130134
l.mu.Unlock()
131135

132-
o.stop = append(o.stop, func() { l.stopObserving(le) })
136+
o.stop = func() { l.stopObserving(le) }
133137

134138
return o
135139
}
@@ -150,14 +154,14 @@ func (l *EventObserverList[V]) stopObserving(le *list.Element) {
150154

151155
type EventObserver[V any] struct {
152156
logger logger.Logger
153-
stop []func()
157+
stop func()
154158
ch chan V
155159
}
156160

157161
func NewEventObserver[V any](stopFunc func()) (*EventObserver[V], func(v V)) {
158162
o := &EventObserver[V]{
159163
logger: logger.GetLogger(),
160-
stop: []func(){stopFunc},
164+
stop: stopFunc,
161165
ch: make(chan V, defaultQueueSize),
162166
}
163167
return o, o.emit
@@ -172,9 +176,7 @@ func (o *EventObserver[V]) emit(v V) {
172176
}
173177

174178
func (o *EventObserver[V]) Stop() {
175-
for _, stop := range o.stop {
176-
stop()
177-
}
179+
o.stop()
178180
}
179181

180182
func (o *EventObserver[V]) Events() <-chan V {

utils/event_emitter_test.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,9 +43,11 @@ func TestEventEmitter(t *testing.T) {
4343
default:
4444
}
4545

46+
ao0.Stop()
47+
4648
keys := emitter.ObservedKeys()
4749
sort.Strings(keys)
48-
require.Equal(t, []string{"a", "b"}, keys)
50+
require.Equal(t, []string{"b"}, keys)
4951
})
5052

5153
t.Run("observer", func(t *testing.T) {

utils/timeoutqueue.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -100,11 +100,11 @@ func (q *TimeoutQueue[T]) remove(i *TimeoutQueueItem[T]) {
100100
i.prev = nil
101101
}
102102

103-
func (q *TimeoutQueue[T]) IterateAfter(timeout time.Duration) *timeoutQueueIterator[T] {
103+
func (q *TimeoutQueue[T]) IterateAfter(timeout time.Duration) timeoutQueueIterator[T] {
104104
return newTimeoutQueueIterator(q, timeout, false)
105105
}
106106

107-
func (q *TimeoutQueue[T]) IterateRemoveAfter(timeout time.Duration) *timeoutQueueIterator[T] {
107+
func (q *TimeoutQueue[T]) IterateRemoveAfter(timeout time.Duration) timeoutQueueIterator[T] {
108108
return newTimeoutQueueIterator(q, timeout, true)
109109
}
110110

@@ -115,8 +115,8 @@ type timeoutQueueIterator[T any] struct {
115115
item *TimeoutQueueItem[T]
116116
}
117117

118-
func newTimeoutQueueIterator[T any](q *TimeoutQueue[T], timeout time.Duration, remove bool) *timeoutQueueIterator[T] {
119-
return &timeoutQueueIterator[T]{
118+
func newTimeoutQueueIterator[T any](q *TimeoutQueue[T], timeout time.Duration, remove bool) timeoutQueueIterator[T] {
119+
return timeoutQueueIterator[T]{
120120
q: q,
121121
time: time.Now().Add(-timeout),
122122
remove: remove,

0 commit comments

Comments
 (0)