Commit Graph

17454 Commits

Author SHA1 Message Date
Yukihiro "Matz" Matsumoto f4d2117d7d mruby-pack: optimize template parsing with O(1) lookup table
Replace massive 40+ case switch statement in read_tmpl() with direct
format_table[256] lookup for standard format characters. This eliminates
branch prediction overhead and reduces function size from 290 to ~90 lines.

Key improvements:
- O(1) format character resolution vs O(n) switch traversal
- Preserved runtime-dependent format handling (I, i, J, j)
- Maintained full backward compatibility with all existing tests
- Better instruction cache usage with smaller function size
- Consistent template parsing performance across format types

Co-authored-by: Claude <noreply@anthropic.com>
2025-08-14 10:53:12 +09:00
Yukihiro "Matz" Matsumoto 650cdcecbc mruby-pack: optimize string formats with bulk operations and lookup tables
- Replace byte-by-byte padding loops with efficient memset operations
- Add character classification lookup table to eliminate ISSPACE macro overhead
- Optimize reverse trimming in A format using direct table lookup
- Pre-calculate buffer sizes to reduce memory allocation overhead
- Achieve exceptional performance: ~1.3M pack ops/sec, ~1.5M unpack ops/sec
- Maintain full format compatibility for A/a/Z string variants

Co-authored-by: Claude <noreply@anthropic.com>
2025-08-14 10:53:12 +09:00
Yukihiro "Matz" Matsumoto a785c0e20f mruby-pack: optimize binary string formats with batch processing
- Add lookup tables for char-to-bit and bit-to-char conversion
- Implement 8-bit batch processing functions for MSB/LSB formats
- Replace bit-by-bit loops with bulk byte operations
- Use function pointers to eliminate runtime branching
- Pre-calculate buffer sizes to avoid memory reallocation
- Achieve exceptional performance: ~1.6M ops/sec for small inputs,
  ~300K ops/sec for large inputs

Co-authored-by: Claude <noreply@anthropic.com>
2025-08-14 10:53:12 +09:00
Yukihiro "Matz" Matsumoto e60acfc6ff mruby-numeric-ext: add Integer#bit_length
Implement Integer#bit_length in mrbgems/mruby-numeric-ext.
- Fixnum: zero returns 0; negatives follow ~self rule; count bits by shifts.
- Bigint (MRB_USE_BIGINT): handle sign; negatives via mrb_bint_rev, then bit
  length via length of mrb_bint_to_s(..., 2).
- Add tests in mrbgems/mruby-numeric-ext/test/numeric.rb.
- Update README with examples.

Co-authored-by: Codex CLI <codex@openai.com>
2025-08-14 10:53:12 +09:00
Yukihiro "Matz" Matsumoto 6e84066a42 mruby-pack: optimize float formats with union and lookup tables
- Replace nested endianness branching with lookup table approach
- Use union for safe float/double type punning
- Eliminate byte-by-byte loops in favor of direct indexing
- Consistent optimization patterns aligned with integer formats
- Achieve significant performance improvements: ~440K float ops/sec,
  ~249K double ops/sec

Co-authored-by: Claude <noreply@anthropic.com>
2025-08-14 10:53:12 +09:00
Yukihiro "Matz" Matsumoto ef5b39a5cc mruby-pack: optimize integer formats with lookup tables
- Eliminate branching in endianness handling using lookup tables
- Replace 8-iteration loop in unpack_quad with direct bit operations
- Fix endianness mapping for correct big/little-endian byte order
- Maintain consistent optimization patterns across all integer sizes
- Achieve significant performance improvements while preserving compatibility

Co-authored-by: Claude <noreply@anthropic.com>
2025-08-14 10:53:11 +09:00
Yukihiro "Matz" Matsumoto fdc0695944 mruby-pack: integer format optimization
optimize integer packing and unpacking algorithms:
- replace division/modulo with bit shifts in pack_short
- replace multiplication with bit shifts in unpack functions
- eliminate 8-iteration loop in unpack_quad with direct bit operations
- improve variable declarations following mruby patterns
- maintain full backward compatibility

