This commit is contained in:
2025-08-12 10:36:26 +02:00
parent 4cd5b51085
commit 6b46b3dbaa
30 changed files with 472 additions and 747 deletions

View File

@@ -25,63 +25,58 @@ pub mut:
pub fn biz_model_example(name string) BizModel {
mut biz_model := BizModel{
name: name
name: name
intlist: []int{len: 10, init: rand.intn(100) or { 0 }}
intstr: []string{len: 10, init: rand.string(5)}
mymap: map[string]SubItem{}
intstr: []string{len: 10, init: rand.string(5)}
mymap: map[string]SubItem{}
}
for i in 0 .. 100 {
sub_item := SubItem{
name: 'SubItem ${i}'
name: 'SubItem ${i}'
intlist: []int{len: 5, init: rand.intn(50) or { 0 }}
intstr: []string{len: 5, init: rand.string(3)}
intstr: []string{len: 5, init: rand.string(3)}
}
biz_model.mymap['item_${i}'] = sub_item
}
return biz_model
}
pub fn get(name string) !BizModel {
rlock bizmodels {
if ! (name in bizmodels) {
if name !in bizmodels {
return error("can't find bizmodel: ${name}")
}
return bizmodels[name] or { panic("bug") }
return bizmodels[name] or { panic('bug') }
}
return error("bug")
return error('bug')
}
pub fn getset(name string) BizModel {
lock bizmodels {
if ! (name in bizmodels) {
set(biz_model_example(name))
if name !in bizmodels {
set(biz_model_example(name))
}
return bizmodels[name] or { panic("bug") }
return bizmodels[name] or { panic('bug') }
}
return BizModel{} //weird we need to do this
return BizModel{} // weird we need to do this
}
pub fn set(bizmodel BizModel) {
lock bizmodels {
bizmodels[bizmodel.name] = bizmodel
}
}
fn fill_biz_models(nr int) {
for i in 0 .. nr {
getset("bm${i}")
getset('bm${i}')
}
rlock bizmodels{
//check we have enough bizmodels in mem
rlock bizmodels {
// check we have enough bizmodels in mem
assert bizmodels.len == nr
}
}
fn get_memory_usage() i64 {
@@ -93,41 +88,39 @@ fn get_memory_usage() i64 {
fn main() {
sw := time.new_stopwatch()
nr:=100
nr_new :=100000 //make small changes, the garbage collector should keep it clean
nr := 100
nr_new := 100000 // make small changes, the garbage collector should keep it clean
fill_biz_models(nr)
memory_usage_1 := get_memory_usage()
println('Memory usage after creating ${nr} BizModels: ${memory_usage_1} KB')
println('Time taken: ${sw.elapsed().milliseconds()} ms')
for _ in 0 .. nr_new {
currentnr:=rand.intn(nr-1) or {panic(err)}
mut new_model:= get("bm${currentnr}")!
//new_model.intlist = new_model.intlist.map(it + rand.intn(10) or { 0 })
currentnr := rand.intn(nr - 1) or { panic(err) }
mut new_model := get('bm${currentnr}')!
// new_model.intlist = new_model.intlist.map(it + rand.intn(10) or { 0 })
new_model.intstr = new_model.intstr.map(it + rand.string(2))
mut new_model2:= get("bm${currentnr}")!
assert new_model2.intstr != new_model.intstr //should be different because was not a reference
mut new_model2 := get('bm${currentnr}')!
assert new_model2.intstr != new_model.intstr // should be different because was not a reference
set(new_model)
mut new_model3:= get("bm${currentnr}")!
assert new_model3.intstr == new_model.intstr //should be different because was not a reference
mut new_model3 := get('bm${currentnr}')!
assert new_model3.intstr == new_model.intstr // should be different because was not a reference
}
rlock bizmodels{
//check we have enough bizmodels in mem
rlock bizmodels {
// check we have enough bizmodels in mem
assert bizmodels.len == nr
}
}
println("wait 1 sec")
//lets make sure garbage collector works
println('wait 1 sec')
// lets make sure garbage collector works
time.sleep(1 * time.second)
memory_usage_2 := get_memory_usage()
println('Memory usage after creating ${nr_new} random BizModels: ${memory_usage_2} KB')
println('Time taken: ${sw.elapsed().milliseconds()} ms')
println("QUESTION: why does memory go up?, we didn't add to the memory should have been equal...")
}
}