Skip to content

Commit f6bbb4f

Browse files
committed
address comments
Signed-off-by: Huabing Zhao <[email protected]>
1 parent a9e57a6 commit f6bbb4f

File tree

1 file changed

+15
-14
lines changed

1 file changed

+15
-14
lines changed

internal/message/watchutil.go

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -114,25 +114,26 @@ func coalesceUpdates[K comparable, V any](runner string, updates []watchable.Upd
114114
return updates
115115
}
116116

117-
result := make([]watchable.Update[K, V], 0, len(updates))
118-
indexByKey := make(map[K]int, len(updates))
117+
seen := make(map[K]struct{}, len(updates))
118+
write := len(updates) - 1
119119

120-
for i := len(updates) - 1; i >= 0; i-- {
121-
update := updates[i]
122-
if _, ok := indexByKey[update.Key]; ok {
120+
for read := len(updates) - 1; read >= 0; read-- {
121+
update := updates[read]
122+
if _, ok := seen[update.Key]; ok {
123123
continue
124124
}
125-
126-
indexByKey[update.Key] = len(result)
127-
result = append(result, update)
125+
seen[update.Key] = struct{}{}
126+
updates[write] = update
127+
write--
128128
}
129129

130-
// Reverse the result slice to restore the original order of the last updates for each key
131-
for left, right := 0, len(result)-1; left < right; left, right = left+1, right-1 {
132-
result[left], result[right] = result[right], result[left]
130+
result := updates[write+1:]
131+
if len(result) != len(updates) {
132+
logger.WithValues("runner", runner).Info(
133+
"coalesced updates",
134+
"count", len(result),
135+
"before", len(updates),
136+
)
133137
}
134-
135-
logger.WithValues("runner", runner).Info("coalesced updates", "count", len(result), "before", len(updates))
136-
137138
return result
138139
}

0 commit comments

Comments
 (0)