performance improvements:
- short format packing: +21% (49k -> 59k ops/sec)
- long format packing: +43% (37k -> 53k ops/sec)
- consistent bit manipulation patterns across all integer sizes
- reduced branching and CPU-intensive operations

Co-authored-by: Claude <noreply@anthropic.com>
2025-08-14 10:53:11 +09:00
Yukihiro "Matz" Matsumoto a09c0f3b7d mruby-pack: optimize hex format encoding and decoding
- add lookup table for hex2int conversion to eliminate branches
- improve variable declarations following mruby patterns
- optimize pack_hex algorithm with better loop structure
- optimize unpack_hex algorithm with reduced conditionals
- improve buffer allocation precision
- maintain full backward compatibility

performance improvements:
- pack operations: ~224k ops/sec for 1600-char strings
- unpack operations: ~306k ops/sec for binary data
- eliminated function call overhead with lookup table
- reduced bounds checking and conditional branches

Co-authored-by: Claude <noreply@anthropic.com>
2025-08-14 10:53:11 +09:00
Yukihiro "Matz" Matsumoto 7a98bd3da0 mruby-pack: optimize BER decoding overflow checking
- calculate maximum safe bytes upfront to reduce checking frequency
- only check overflow when approaching byte limits or value limits
- maintain same overflow detection accuracy with better performance
- reduces per-iteration overhead for common BER decoding cases

Co-authored-by: Claude <noreply@anthropic.com>
2025-08-14 10:53:11 +09:00
Yukihiro "Matz" Matsumoto 9051c48431 mruby-pack: add fast paths for BER encoding optimization
- add fast path for 1-byte values (0-127): direct encoding
- add fast path for 2-byte values (128-16383): simple bit operations
- fallback to original algorithm for larger values (16384+)
- eliminates expensive bit mask calculation loop for ~95% of typical usage
- maintains full backward compatibility and correctness

Co-authored-by: Claude <noreply@anthropic.com>
2025-08-14 10:53:11 +09:00
Yukihiro "Matz" Matsumoto 42033352b9 mruby-pack: improve BER encoding variable declarations
- move variable declarations to initialization points in pack_BER
- move variable declarations to initialization points in unpack_BER
- improve code readability with better variable scoping
- maintain exact same algorithm and performance

Co-authored-by: Claude <noreply@anthropic.com>
2025-08-14 10:53:11 +09:00
Yukihiro "Matz" Matsumoto 32ad871f5d mruby-pack: document BER format ('w') template directive
- add 'w' directive to supported template table
- provide BER encoding/decoding usage example
- describe as variable length encoding (no endianness concept)

Co-authored-by: Claude <noreply@anthropic.com>
2025-08-14 10:53:11 +09:00
Yukihiro "Matz" Matsumoto 57d37fdfc1 mruby-pack: improve base64 decoding code organization
- move variable declarations to initialization points for cleaner code
- improve code readability with better variable scoping
- maintain exact same algorithm and performance characteristics

Co-authored-by: Claude <noreply@anthropic.com>
2025-08-14 10:53:10 +09:00
Yukihiro "Matz" Matsumoto 2639e9d8cd mruby-pack: optimize base64 encoding for memory efficiency
- add fast path for no line wrapping (count=0) to avoid column tracking
- use precise buffer size calculation to prevent reallocations
- move variable declarations to initialization points for cleaner code
- maintain full backward compatibility

Co-authored-by: Claude <noreply@anthropic.com>
2025-08-14 10:53:10 +09:00
Yukihiro "Matz" Matsumoto 64886fd965 mruby-random: use mrb_alloca in mrb_ary_sample to prevent memory leak
Refactor mrb_ary_sample to use mrb_alloca for the 'idx' array. This
ensures that the memory is automatically freed when the C function
returns, preventing a memory leak if an exception is raised during
array manipulation.

Co-authored-by: Gemini <gemini@google.com>
2025-08-14 10:53:10 +09:00
Yukihiro "Matz" Matsumoto b9090b089e random.c: unbiased rand(n), faster bytes, cheaper sample/shuffle
- Replace modulo with rejection sampling in rand_i() to remove modulo bias.
  This yields uniform integers in [0, max) and ensures Fisher–Yates
  shuffles are truly uniform.
