mirror of
https://github.com/mruby/mruby
synced 2026-06-08 16:11:16 +00:00
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:
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user