aboutsummaryrefslogtreecommitdiff
path: root/base/runtime
diff options
context:
space:
mode:
authorKarl Zylinski <karl@zylinski.se>2024-06-29 08:23:39 +0200
committerKarl Zylinski <karl@zylinski.se>2024-06-29 08:57:21 +0200
commit679f9b4e413fb1cd9aad953cc7a7a287f5abf226 (patch)
treee96ec7d669312c1d9a280e9a8d35b983562daabb /base/runtime
parent06652bebce111640930a513c825e59f7b37bbd4f (diff)
Made default capacity of dynamic arrays more consistent.
Before this if you do `arr: [dynamic]int` and then append to arr, then it will have capacity 8. But if you did `arr := make([dynamic]int, context.temp_allocator)` then arr would have capacity 16. Now both `arr: [dynamic]int` and `arr := make([dynamic]int, context.temp_allocator)` will resut in arr having zero 0. The only reason to use `make` without an explicit len or cap now is because you want to set it up for a non-default allocator. After the first call to `append` it will now in both cases have capacity 8. I also updated the documentation on the strings builder, both to reflect this, and also to fix it incorrectly saying that len would be 'max(16,len)', which wasn't true even before these changes.
Diffstat (limited to 'base/runtime')
-rw-r--r--base/runtime/core_builtin.odin10
-rw-r--r--base/runtime/core_builtin_soa.odin7
2 files changed, 9 insertions, 8 deletions
diff --git a/base/runtime/core_builtin.odin b/base/runtime/core_builtin.odin
index 7758b29a5..4601bca9d 100644
--- a/base/runtime/core_builtin.odin
+++ b/base/runtime/core_builtin.odin
@@ -268,7 +268,7 @@ new_clone :: proc(data: $T, allocator := context.allocator, loc := #caller_locat
return
}
-DEFAULT_RESERVE_CAPACITY :: 16
+DEFAULT_DYNAMIC_ARRAY_CAPACITY :: 8
@(require_results)
make_aligned :: proc($T: typeid/[]$E, #any_int len: int, alignment: int, allocator := context.allocator, loc := #caller_location) -> (T, Allocator_Error) #optional_allocator_error {
@@ -295,7 +295,7 @@ make_slice :: proc($T: typeid/[]$E, #any_int len: int, allocator := context.allo
// Note: Prefer using the procedure group `make`.
@(builtin, require_results)
make_dynamic_array :: proc($T: typeid/[dynamic]$E, allocator := context.allocator, loc := #caller_location) -> (T, Allocator_Error) #optional_allocator_error {
- return make_dynamic_array_len_cap(T, 0, DEFAULT_RESERVE_CAPACITY, allocator, loc)
+ return make_dynamic_array_len_cap(T, 0, 0, allocator, loc)
}
// `make_dynamic_array_len` allocates and initializes a dynamic array. Like `new`, the first argument is a type, not a value.
// Unlike `new`, `make`'s return value is the same as the type of its argument, not a pointer to it.
@@ -423,8 +423,8 @@ _append_elem :: #force_inline proc(array: ^$T/[dynamic]$E, arg: E, should_zero:
return 1, nil
} else {
if cap(array) < len(array)+1 {
- // Same behavior as _append_elems but there's only one arg, so we always just add 8.
- cap := 2 * cap(array) + 8
+ // Same behavior as _append_elems but there's only one arg, so we always just add DEFAULT_DYNAMIC_ARRAY_CAPACITY.
+ cap := 2 * cap(array) + DEFAULT_DYNAMIC_ARRAY_CAPACITY
// do not 'or_return' here as it could be a partial success
if should_zero {
@@ -473,7 +473,7 @@ _append_elems :: #force_inline proc(array: ^$T/[dynamic]$E, should_zero: bool, l
return arg_len, nil
} else {
if cap(array) < len(array)+arg_len {
- cap := 2 * cap(array) + max(8, arg_len)
+ cap := 2 * cap(array) + max(DEFAULT_DYNAMIC_ARRAY_CAPACITY, arg_len)
// do not 'or_return' here as it could be a partial success
if should_zero {
diff --git a/base/runtime/core_builtin_soa.odin b/base/runtime/core_builtin_soa.odin
index 547df0a2b..816df13e2 100644
--- a/base/runtime/core_builtin_soa.odin
+++ b/base/runtime/core_builtin_soa.odin
@@ -147,7 +147,7 @@ make_soa_slice :: proc($T: typeid/#soa[]$E, length: int, allocator := context.al
@(builtin, require_results)
make_soa_dynamic_array :: proc($T: typeid/#soa[dynamic]$E, allocator := context.allocator, loc := #caller_location) -> (array: T, err: Allocator_Error) #optional_allocator_error {
context.allocator = allocator
- reserve_soa(&array, DEFAULT_RESERVE_CAPACITY, loc) or_return
+ reserve_soa(&array, 0, loc) or_return
return array, nil
}
@@ -280,7 +280,8 @@ append_soa_elem :: proc(array: ^$T/#soa[dynamic]$E, arg: E, loc := #caller_locat
}
if cap(array) <= len(array) + 1 {
- cap := 2 * cap(array) + 8
+ // Same behavior as append_soa_elems but there's only one arg, so we always just add DEFAULT_DYNAMIC_ARRAY_CAPACITY.
+ cap := 2 * cap(array) + DEFAULT_DYNAMIC_ARRAY_CAPACITY
err = reserve_soa(array, cap, loc) // do not 'or_return' here as it could be a partial success
}
@@ -337,7 +338,7 @@ append_soa_elems :: proc(array: ^$T/#soa[dynamic]$E, args: ..E, loc := #caller_l
}
if cap(array) <= len(array)+arg_len {
- cap := 2 * cap(array) + max(8, arg_len)
+ cap := 2 * cap(array) + max(DEFAULT_DYNAMIC_ARRAY_CAPACITY, arg_len)
err = reserve_soa(array, cap, loc) // do not 'or_return' here as it could be a partial success
}
arg_len = min(cap(array)-len(array), arg_len)