Copilot commented on code in PR #889:
URL:
https://github.com/apache/skywalking-banyandb/pull/889#discussion_r2598949943
##########
banyand/internal/sidx/part.go:
##########
@@ -772,6 +772,9 @@ func (mp *memPart) mustInitFromElements(es *elements) {
bw.Flush(mp.partMetadata)
// Update key range in part metadata
+ sort.Slice(es.userKeys, func(i, j int) bool {
+ return es.userKeys[i] < es.userKeys[j]
+ })
if len(es.userKeys) > 0 {
mp.partMetadata.MinKey = es.userKeys[0]
mp.partMetadata.MaxKey = es.userKeys[len(es.userKeys)-1]
Review Comment:
Consider using a linear scan to find min/max instead of sorting the entire
slice. This would reduce complexity from O(n log n) to O(n):
```go
// Update key range in part metadata
if len(es.userKeys) > 0 {
minKey, maxKey := es.userKeys[0], es.userKeys[0]
for _, key := range es.userKeys[1:] {
if key < minKey {
minKey = key
}
if key > maxKey {
maxKey = key
}
}
mp.partMetadata.MinKey = minKey
mp.partMetadata.MaxKey = maxKey
}
```
This optimization would be more efficient, especially for large datasets.
```suggestion
if len(es.userKeys) > 0 {
minKey, maxKey := es.userKeys[0], es.userKeys[0]
for _, key := range es.userKeys[1:] {
if key < minKey {
minKey = key
}
if key > maxKey {
maxKey = key
}
}
mp.partMetadata.MinKey = minKey
mp.partMetadata.MaxKey = maxKey
```
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]