Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

builtin: zero out internal map/array pointers on m.free(), to reduce the work for the GC mark phase for non escaping maps/arrays, used in hot loops #18415

Conversation

spytheman
Copy link
Member

@spytheman spytheman commented Jun 11, 2023

Note the maximum RAM usage for the following program. It is significantly lower with this PR (1611608KB vs 1918948KB):

Program:

fn work_with_map() {
	mut m := map[string]int{}
	for i in 0 .. 4_000_000 {
		m['k_${i}'] = i
	}
	eprintln('   > m.len: ${m.len:10}')
	unsafe { m.free() }
}

fn main() {
	eprintln('start')
	for k in 0 .. 10 {
		work_with_map()
		eprintln('k: ${k:10}')
	}
	eprintln('stop')
}

On master:

#0 17:19:41 ᛋ master /v/vnew❱xtime ./m_old
start
   > m.len:    4000000
k:          0
GC Warning: Repeated allocation of very large block (appr. size 67112960):
        May lead to memory leak and poor performance
   > m.len:    4000000
k:          1
   > m.len:    4000000
k:          2
   > m.len:    4000000
k:          3
   > m.len:    4000000
k:          4
GC Warning: Repeated allocation of very large block (appr. size 17362944):
        May lead to memory leak and poor performance
   > m.len:    4000000
k:          5
   > m.len:    4000000
k:          6
   > m.len:    4000000
k:          7
   > m.len:    4000000
k:          8
GC Warning: Repeated allocation of very large block (appr. size 63430656):
        May lead to memory leak and poor performance
   > m.len:    4000000
k:          9
stop
CPU: 41.41s     Real: 39.06s    Elapsed: 0:39.06        RAM: 1918948KB  ./m_old
#0 17:20:27 ᛋ master /v/vnew❱

With this PR:

#0 17:20:33 ᛋ builtin_make_gc_mark_more_efficient_for_freed_v_maps_and_arrays /v/vnew❱xtime ./m_new
start
GC Warning: Repeated allocation of very large block (appr. size 39600128):
        May lead to memory leak and poor performance
GC Warning: Repeated allocation of very large block (appr. size 63430656):
        May lead to memory leak and poor performance
   > m.len:    4000000
k:          0
   > m.len:    4000000
k:          1
   > m.len:    4000000
k:          2
GC Warning: Repeated allocation of very large block (appr. size 71360512):
        May lead to memory leak and poor performance
   > m.len:    4000000
k:          3
   > m.len:    4000000
k:          4
   > m.len:    4000000
k:          5
   > m.len:    4000000
k:          6
   > m.len:    4000000
k:          7
   > m.len:    4000000
k:          8
   > m.len:    4000000
k:          9
stop
CPU: 42.96s     Real: 39.42s    Elapsed: 0:39.42        RAM: 1611608KB  ./m_new
#0 17:21:18 ᛋ builtin_make_gc_mark_more_efficient_for_freed_v_maps_and_arrays /v/vnew❱

…the work for the GC mark phase for non escaping maps/arrays, used in hot loops
@spytheman spytheman marked this pull request as draft June 11, 2023 14:30
@medvednikov
Copy link
Member

All tests pass. Good job!

@spytheman spytheman marked this pull request as ready for review June 14, 2023 11:58
@spytheman spytheman merged commit 6806086 into vlang:master Jun 14, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants