Commit Graph

17118 Commits

Author SHA1 Message Date
Yukihiro "Matz" Matsumoto dc2c2f6bde mruby-string-ext: optimize chars method with C fast path
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.
2025-06-27 09:01:19 +09:00
Yukihiro "Matz" Matsumoto 2d6c2179dd mruby-string-ext: optimize strip methods with C implementation
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.
2025-06-27 09:01:19 +09:00
Yukihiro "Matz" Matsumoto 2008d1733b mruby-set: use || operator in merge and subtract methods 2025-06-27 09:01:19 +09:00
Yukihiro "Matz" Matsumoto 62affa290f mruby-set: simplify initialize method return flow 2025-06-27 09:01:19 +09:00
Yukihiro "Matz" Matsumoto 6db72e01fe mruby-set: simplify optimized method pattern with || operator 2025-06-27 09:01:19 +09:00
Yukihiro "Matz" Matsumoto 192880c1e7 mruby-set: refactor internal methods for cleaner enumerable logic
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>
2025-06-27 09:01:19 +09:00
Yukihiro "Matz" Matsumoto 456c8c97e8 mruby-set: optimize and refactor set_is_set()
- Use a direct type check instead of `mrb_obj_is_kind_of()` for efficiency.
- Remove the unused `mrb_state*` argument from the function signature.
2025-06-26 08:11:38 +09:00
Yukihiro "Matz" Matsumoto 3b2e2e0901 mruby-set: refactor kset_resize to prevent GC memory leak
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.
2025-06-26 07:56:49 +09:00
Yukihiro "Matz" Matsumoto 9c07b08a6c Merge pull request #6565 from jbampton/add-more-rubo-cops 2025-06-25 17:54:18 +09:00
Yukihiro "Matz" Matsumoto d29c9c5ef7 Merge pull request #6561 from jbampton/pre-commit-clean-up 2025-06-25 17:52:22 +09:00
Yukihiro "Matz" Matsumoto c6801740a0 mruby-set (kset_resize): n_bucket inconsistency when GC happens 2025-06-25 17:47:49 +09:00
Yukihiro "Matz" Matsumoto ae39a9e56f mruby-io (mrb_file_basename): add type cast to fix mixed signedness 2025-06-25 17:47:48 +09:00
Yukihiro "Matz" Matsumoto c7bbd8e10f mruby-set: add mrb_static_assert_object_size
The C struct that implements Ruby object should fit in RVALUE.
2025-06-25 17:47:48 +09:00
Yukihiro "Matz" Matsumoto 5963fbb56b mruby-set: refactor set_reset
- implement kset_rehash()
- use kset_rehash() in set_reset()
- remove no longer used kset_copy()
2025-06-25 17:47:48 +09:00
Yukihiro "Matz" Matsumoto 4be57dd807 mruby-set: reduce forward declarations 2025-06-25 17:47:48 +09:00
Yukihiro "Matz" Matsumoto ea913f62be mruby-set (set_flatten_recursive): rename argument names 2025-06-25 17:47:48 +09:00
Yukihiro "Matz" Matsumoto 3f06d8e5ae mruby-set: adjust mrb_gc_arena_restore() places
- not before `return` statement (VM will do)
- at the bottom of KSET_FOREACH loop body
2025-06-25 17:47:47 +09:00
Yukihiro "Matz" Matsumoto 2644086977 mruby-set: remove names with kh that remain from last change 2025-06-25 17:47:47 +09:00
John Bampton b4a1e98f2e Rubocop: fix target Ruby version; add two more cops; fix lint error
https://docs.rubocop.org/rubocop/cops_layout.html#layoutassignmentindentation

