Skip to content

Commit 3b83b40

Browse files
committed
Refactor stats tracking and memory management in view modules
- Replace direct stats field increments with dedicated increment_* methods in Stats - Update all view constructors and generators (ContainerView, ImageView, TextView, RtfView) to use new stats increment methods - Add clear() methods to View and Layout for improved GC and memory cleanup - Refactor Window update logic: ensure proper clearing of views/layouts, refresh UI, and use update_max_renderers for stats - Minor improvements to stats output formatting
1 parent 3ac324f commit 3b83b40

File tree

7 files changed

+71
-42
lines changed

7 files changed

+71
-42
lines changed

stats.v

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,32 @@ mut:
1515
max_renderers usize
1616
}
1717

18+
fn (mut stats Stats) increment_container_views() {
19+
stats.container_views += 1
20+
}
21+
22+
fn (mut stats Stats) increment_text_views() {
23+
stats.text_views += 1
24+
}
25+
26+
fn (mut stats Stats) increment_image_views() {
27+
stats.image_views += 1
28+
}
29+
30+
fn (mut stats Stats) increment_rtf_views() {
31+
stats.rtf_views += 1
32+
}
33+
34+
fn (mut stats Stats) update_max_renderers(count usize) {
35+
if count > stats.max_renderers {
36+
stats.max_renderers = count
37+
}
38+
}
39+
40+
fn (mut stats Stats) increment_layouts() {
41+
stats.layouts += 1
42+
}
43+
1844
fn (window &Window) stats() string {
1945
mut tx := []string{}
2046
tx << ''
@@ -31,7 +57,7 @@ fn (window &Window) stats() string {
3157
fn (window &Window) view_stats() string {
3258
mut tx := []string{}
3359
tx << ''
34-
tx << 'Views'
60+
tx << 'Views Generated'
3561
tx << stat_sub_div
3662
tx << 'container views ${cm(gui_stats.container_views):17}'
3763
tx << 'text views ${cm(gui_stats.text_views):17}'

view.v

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,3 +25,21 @@ fn generate_layout(view &View, mut window Window) Layout {
2525
}
2626
return layout
2727
}
28+
29+
// clear is feeble attempt to get the GC to collect views
30+
fn (mut view View) clear() {
31+
for mut content in view.content {
32+
content.clear()
33+
}
34+
unsafe { view.content.reset() }
35+
view.content.clear()
36+
}
37+
38+
// clear is feeble attempt to get the GC to collect layouts
39+
fn (mut layout Layout) clear() {
40+
for mut child in layout.children {
41+
child.clear()
42+
}
43+
unsafe { layout.children.reset() }
44+
layout.children.clear()
45+
}

view_container.v

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ mut:
6060
fn (cv ContainerView) generate(mut _ Window) Layout {
6161
assert cv.shape_type in [.rectangle, .circle]
6262
$if !prod {
63-
gui_stats.layouts += 1
63+
gui_stats.increment_layouts()
6464
}
6565
if cv.invisible {
6666
return Layout{}
@@ -224,6 +224,10 @@ pub:
224224
// its content top-to-bottom or left_to_right. A `.none` axis allows a
225225
// container to behave as a canvas with no additional layout.
226226
fn container(cfg ContainerCfg) View {
227+
$if !prod {
228+
gui_stats.increment_container_views()
229+
}
230+
227231
if cfg.invisible {
228232
return ContainerView{
229233
over_draw: true // removes it from spacing calculations
@@ -278,10 +282,6 @@ fn container(cfg ContainerCfg) View {
278282
else { cfg.content }
279283
}
280284

281-
$if !prod {
282-
gui_stats.container_views += 1
283-
}
284-
285285
return ContainerView{
286286
id: cfg.id
287287
id_focus: cfg.id_focus

view_image.v

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ pub:
3535

3636
fn (iv &ImageView) generate(mut window Window) Layout {
3737
$if !prod {
38-
gui_stats.layouts += 1
38+
gui_stats.increment_layouts()
3939
}
4040
if iv.invisible {
4141
return Layout{}
@@ -68,7 +68,7 @@ fn (iv &ImageView) generate(mut window Window) Layout {
6868

6969
pub fn image(cfg ImageCfg) View {
7070
$if !prod {
71-
gui_stats.image_views += 1
71+
gui_stats.increment_image_views()
7272
}
7373
return ImageView{
7474
id: cfg.id

view_rtf.v

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ pub:
4040

4141
fn (rtf &RtfView) generate(mut window Window) Layout {
4242
$if !prod {
43-
gui_stats.layouts += 1
43+
gui_stats.increment_layouts()
4444
}
4545

4646
if rtf.invisible {
@@ -79,13 +79,13 @@ fn (rtf &RtfView) generate(mut window Window) Layout {
7979

8080
// rtf creates a view from the given [RtfCfg](#RtfCfg)
8181
pub fn rtf(cfg RtfCfg) View {
82-
mut ll := datatypes.LinkedList[TextSpan]{}
83-
ll.push_many(cfg.spans)
84-
8582
$if !prod {
86-
gui_stats.rtf_views += 1
83+
gui_stats.increment_rtf_views()
8784
}
8885

86+
mut ll := datatypes.LinkedList[TextSpan]{}
87+
ll.push_many(cfg.spans)
88+
8989
return RtfView{
9090
id: cfg.id
9191
id_focus: cfg.id_focus

view_text.v

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ mut:
2121

2222
fn (t &TextView) generate(mut window Window) Layout {
2323
$if !prod {
24-
gui_stats.layouts += 1
24+
gui_stats.increment_layouts()
2525
}
2626
if t.cfg.invisible {
2727
return Layout{}
@@ -105,7 +105,7 @@ fn (t &TextCfg) free() {
105105
// operations. See [TextCfg](#TextCfg)
106106
pub fn text(cfg TextCfg) View {
107107
$if !prod {
108-
gui_stats.text_views += 1
108+
gui_stats.increment_text_views()
109109
}
110110
return TextView{
111111
cfg: &cfg

window.v

Lines changed: 12 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,7 @@ pub fn window(cfg &WindowCfg) &Window {
108108
w.blinky_cursor_animation()
109109
}
110110
on_init(w)
111+
w.update_window()
111112
}
112113
sample_count: int(cfg.samples)
113114
)
@@ -128,12 +129,12 @@ fn frame_fn(mut window Window) {
128129
window.ui.begin()
129130
renderers_draw(window.renderers, window)
130131
window.ui.end()
131-
gc_collect() // revisit gc_collect() once leak is found. Strikes me as a performance issue maybe - mrw
132132
sapp.set_mouse_cursor(window.view_state.mouse_cursor)
133133
$if trace_update_window_calls ? {
134134
println(window.update_window_calls)
135135
}
136136
window.update_window_calls = 0
137+
gc_collect() // revisit gc_collect() once leak is found. Strikes me as a performance issue maybe - mrw
137138
window.unlock()
138139
}
139140

@@ -227,53 +228,37 @@ fn event_fn(ev &gg.Event, mut w Window) {
227228
// and replaces the current view generator.
228229
pub fn (mut window Window) update_view(gen_view fn (&Window) View) {
229230
// Order matters here. Clear the view state first
230-
window.view_state.clear(mut window)
231-
view := gen_view(window)
232-
mut layout := window.compose_layout(view)
233-
234231
window.lock()
232+
window.view_state.clear(mut window)
235233
window.view_generator = gen_view
236-
window.layout = layout
237-
window.renderers.clear()
238-
clip_rect := window.window_rect()
239-
background := window.color_background()
240-
render_layout(mut layout, background, clip_rect, mut window)
241-
242-
$if !prod {
243-
if window.renderers.len > gui_stats.max_renderers {
244-
gui_stats.max_renderers = usize(window.renderers.len)
245-
}
246-
}
247-
248234
window.unlock()
249-
window.ui.refresh_ui()
250235
}
251236

252237
// update_window generates a new layout from the window's current
253238
// view generator.
254-
pub fn (mut window Window) update_window() {
239+
fn (mut window Window) update_window() {
255240
window.update_window_calls++
256241
if window.update_window_calls > window.max_update_window_calls_per_frame {
257242
return
258243
}
259244

260245
window.lock()
261-
262-
view := window.view_generator(window)
246+
mut view := window.view_generator(window)
247+
mut old_layout := window.layout
263248
window.layout = window.compose_layout(view)
264249
window.renderers.clear()
265250
clip_rect := window.window_rect()
266251
background := window.color_background()
267252
render_layout(mut window.layout, background, clip_rect, mut window)
253+
window.unlock()
254+
255+
view.clear()
256+
old_layout.clear()
257+
window.ui.refresh_ui()
268258

269259
$if !prod {
270-
if window.renderers.len > gui_stats.max_renderers {
271-
gui_stats.max_renderers = usize(window.renderers.len)
272-
}
260+
gui_stats.update_max_renderers(usize(window.renderers.len))
273261
}
274-
275-
window.unlock()
276-
window.ui.refresh_ui()
277262
}
278263

279264
// compose_layout produces a layout from the given view that is

0 commit comments

Comments
 (0)