1
0
mirror of https://github.com/vlang/v.git synced 2023-08-10 21:13:21 +03:00

sync,strings,cgen: reduce memory consumption in cgen

This commit is contained in:
Delyan Angelov
2021-10-29 21:01:07 +03:00
parent 7e0f2fcda9
commit a7d4236337
4 changed files with 133 additions and 58 deletions

View File

@ -81,12 +81,10 @@ pub fn (mut pool PoolProcessor) work_on_pointers(items []voidptr) {
if pool.njobs > 0 {
njobs = pool.njobs
}
pool.items = []
pool.results = []
pool.thread_contexts = []
pool.thread_contexts = []voidptr{len: items.len}
pool.results = []voidptr{len: items.len}
pool.items = []voidptr{cap: items.len}
pool.items << items
pool.results = []voidptr{len: (pool.items.len)}
pool.thread_contexts << []voidptr{len: (pool.items.len)}
pool.waitgroup.add(njobs)
for i := 0; i < njobs; i++ {
if njobs > 1 {
@ -129,13 +127,22 @@ pub fn (pool &PoolProcessor) get_result<T>(idx int) T {
// get_results - get a list of type safe results in the main thread.
pub fn (pool &PoolProcessor) get_results<T>() []T {
mut res := []T{}
mut res := []T{cap: pool.results.len}
for i in 0 .. pool.results.len {
res << *(&T(pool.results[i]))
}
return res
}
// get_results_ref - get a list of type safe results in the main thread.
pub fn (pool &PoolProcessor) get_results_ref<T>() []&T {
mut res := []&T{cap: pool.results.len}
for i in 0 .. pool.results.len {
res << &T(pool.results[i])
}
return res
}
// set_shared_context - can be called during the setup so that you can
// provide a context that is shared between all worker threads, like
// common options/settings.

View File

@ -34,6 +34,11 @@ fn test_work_on_strings() {
println(x.s)
assert x.s.len > 1
}
println('---------- pool_s.get_results_ref: --------------')
for x in pool_s.get_results_ref<SResult>() {
println(x.s)
assert x.s.len > 1
}
}
fn test_work_on_ints() {
@ -49,4 +54,9 @@ fn test_work_on_ints() {
println(x.i)
assert x.i > 100
}
println('---------- pool_i.get_results_ref: --------------')
for x in pool_i.get_results_ref<IResult>() {
println(x.i)
assert x.i > 100
}
}