https://docs.rubocop.org/rubocop/cops_layout.html#layoutblockendnewline
2025-06-25 18:02:53 +10:00
Yukihiro "Matz" Matsumoto 84a4bd16ac Merge pull request #6563 from jbampton/rubocop-indentation 2025-06-25 16:12:27 +09:00
Yukihiro "Matz" Matsumoto e333c9fbc9 Merge pull request #6562 from jbampton/misc-fix-spelling-word-case 2025-06-25 16:10:38 +09:00
Yukihiro "Matz" Matsumoto dfb065c95a Merge pull request #6564 from jbampton/remove-jumanjihouse-hooks 2025-06-25 16:09:10 +09:00
Yukihiro "Matz" Matsumoto 0edd0aec73 mruby-set: use KSET_FOREACH macro extensively
Implemented by: Rovo Dev
2025-06-25 13:33:39 +09:00
Yukihiro "Matz" Matsumoto fc6d7cc424 mruby-set: new helper function set_ensure_initialized 2025-06-25 13:23:21 +09:00
Yukihiro "Matz" Matsumoto 29bc46703b mruby-set: small refactoring 2025-06-25 13:22:13 +09:00
Yukihiro "Matz" Matsumoto b1dc550892 mruby-set: remove names with kh that stands for khash no longer used 2025-06-25 13:06:21 +09:00
Yukihiro "Matz" Matsumoto 0d1c22150e mruby-set: Simplify code with convenience macros and cleanup
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
2025-06-25 11:17:53 +09:00
John Bampton 62f4de0c01 Remove jumanjihouse pre-commit hooks no longer maintained 2025-06-25 12:11:06 +10:00
John Bampton 4e5db98128 pre-commit add rubocop with one rule spaces for indentation
https://docs.rubocop.org/rubocop/integration_with_other_tools.html#git-pre-commit-hook-integration-with-pre-commit
2025-06-25 12:02:35 +10:00
Yukihiro "Matz" Matsumoto a82911b35f mruby-set: replace khash dependency with embedded kset implementation
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.
2025-06-25 10:53:42 +09:00
John Bampton 383cd6a936 misc: fix spelling word case 2025-06-25 10:46:23 +10:00
John Bampton 420a60ee9f pre-commit updates and fix prettier entrypoint
https://nodejs.org/en/download

https://www.npmjs.com/package/prettier

prettier fix ups
2025-06-24 22:20:38 +10:00
Yukihiro "Matz" Matsumoto 88a7b782c6 Merge pull request #6560 from mruby/set-gc-marking-v2 2025-06-24 14:10:22 +09:00
Yukihiro "Matz" Matsumoto a6a0346e1c mruby-os-memsize: support Set class 2025-06-24 13:56:07 +09:00
Yukihiro "Matz" Matsumoto 132561418b mruby-set: update Set class to use struct RSet not struct RData 2025-06-24 13:45:14 +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 4e08fae75a mruby-compiler: add comprehensive comments to codegen.c 2025-06-24 09:50:37 +09:00
Yukihiro "Matz" Matsumoto 84a983c847 backtrace.c: add macros for clarity 2025-06-24 09:49:37 +09:00
Yukihiro Matz Matsumoto dcd661efb8 File.basename: add support for suffix removal and enhance tests 2025-06-23 22:49:43 +00:00
Yukihiro "Matz" Matsumoto 791b374155 khash.h: reduce KHASH_DEFAULT_SIZE from 32 to 8 2025-06-23 17:07:31 +09:00
Yukihiro "Matz" Matsumoto c152f3a79a mruby-set: add comprehensive tests for Set operations: +, -, & 2025-06-23 17:07:31 +09:00
Yukihiro "Matz" Matsumoto 18b45c7ab5 mruby-set: change string representation from #<Set: {1}> to Set[1] 2025-06-23 17:07:31 +09:00
Yukihiro "Matz" Matsumoto 561a3eac82 mruby-set: make eql? an alias of == method 2025-06-23 17:07:30 +09:00
Yukihiro "Matz" Matsumoto 20b32531ea mruby-set: Fix memory leak in Set#flatten! and simplify implementation
- 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
2025-06-23 17:07:30 +09:00
Yukihiro "Matz" Matsumoto 9d739cdad8 mruby-set: use set_is_set extensively 2025-06-23 17:07:30 +09:00
Yukihiro "Matz" Matsumoto f47609574a mruby-set: remove some AI generated work-log comments 2025-06-23 17:07:30 +09:00
Yukihiro "Matz" Matsumoto ea0ceff7fb mruby-set: implement Set#flatten and flatten! in C
The code is generated by Cursor.
2025-06-23 17:07:30 +09:00
Yukihiro "Matz" Matsumoto 73c61e4a60 mruby-set: optimize inspect/to_s to avoid intermediate array 2025-06-23 17:07:29 +09:00
Yukihiro "Matz" Matsumoto e2982bdb57 mruby-set: optimize join method to avoid intermediate array 2025-06-23 17:07:29 +09:00
Yukihiro "Matz" Matsumoto 0430f1e9b8 mruby-set: optimize eql? by checking fast path 2025-06-23 17:07:29 +09:00