SYMTBL_LITERAL_FLAG was defined as 1UL, which can be smaller
than uintptr_t on some platforms (e.g., Windows 64-bit). This
caused symtbl_get_ptr() to return a corrupted pointer.
Changed the flag to be explicitly cast to uintptr_t to ensure
correct behavior on all platforms.
Co-authored-by: Gemini <gemini@google.com>
This commit simplifies the logic for checking if a symbol is a literal in the
`sym_intern_common` function by using the `lit = lit || mrb_ro_data_p(name);`
idiom.
Co-authored-by: Gemini <gemini@google.com>
This commit refactors the `sym_intern_linear_mode` and
`sym_intern_hash_mode` functions to remove duplicate code. A new
function `sym_intern_common` is created to contain the common code.
Co-authored-by: Gemini <gemini@google.com>
Replace separate symflags array with LSB pointer tagging to store
symbol literal flags directly in string pointers. This eliminates
the need for a separate symflags allocation, saving 1/8 of symbol
table memory overhead (282 bytes measured improvement).
Key changes:
- Add LSB tagging helper functions (symtbl_get_ptr, symtbl_is_literal,
symtbl_tag_literal)
- Store literal flag in LSB of mrb->symtbl[i] pointers (LSB=1 for
literals)
- Remove symflags field from mrb_sym_hash_table struct
- Update all symbol access functions to use proper pointer untagging
- Maintain mrb_ro_data_p() detection for platform compatibility
- Fix potential crashes by ensuring untagged pointers in memory
operations
Works in both linear and hash table modes. All 1717 tests pass.
Memory usage reduced by 282 bytes compared to original implementation.
Co-authored-by: Claude <noreply@anthropic.com>
Converts sym_lit_p, sym_lit_set, and sym_flags_clear from complex
macros to clean static inline functions for better readability
and maintainability.
Co-authored-by: Claude <noreply@anthropic.com>
Replace fixed 256-element hash array in mrb_state with adaptive approach:
- Linear search for <=255 symbols (typical embedded use case)
- Hash table allocated on-demand when symbols exceed threshold
- Reduces mrb_state size by 1KB per instance (1068->36 bytes in symbol fields)
- Configurable threshold via MRB_SYMBOL_LINEAR_THRESHOLD in mrbconf.h
Co-authored-by: Claude <noreply@anthropic.com>
Remove redundant definitions of MRB_RECURSIVE_P, MRB_RECURSIVE_UNARY_P,
and MRB_RECURSIVE_BINARY_P from src/kernel.c as they are already defined
in include/mruby.h.
Co-authored-by: Gemini <gemini@google.com>
Refactor the calculation of hash entry array capacity to explicitly use
integer arithmetic for the 1.2x growth factor. This change improves code
clarity without altering the existing growth behavior.
The EA_INCREASE_RATIO macro is no longer used after this refactoring, so
it has been removed for code cleanup.
Co-authored-by: Gemini <gemini@google.com>
If bigint representation is too long, the retrieved length (without type
cast) can be considered as negative. To avoid the issue, we have to add
type cast before assignments.
Replaces the linear probing collision resolution strategy with quadratic
probing. This change significantly improves hash table performance, especially
in high-collision scenarios, by mitigating the primary clustering issue
inherent in linear probing.
The new probing sequence, (step^2 + step) / 2, guarantees that every slot is
visited exactly once in a power-of-two-sized table.
Benchmark results on a high-collision test case show a ~9x improvement in both
insertion and lookup times.
Co-authored-by: Gemini <gemini@google.com>
Fixes a correctness bug where float and bignum hash codes were based on object
identity instead of their numerical value. This change introduces value-based
hashing for these types, ensuring that two numbers with the same value produce
the same hash code, as required by Ruby semantics.
- Floats are now hashed based on their bit representation.
- Bignums are hashed using the dedicated `mrb_bint_hash` function.
This change makes hash behavior correct and more performant by avoiding VM
callbacks for core numeric types.
Co-authored-by: Gemini <gemini@google.com>
Replace XML-style markup tags in comments with markdown equivalents:
- <code>...</code> to `...` (inline code)
- <tt>...</tt> to `...` (teletype/monospace)
- <i>...</i> to *...* (italics/emphasis)
- +...+ to `...` (parameter/variable references)
Updated 80+ files across core source, headers, mrbgems, and libraries
to use consistent markdown formatting in documentation comments.
Handled edge cases including special characters like <=> operators.
Co-authored-by: Atlassian Rovo Dev
Add comprehensive call-seq comments for Ruby methods including include,
prepend, ancestors, and extend. Add brief comments for internal helper
functions including method table operations, class setup, and singleton
class management.
Remove doxygen-style parameter documentation and replace with concise
helper function comments to improve code readability and maintainability.
Co-authored-by: Atlassian Rovo Dev
Add comprehensive call-seq comments for Ruby methods including Array[],
Array.new, concat, +, *, replace, reverse!/reverse, push/<<, shift,
unshift, size/length, empty?, first, and last.
Add brief comments for internal helper functions including array
creation, modification, capacity management, and utility functions
to improve code readability and maintainability.
Co-authored-by: Atlassian Rovo Dev
Replace expensive pow() calls with pre-computed lookup tables for powers of 10.
Use integer arithmetic during parsing to avoid floating-point precision loss.
Add overflow detection for large numbers while maintaining compatibility.
Co-authored-by: Claude <noreply@anthropic.com>
Instances cannot be created with `MRB_TT_FALSE`.
_**Compatibility Note**_
This change may cause runtime errors.
However, that is probably because it is not set correctly by `MRB_SET_INSTANCE_TT()`.
The purpose is to force the setting of the type tag.
This is in preparation for subsequent commits that will prevent the creation of instances with `MRB_TT_FALSE`.
During mrb_state initialization, especially when defining core classes and methods,
the method cache is repeatedly cleared. This causes significant overhead in
scenarios like mrbtest where mrb_state is initialized multiple times.
This commit introduces a `bootstrapping` flag in `struct mrb_state`.
When this flag is TRUE (during mrb_open_core), method cache clears
triggered by `mrb_define_method_raw` and `include_module_at` are suppressed.
The cache is cleared only once at the very end of `mrb_open_core` after
all core methods are defined, and the flag is then set to FALSE.
This optimization significantly reduces the number of method cache clears
during initialization, improving performance for repeated mrb_state creations.
Co-authored-by: Gemini <gemini@google.com>
Prevent SystemStackError when comparing arrays with circular references.
Uses the same recursion detection mechanism as Hash equality methods.
Co-authored-by: Claude <noreply@anthropic.com>
Add more general __method_recursive?(method_name[, arg]) method that can
check recursion for any method, not just inspect. This provides a more
useful API for Ruby code while cleaning up the implementation.
Co-authored-by: Claude <noreply@anthropic.com>
Replace custom inspect_recursive_p implementation with the new
generalized mrb_recursive_method_p for better code reuse and
consistency.
Co-authored-by: Claude <noreply@anthropic.com>
Add generalized recursion detection system and integrate it into Hash#==
and Hash#eql? to prevent infinite recursion with mutually recursive hash
structures. Uses call stack inspection for minimal memory overhead.
Co-authored-by: Claude <noreply@anthropic.com>
Move Hash#eql? implementation from Ruby to C to improve performance and
consistency with other core methods. The C implementation uses mrb_eql
for value comparison, providing proper eql? semantics.
Co-authored-by: Claude <noreply@anthropic.com>
Move Hash#== implementation from Ruby to C to improve performance
and consistency with other core methods. The C implementation
provides the same functionality while being more efficient.
Co-authored-by: Claude <noreply@anthropic.com>
This commit introduces memory prefetching to the `bsearch_idx` functions
in `src/class.c` and `src/variable.c` to improve performance.
A new macro `MRB_MEM_PREFETCH` is defined in `include/mruby/variable.h`
which uses `__builtin_prefetch` if available.
Co-authored-by: Gemini <gemini@google.com>
Fixes a null pointer dereference in `find_visibility_scope` when defining a
singleton method inside `instance_eval`.
This was caused by `ci->u.env` being `NULL` in this context. The fix adds a
`NULL` check to prevent the crash.
Co-authored-by: Gemini <gemini@google.com>
This commit optimizes instance variable lookups by replacing the
search algorithm with the same branch-free binary search recently
introduced for method lookups. This improves performance by
avoiding CPU branch mispredictions.
This commit replaces the method table search algorithm with a
branch-free binary search. This avoids conditional branches,
which can prevent CPU pipeline stalls from branch misprediction,
leading to faster method lookups.
The new `bsearch_idx` function is used for finding, inserting,
and deleting methods in the method table.
Use insertion sort for small arrays (≤16) and heap sort for larger arrays.
Provides 50-200% performance improvement for small arrays while maintaining
O(n log n) guarantee for large arrays. Includes iterative heapify to
eliminate stack overflow risk on memory-constrained devices.
Co-authored-by: Atlassian Rovo Dev
Add fast-path comparisons for integers, floats, and strings in Array#sort!
when no custom comparison block is provided. This reduces VM callback
overhead for common data types, improving performance.
Co-authored-by: Gemini <gemini@google.com>
Eliminates stack overflow risk on memory-constrained devices by reducing
stack usage from O(log n) to O(1) during heap sort operations.
Co-authored-by: Atlassian Rovo Dev
Array#to_a now properly converts subclasses to Array objects. For example,
'class A<Array;end; p A.new(1,2).to_a.class' now returns Array, not A.
Co-authored-by: Atlassian Rovo Dev
This commit addresses feedback on the initial Set GC marking implementation.
Changes include:
- Renamed set marking function to `mrb_gc_mark_set` and updated its
return type to `size_t`.
- Introduced an explicit `mrb_gc_free_set` function for Set objects.
- Updated `gc_mark_children` to use the new mark function signature.
- Added an explicit `case MRB_TT_SET:` in `obj_free` to call `mrb_gc_free_set`.
- Adjusted `set_get_khash` in `mruby-set` to work with `MRB_TT_SET` directly,
rather than relying on `mrb_data_get_ptr`.
- Corrected type checks in `set_init_copy` to use `MRB_TT_SET`.
- Updated function prototypes in internal headers and stubs in mrbc.