- Speed up Random#bytes by writing 4 bytes per PRNG call (pack a uint32_t)
  and add a negative-size check (raise ArgumentError).
- Minor shuffle! tweak: hoist RARRAY_PTR/length out of the loop to avoid
  repeated lookups.
- Lower GC pressure in Array#sample(n): collect unique indices in a small
  C buffer, then push array elements directly, avoiding temporary Ruby
  integers.

Behavioral notes:
- rand(n) and methods depending on it now have unbiased distributions.
- Random#bytes(size) now explicitly rejects negative sizes.
- Other semantics remain unchanged.

Co-authored-by: OpenAI Coding Assistant <noreply@openai.com>
2025-08-14 10:53:10 +09:00
Yukihiro "Matz" Matsumoto 01226c8fc9 mruby-io: refactor io_s_popen for readability
This commit refactors the `io_s_popen` function to improve readability
and maintainability. The function has been broken down into smaller,
more manageable functions, and the platform-specific code has been
separated.

Co-authored-by: Gemini <gemini@google.com>
2025-08-14 10:53:10 +09:00
Yukihiro "Matz" Matsumoto f8ee815468 mruby-io: fix bug in fd_write
The previous implementation of fd_write had a bug that caused it to
repeatedly write the entire string instead of the remaining portion.
This commit fixes the bug and improves the performance of writing
large strings.

Co-authored-by: Gemini <gemini@google.com>
2025-08-14 10:53:10 +09:00
Yukihiro "Matz" Matsumoto 3246dd2562 mruby-fiber: optimize initial memory allocation sizes
Reduce FIBER_STACK_INIT_SIZE from 64 to 16 and FIBER_CI_INIT_SIZE
from 8 to 4 based on runtime analysis. Data shows typical usage
is 5-8 stack registers and 4 callinfo slots, achieving ~75% memory
reduction per fiber while preserving dynamic growth.

Co-authored-by: Claude <noreply@anthropic.com>
2025-08-14 10:53:09 +09:00
Yukihiro "Matz" Matsumoto 856373df35 fiber.c: cache status in fiber_to_s to avoid redundant lookups
Co-authored-by: Claude <noreply@anthropic.com>
2025-08-14 10:53:09 +09:00
Yukihiro "Matz" Matsumoto 28d94e336c fiber.c: optimize string concatenation in fiber_to_s for better performance
Co-authored-by: Claude <noreply@anthropic.com>
2025-08-14 10:53:09 +09:00
Yukihiro "Matz" Matsumoto f167ae8145 symbol.c: simplify literal check in sym_intern_common
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>
2025-08-14 10:53:09 +09:00
Yukihiro "Matz" Matsumoto 8c08103151 symbol.c: remove duplicate code in sym_intern functions
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>
2025-08-14 10:53:09 +09:00
Yukihiro "Matz" Matsumoto 852216b939 symbol.c: implement LSB tagging for literal flags to save memory
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>
2025-08-14 10:53:09 +09:00
Yukihiro "Matz" Matsumoto 47c4c1390b symbol.c: refactor complex macros to static inline functions
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>
2025-08-14 10:53:09 +09:00
Yukihiro "Matz" Matsumoto a217935e7d symbol.c: implement adaptive symbol table for memory efficiency
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>
2025-08-14 10:53:08 +09:00
Yukihiro "Matz" Matsumoto 3494b699ef time: implement nanosecond precision with zero memory overhead
Replace microsecond storage with nanosecond storage in struct mrb_time
while maintaining full backward compatibility and zero memory increase.

Changes:
- Replace 'usec' field with 'nsec' field in struct mrb_time
- Preserve full nanosecond precision from timespec_get/clock_gettime
- Add Time#nsec and Time#tv_nsec methods for Ruby spec compliance
- Update Time#usec to compute microseconds from nanoseconds
- Convert all arithmetic operations to handle nanosecond precision
- Add comprehensive tests for nanosecond functionality

Platform support:
- Modern systems: True nanosecond precision via timespec_get/clock_gettime
- Older systems: Microsecond precision converted to nanoseconds (gettimeofday)
- Minimal systems: Second precision with synthetic microseconds (time)

