mirror of
https://github.com/ruby/ruby.git
synced 2022-11-09 12:17:21 -05:00
d594a5a8bd
Object Shapes is used for accessing instance variables and representing the "frozenness" of objects. Object instances have a "shape" and the shape represents some attributes of the object (currently which instance variables are set and the "frozenness"). Shapes form a tree data structure, and when a new instance variable is set on an object, that object "transitions" to a new shape in the shape tree. Each shape has an ID that is used for caching. The shape structure is independent of class, so objects of different types can have the same shape. For example: ```ruby class Foo def initialize # Starts with shape id 0 @a = 1 # transitions to shape id 1 @b = 1 # transitions to shape id 2 end end class Bar def initialize # Starts with shape id 0 @a = 1 # transitions to shape id 1 @b = 1 # transitions to shape id 2 end end foo = Foo.new # `foo` has shape id 2 bar = Bar.new # `bar` has shape id 2 ``` Both `foo` and `bar` instances have the same shape because they both set instance variables of the same name in the same order. This technique can help to improve inline cache hits as well as generate more efficient machine code in JIT compilers. This commit also adds some methods for debugging shapes on objects. See `RubyVM::Shape` for more details. For more context on Object Shapes, see [Feature: #18776] Co-Authored-By: Aaron Patterson <tenderlove@ruby-lang.org> Co-Authored-By: Eileen M. Uchitelle <eileencodes@gmail.com> Co-Authored-By: John Hawthorn <john@hawthorn.email>
110 lines
3 KiB
C
110 lines
3 KiB
C
#ifndef RUBY_INTERNAL_H /*-*-C-*-vi:se ft=c:*/
|
|
#define RUBY_INTERNAL_H 1
|
|
/**
|
|
* @author $Author$
|
|
* @date Tue May 17 11:42:20 JST 2011
|
|
* @copyright Copyright (C) 2011 Yukihiro Matsumoto
|
|
* @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.
|
|
*/
|
|
#include "ruby/internal/config.h"
|
|
|
|
#ifdef __cplusplus
|
|
# error not for C++
|
|
#endif
|
|
|
|
#define LIKELY(x) RB_LIKELY(x)
|
|
#define UNLIKELY(x) RB_UNLIKELY(x)
|
|
|
|
#define numberof(array) ((int)(sizeof(array) / sizeof((array)[0])))
|
|
#define roomof(x, y) (((x) + (y) - 1) / (y))
|
|
#define type_roomof(x, y) roomof(sizeof(x), sizeof(y))
|
|
|
|
/* Prevent compiler from reordering access */
|
|
#define ACCESS_ONCE(type,x) (*((volatile type *)&(x)))
|
|
|
|
#include "ruby/ruby.h"
|
|
|
|
/* Following macros were formerly defined in this header but moved to somewhere
|
|
* else. In order to detect them we undef here. */
|
|
|
|
/* internal/array.h */
|
|
#undef RARRAY_AREF
|
|
|
|
/* internal/class.h */
|
|
#undef RClass
|
|
#undef RCLASS_SUPER
|
|
|
|
/* internal/gc.h */
|
|
#undef NEWOBJ_OF
|
|
#undef RB_NEWOBJ_OF
|
|
#undef RB_OBJ_WRITE
|
|
|
|
/* internal/hash.h */
|
|
#undef RHASH_IFNONE
|
|
#undef RHASH_SIZE
|
|
#undef RHASH_TBL
|
|
#undef RHASH_EMPTY_P
|
|
|
|
/* internal/struct.h */
|
|
#undef RSTRUCT_LEN
|
|
#undef RSTRUCT_PTR
|
|
#undef RSTRUCT_SET
|
|
#undef RSTRUCT_GET
|
|
|
|
/* Also, we keep the following macros here. They are expected to be
|
|
* overridden in each headers. */
|
|
|
|
/* internal/array.h */
|
|
#define rb_ary_new_from_args(...) rb_nonexistent_symbol(__VA_ARGS__)
|
|
|
|
/* internal/io.h */
|
|
#define rb_io_fptr_finalize(...) rb_nonexistent_symbol(__VA_ARGS__)
|
|
|
|
/* internal/string.h */
|
|
#define rb_fstring_cstr(...) rb_nonexistent_symbol(__VA_ARGS__)
|
|
|
|
/* internal/symbol.h */
|
|
#define rb_sym_intern_ascii_cstr(...) rb_nonexistent_symbol(__VA_ARGS__)
|
|
|
|
/* internal/vm.h */
|
|
#define rb_funcallv(...) rb_nonexistent_symbol(__VA_ARGS__)
|
|
#define rb_method_basic_definition_p(...) rb_nonexistent_symbol(__VA_ARGS__)
|
|
|
|
|
|
/* MRI debug support */
|
|
|
|
/* gc.c */
|
|
void rb_obj_info_dump(VALUE obj);
|
|
void rb_obj_info_dump_loc(VALUE obj, const char *file, int line, const char *func);
|
|
|
|
/* debug.c */
|
|
|
|
RUBY_SYMBOL_EXPORT_BEGIN
|
|
void ruby_debug_breakpoint(void);
|
|
PRINTF_ARGS(void ruby_debug_printf(const char*, ...), 1, 2);
|
|
RUBY_SYMBOL_EXPORT_END
|
|
|
|
// show obj data structure without any side-effect
|
|
#define rp(obj) rb_obj_info_dump_loc((VALUE)(obj), __FILE__, __LINE__, RUBY_FUNCTION_NAME_STRING)
|
|
|
|
// same as rp, but add message header
|
|
#define rp_m(msg, obj) do { \
|
|
fputs((msg), stderr); \
|
|
rb_obj_info_dump((VALUE)(obj)); \
|
|
} while (0)
|
|
|
|
// `ruby_debug_breakpoint()` does nothing,
|
|
// but breakpoint is set in run.gdb, so `make gdb` can stop here.
|
|
#define bp() ruby_debug_breakpoint()
|
|
|
|
#define RBOOL(v) ((v) ? Qtrue : Qfalse)
|
|
#define RB_BIGNUM_TYPE_P(x) RB_TYPE_P((x), T_BIGNUM)
|
|
|
|
#ifndef __MINGW32__
|
|
#undef memcpy
|
|
#define memcpy ruby_nonempty_memcpy
|
|
#endif
|
|
#endif /* RUBY_INTERNAL_H */
|