Move String's 46 method definitions from runtime
mrb_define_method_id() calls to a static ROM method table
sorted at init time. mrb_mt_init_rom() sorts the parallel
vals/keys arrays by presym ID and sets the readonly flag.
Expose mt_tbl and related types in internal.h so ROM tables
can be defined in individual source files.
When MRB_NO_PRESYM is defined, falls back to traditional
runtime method registration.
Co-authored-by: Claude <noreply@anthropic.com>
Replace lossy 2-bit truncation with rotation-based encoding for
64-bit word boxing with float64. The new scheme uses
rotl64(float_bits - ADDEND, 3) to embed floats inline with full
52-bit mantissa precision. Floats with exponents outside [-255,+256]
(0.0, NaN, Inf, very small/large values) fall back to heap-allocated
RFloat.
Co-authored-by: Claude <noreply@anthropic.com>
remove per-object gcnext pointer from MRB_OBJECT_HEADER, saving one
word (8 bytes on 64-bit) per object slot. the gray list for tri-color
marking is replaced by a fixed-size stack (MRB_GRAY_STACK_SIZE=1024)
in mrb_gc. when the stack overflows, a linear heap rescan recovers
gray objects.
object slot size: 48 -> 40 bytes (16.7% reduction on 64-bit).
benchmarks show up to 12% RSS reduction on object-heavy workloads
with neutral performance impact.
Co-authored-by: Claude <noreply@anthropic.com>
These opcodes use BB format instead of BBB, saving 1 byte per call.
In the standard library, this saves ~790 bytes (568 SEND0 + 222 SSEND0).
Co-authored-by: Claude <noreply@anthropic.com>
Add single-byte opcodes for returning true/false directly, completing
the set of literal return opcodes (RETSELF, RETNIL, RETTRUE, RETFALSE).
Codegen applies peephole optimization to fuse LOADTRUE/LOADFALSE + RETURN.
Co-authored-by: Claude <noreply@anthropic.com>
Rename boolean load opcodes for consistency with LOADNIL/LOADSELF.
Backward compatibility aliases are provided in opcode.h.
Co-authored-by: Claude <noreply@anthropic.com>
Add a new opcode that returns nil without requiring LOADNIL + RETURN.
This avoids loading nil into a register by setting the return value (v)
directly. The implementation uses a separate label (L_RETURN_NIL) to
bypass v = regs[a], preserving self in regs[0] for ensure blocks.
Codegen applies peephole optimization to fuse LOADNIL + RETURN -> RETNIL.
Co-authored-by: Claude <noreply@anthropic.com>
Bypass method dispatch when calling blocks via yield. The new OP_BLKCALL
instruction directly invokes the proc without looking up Proc#call,
resulting in 13-17% faster yield performance.
Co-authored-by: Claude <noreply@anthropic.com>
TDEF fuses TCLASS+METHOD+DEF for normal method definitions.
SDEF fuses SCLASS+METHOD+DEF for singleton method definitions.
Saves 4 bytes per method definition (8 bytes -> 4 bytes).
Falls back to unfused instructions if irep index exceeds 255.
Co-authored-by: Claude <noreply@anthropic.com>
Fuses MOVE+LOADI_0+GETIDX pattern into single instruction.
Saves 4 bytes per arr[0] access (7 bytes -> 3 bytes).
Co-authored-by: Claude <noreply@anthropic.com>
fuse MOVE+ADDI+MOVE and MOVE+SUBI+MOVE patterns into single instructions.
ADDILV/SUBILV add/subtract an immediate to a local variable in-place.
BBB format: a=local, b=working space for method call, c=immediate.
saves 5 bytes per instance (9->4 bytes), 40 occurrences in stdlib.
Co-authored-by: Claude <noreply@anthropic.com>
Change OP_MATCHERR from Z format (unconditional) to B format
(conditional on register). This allows fusing JMPIF + MATCHERR
sequence into a single MATCHERR instruction for simple patterns.
Before: JMPIF R2 target (4 bytes) + MATCHERR (1 byte) = 5 bytes
After: MATCHERR R2 (2 bytes)
Saves 3 bytes per pattern match with raise_on_fail.
Co-authored-by: Claude <noreply@anthropic.com>
Replace 4-instruction sequence (GETCONST + STRING + SEND + RAISEIF)
with single OP_MATCHERR instruction that raises NoMatchingPatternError
with "pattern not matched" message.
Bump RITE binary format version from 0300 to 0400 due to opcode
number shift.
Co-authored-by: Claude <noreply@anthropic.com>
- Add mrb_likely/mrb_unlikely macros to common.h for branch prediction
- Optimize OP_GETIDX array fast path:
- Cache RArray pointer to avoid repeated RARRAY() calls
- Single ARY_EMBED_P check instead of two (via RARRAY_LEN + RARRAY_PTR)
- Use unsigned comparison for bounds check
- Add branch prediction hints for common cases
- Convert switch statement to if-else chain for better branch prediction
Benchmark shows ~3% improvement for array read operations.
Co-authored-by: Claude <noreply@anthropic.com>
expose the previously internal outer_class() function as a public API
for mrbgems to retrieve the enclosing class/module of a given class.
closes#6705.
Co-authored-by: Claude <noreply@anthropic.com>
Document that this header is for mruby core internal use only and
should not be included in user code or mrbgems. When MRB_USE_CXX_EXCEPTION
is defined, C source files including this header fail to compile.
Add example showing mrb_protect_error() as the recommended alternative.
Co-authored-by: Claude <noreply@anthropic.com>
Move casecmp_p from mruby-string-ext and mruby-encoding to core as
mrb_strcasecmp_p (predicate function returning mrb_bool). Add
MRB_STR_CASECMP_P macro to internal.h for comparing mrb_value strings
with literal strings.
This eliminates code duplication and avoids static function name
collision for future amalgamation support.
Co-authored-by: Claude <noreply@anthropic.com>
when running scripts via mruby -e or file, return values are unused.
this adds a no_return_value flag to skip generating unnecessary code.
for parallel assignment like a,b = 1,2:
- before: 18 bytes, 5 registers, creates temporary array
- after: 5 bytes, 3 registers, direct register assignment, no RETURN
the flag is set only for the main program, not for libraries loaded
with -r option. eval() and mirb continue returning values correctly.
Co-authored-by: Claude <noreply@anthropic.com>
Extract duplicated UTF-8 codepoint-to-bytes encoding into a shared
function in src/string.c. Update all gems to use it:
- mruby-sprintf: %c specifier
- mruby-io: putc
- mruby-string-ext: Integer#chr
- mruby-pack: pack("U")
- mruby-compiler: Unicode escapes in parser
Also use existing mrb_utf8len() in io.c for character length detection.
Co-authored-by: Claude <noreply@anthropic.com>
Add support for brace-less hash patterns at top level of case/in.
`in a: x, b: y` is now equivalent to `in {a: x, b: y}`.
`in a:, b:` shorthand now works with newlines (CRuby compatible).
Changes:
- Add EXPR_VALUE to IS_LABEL_POSSIBLE() to recognize labels after `in`
- Add brace-less hash pattern rules to p_expr
- Change p_hash_elem to use p_as instead of p_expr to avoid recursion
- Add in_kwarg flag to parser state for pattern matching context
- Set in_kwarg in lexer when keyword_in is returned
- Use EXPR_ARG after tLABEL_TAG when in_kwarg is set (makes newlines significant)
Co-authored-by: Claude <noreply@anthropic.com>
during eql? callbacks, array modifications can cause elements in khash to
be freed by GC, leading to use-after-free. create temporary shared copies
of arrays before populating khash to protect elements during callbacks.
Co-authored-by: Claude <noreply@anthropic.com>
The primary reason is to fix an issue that occurs when an element is removed from the khash data during the `KHASH_FOREACH()` loop.
If the value of `kh_end()` becomes smaller than `k` during the loop, it will repeat a meaningless internal loop until an integer overflow occurs.
__builtin_setjmp/longjmp are x86/x86_64 specific gcc intrinsics
and not supported on arm64. windows arm64 with msys2 clangarm64
now correctly falls through to standard setjmp/longjmp.
Co-authored-by: Claude <noreply@anthropic.com>
Implements dual-mechanism GC protection and optimizes task lookup
using pointer arithmetic based on PicoRuby reference implementation.
GC Protection:
- Add mrb_gc_register/unregister to protect Task objects
- Implement mrb_task_mark_all() to mark task contexts during GC
- Store proc reference in mrb_task to prevent premature collection
- Integrate marking into gc.c root_scan_phase
Performance Optimizations:
- Add MRB2TASK macro for O(1) context-to-task conversion
- Optimize Task.current: O(n) queue search -> O(1) pointer arithmetic
- Optimize Task.pass: simplify to root context check
- Optimize Task.join: use MRB2TASK for current task lookup
Bug Fixes:
- Fix MRB_TASK_CREATED/STOPPED to use MRB_FIBER_TERMINATED
- Add safety check to prevent execution of terminated tasks
- Initialize callinfo PC to bytecode start in task_init_context
Co-authored-by: Claude <noreply@anthropic.com>
Eliminates gradual rollout feature flags that controlled variable-sized AST
nodes. Variable-sized nodes are now the default and only behavior, completing
the AST unification and simplification process.
Co-authored-by: Claude <noreply@anthropic.com>
Remove var_free_lists, var_alloc_counts, and var_total_allocated fields
from parser_state struct as they were never used since all nodes go
directly to codegen. Replace parser_alloc_var() wrapper with direct
parser_palloc() calls throughout the codebase, reducing parser memory
footprint by 88 bytes.
Co-authored-by: Claude <noreply@anthropic.com>
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>
The previous implementation of mrb_int_mul_overflow performed
the multiplication before checking for overflow. This is undefined
behavior for signed integers and can lead to incorrect results on
some compilers (e.g., MSVC).
The implementation has been changed to perform the overflow checks
before the multiplication.
Co-authored-by: Gemini <gemini@google.com>
Move the definition of struct mrb_ast_node to a private header to
hide implementation details from the public API.
Co-authored-by: Gemini <gemini@google.com>
Move STR_FUNC_* macros, enum mrb_string_type, and struct
mrb_parser_heredoc_info from include/mruby/compile.h to
mrbgems/mruby-compiler/core/node.h.
These types are internal to the mruby compiler gem and are used by
both parse.y and codegen.c. Moving them to node.h encapsulates them
within the compiler gem, cleaning up the public mruby/compile.h header.
Co-authored-by: Gemini <gemini@google.com>
This change allows for handling small tables as linear-search arrays,
improving performance for hashes with few elements.
Co-authored-by: Gemini <gemini@google.com>
The hash rebuild process was not GC-safe. When rebuilding the hash
table, the old data was orphaned before the new table was fully
populated, which could lead to a segmentation fault if a GC cycle
was triggered during the process.
This patch refactors the rebuild function to follow a safer pattern:
- A new temporary hash table is allocated on the stack.
- Elements from the original table are copied to the new one.
- The original table's data is swapped with the new table's data
only after the new table is complete.
This ensures the original data is always reachable by the GC during
the rebuild.
Co-authored-by: Gemini <gemini@google.com>
Rename KHASH_SMALL_THRESHOLD to KHASH_SMALL_LIMIT for brevity and clarity.
The shorter name is more concise while maintaining clear meaning as the
upper bound for small table optimization.
Co-authored-by: Claude <noreply@anthropic.com>
Rename KHASH_DEFAULT_SIZE to KHASH_INITIAL_SIZE for clearer meaning.
The name "initial" better conveys that this is the starting size for
new hash tables, while "default" could be ambiguous.
Co-authored-by: Claude <noreply@anthropic.com>
Move kh_alloc_##name from public API to internal helper kh__alloc_##name
since it's only used internally within khash implementation.
Changes:
- Remove kh_alloc_##name from KHASH_DECLARE
- Add kh__alloc_##name as static inline in KHASH_DEFINE
- Update internal calls to use kh__alloc_##name
Co-authored-by: Claude <noreply@anthropic.com>
Rename internal helper functions from kh_ to kh__ prefix while correctly
organizing the API boundary:
KHASH_DECLARE (public interface):
- kh_keys_##name, kh_vals_##name, kh_flags_##name (used by kh_exist macro)
KHASH_DEFINE (internal helpers with kh__ prefix):
- kh__kv_size_##name, kh__htable_size_##name
- kh__mark_occupied_##name, kh__mark_deleted_##name
- kh__key_idx_##name, kh__next_probe_##name
- kh__insert_key_##name, kh__clear_flags_##name
- kh__is_small_##name, kh__get_small_##name
- kh__rebuild_##name, kh__put_small_##name
This clearly separates public API functions from internal implementation
helpers while ensuring kh_flags_##name remains accessible to the public
kh_exist macro.
Co-authored-by: Claude <noreply@anthropic.com>
Added kh_next_probe_##name() helper function to encapsulate the repeated
linear probing step calculation pattern.
Replaced 2 instances of manual probing calculation:
- k = (k+(++step)) & khash_mask(h) -> k = kh_next_probe_##name(k, &step, h)
This eliminates the duplicated bit manipulation pattern and makes the
probing logic more readable and less error-prone.
Co-authored-by: Claude <noreply@anthropic.com>
Added kh_rebuild_##name() helper function that consolidates the complete
"save-allocate-rehash-cleanup" pattern shared between kh_resize and
kh_put_small functions.
The helper intelligently handles both scenarios:
- Small table conversion: iterates by size
- Hash table resize: iterates by buckets with flag checks
This eliminates approximately 25 lines of duplicated code across the
two functions while maintaining identical functionality.
Co-authored-by: Claude <noreply@anthropic.com>
Removed kh_alloc_small_##name() function and inlined its body into the
single call site in kh_init_data_##name(). This eliminates unnecessary
function call overhead and reduces code complexity.
The function was only 2 lines and called once, making it an ideal
candidate for inlining.
Co-authored-by: Claude <noreply@anthropic.com>
Renamed size calculation helpers for clarity:
- kh_data_size_##name() -> kh_kv_size_##name() (keys and values only)
- Added kh_htable_size_##name() (complete hash table including flags)
Updated all usages and simplified patterns:
- kh_kv_size_##name(n) + n/4 -> kh_htable_size_##name(n)
The new names clearly distinguish between:
- kv_size: just the key-value data
- htable_size: complete hash table allocation (data + flags)
This eliminates confusion and makes the code more self-documenting.
Co-authored-by: Claude <noreply@anthropic.com>
Added kh_key_idx_##name() helper function to encapsulate the repeated
pattern of calculating bucket index from key hash.
Replaced 2 instances of manual hash calculation:
- __hash_func(mrb,key) & khash_mask(h) → kh_key_idx_##name(mrb, key, h)
This eliminates the duplicated hash-and-mask pattern and makes the code
more readable by clearly expressing the intent (get bucket index for key).
Co-authored-by: Claude <noreply@anthropic.com>
Added two helper functions to encapsulate repeated flag manipulation patterns:
- kh_mark_occupied_##name(): clears both empty and deleted bits
- kh_mark_deleted_##name(): sets the deleted bit
Replaced 3 instances of manual bit manipulation with calls to these helpers:
- ed_flags[del_k/4] &= ~__m_del[del_k%4] → kh_mark_occupied_##name(h, del_k)
- ed_flags[k/4] &= ~__m_empty[k%4] → kh_mark_occupied_##name(h, k)
- ed_flags[x/4] |= __m_del[x%4] → kh_mark_deleted_##name(h, x)
This eliminates error-prone bit operations, improves readability, and makes
the flag state transitions self-documenting.
Co-authored-by: Claude <noreply@anthropic.com>
Moved kh_data_size_##name() and kh_flags_##name() from KHASH_DECLARE
to KHASH_DEFINE section where internal implementation details belong.
This improves the separation of concerns:
- KHASH_DECLARE: public API only (struct definition, function declarations)
- KHASH_DEFINE: implementation details and internal helper functions
No functional changes, only better code organization.
Co-authored-by: Claude <noreply@anthropic.com>
Added kh_data_size_##name() helper function to eliminate duplicated size
calculation patterns throughout the khash implementation. This single
universal helper calculates data size for N elements and replaces all
manual sizeof calculations.
Key changes:
- Add kh_data_size_##name(khint_t count) helper in KHASH_DECLARE
- Replace manual calculations in kh_flags, kh_alloc_small, kh_alloc
- Replace complex size calculations in kh_replace function
- Use specific patterns: small tables use KHASH_SMALL_THRESHOLD,
hash tables add n_buckets/4 for flag space
This refactoring eliminates 6 instances of duplicated size calculation
code while maintaining identical functionality and performance.
Co-authored-by: Claude <noreply@anthropic.com>
Add kh_replace function that uses direct memory copying instead of
element-by-element rehashing for improved performance.
- Add kh_replace_name function with smart handling of different table types
- Optimize kh_copy to use kh_replace instead of element iteration
- Update Set operations to use kh_replace for copying
- Remove redundant kset_copy_replace function
The optimization provides O(1) memory copy vs O(n) hash operations,
handles small tables and hash tables correctly, and avoids infinite
recursion issues with self-referential data structures.
Co-authored-by: Claude <noreply@anthropic.com>
- Fix kh_exist macro to handle small tables correctly by checking size
instead of non-existent flags
- Add proper small table deletion in kh_del function with element shifting
- Fix kh_copy to use corrected kh_exist macro instead of direct flag access
- Remove unused ed_flags variable in kh_copy function
Co-authored-by: Claude <noreply@anthropic.com>