From 9ad7620a56c9860527c0709fc1b1d8172a789136 Mon Sep 17 00:00:00 2001 From: iFwu <16523864+iFwu@users.noreply.github.com> Date: Fri, 20 Nov 2026 21:54:56 +0800 Subject: [PATCH 1/2] fix(core): free the views a list of views drops past its new end A list of views cut back to fewer reused its array and left the views it dropped in the slots past its new end, so each stayed in memory, and through it the whole subtree it was in, until the list grew back that long: a view's children synced to fewer (syncChildren), a Dynamic or Show built again with fewer views (clear), an Each with fewer rows (setChildren), a text's attachments set again or dropped by SpansFunc (an element's children and spans, and the spans SpansFunc builds again), and the views told their layout in the last commit (laidOut). A chat list that mounts a few hundred rows while scrolled back and drops all but the last few dozen at its end kept every dropped row, close to 100 MB in a headless run. Each list now clears the slots it no longer uses, as accessories, canvases and labels already do. Fixes #1021 --- CHANGELOG.md | 1 + core/commit.go | 4 ++ core/core_test.go | 101 ++++++++++++++++++++++++++++++++++++++++++++++ core/ops.go | 2 + core/tree.go | 6 +++ 5 files changed, 114 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 5adb543e0..08ea795c9 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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, and those last told their layout, 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 diff --git a/core/commit.go b/core/commit.go index 418071d1e..d42672b95 100644 --- a/core/commit.go +++ b/core/commit.go @@ -281,6 +281,7 @@ func (t *Tree) commitRound() { } t.sendSticky() t.sizeCanvases() + clear(t.laidOut) t.laidOut = t.laidOut[:0] t.emitPictureInPictures() t.emitAccessories() @@ -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 diff --git a/core/core_test.go b/core/core_test.go index 052fd68cf..bc3c2a0b3 100644 --- a/core/core_test.go +++ b/core/core_test.go @@ -799,6 +799,107 @@ 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...) + }) +} + func TestCatchShowsFallbackAndResets(t *testing.T) { var fail *Signal[bool] var reset func() diff --git a/core/ops.go b/core/ops.go index aeade513e..f631cba01 100644 --- a/core/ops.go +++ b/core/ops.go @@ -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 diff --git a/core/tree.go b/core/tree.go index da574702f..c1c800117 100644 --- a/core/tree.go +++ b/core/tree.go @@ -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) } @@ -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) } @@ -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 { From 839126b97a0651d5784753c7977d53facba8c8f9 Mon Sep 17 00:00:00 2001 From: iFwu <16523864+iFwu@users.noreply.github.com> Date: Fri, 20 Nov 2026 23:33:06 +0800 Subject: [PATCH 2/2] fix(core): free a removed List from the tree's list of lists updateLists filtered t.lists in place and left the lists it dropped in the slots past its new end, so a removed List stayed in memory, and through it the whole subtree it was in, while another list stayed. It now clears what is past the lists still live, as the other lists of views do. Fixes #1021 --- CHANGELOG.md | 2 +- core/core_test.go | 17 +++++++++++++++++ core/list.go | 1 + 3 files changed, 19 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 08ea795c9..a82daf809 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -104,7 +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, and those last told their layout, 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). +- 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 diff --git a/core/core_test.go b/core/core_test.go index bc3c2a0b3..a18abcb13 100644 --- a/core/core_test.go +++ b/core/core_test.go @@ -898,6 +898,23 @@ func TestViewsDroppedFromListsAreFreed(t *testing.T) { 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) { diff --git a/core/list.go b/core/list.go index deb6f451c..03cdcab3c 100644 --- a/core/list.go +++ b/core/list.go @@ -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 {