Skip to content

Commit

Permalink
gc: extend optimized mode to channel buffers (#10443)
Browse files Browse the repository at this point in the history
  • Loading branch information
UweKrueger committed Jun 14, 2021
1 parent e5debbb commit a843758
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 11 deletions.
38 changes: 34 additions & 4 deletions vlib/sync/channels.v
Expand Up @@ -121,15 +121,19 @@ pub:

pub fn new_channel<T>(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
Expand All @@ -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 {
Expand Down
4 changes: 2 additions & 2 deletions vlib/sync/pool/pool.v
Expand Up @@ -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)
Expand All @@ -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
Expand Down
4 changes: 2 additions & 2 deletions vlib/sync/waitgroup.v
Expand Up @@ -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() {
Expand Down
9 changes: 6 additions & 3 deletions vlib/v/gen/c/cgen.v
Expand Up @@ -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 {
Expand Down Expand Up @@ -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
Expand Down
9 changes: 9 additions & 0 deletions vlib/v/markused/markused.v
Expand Up @@ -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',
Expand Down Expand Up @@ -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'] {
Expand Down

0 comments on commit a843758

Please sign in to comment.