Implementation includes optimized lookup tables for encoding/decoding,
comprehensive test coverage, and integration with existing pack/unpack
dispatch.
Co-authored-by: Claude <noreply@anthropic.com>
Reorganize switch statement cases in pack and unpack functions by grouping
formats with similar function signatures together. This improves branch
prediction and CPU pipeline efficiency by reducing branch misprediction
overhead in the hot dispatch paths.
Key improvements:
- Pack dispatch: grouped by signature patterns (integer, float, string)
- Unpack dispatch: optimized both COUNT2 and element-by-element switches
- Better instruction cache usage through logical code organization
- Enhanced branch prediction for frequently used format combinations
- Maintained full backward compatibility with all existing functionality
Co-authored-by: Claude <noreply@anthropic.com>
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>
- 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>
- 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>
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>
- 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>
- 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>
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>
- 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>
- 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>
- 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>
- 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>
- 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>
- 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>
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>
- 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>
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>
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>
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>
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>
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>
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>
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>
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>
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>
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>
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>
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>
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>
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>
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>
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>
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>
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>
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>
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>
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>
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>
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>
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>
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>
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>