Commit Graph

6209 Commits

Author SHA1 Message Date
Yukihiro "Matz" Matsumoto 8cd64f6e37 variable.c: improve binary search logic in bsearch_idx
Co-authored-by: Gemini <gemini@google.com>
2025-08-14 10:52:54 +09:00
Yukihiro "Matz" Matsumoto 50072c71ab variable.c: clarify memory allocation calls
Co-authored-by: Gemini <gemini@google.com>
2025-08-14 10:52:54 +09:00
Yukihiro "Matz" Matsumoto 41c5fc4dc6 variable.c: remove unused macros
Co-authored-by: Gemini <gemini@google.com>
2025-08-14 10:52:53 +09:00
Yukihiro "Matz" Matsumoto 4d92444317 hash.c: clarify EA growth and remove unused macro
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>
2025-08-14 10:52:51 +09:00
Yukihiro "Matz" Matsumoto 0e91696397 dump.c: add type cast to retrieve bigint length from the pool
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.
2025-08-14 10:52:51 +09:00
Yukihiro "Matz" Matsumoto 6f5dd98951 hash.c: improve performance with quadratic probing
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>
2025-08-14 10:52:51 +09:00
Yukihiro "Matz" Matsumoto 57d398b105 hash.c: use value-based hash for numbers
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>
2025-08-14 10:52:50 +09:00
Yukihiro "Matz" Matsumoto 07b803e28a docs: replace xml-style markup with markdown in comments
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
2025-08-14 10:52:49 +09:00
Yukihiro "Matz" Matsumoto 576069f2fb class: add call-seq comments and helper function documentation
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
2025-08-14 10:52:48 +09:00
Yukihiro "Matz" Matsumoto 9fd79ff0f8 array: add call-seq comments and helper function documentation
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
2025-08-14 10:52:48 +09:00
Yukihiro "Matz" Matsumoto cb298607b1 readfloat: improve accuracy and performance with lookup tables and integer arithmetic
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>
2025-08-14 10:52:43 +09:00
Hendrik d7edd3dbc1 fix bigint on raspberry pi
this fixes an issue where base can be out of range on a raspberry pi.
2025-08-10 09:21:38 +02:00
dearblue 8064d4587f Improved iseq annotations for new and != 2025-08-01 23:04:32 +09:00
Yukihiro "Matz" Matsumoto 54ee91156a Merge pull request #6582 from dearblue/type-tag 2025-07-22 08:26:14 +09:00
dearblue bd0f111397 Stricter type tag in mrb_obj_alloc()
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()`.
2025-07-21 22:44:39 +09:00
dearblue 8b20f346f3 Setting the type tag with boot_defclass()
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`.
2025-07-21 22:43:27 +09:00
dearblue 91f2dca111 Merge mrb_obj_iv_inspect() into mrb_obj_inspect()
`mrb_obj_iv_inspect()` is an internal implementation function and is not called by any function other than `mrb_obj_inspect()`.
2025-07-21 20:54:50 +09:00
Yukihiro "Matz" Matsumoto 95656c40ff state.c: optimize mrb_state initialization by deferring method cache clear
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>
2025-07-11 10:09:38 +09:00
Yukihiro "Matz" Matsumoto 2735340702 kernel.c: remove mrb_inspect_recursive_p(); #5531
And use mrb_recursive_method_p() and its helper methods.

