Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,7 @@
- Use the available width and final alignment for ellipsized text that wraps past `MaxLines` on Apple platforms (#965 by @J4ck1e2000).
- Keep what `ui.SidebarShell`'s sidebar, footer, headers and content hold and `ui.NavHeader`'s buttons clear of the left and right safe area, as on an iPhone in landscape, without padding a page inside the shell twice, and drop the gallery's own padding for it (#835 by @houko).
- Have `nsgotest` count a press of another view as a new click on the macOS and Linux targets, as headless, so a press on a page that a press just pushed or popped reaches it instead of going to the page's previous view as a double click's second (#854 by @houko).
- Free the views a view's children, a `Dynamic`, an `Each` or a text's attachments drop, those last told their layout, and a removed `List`, instead of the lists that held them keeping them past their new end, and through each the whole subtree it was in, until the list grows back that long (#1021 by @iFwu).

## [0.2.0] - 2026-11-19

Expand Down
4 changes: 4 additions & 0 deletions core/commit.go
Original file line number Diff line number Diff line change
Expand Up @@ -281,6 +281,7 @@ func (t *Tree) commitRound() {
}
t.sendSticky()
t.sizeCanvases()
clear(t.laidOut)
t.laidOut = t.laidOut[:0]
t.emitPictureInPictures()
t.emitAccessories()
Expand Down Expand Up @@ -1196,6 +1197,9 @@ func (t *Tree) syncChildren(p *node, want []*node) {
t.items = append(t.items, driver.MountItem{Kind: driver.ItemInsert, ID: c.id, Parent: p.id, Before: before})
}
p.sent.children = append(p.sent.children[:0], want...)
if len(want) < len(old) {
clear(old[len(want):]) // still in the array, which would keep the views dropped
}
}

// move is the insert of a view inserted before into a parent, put off
Expand Down
118 changes: 118 additions & 0 deletions core/core_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -799,6 +799,124 @@ func TestEachKeepsRowsOfEqualItems(t *testing.T) {
}
}

// A view dropped from a list of views is freed: a list cut back to fewer views keeps none of the others in its array
// past its end, where each would stay in memory with the whole subtree it was in.
func TestViewsDroppedFromListsAreFreed(t *testing.T) {
freed := func(t *testing.T, h *host, views ...weak.Pointer[node]) {
t.Helper()
goruntime.GC()
for _, v := range views {
if n := v.Value(); n != nil {
t.Errorf("%s stays in memory", n.props.TestID)
}
}
goruntime.KeepAlive(h.tree)
}
views := func(prefix string, n int, opts View) Group {
g := Group{}
for i := range n {
v := opts
v.TestID = fmt.Sprint(prefix, i)
g = append(g, v)
}
return g
}
box := func(id string) Span {
return Span{Attachment: View{TestID: id, Style: Style{Width: Pt(10), Height: Pt(6)}}}
}

t.Run("rows an Each drops", func(t *testing.T) {
var items *Signal[[]int]
h := newHost(t, Size{Width: 100, Height: 200}, func(cx *Scope) Node {
items = NewSignalFunc(cx, []int{0, 1, 2, 3}, nil)
return View{Children: []Node{Each(items.Get, func(i int) int { return i },
func(cx *Scope, item func() int, _ func() int) Node { return View{TestID: fmt.Sprint("row", item())} })}}
})
h.commit()
dropped := []weak.Pointer[node]{weak.Make(h.find("row2")), weak.Make(h.find("row3"))}
items.Set([]int{0, 1})
h.commit()
freed(t, h, dropped...)
})

t.Run("views a Dynamic builds again, fewer", func(t *testing.T) {
var n *Signal[int]
h := newHost(t, Size{Width: 100, Height: 200}, func(cx *Scope) Node {
n = NewSignal(cx, 4)
return View{Children: []Node{Dynamic(func(*Scope) Node { return views("view", n.Get(), View{}) })}}
})
h.commit()
dropped := []weak.Pointer[node]{weak.Make(h.find("view2")), weak.Make(h.find("view3"))}
n.Set(2)
h.commit()
freed(t, h, dropped...)
})

t.Run("attachments an element's text is set again without", func(t *testing.T) {
var el *Element
h := newHost(t, Size{Width: 200, Height: 100}, func(cx *Scope) Node {
el = NewElement(cx, Text{Spans: []Span{{Content: "ab "}, box("box0"), box("box1")}})
return View{Children: []Node{el}}
})
h.commit()
dropped := []weak.Pointer[node]{weak.Make(h.find("box0")), weak.Make(h.find("box1"))}
el.Set(Text{Spans: []Span{{Content: "ab"}}})
h.commit()
freed(t, h, dropped...)
})

t.Run("attachments a text's spans drop", func(t *testing.T) {
var boxes *Signal[int]
h := newHost(t, Size{Width: 200, Height: 100}, func(cx *Scope) Node {
boxes = NewSignal(cx, 2)
return View{Children: []Node{Text{SpansFunc: func() []Span {
spans := []Span{{Content: "ab "}}
for i := range boxes.Get() {
spans = append(spans, box(fmt.Sprint("box", i)))
}
return spans
}}}}
})
h.commit()
dropped := []weak.Pointer[node]{weak.Make(h.find("box0")), weak.Make(h.find("box1"))}
boxes.Set(0)
h.commit()
freed(t, h, dropped...)
})

t.Run("views that heard their last layout", func(t *testing.T) {
var shown *Signal[bool]
h := newHost(t, Size{Width: 100, Height: 200}, func(cx *Scope) Node {
shown = NewSignal(cx, true)
return View{Children: []Node{Show{When: shown.Get,
Then: func(*Scope) Node { return views("heard", 2, View{OnLayout: func(Bounds) {}}) },
Else: func(*Scope) Node { return views("plain", 2, View{}) }}}}
})
h.commit()
dropped := []weak.Pointer[node]{weak.Make(h.find("heard0")), weak.Make(h.find("heard1"))}
shown.Set(false)
h.commit()
freed(t, h, dropped...)
})

t.Run("lists the tree updates", func(t *testing.T) {
var shown *Signal[bool]
list := func(id string) List {
return List{TestID: id, Count: func() int { return 3 }, Style: Style{Height: Pt(90)},
Row: func(*Scope, ListRow) Node { return View{Style: Style{Height: Pt(30)}} }}
}
h := newHost(t, Size{Width: 100, Height: 200}, func(cx *Scope) Node {
shown = NewSignal(cx, true)
return View{Children: []Node{list("kept"), Show{When: shown.Get, Then: func(*Scope) Node { return list("list") }}}}
})
h.commit()
dropped := []weak.Pointer[node]{weak.Make(h.find("list"))}
shown.Set(false)
h.commit()
freed(t, h, dropped...)
})
}

func TestCatchShowsFallbackAndResets(t *testing.T) {
var fail *Signal[bool]
var reset func()
Expand Down
1 change: 1 addition & 0 deletions core/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -1097,6 +1097,7 @@ func (t *Tree) updateLists() bool {
}
changed = t.within2(l, func() bool { return t.updateList(l) }) || changed
}
clear(t.lists[len(live):]) // still in the array, which would keep the lists removed
t.lists = live
slices.SortStableFunc(parents, func(a, b *node) int { return a.list.depth - b.list.depth })
for _, l := range parents {
Expand Down
2 changes: 2 additions & 0 deletions core/ops.go
Original file line number Diff line number Diff line change
Expand Up @@ -255,8 +255,10 @@ func (t *Tree) setText(cx *Scope, n *node, v Text) {
for _, c := range n.children {
t.discard(c)
}
clear(n.children)
n.children = n.children[:0]
t.childrenChanged(n)
clear(tn.spans)
tn.spans, tn.attachments = tn.spans[:0], 0
for _, sp := range spans {
style := sp.Style
Expand Down
6 changes: 6 additions & 0 deletions core/tree.go
Original file line number Diff line number Diff line change
Expand Up @@ -1024,6 +1024,7 @@ func (t *Tree) clear(region *node) {
for _, c := range region.children {
t.discard(c)
}
clear(region.children)
region.children = region.children[:0]
t.childrenChanged(region)
}
Expand Down Expand Up @@ -1092,7 +1093,11 @@ func (t *Tree) discard(n *node) {

// setChildren replaces a region's children with nodes already in it.
func (t *Tree) setChildren(region *node, children []*node) {
old := region.children
region.children = append(region.children[:0], children...)
if len(children) < len(old) {
clear(old[len(children):]) // still in the array, which would keep the views dropped
}
t.childrenChanged(region)
}

Expand Down Expand Up @@ -1882,6 +1887,7 @@ func (t *Tree) mountText(cx *Scope, parent *node, v Text) {
kept := make([]bool, len(olds))
moved := false // the attachments' views, or their order, changed
k := 0 // the attachments so far
clear(n.text.spans)
n.text.spans = n.text.spans[:0]
for _, sp := range spans {
if sp.Attachment == nil {
Expand Down