mirror of
https://github.com/mruby/mruby
synced 2026-06-08 16:11:16 +00:00
vm.c: use 1.5x stack growth instead of linear
Change default stack growth from linear (+128) to exponential (1.5x). This reduces reallocation frequency while maintaining reasonable memory usage. The minimum growth is still MRB_STACK_GROWTH (128) to ensure small programs don't over-allocate. MRB_STACK_EXTEND_DOUBLING (2x growth) remains available for maximum performance when memory is not a concern. Co-authored-by: Claude <noreply@anthropic.com>
This commit is contained in:
@@ -61,12 +61,12 @@ end
|
||||
`MRB_STACK_EXTEND_DOUBLING`
|
||||
|
||||
- If defined doubles the stack size when extending it.
|
||||
- Otherwise extends stack with `MRB_STACK_GROWTH`.
|
||||
- Otherwise extends stack with 1.5x growth (minimum `MRB_STACK_GROWTH`).
|
||||
|
||||
`MRB_STACK_GROWTH`
|
||||
|
||||
- Default value is `128`.
|
||||
- Used in stack extending.
|
||||
- Minimum stack growth size when extending.
|
||||
- Ignored when `MRB_STACK_EXTEND_DOUBLING` is defined.
|
||||
|
||||
`MRB_STACK_MAX`
|
||||
|
||||
@@ -169,13 +169,17 @@ stack_extend_alloc(mrb_state *mrb, mrb_int room)
|
||||
else
|
||||
size += room;
|
||||
#else
|
||||
/* Use linear stack growth.
|
||||
/* Use 1.5x stack growth.
|
||||
It is slightly slower than doubling the stack space,
|
||||
but it saves memory on small devices. */
|
||||
if (room <= MRB_STACK_GROWTH)
|
||||
size += MRB_STACK_GROWTH;
|
||||
else
|
||||
size += room;
|
||||
{
|
||||
size_t newsize = size + (size >> 1); /* 1.5x growth */
|
||||
if (newsize < size + MRB_STACK_GROWTH)
|
||||
newsize = size + MRB_STACK_GROWTH;
|
||||
if (newsize < size + (size_t)room)
|
||||
newsize = size + room;
|
||||
size = newsize;
|
||||
}
|
||||
#endif
|
||||
|
||||
mrb_value *newstack = (mrb_value*)mrb_realloc(mrb, mrb->c->stbase, sizeof(mrb_value) * size);
|
||||
|
||||
Reference in New Issue
Block a user