Skip to content

Commit

Permalink
Update logic to remove element from slice, avoid allocations (open-te…
Browse files Browse the repository at this point in the history
…lemetry#8856)

Signed-off-by: Bogdan Drutu <bogdandrutu@gmail.com>
  • Loading branch information
bogdandrutu authored Nov 13, 2023
1 parent 8e0f682 commit ffe1a29
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 6 deletions.
11 changes: 6 additions & 5 deletions exporter/exporterhelper/internal/persistent_storage.go
Original file line number Diff line number Diff line change
Expand Up @@ -349,13 +349,14 @@ func (pcs *persistentContiguousStorage) itemDispatchingStart(ctx context.Context

// itemDispatchingFinish removes the item from the list of currently dispatched items and deletes it from the persistent queue
func (pcs *persistentContiguousStorage) itemDispatchingFinish(ctx context.Context, index itemIndex) error {
var updatedDispatchedItems []itemIndex
for _, it := range pcs.currentlyDispatchedItems {
if it != index {
updatedDispatchedItems = append(updatedDispatchedItems, it)
lenCDI := len(pcs.currentlyDispatchedItems)
for i := 0; i < lenCDI; i++ {
if pcs.currentlyDispatchedItems[i] == index {
pcs.currentlyDispatchedItems[i] = pcs.currentlyDispatchedItems[lenCDI-1]
pcs.currentlyDispatchedItems = pcs.currentlyDispatchedItems[:lenCDI-1]
break
}
}
pcs.currentlyDispatchedItems = updatedDispatchedItems

setOp := storage.SetOperation(currentlyDispatchedItemsKey, itemIndexArrayToBytes(pcs.currentlyDispatchedItems))
deleteOp := storage.DeleteOperation(getItemKey(index))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -235,7 +235,7 @@ func TestPersistentStorage_CurrentlyProcessedItems(t *testing.T) {
}

// The queue should be now empty
requireCurrentlyDispatchedItemsEqual(t, newPs, nil)
requireCurrentlyDispatchedItemsEqual(t, newPs, []itemIndex{})
assert.Eventually(t, func() bool {
return newPs.size() == 0
}, 5*time.Second, 10*time.Millisecond)
Expand Down

0 comments on commit ffe1a29

Please sign in to comment.