ruby/internal/object.h
Jean Boussier b0b1dbe04b Allow inlining of rb_equal and rb_eql fast paths
|                             |compare-ruby|built-ruby|
|:----------------------------|-----------:|---------:|
|include_int_miss_100000      |      3.428k|    6.290k|
|                             |           -|     1.83x|
|include_sym_miss_100000      |      2.696k|    6.290k|
|                             |           -|     2.33x|
|include_float_miss_100000    |      2.898k|    6.104k|
|                             |           -|     2.11x|
|include_int_in_mixed_100000  |     511.502|   599.835|
|                             |           -|     1.17x|

Further allow `rb_eql` inlining
2026-10-30 14:45:20 +01:00

152 lines
4.6 KiB
C

#ifndef INTERNAL_OBJECT_H /*-*-C-*-vi:se ft=c:*/
#define INTERNAL_OBJECT_H
/**
* @author Ruby developers <ruby-core@ruby-lang.org>
* @copyright This file is a part of the programming language Ruby.
* Permission is hereby granted, to either redistribute and/or
* modify this file, provided that the conditions mentioned in the
* file COPYING are met. Consult the file for details.
* @brief Internal header for Object.
*/
#include "internal/basic_operators.h"
#include "internal/string.h"
#include "internal/vm.h"
#include "ruby/ruby.h" /* for VALUE */
/* object.c */
VALUE rb_class_allocate_instance(VALUE klass);
VALUE rb_class_allocate_instance_capa(VALUE klass, uint8_t /* attr_index_t */ max_iv_count);
VALUE rb_class_search_ancestor(VALUE klass, VALUE super);
NORETURN(void rb_undefined_alloc(VALUE klass));
double rb_num_to_dbl(VALUE val);
VALUE rb_obj_dig(int argc, VALUE *argv, VALUE self, VALUE notfound);
VALUE rb_obj_clone_setup(VALUE obj, VALUE clone, VALUE kwfreeze);
VALUE rb_obj_dup_setup(VALUE obj, VALUE dup);
VALUE rb_immutable_obj_clone(int, VALUE *, VALUE);
VALUE rb_check_convert_type_with_id_slow(VALUE,int,const char*,ID);
int rb_bool_expected(VALUE, const char *, int raise);
static inline VALUE
rb_check_convert_type_with_id(VALUE val, int type, const char *tname, ID method)
{
if (RB_TYPE_P(val, type) && type != T_DATA) return val;
return rb_check_convert_type_with_id_slow(val, type, tname, method);
}
static inline void RBASIC_CLEAR_CLASS(VALUE obj);
static inline void RBASIC_SET_CLASS_RAW(VALUE obj, VALUE klass);
static inline void RBASIC_SET_CLASS(VALUE obj, VALUE klass);
RUBY_SYMBOL_EXPORT_BEGIN
/* object.c (export) */
int rb_opts_exception_p(VALUE opts, int default_value);
RUBY_SYMBOL_EXPORT_END
CONSTFUNC(VALUE rb_obj_equal(VALUE obj1, VALUE obj2));
CONSTFUNC(VALUE rb_obj_not(VALUE obj));
VALUE rb_obj_not_equal(VALUE obj1, VALUE obj2);
void rb_obj_copy_ivar(VALUE dest, VALUE obj);
VALUE rb_false(VALUE obj);
VALUE rb_convert_type_with_id(VALUE v, int t, const char* nam, ID mid);
VALUE rb_obj_size(VALUE self, VALUE args, VALUE obj);
VALUE rb_get_freeze_opt(int argc, VALUE *argv);
static inline void
RBASIC_SET_CLASS_RAW(VALUE obj, VALUE klass)
{
const VALUE *ptr = &RBASIC(obj)->klass;
*(VALUE *)ptr = klass;
}
static inline void
RBASIC_CLEAR_CLASS(VALUE obj)
{
RBASIC_SET_CLASS_RAW(obj, 0);
}
static inline void
RBASIC_SET_CLASS(VALUE obj, VALUE klass)
{
VALUE oldv = RBASIC_CLASS(obj);
RBASIC_SET_CLASS_RAW(obj, klass);
RB_OBJ_WRITTEN(obj, oldv, klass);
}
static inline void
ROBJECT_SET_EXTENDED(VALUE obj, VALUE fields_obj)
{
RUBY_ASSERT(RB_TYPE_P(obj, T_OBJECT));
RUBY_ASSERT(RB_TYPE_P(fields_obj, T_IMEMO));
RB_OBJ_WRITE(obj, &ROBJECT(obj)->as.extended, fields_obj);
}
static inline size_t
rb_obj_embedded_size(uint32_t fields_count)
{
size_t size = offsetof(struct RObject, as.ary) + (sizeof(VALUE) * fields_count);
// Ensure enough room for the heap pointer if this expands
if (size < sizeof(struct RObject)) size = sizeof(struct RObject);
return size;
}
static inline VALUE
opt_equality_specialized_miss(VALUE recv, VALUE obj)
{
RBIMPL_ASSERT_OR_ASSUME(recv != obj);
if (BASIC_OP_UNREDEFINED_P(BOP_EQ, ANY_REDEFINED_OP_FLAG)) {
if (recv == Qfalse) return Qfalse;
if (IMMEDIATE_P(recv)) {
// Numeric immediates can be equal to different VALUE via coercion.
if (FIXNUM_P(recv)) {
if (!SPECIAL_CONST_P(obj) || FLONUM_P(obj)) return Qundef;
}
else if (FLONUM_P(recv)) {
if (!SPECIAL_CONST_P(obj) || FIXNUM_P(obj)) return Qundef;
}
return Qfalse;
}
VALUE klass = RBASIC_CLASS(recv);
if (klass == rb_cString) {
if (RB_TYPE_P(obj, T_STRING)) {
return rb_str_eql_internal(recv, obj);
}
}
else if (klass == rb_cSymbol) {
return Qfalse;
}
}
return Qundef;
}
static inline VALUE
rb_equal_inline(VALUE obj1, VALUE obj2)
{
if (obj1 == obj2) return Qtrue;
VALUE result = opt_equality_specialized_miss(obj1, obj2);
if (UNDEF_P(result)) {
result = rb_equal_opt(obj1, obj2);
}
return result;
}
#define rb_equal rb_equal_inline
static inline int
rb_eql_inline(VALUE obj1, VALUE obj2)
{
if (obj1 == obj2) return TRUE;
VALUE result = opt_equality_specialized_miss(obj1, obj2);
if (UNDEF_P(result)) {
result = rb_eql_opt(obj1, obj2);
}
return RTEST(result);
}
#define rb_eql rb_eql_inline
#endif /* INTERNAL_OBJECT_H */