Co-authored-by: Claude <noreply@anthropic.com>
2025-07-11 10:09:37 +09:00
Yukihiro "Matz" Matsumoto 38882aecee array.c: add recursion detection to Array#== and Array#eql?
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>
2025-07-11 10:09:37 +09:00
Yukihiro "Matz" Matsumoto 5a85350121 kernel.c: replace __inspect_recursive? with __method_recursive?
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>
2025-07-11 10:09:36 +09:00
Yukihiro "Matz" Matsumoto b64fc03182 kernel.c: simplify mrb_inspect_recursive_p using new recursion detection
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>
2025-07-11 10:09:36 +09:00
Yukihiro "Matz" Matsumoto 419c8ebfb2 hash.c: add recursion detection to prevent SystemStackError; fix #5531
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>
2025-07-11 10:09:36 +09:00
Yukihiro "Matz" Matsumoto 96355f33b2 hash.c: implement Hash#eql? in C for better performance
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>
2025-07-11 10:09:36 +09:00
Yukihiro "Matz" Matsumoto 633317a809 hash.c: implement Hash#== in C for better performance
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>
2025-07-11 10:09:35 +09:00
Yukihiro "Matz" Matsumoto 670b54f859 variable.h: add prefetch to bsearch_idx
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>
2025-07-11 10:09:35 +09:00
Yukihiro "Matz" Matsumoto 0da6df6063 class.c: prevent crash in instance_eval; fix #6570
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>
2025-07-05 18:32:47 +09:00
Yukihiro "Matz" Matsumoto e4b83f688e variable.c: use branch-free binary search for ivars
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.
2025-07-05 18:32:47 +09:00
Yukihiro "Matz" Matsumoto 1167636998 class.c: optimize method lookup with branch-free binary search
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.
2025-07-05 18:32:46 +09:00
Yukihiro "Matz" Matsumoto cf8faed585 array: implement hybrid sorting with algorithm selection
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
2025-06-30 23:31:05 +09:00
Yukihiro "Matz" Matsumoto 6fab19cd82 arry.c: optimize Array#sort! comparison for common types
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>
2025-06-30 14:42:23 +09:00
Yukihiro "Matz" Matsumoto 6d8c673efb array.c: replace recursive heapify with iterative implementation
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
2025-06-30 14:41:04 +09:00
Yukihiro "Matz" Matsumoto 05ad26f92a array: implement to_a and entries methods in c
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
2025-06-30 12:21:29 +09:00
Yukihiro "Matz" Matsumoto c4464fa25a array.c: expose mrb_ary_dup() as a new C API 2025-06-29 20:46:51 +09:00
google-labs-jules[bot] dfd7251223 Refactor: Improve Set GC marking and freeing
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.
2025-06-24 04:02:41 +00:00
Yukihiro "Matz" Matsumoto 84a983c847 backtrace.c: add macros for clarity 2025-06-24 09:49:37 +09:00
Yukihiro "Matz" Matsumoto eb1820d75e symbol.c: remove unused <mruby/khash.h> header 2025-06-23 17:07:28 +09:00
Yukihiro "Matz" Matsumoto 695207be68 hash.c (mrb_obj_hash_code): expose hash function 2025-06-23 17:07:21 +09:00
Yukihiro "Matz" Matsumoto 4cd7f523fb Merge pull request #6555 from dearblue/mt_foreach 2025-06-21 06:33:35 +09:00
dearblue 5931aeb825 mrb_mt_foreach() needs to update the pointer at each loop
When calling a user function, a pointer retrieved outside of a loop may be invalidated.
2025-06-19 22:35:09 +09:00
dearblue afd5805224 iv_foreach() needs to update the pointer at each loop
When calling a user function, a pointer retrieved outside of a loop may be invalidated.
2025-06-19 22:25:13 +09:00
Yukihiro "Matz" Matsumoto 262d3715f5 proc.c: make Proc#initialize_copy private 2025-06-15 14:14:24 +09:00
Yukihiro "Matz" Matsumoto 8cb427b75c hash.c: make Hash#initialize_copy private 2025-06-15 14:13:56 +09:00
Yukihiro "Matz" Matsumoto a25a896caf range.c: make Range#initialize_copy private 2025-06-15 14:12:58 +09:00
Yukihiro "Matz" Matsumoto 65369ca1e6 array.c: make Array#initialize_copy private 2025-06-15 14:12:12 +09:00
Yukihiro "Matz" Matsumoto ff2464e879 string.c: make String#initialize_copy private 2025-06-15 14:11:54 +09:00
Yukihiro "Matz" Matsumoto 35d073a986 vm.c: fix unintended argument renaming by Google Jules; #6533 2025-06-06 11:53:14 +09:00
Yukihiro "Matz" Matsumoto 2b75da7335 Merge pull request #6553 from mruby/add-vm-comments 2025-06-06 08:15:41 +09:00
google-labs-jules[bot] da9365f98e Add descriptive comments for MRB_API functions in src/vm.c
This commit adds Doxygen-style comments to several MRB_API functions
in src/vm.c to improve code readability and documentation.

The following functions were commented:
- mrb_stack_extend
- mrb_protect_error
- mrb_funcall
- mrb_funcall_id
- mrb_funcall_with_block
- mrb_funcall_argv
- mrb_yield_with_class
- mrb_yield_argv
- mrb_yield
- mrb_vm_run
- mrb_vm_exec
- mrb_top_run

Additionally, the parameter name 'self' in mrb_yield_with_class was
renamed to 'self_obj' for better clarity and consistency with the new comment.
2025-06-05 23:14:43 +00:00
Yukihiro "Matz" Matsumoto e55b3f0c55 Merge pull request #6552 from mruby/fix-comments-variable-c 2025-06-06 08:14:17 +09:00