Skip to content

Commit

Permalink
Remove some unneeded locks within gridwrap, list and richtext
Browse files Browse the repository at this point in the history
  • Loading branch information
Jacalz committed Jan 11, 2025
1 parent 1aeb76d commit 39cc465
Show file tree
Hide file tree
Showing 3 changed files with 3 additions and 22 deletions.
12 changes: 3 additions & 9 deletions widget/gridwrap.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"fmt"
"math"
"sort"
"sync"

"fyne.io/fyne/v2"
"fyne.io/fyne/v2/canvas"
Expand Down Expand Up @@ -143,9 +142,7 @@ func (l *GridWrap) RefreshItem(id GridWrapItemID) {
}
l.BaseWidget.Refresh()
lo := l.scroller.Content.(*fyne.Container).Layout.(*gridWrapLayout)
lo.renderLock.Lock() // ensures we are not changing visible info in render code during the search
item, ok := lo.searchVisible(lo.visible, id)
lo.renderLock.Unlock()
if ok {
lo.setupGridItem(item, id, l.focused && l.currentFocus == id)
}
Expand Down Expand Up @@ -510,10 +507,9 @@ type gridItemAndID struct {
type gridWrapLayout struct {
list *GridWrap

itemPool async.Pool[fyne.CanvasObject]
slicePool async.Pool[*[]gridItemAndID]
visible []gridItemAndID
renderLock sync.Mutex
itemPool async.Pool[fyne.CanvasObject]
slicePool async.Pool[*[]gridItemAndID]
visible []gridItemAndID
}

func newGridWrapLayout(list *GridWrap) fyne.Layout {
Expand Down Expand Up @@ -608,7 +604,6 @@ func (l *gridWrapLayout) updateGrid(refresh bool) {
// code here is a mashup of listLayout.updateList and gridWrapLayout.Layout
padding := l.list.Theme().Size(theme.SizeNamePadding)

l.renderLock.Lock()
length := 0
if f := l.list.Length; f != nil {
length = f()
Expand Down Expand Up @@ -679,7 +674,6 @@ func (l *gridWrapLayout) updateGrid(refresh bool) {
visiblePtr := l.slicePool.Get()
visible := (*visiblePtr)[:0]
visible = append(visible, l.visible...)
l.renderLock.Unlock() // user code should not be locked

for _, obj := range visible {
l.setupGridItem(obj.item, obj.id, l.list.focused && l.list.currentFocus == obj.id)
Expand Down
7 changes: 0 additions & 7 deletions widget/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"fmt"
"math"
"sort"
"sync"

"fyne.io/fyne/v2"
"fyne.io/fyne/v2/canvas"
Expand Down Expand Up @@ -132,9 +131,7 @@ func (l *List) RefreshItem(id ListItemID) {
}
l.BaseWidget.Refresh()
lo := l.scroller.Content.(*fyne.Container).Layout.(*listLayout)
lo.renderLock.RLock() // ensures we are not changing visible info in render code during the search
item, ok := lo.searchVisible(lo.visible, id)
lo.renderLock.RUnlock()
if ok {
lo.setupListItem(item, id, l.focused && l.currentFocus == id)
}
Expand Down Expand Up @@ -611,7 +608,6 @@ type listLayout struct {
visible []listItemAndID
slicePool async.Pool[*[]listItemAndID]
visibleRowHeights []float32
renderLock sync.RWMutex
}

func newListLayout(list *List) fyne.Layout {
Expand Down Expand Up @@ -688,7 +684,6 @@ func (l *listLayout) setupListItem(li *listItem, id ListItemID, focus bool) {
func (l *listLayout) updateList(newOnly bool) {
th := l.list.Theme()
separatorThickness := th.Size(theme.SizeNamePadding)
l.renderLock.Lock()
width := l.list.Size().Width
length := 0
if f := l.list.Length; f != nil {
Expand All @@ -706,7 +701,6 @@ func (l *listLayout) updateList(newOnly bool) {

offY, minRow := l.calculateVisibleRowHeights(l.list.itemMin.Height, length, th)
if len(l.visibleRowHeights) == 0 && length > 0 { // we can't show anything until we have some dimensions
l.renderLock.Unlock() // user code should not be locked
return
}

Expand Down Expand Up @@ -759,7 +753,6 @@ func (l *listLayout) updateList(newOnly bool) {
visiblePtr := l.slicePool.Get()
visible := (*visiblePtr)[:0]
visible = append(visible, l.visible...)
l.renderLock.Unlock() // user code should not be locked

if newOnly {
for _, vis := range visible {
Expand Down
6 changes: 0 additions & 6 deletions widget/richtext.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"image/color"
"math"
"strings"
"sync"
"unicode"

"github.com/go-text/typesetting/di"
Expand Down Expand Up @@ -44,7 +43,6 @@ type RichText struct {
prop *canvas.Rectangle // used to apply text minsize to the scroller `scr`, if present - TODO improve #2464

visualCache map[RichTextSegment][]fyne.CanvasObject
cacheLock sync.Mutex
minCache fyne.Size
}

Expand Down Expand Up @@ -205,8 +203,6 @@ func (t *RichText) deleteFromTo(lowBound int, highBound int) []rune {
// cachedSegmentVisual returns a cached segment visual representation.
// The offset value is > 0 if the segment had been split and so we need multiple objects.
func (t *RichText) cachedSegmentVisual(seg RichTextSegment, offset int) fyne.CanvasObject {
t.cacheLock.Lock()
defer t.cacheLock.Unlock()
if t.visualCache == nil {
t.visualCache = make(map[RichTextSegment][]fyne.CanvasObject)
}
Expand All @@ -225,8 +221,6 @@ func (t *RichText) cachedSegmentVisual(seg RichTextSegment, offset int) fyne.Can
}

func (t *RichText) cleanVisualCache() {
t.cacheLock.Lock()
defer t.cacheLock.Unlock()
if len(t.visualCache) <= len(t.Segments) {
return
}
Expand Down

0 comments on commit 39cc465

Please sign in to comment.