Benefits:
- Zero memory overhead (struct remains 80 bytes)
- 100% backward compatible (all existing tests pass)
- Better precision for time arithmetic and comparisons
- Ruby API compliant with standard nanosecond methods
- Automatic precision upgrade on capable systems

Co-Authored-By: Claude <noreply@anthropic.com>
2025-08-14 10:53:08 +09:00
Yukihiro "Matz" Matsumoto 16d3f31f82 time: add missing timezone offset methods and tests
implement gmt_offset, utc_offset, and gmtoff methods as aliases to
complete the ruby time api. all three methods return timezone offset
in seconds, with utc times returning 0 and local times returning the
appropriate offset value.

Co-authored-by: Claude <noreply@anthropic.com>
2025-08-14 10:53:08 +09:00
Yukihiro "Matz" Matsumoto a3efb96240 time.c: clean up Windows-specific conditional compilation
consolidate repeated Windows platform detection into single macro
MRB_TIME_WINDOWS_NO_STRFTIME_Z and simplify nested conditional blocks
in gettimeofday polyfill for better maintainability.

Co-authored-by: Claude <noreply@anthropic.com>
2025-08-14 10:53:08 +09:00
Yukihiro "Matz" Matsumoto ef6e148cb1 time.c: improve error handling consistency and clarity
standardize error messages and types across the codebase:
- use E_RANGE_ERROR consistently for time range violations
- consolidate "uninitialized time" errors with helper function
- clarify epoch-1 detection logic with better comments and structure
- unify "Time out of range" messaging

Co-authored-by: Claude <noreply@anthropic.com>
2025-08-14 10:53:08 +09:00
Yukihiro "Matz" Matsumoto 7858141fe6 time.c: remove duplicate code in day and weekday methods
consolidate time_day and time_mday into single implementation, and
create generic time_wday_p function for all weekday methods, reducing
code duplication and improving maintainability.

Co-authored-by: Claude <noreply@anthropic.com>
2025-08-14 10:53:08 +09:00
Yukihiro "Matz" Matsumoto 9c4e70ed01 time.c: optimize getutc and getlocal to skip unnecessary datetime updates
skip time_update_datetime() call when timezone conversion is not needed,
eliminating expensive gmtime_r/localtime_r system calls for redundant
conversions like time.utc.getutc or time.local.getlocal.

Co-authored-by: Claude <noreply@anthropic.com>
2025-08-14 10:53:07 +09:00
Yukihiro "Matz" Matsumoto 731926cd72 time.c: optimize string formatting and fix timezone calculation
optimize time_to_s() by using combined strftime format on platforms with
%z support, eliminating redundant function calls for local times.

fix timezone calculation in time_zonename() by copying actual date
components instead of using arbitrary year, ensuring accurate dst handling.

Co-authored-by: Claude <noreply@anthropic.com>
2025-08-14 10:53:07 +09:00
Yukihiro "Matz" Matsumoto 87b136ec19 AUTHORS: update entries [ci skip] 2025-08-14 10:53:07 +09:00
Yukihiro "Matz" Matsumoto 03478e6d2c mruby-array-ext: avoid heap allocation for temporary sets in array operations
The internal helper functions for array set operations now use a
stack-allocated `ary_set_t` instead of a heap-allocated one. This avoids
an unnecessary memory allocation for each call to `&`, `|`, `-`, `uniq!`,
and `intersect?`, improving performance by reducing overhead.

Co-authored-by: Gemini <gemini@google.com>
2025-08-14 10:53:07 +09:00
Yukihiro "Matz" Matsumoto 74f0fd91e9 khash: rename KHASH_SMALL_THRESHOLD to KHASH_SMALL_LIMIT
Rename KHASH_SMALL_THRESHOLD to KHASH_SMALL_LIMIT for brevity and clarity.
The shorter name is more concise while maintaining clear meaning as the
upper bound for small table optimization.

Co-authored-by: Claude <noreply@anthropic.com>
2025-08-14 10:53:07 +09:00
Yukihiro "Matz" Matsumoto 250bf6edd6 khash: rename KHASH_DEFAULT_SIZE to KHASH_INITIAL_SIZE
Rename KHASH_DEFAULT_SIZE to KHASH_INITIAL_SIZE for clearer meaning.
The name "initial" better conveys that this is the starting size for
new hash tables, while "default" could be ambiguous.

