From a8437584aa2ad375ffee13ffec3802b70c27f758 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Uwe=20Kr=C3=BCger?= <45282134+UweKrueger@users.noreply.github.com> Date: Mon, 14 Jun 2021 17:12:47 +0200 Subject: [PATCH] gc: extend optimized mode to channel buffers (#10443) --- vlib/sync/channels.v | 38 ++++++++++++++++++++++++++++++++++---- vlib/sync/pool/pool.v | 4 ++-- vlib/sync/waitgroup.v | 4 ++-- vlib/v/gen/c/cgen.v | 9 ++++++--- vlib/v/markused/markused.v | 9 +++++++++ 5 files changed, 53 insertions(+), 11 deletions(-) diff --git a/vlib/sync/channels.v b/vlib/sync/channels.v index 7fc11b5d5d..74b8bd4852 100644 --- a/vlib/sync/channels.v +++ b/vlib/sync/channels.v @@ -121,15 +121,19 @@ pub: pub fn new_channel(n u32) &Channel { st := sizeof(T) - return new_channel_st(n, st) + if isreftype(T) { + return new_channel_st(n, st) + } else { + return new_channel_st_noscan(n, st) + } } fn new_channel_st(n u32, st u32) &Channel { wsem := if n > 0 { n } else { 1 } rsem := if n > 0 { u32(0) } else { 1 } rbuf := if n > 0 { unsafe { malloc(int(n * st)) } } else { &byte(0) } - sbuf := if n > 0 { vcalloc(int(n * 2)) } else { &byte(0) } - mut ch := &Channel{ + sbuf := if n > 0 { vcalloc_noscan(int(n * 2)) } else { &byte(0) } + mut ch := Channel{ objsize: st cap: n write_free: n @@ -143,7 +147,33 @@ fn new_channel_st(n u32, st u32) &Channel { ch.readsem.init(rsem) ch.writesem_im.init(0) ch.readsem_im.init(0) - return ch + return &ch +} + +fn new_channel_st_noscan(n u32, st u32) &Channel { + $if gcboehm_opt ? { + wsem := if n > 0 { n } else { 1 } + rsem := if n > 0 { u32(0) } else { 1 } + rbuf := if n > 0 { unsafe { malloc_noscan(int(n * st)) } } else { &byte(0) } + sbuf := if n > 0 { vcalloc_noscan(int(n * 2)) } else { &byte(0) } + mut ch := Channel{ + objsize: st + cap: n + write_free: n + read_avail: 0 + ringbuf: rbuf + statusbuf: sbuf + write_subscriber: 0 + read_subscriber: 0 + } + ch.writesem.init(wsem) + ch.readsem.init(rsem) + ch.writesem_im.init(0) + ch.readsem_im.init(0) + return &ch + } $else { + return new_channel_st(n, st) + } } pub fn (ch &Channel) auto_str(typename string) string { diff --git a/vlib/sync/pool/pool.v b/vlib/sync/pool/pool.v index 7171d3a73a..b2c5340cdd 100644 --- a/vlib/sync/pool/pool.v +++ b/vlib/sync/pool/pool.v @@ -45,7 +45,7 @@ pub fn new_pool_processor(context PoolProcessorConfig) &PoolProcessor { if isnil(context.callback) { panic('You need to pass a valid callback to new_pool_processor.') } - mut pool := &PoolProcessor{ + mut pool := PoolProcessor{ items: [] results: [] shared_context: voidptr(0) @@ -55,7 +55,7 @@ pub fn new_pool_processor(context PoolProcessorConfig) &PoolProcessor { thread_cb: voidptr(context.callback) } pool.waitgroup.init() - return pool + return &pool } // set_max_jobs gives you the ability to override the number diff --git a/vlib/sync/waitgroup.v b/vlib/sync/waitgroup.v index e9b3d170e6..ffa9af52c0 100644 --- a/vlib/sync/waitgroup.v +++ b/vlib/sync/waitgroup.v @@ -26,9 +26,9 @@ mut: } pub fn new_waitgroup() &WaitGroup { - mut wg := &WaitGroup{} + mut wg := WaitGroup{} wg.init() - return wg + return &wg } pub fn (mut wg WaitGroup) init() { diff --git a/vlib/v/gen/c/cgen.v b/vlib/v/gen/c/cgen.v index 7d340bf7e7..6b66904f44 100644 --- a/vlib/v/gen/c/cgen.v +++ b/vlib/v/gen/c/cgen.v @@ -3116,7 +3116,8 @@ fn (mut g Gen) expr(node ast.Expr) { } ast.ChanInit { elem_typ_str := g.typ(node.elem_type) - g.write('sync__new_channel_st(') + noscan := g.check_noscan(node.elem_type) + g.write('sync__new_channel_st${noscan}(') if node.has_cap { g.expr(node.cap_expr) } else { @@ -6074,8 +6075,10 @@ fn (mut g Gen) type_default(typ_ ast.Type) string { return g.type_default((sym.info as ast.Alias).parent_type) } .chan { - elemtypstr := g.typ(sym.chan_info().elem_type) - return 'sync__new_channel_st(0, sizeof($elemtypstr))' + elem_type := sym.chan_info().elem_type + elemtypstr := g.typ(elem_type) + noscan := g.check_noscan(elem_type) + return 'sync__new_channel_st${noscan}(0, sizeof($elemtypstr))' } .array { elem_typ := sym.array_info().elem_type diff --git a/vlib/v/markused/markused.v b/vlib/v/markused/markused.v index 989d41c6f7..3bb1922280 100644 --- a/vlib/v/markused/markused.v +++ b/vlib/v/markused/markused.v @@ -56,6 +56,7 @@ pub fn mark_used(mut table ast.Table, pref &pref.Preferences, ast_files []&ast.F // string. methods '18.add', '18.trim_space', + '18.repeat', '18.replace', '18.clone', '18.clone_static', @@ -302,6 +303,14 @@ pub fn mark_used(mut table ast.Table, pref &pref.Preferences, ast_files []&ast.F || k.starts_with('map_') { walker.fn_decl(mut mfn) } + if pref.gc_mode in [.boehm_full_opt, .boehm_incr_opt] { + if k in ['new_map_noscan_key', 'new_map_noscan_value', 'new_map_noscan_key_value', + 'new_map_init_noscan_key', 'new_map_init_noscan_value', + 'new_map_init_noscan_key_value', + ] { + walker.fn_decl(mut mfn) + } + } } } else { for map_fn_name in ['new_map', 'new_map_init', 'map_hash_string', 'new_dense_array'] {