mruby-compiler: add infrastructure for variable-sized ast nodes

This commit introduces the core infrastructure for variable-sized AST
nodes, designed to improve memory efficiency. The previous fixed-size
nodes are replaced by nodes that can store data inline, such as
strings and integers, reducing pointer indirection and memory overhead.

Key changes include:
- A generic variable-sized node header (`mrb_ast_var_header`).
- A size-class-based memory allocation system for these nodes.
- Implementation of variable-sized nodes for core types: symbols,
  strings, integers, and variables (lvar, gvar, ivar, cvar).
- Integration into the parser and code generator, controlled by a
  feature flag.
- Centralized and improved type-casting macros for AST nodes.

Co-authored-by: Claude <noreply@anthropic.com>
This commit is contained in:
Yukihiro "Matz" Matsumoto
2025-08-27 07:58:08 +09:00
parent 8c36e227e7
commit ac02635ba5
5 changed files with 2449 additions and 1886 deletions
+6
View File
@@ -33,6 +33,7 @@ typedef struct mrb_ccontext {
mrb_bool keep_lv:1;
mrb_bool no_optimize:1;
mrb_bool no_ext_ops:1;
mrb_bool use_variable_nodes:1; /* Enable variable-sized AST nodes */
const struct RProc *upper;
size_t parser_nerr;
@@ -133,6 +134,11 @@ struct mrb_parser_state {
uint16_t filename_table_length;
uint16_t current_filename_index;
/* Variable-sized node management */
struct mrb_ast_node *var_free_lists[5]; /* Free lists by size class (SIZE_CLASS_COUNT) */
size_t var_alloc_counts[5]; /* Allocation tracking by size class */
size_t var_total_allocated; /* Total variable node memory */
mrb_bool var_nodes_enabled:1; /* Feature flag for gradual rollout */
mrb_ast_node *nvars;
};