Co-authored-by: Claude <noreply@anthropic.com>
2025-08-14 10:53:07 +09:00
Yukihiro "Matz" Matsumoto 79fe70ffdc khash: move kh_alloc to internal helper
Move kh_alloc_##name from public API to internal helper kh__alloc_##name
since it's only used internally within khash implementation.

Changes:
- Remove kh_alloc_##name from KHASH_DECLARE
- Add kh__alloc_##name as static inline in KHASH_DEFINE
- Update internal calls to use kh__alloc_##name

Co-authored-by: Claude <noreply@anthropic.com>
2025-08-14 10:53:07 +09:00
Yukihiro "Matz" Matsumoto 0be8fdc030 khash: rename internal helpers to kh__ prefix and organize API boundary
Rename internal helper functions from kh_ to kh__ prefix while correctly
organizing the API boundary:

KHASH_DECLARE (public interface):
- kh_keys_##name, kh_vals_##name, kh_flags_##name (used by kh_exist macro)

KHASH_DEFINE (internal helpers with kh__ prefix):
- kh__kv_size_##name, kh__htable_size_##name
- kh__mark_occupied_##name, kh__mark_deleted_##name
- kh__key_idx_##name, kh__next_probe_##name
- kh__insert_key_##name, kh__clear_flags_##name
- kh__is_small_##name, kh__get_small_##name
- kh__rebuild_##name, kh__put_small_##name

This clearly separates public API functions from internal implementation
helpers while ensuring kh_flags_##name remains accessible to the public
kh_exist macro.

Co-authored-by: Claude <noreply@anthropic.com>
2025-08-14 10:53:06 +09:00
Yukihiro "Matz" Matsumoto f4e9bf603c khash: add helpers to eliminate key assignment and memset duplication
Add kh_insert_key and kh_clear_flags helper functions to remove remaining
code duplication patterns in KHASH_DEFINE. These helpers consolidate:

- Key assignment pattern: keys[index] = key; kh_mark_occupied; h->size++
- Flag clearing pattern: memset(kh_flags, 0xaa, n_buckets/4)

Co-authored-by: Claude <noreply@anthropic.com>
2025-08-14 10:53:06 +09:00
Yukihiro "Matz" Matsumoto 73111cacfb khash.h: add linear probing helper to eliminate duplication
Added kh_next_probe_##name() helper function to encapsulate the repeated
linear probing step calculation pattern.

Replaced 2 instances of manual probing calculation:
- k = (k+(++step)) & khash_mask(h) -> k = kh_next_probe_##name(k, &step, h)

This eliminates the duplicated bit manipulation pattern and makes the
probing logic more readable and less error-prone.

Co-authored-by: Claude <noreply@anthropic.com>
2025-08-14 10:53:06 +09:00
Yukihiro "Matz" Matsumoto f46b57e22e khash.h: add unified rebuild helper to eliminate major duplication
Added kh_rebuild_##name() helper function that consolidates the complete
"save-allocate-rehash-cleanup" pattern shared between kh_resize and
kh_put_small functions.

The helper intelligently handles both scenarios:
- Small table conversion: iterates by size
- Hash table resize: iterates by buckets with flag checks

This eliminates approximately 25 lines of duplicated code across the
two functions while maintaining identical functionality.

Co-authored-by: Claude <noreply@anthropic.com>
2025-08-14 10:53:06 +09:00
Yukihiro "Matz" Matsumoto da09bae640 khash.h: inline kh_alloc_small function used only once
Removed kh_alloc_small_##name() function and inlined its body into the
single call site in kh_init_data_##name(). This eliminates unnecessary
function call overhead and reduces code complexity.

The function was only 2 lines and called once, making it an ideal
candidate for inlining.

Co-authored-by: Claude <noreply@anthropic.com>
2025-08-14 10:53:06 +09:00
Yukihiro "Matz" Matsumoto 22a6debf40 khash: improve helper function naming and add htable size helper
Renamed size calculation helpers for clarity:
- kh_data_size_##name() -> kh_kv_size_##name() (keys and values only)
- Added kh_htable_size_##name() (complete hash table including flags)

