Skip to content

Commit b4113fc

Browse files
committed
Replace O(n²) bubble sort with sort.Slice in POTA tracker
1 parent 286321c commit b4113fc

1 file changed

Lines changed: 4 additions & 7 deletions

File tree

backend/pota/client.go

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
"fmt"
77
"io"
88
"net/http"
9+
"sort"
910
"strings"
1011
"sync"
1112
"time"
@@ -138,13 +139,9 @@ func (c *InMemorySpotTracker) UpdateSpot(ctx context.Context, sp spot.Spot, mode
138139
entries = append(entries, expEntry{k, v.expiry})
139140
}
140141
// Sort by expiry time (ascending)
141-
for i := 0; i < len(entries)-1; i++ {
142-
for j := i + 1; j < len(entries); j++ {
143-
if entries[j].expiry.Before(entries[i].expiry) {
144-
entries[i], entries[j] = entries[j], entries[i]
145-
}
146-
}
147-
}
142+
sort.Slice(entries, func(i, j int) bool {
143+
return entries[i].expiry.Before(entries[j].expiry)
144+
})
148145
// Remove oldest entries until we're under maxCacheSize
149146
removeCount := len(c.cache) - c.maxCacheSize
150147
for i := 0; i < removeCount && i < len(entries); i++ {

0 commit comments

Comments
 (0)