Skip to content

Commit f483bef

Browse files
jhawthornbyroot
andcommitted
Add shape_id to RBasic under 32 bit
This makes `RBobject` `4B` larger on 32 bit systems but simplifies the implementation a lot. [Feature #21353] Co-authored-by: Jean Boussier <[email protected]>
1 parent 2295384 commit f483bef

File tree

16 files changed

+54
-203
lines changed

16 files changed

+54
-203
lines changed

class.c

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -42,21 +42,13 @@
4242
* 2: RCLASS_PRIME_CLASSEXT_PRIME_WRITABLE
4343
* This class's prime classext is the only classext and writable from any namespaces.
4444
* If unset, the prime classext is writable only from the root namespace.
45-
* if !SHAPE_IN_BASIC_FLAGS
46-
* 4-19: SHAPE_FLAG_MASK
47-
* Shape ID for the class.
48-
* endif
4945
*/
5046

5147
/* Flags of T_ICLASS
5248
*
5349
* 2: RCLASS_PRIME_CLASSEXT_PRIME_WRITABLE
5450
* This module's prime classext is the only classext and writable from any namespaces.
5551
* If unset, the prime classext is writable only from the root namespace.
56-
* if !SHAPE_IN_BASIC_FLAGS
57-
* 4-19: SHAPE_FLAG_MASK
58-
* Shape ID. This is set but not used.
59-
* endif
6052
*/
6153

6254
/* Flags of T_MODULE
@@ -71,10 +63,6 @@
7163
* If unset, the prime classext is writable only from the root namespace.
7264
* 3: RMODULE_IS_REFINEMENT
7365
* Module is used for refinements.
74-
* if !SHAPE_IN_BASIC_FLAGS
75-
* 4-19: SHAPE_FLAG_MASK
76-
* Shape ID for the module.
77-
* endif
7866
*/
7967

8068
#define METACLASS_OF(k) RBASIC(k)->klass

gc.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1965,6 +1965,7 @@ build_id2ref_i(VALUE obj, void *data)
19651965
}
19661966
break;
19671967
case T_IMEMO:
1968+
case T_NONE:
19681969
break;
19691970
default:
19701971
if (rb_shape_obj_has_id(obj)) {

gc/default/default.c

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
#include "ruby/util.h"
2929
#include "ruby/vm.h"
3030
#include "ruby/internal/encoding/string.h"
31+
#include "internal/object.h"
3132
#include "ccan/list/list.h"
3233
#include "darray.h"
3334
#include "gc/gc.h"
@@ -2123,6 +2124,9 @@ newobj_init(VALUE klass, VALUE flags, int wb_protected, rb_objspace_t *objspace,
21232124
GC_ASSERT((flags & FL_WB_PROTECTED) == 0);
21242125
RBASIC(obj)->flags = flags;
21252126
*((VALUE *)&RBASIC(obj)->klass) = klass;
2127+
#if RBASIC_SHAPE_ID_FIELD
2128+
RBASIC(obj)->shape_id = 0;
2129+
#endif
21262130

21272131
int t = flags & RUBY_T_MASK;
21282132
if (t == T_CLASS || t == T_MODULE || t == T_ICLASS) {
@@ -2968,7 +2972,7 @@ rb_gc_impl_shutdown_free_objects(void *objspace_ptr)
29682972
if (RB_BUILTIN_TYPE(vp) != T_NONE) {
29692973
rb_gc_obj_free_vm_weak_references(vp);
29702974
if (rb_gc_obj_free(objspace, vp)) {
2971-
RBASIC(vp)->flags = 0;
2975+
RBASIC_RESET_FLAGS(vp);
29722976
}
29732977
}
29742978
}
@@ -3042,7 +3046,7 @@ rb_gc_impl_shutdown_call_finalizer(void *objspace_ptr)
30423046
if (rb_gc_shutdown_call_finalizer_p(vp)) {
30433047
rb_gc_obj_free_vm_weak_references(vp);
30443048
if (rb_gc_obj_free(objspace, vp)) {
3045-
RBASIC(vp)->flags = 0;
3049+
RBASIC_RESET_FLAGS(vp);
30463050
}
30473051
}
30483052
}
@@ -9361,6 +9365,7 @@ rb_gc_impl_init(void)
93619365
VALUE gc_constants = rb_hash_new();
93629366
rb_hash_aset(gc_constants, ID2SYM(rb_intern("DEBUG")), GC_DEBUG ? Qtrue : Qfalse);
93639367
rb_hash_aset(gc_constants, ID2SYM(rb_intern("BASE_SLOT_SIZE")), SIZET2NUM(BASE_SLOT_SIZE - RVALUE_OVERHEAD));
9368+
rb_hash_aset(gc_constants, ID2SYM(rb_intern("RBASIC_SIZE")), SIZET2NUM(sizeof(struct RBasic)));
93649369
rb_hash_aset(gc_constants, ID2SYM(rb_intern("RVALUE_OVERHEAD")), SIZET2NUM(RVALUE_OVERHEAD));
93659370
rb_hash_aset(gc_constants, ID2SYM(rb_intern("HEAP_PAGE_OBJ_LIMIT")), SIZET2NUM(HEAP_PAGE_OBJ_LIMIT));
93669371
rb_hash_aset(gc_constants, ID2SYM(rb_intern("HEAP_PAGE_BITMAP_SIZE")), SIZET2NUM(HEAP_PAGE_BITMAP_SIZE));

gc/mmtk/mmtk.c

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
#include "ruby/assert.h"
55
#include "ruby/atomic.h"
66
#include "ruby/debug.h"
7+
#include "internal/object.h"
78

89
#include "gc/gc.h"
910
#include "gc/gc_impl.h"
@@ -453,6 +454,7 @@ rb_gc_impl_init(void)
453454
{
454455
VALUE gc_constants = rb_hash_new();
455456
rb_hash_aset(gc_constants, ID2SYM(rb_intern("BASE_SLOT_SIZE")), SIZET2NUM(sizeof(VALUE) * 5));
457+
rb_hash_aset(gc_constants, ID2SYM(rb_intern("RBASIC_SIZE")), SIZET2NUM(sizeof(struct RBasic)));
456458
rb_hash_aset(gc_constants, ID2SYM(rb_intern("RVALUE_OVERHEAD")), INT2NUM(0));
457459
rb_hash_aset(gc_constants, ID2SYM(rb_intern("RVARGC_MAX_ALLOCATE_SIZE")), LONG2FIX(640));
458460
// Pretend we have 5 size pools
@@ -1019,7 +1021,7 @@ rb_gc_impl_shutdown_call_finalizer(void *objspace_ptr)
10191021

10201022
if (rb_gc_shutdown_call_finalizer_p(obj)) {
10211023
rb_gc_obj_free(objspace_ptr, obj);
1022-
RBASIC(obj)->flags = 0;
1024+
RBASIC_RESET_FLAGS(obj);
10231025
}
10241026
}
10251027
mmtk_free_raw_vec_of_obj_ref(registered_candidates);

include/ruby/internal/core/rbasic.h

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,12 @@ enum ruby_rvalue_flags {
5555
RVALUE_EMBED_LEN_MAX = RBIMPL_RVALUE_EMBED_LEN_MAX
5656
};
5757

58+
#if (SIZEOF_VALUE < SIZEOF_UINT64_T)
59+
#define RBASIC_SHAPE_ID_FIELD 1
60+
#else
61+
#define RBASIC_SHAPE_ID_FIELD 0
62+
#endif
63+
5864
/**
5965
* Ruby object's base components. All Ruby objects have them in common.
6066
*/
@@ -85,6 +91,10 @@ RBasic {
8591
*/
8692
const VALUE klass;
8793

94+
#if RBASIC_SHAPE_ID_FIELD
95+
VALUE shape_id;
96+
#endif
97+
8898
#ifdef __cplusplus
8999
public:
90100
RBIMPL_ATTR_CONSTEXPR(CXX11)
@@ -100,6 +110,9 @@ RBasic {
100110
RBasic() :
101111
flags(RBIMPL_VALUE_NULL),
102112
klass(RBIMPL_VALUE_NULL)
113+
#if RBASIC_SHAPE_ID_FIELD
114+
, shape_id(RBIMPL_VALUE_NULL)
115+
#endif
103116
{
104117
}
105118
#endif

internal/class.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -577,7 +577,7 @@ RCLASS_FIELDS_COUNT(VALUE obj)
577577
return count;
578578
}
579579
else {
580-
return RSHAPE(RCLASS_SHAPE_ID(obj))->next_field_index;
580+
return RSHAPE(RBASIC_SHAPE_ID(obj))->next_field_index;
581581
}
582582
}
583583

internal/object.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,4 +60,13 @@ RBASIC_SET_CLASS(VALUE obj, VALUE klass)
6060
RBASIC_SET_CLASS_RAW(obj, klass);
6161
RB_OBJ_WRITTEN(obj, oldv, klass);
6262
}
63+
64+
static inline void
65+
RBASIC_RESET_FLAGS(VALUE obj)
66+
{
67+
RBASIC(obj)->flags = 0;
68+
#if RBASIC_SHAPE_ID_FIELD
69+
RBASIC(obj)->shape_id = 0;
70+
#endif
71+
}
6372
#endif /* INTERNAL_OBJECT_H */

