From 8afdc1835fa7aade5da73fa5b09a0babdf966c54 Mon Sep 17 00:00:00 2001 From: Drew Weymouth Date: Fri, 17 Nov 2023 20:13:29 -0800 Subject: [PATCH] use binary search to search visible slices --- widget/list.go | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) diff --git a/widget/list.go b/widget/list.go index cbde9bdb79..e300e3fc98 100644 --- a/widget/list.go +++ b/widget/list.go @@ -752,10 +752,20 @@ func (l *listLayout) updateSeparators() { } } +// invariant: visible is in ascending order of IDs func (l *listLayout) searchVisible(visible []itemAndID, id ListItemID) int { - for i, v := range visible { - if v.id == id { - return i + // binary search + low := 0 + high := len(visible) - 1 + for low <= high { + mid := (low + high) / 2 + if visible[mid].id == id { + return mid + } + if visible[mid].id > id { + high = mid - 1 + } else { + low = mid + 1 } } return -1