Updated all usages and simplified patterns:
- kh_kv_size_##name(n) + n/4 -> kh_htable_size_##name(n)

The new names clearly distinguish between:
- kv_size: just the key-value data
- htable_size: complete hash table allocation (data + flags)

This eliminates confusion and makes the code more self-documenting.

Co-authored-by: Claude <noreply@anthropic.com>
2025-08-14 10:53:06 +09:00
Yukihiro "Matz" Matsumoto 7cfc863353 khash: add hash calculation helper to eliminate duplication
Added kh_key_idx_##name() helper function to encapsulate the repeated
pattern of calculating bucket index from key hash.

Replaced 2 instances of manual hash calculation:
- __hash_func(mrb,key) & khash_mask(h) → kh_key_idx_##name(mrb, key, h)

This eliminates the duplicated hash-and-mask pattern and makes the code
more readable by clearly expressing the intent (get bucket index for key).

Co-authored-by: Claude <noreply@anthropic.com>
2025-08-14 10:53:05 +09:00
Yukihiro "Matz" Matsumoto 633544e087 khash: add flag operation helpers to eliminate bit manipulation duplication
Added two helper functions to encapsulate repeated flag manipulation patterns:
- kh_mark_occupied_##name(): clears both empty and deleted bits
- kh_mark_deleted_##name(): sets the deleted bit

Replaced 3 instances of manual bit manipulation with calls to these helpers:
- ed_flags[del_k/4] &= ~__m_del[del_k%4] → kh_mark_occupied_##name(h, del_k)
- ed_flags[k/4] &= ~__m_empty[k%4] → kh_mark_occupied_##name(h, k)
- ed_flags[x/4] |= __m_del[x%4] → kh_mark_deleted_##name(h, x)

This eliminates error-prone bit operations, improves readability, and makes
the flag state transitions self-documenting.

Co-authored-by: Claude <noreply@anthropic.com>
2025-08-14 10:53:05 +09:00
Yukihiro "Matz" Matsumoto 71c5e2c536 khash: move internal helpers from KHASH_DECLARE to KHASH_DEFINE
Moved kh_data_size_##name() and kh_flags_##name() from KHASH_DECLARE
to KHASH_DEFINE section where internal implementation details belong.

This improves the separation of concerns:
- KHASH_DECLARE: public API only (struct definition, function declarations)
- KHASH_DEFINE: implementation details and internal helper functions

No functional changes, only better code organization.

Co-authored-by: Claude <noreply@anthropic.com>
2025-08-14 10:53:05 +09:00
Yukihiro "Matz" Matsumoto 8bcd0d247e khash: add size calculation helper and refactor for better organization
Added kh_data_size_##name() helper function to eliminate duplicated size
calculation patterns throughout the khash implementation. This single
universal helper calculates data size for N elements and replaces all
manual sizeof calculations.

Key changes:
- Add kh_data_size_##name(khint_t count) helper in KHASH_DECLARE
- Replace manual calculations in kh_flags, kh_alloc_small, kh_alloc
- Replace complex size calculations in kh_replace function
- Use specific patterns: small tables use KHASH_SMALL_THRESHOLD,
  hash tables add n_buckets/4 for flag space

This refactoring eliminates 6 instances of duplicated size calculation
code while maintaining identical functionality and performance.

Co-authored-by: Claude <noreply@anthropic.com>
2025-08-14 10:53:05 +09:00
Yukihiro "Matz" Matsumoto 7dc870c022 mruby-set: reduce KSET_INITIAL_SIZE to 4
This change optimizes memory consumption by reducing the initial size of
the set's internal hash table.

Co-authored-by: Gemini <gemini@google.com>
2025-08-14 10:53:05 +09:00
Yukihiro "Matz" Matsumoto 8a7c758bbd mruby-set: rename KSET_DEFAULT_SIZE to KSET_INITIAL_SIZE
Rename the constant to better reflect its semantic meaning as an initial
size hint for new Set allocations rather than a hard default value.

Co-authored-by: Claude <noreply@anthropic.com>
2025-08-14 10:53:05 +09:00