object.c

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -50,10 +50,6 @@
5050
* The object has its instance variables embedded (the array of
5151
* instance variables directly follow the object, rather than being
5252
* on a separately allocated buffer).
53-
* if !SHAPE_IN_BASIC_FLAGS
54-
* 4-19: SHAPE_FLAG_MASK
55-
* Shape ID for the object.
56-
* endif
5753
*/
5854

5955
/*!
@@ -134,8 +130,7 @@ rb_class_allocate_instance(VALUE klass)
134130

135131
RUBY_ASSERT(rb_obj_shape(obj)->type == SHAPE_ROOT);
136132

137-
// Set the shape to the specific T_OBJECT shape.
138-
ROBJECT_SET_SHAPE_ID(obj, rb_shape_root(rb_gc_heap_id_for_size(size)));
133+
RBASIC_SET_SHAPE_ID(obj, rb_shape_root(rb_gc_heap_id_for_size(size)));
139134

140135
#if RUBY_DEBUG
141136
RUBY_ASSERT(!rb_shape_obj_too_complex_p(obj));

ractor.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3667,10 +3667,10 @@ move_leave(VALUE obj, struct obj_traverse_replace_data *data)
36673667
rb_replace_generic_ivar(data->replacement, obj);
36683668
}
36693669

3670-
VALUE flags = T_OBJECT | FL_FREEZE | (RBASIC(obj)->flags & FL_PROMOTED);
3670+
VALUE flags = T_OBJECT | FL_FREEZE | ROBJECT_EMBED | (RBASIC(obj)->flags & FL_PROMOTED);
36713671

36723672
// Avoid mutations using bind_call, etc.
3673-
MEMZERO((char *)obj + sizeof(struct RBasic), char, size - sizeof(struct RBasic));
3673+
MEMZERO((char *)obj, char, sizeof(struct RBasic));
36743674
RBASIC(obj)->flags = flags;
36753675
RBASIC_SET_CLASS_RAW(obj, rb_cRactorMovedObject);
36763676
return traverse_cont;

shape.c

Lines changed: 0 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -347,31 +347,14 @@ rb_shape_lookup(shape_id_t shape_id)
347347
return &GET_SHAPE_TREE()->shape_list[shape_id];
348348
}
349349

350-
#if !SHAPE_IN_BASIC_FLAGS
351-
shape_id_t rb_generic_shape_id(VALUE obj);
352-
#endif
353-
354350
RUBY_FUNC_EXPORTED shape_id_t
355351
rb_obj_shape_id(VALUE obj)
356352
{
357353
if (RB_SPECIAL_CONST_P(obj)) {
358354
return SPECIAL_CONST_SHAPE_ID;
359355
}
360356

361-
#if SHAPE_IN_BASIC_FLAGS
362357
return RBASIC_SHAPE_ID(obj);
363-
#else
364-
switch (BUILTIN_TYPE(obj)) {
365-
case T_OBJECT:
366-
return ROBJECT_SHAPE_ID(obj);
367-
break;
368-
case T_CLASS:
369-
case T_MODULE:
370-
return RCLASS_SHAPE_ID(obj);
371-
default:
372-
return rb_generic_shape_id(obj);
373-
}
374-
#endif
375358
}
376359

377360
size_t

0 commit comments

Comments
 (0)