vm.c: optimize OP_GETIDX with branch hints and reduced checks

- Add mrb_likely/mrb_unlikely macros to common.h for branch prediction
- Optimize OP_GETIDX array fast path:
  - Cache RArray pointer to avoid repeated RARRAY() calls
  - Single ARY_EMBED_P check instead of two (via RARRAY_LEN + RARRAY_PTR)
  - Use unsigned comparison for bounds check
  - Add branch prediction hints for common cases
- Convert switch statement to if-else chain for better branch prediction

Benchmark shows ~3% improvement for array read operations.

Co-authored-by: Claude <noreply@anthropic.com>
This commit is contained in:
Yukihiro "Matz" Matsumoto
2026-01-17 23:12:00 +09:00
parent ed84649fbd
commit a07d9fb62c
2 changed files with 44 additions and 19 deletions
+9
View File
@@ -59,6 +59,15 @@ MRB_BEGIN_DECL
# define mrb_deprecated
#endif
/** Branch prediction hints for optimization. */
#if defined(__GNUC__) || defined(__clang__)
# define mrb_likely(x) __builtin_expect(!!(x), 1)
# define mrb_unlikely(x) __builtin_expect(!!(x), 0)
#else
# define mrb_likely(x) (x)
# define mrb_unlikely(x) (x)
#endif
/** Declare a type or object as an alignment requirement. */
#ifndef mrb_alignas
# if defined(__cplusplus) && __cplusplus >= 201103L