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.
Internal C functions now return a status, allowing Ruby methods
to avoid `is_a?(Set)` checks and simplify the logic for handling
different enumerable types.
Co-authored-by: Gemini <gemini@google.com>
Refactored `kset_resize` and `kset_put2` in `mrbgems/mruby-set/src/set.c`
to address a potential memory leak.
The previous implementation of `kset_resize` could lead to objects
referenced only by `old_keys` being garbage collected if a GC cycle
was triggered during calls to `mrb_obj_hash_code()` or `mrb_eql()`
while rehashing. This was because `s->data` was updated to the new,
empty data block before `old_keys` were fully processed.
Changes:
- Introduced a `kset_raw_put` function to encapsulate the common logic
for inserting an element into a set's underlying arrays (keys/flags).
- Modified `kset_resize` to:
- Keep the `old_data` pointer (and thus `old_keys`) valid and reachable
throughout the rehashing process.
- Allocate `new_data` and populate it using `kset_raw_put` for each
element from `old_data`.
- Free `old_data` only after all elements are successfully copied.
- Update the main set structure (`s->data`, `s->n_buckets`, `s->size`)
after the new data is fully prepared.
- Refactored `kset_put2` to use the `kset_raw_put` function, reducing
code duplication.
This ensures that all mrb_value objects remain reachable during GC
cycles that might occur within the rehashing logic, preventing the
memory leak.
Add convenience macros to reduce code duplication and improve readability:
- kset_is_uninitialized(s) for checking uninitialized sets
- kset_is_empty(s) for checking empty sets
- KSET_FOREACH(s, k) for iterating over set elements
Replace repetitive manual checks and for-loops throughout the codebase
with these macros.
Implemented by: Rovo Dev
Replace the external khash dependency with a custom, memory-optimized
kset implementation that embeds directly into struct RSet. This change
significantly reduces memory consumption and eliminates the need for
khash.h inclusion.
Implemented by: Rovo Dev
Key improvements:
- Embedded kset_t directly in struct RSet (exactly 3 pointers in size)
- Combined memory layout: [keys...][flags...] in single allocation
- Eliminated pointer indirection for better cache performance
- Removed dependency on khash.h and related types (khint_t, khiter_t)
- Maintained full API compatibility with existing mruby-set interface
- Optimized for mrb_value keys with custom hash and equality functions
Technical details:
- kset_t structure: void *data, uint32_t n_buckets, uint32_t size
- Open addressing with linear probing for collision resolution
- 2-bit flags per bucket (empty/deleted) packed efficiently
- Power-of-2 bucket sizing with 75% load factor upper bound
- Integrated GC marking and memory management
Memory savings:
- Eliminates separate khash_t allocation and pointer storage
- Reduces struct RSet from 4 pointers to 3 pointers + embedded data
- More efficient memory layout with better locality of reference
All existing functionality preserved including set operations, iteration,
comparison methods, and Ruby-level API compatibility.
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.
- Remove circular reference check in favor of max depth check only
- Fix memory leak by properly handling errors in set_flatten_bang
- Simplify code by reducing variables and unifying error handling