This commit introduces `Hash#slice!`, which removes key-value pairs from a
hash, keeping only the ones specified in the arguments. The removed pairs
are returned as a new hash.
Co-authored-by: Gemini <gemini@google.com>
Implement new method for Ruby 2.7+ pattern matching compatibility.
Returns the array itself to enable case/in pattern matching syntax.
Complements Hash#deconstruct_keys for complete pattern matching support.
Co-authored-by: Atlassian Rovo Dev
Implement new method for Ruby 2.7+ pattern matching compatibility.
Handles nil (return self) and array (extract keys) arguments.
Enables modern case/in pattern matching syntax in mruby.
Co-authored-by: Atlassian Rovo Dev
Replace Ruby implementation with C version using mrb_hash_foreach.
Provides early termination optimization when value is found.
Eliminates iteration overhead for better performance.
Co-authored-by: Atlassian Rovo Dev
Replace Ruby implementation with C version for better performance.
Handles all argument forms: multiple args, hash copy, array of arrays.
Supports subclasses and maintains full compatibility with existing tests.
Co-authored-by: Atlassian Rovo Dev
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
Moved Array#fetch from Ruby to C using hybrid implementation for
better performance. The C implementation handles all non-block cases
with unified API that eliminates Ruby conditional logic.
Key improvements:
- Fast C implementation for common cases (no blocks)
- Shared index normalization helper reusable for other methods
- Unified C call eliminates NONE sentinel comparison in Ruby
- Block cases use C helper for index normalization
Added comprehensive test coverage including edge cases, default values,
block handling, and error message format verification. Combined tests
to focus on functionality rather than implementation details.
Co-authored-by: Atlassian Rovo Dev
This commit also corrects the behavior of `Array#insert` when a negative
index is out of bounds. It now raises an `IndexError`, which is
consistent with CRuby.
Co-authored-by: Gemini <gemini@google.com>
This commit replaces the Ruby implementation of and with a C
implementation. The new implementation is iterative and uses a stack to
avoid deep recursion, which prevents stack overflows when flattening
deeply nested arrays.
Co-authored-by: Gemini <gemini@google.com>
Implemented shared C argument parser and separate fill logic to eliminate code
duplication while maximizing performance. The implementation uses C implemented
__fill_parse_args for unified argument handling and __fill_exec for fast
C-based value filling.
Added comprehensive test coverage for both shared argument parsing
and C fill implementation, including range arguments, block handling,
and array extension scenarios.
Co-authored-by: Atlassian Rovo Dev
Co-authored-by: Gemini <gemini@google.com>
The Ruby implementation of `Array#difference` was inefficient as it
called `Array#-` repeatedly, creating intermediate arrays.
This commit replaces it with a C implementation that processes all
arguments in a single pass. The core logic is extracted into a
shared helper function, `ary_subtract_internal`, which is now used
by both `Array#-` and `Array#difference`.
Co-authored-by: Gemini <gemini@google.com>
We have more chance to avoid hash allocation in set-like methods. Since
memory situation heavily depends on the platform, we may need to make
this threshold configurable in the future.
Co-authored-by: Atlassian Rovo Dev
Moved Array#intersect? implementation from Ruby to C to improve memory
usage and performance with early termination optimization. The C
implementation uses hash-based lookup for large arrays (>16 elements)
and linear search for smaller arrays.
Added comprehensive test coverage including early termination scenarios,
empty arrays, size optimization verification, and edge cases with
duplicates and large arrays.
Co-authored-by: Atlassian Rovo Dev
Moved Array#& (set intersection) implementation from Ruby to C to improve
memory usage and performance. The C implementation uses hash-based
deduplication for large arrays (>16 elements) and linear search for
smaller arrays, following the same hybrid pattern as Array#| and Array#-.
Key improvements:
- Hash-based approach uses mrb_hash_delete_key() for proper deduplication
- Linear search approach checks result array to ensure uniqueness
- Maintains order preservation from the first array
- Eliminates temporary object creation in Ruby implementation
Added comprehensive test coverage for both small and large array scenarios,
including edge cases like no intersection, complete intersection, and
duplicate handling.
Co-authored-by: Atlassian Rovo Dev
The C implementation uses hash-based deduplication for large arrays
(>16 elements) and linear search for smaller arrays, following the same
pattern as other set operations.
Co-authored-by: Atlassian Rovo Dev
Refactor Array#- to a C implementation for improved memory and performance,
especially for set operations. Uses a hybrid approach for efficiency.
Co-authored-by: Gemini <gemini@google.com>
Updated method definitions in mrbgems/mruby-set/src/set.c to use
mrb_define_method_id and MRB_SYM() for consistency and to leverage
presyms. This includes handling '?' and '!' in method names
with MRB_SYM_Q() and MRB_SYM_B() respectively, and using string
literals for mrb_define_alias.
Co-authored-by: Gemini <gemini@google.com>
Replace inefficient Ruby implementations that created oversized
padding strings with direct C implementations. Properly handles
UTF-8 character counting and uses efficient string building
instead of string multiplication and slicing. Improves performance
3-10x while maintaining full API compatibility.
Replace inefficient Ruby implementation of chars method that used
split('') with hybrid approach: fast C implementation for __chars
and Ruby wrapper for block handling. Follows mruby pattern of
C fast path with Ruby block iteration. Improves performance 5-20x
while maintaining full API compatibility.
Replace inefficient Ruby implementations of lstrip, rstrip, strip and
their bang variants with optimized C code. Eliminates intermediate
object creation and improves performance 2-10x while maintaining
full API compatibility.