@@ -799,35 +799,38 @@ shape_get_next(rb_shape_t *shape, VALUE obj, ID id, bool emit_warnings)
799799 }
800800#endif
801801
802- bool allow_new_shape = true;
803-
804- if (BUILTIN_TYPE (obj ) == T_OBJECT ) {
805- VALUE klass = rb_obj_class (obj );
806- allow_new_shape = RCLASS_VARIATION_COUNT (klass ) < SHAPE_MAX_VARIATIONS ;
802+ VALUE klass ;
803+ switch (BUILTIN_TYPE (obj )) {
804+ case T_CLASS :
805+ case T_MODULE :
806+ klass = rb_singleton_class (obj );
807+ break ;
808+ default :
809+ klass = rb_obj_class (obj );
810+ break ;
807811 }
808812
813+ bool allow_new_shape = RCLASS_VARIATION_COUNT (klass ) < SHAPE_MAX_VARIATIONS ;
809814 bool variation_created = false;
810815 rb_shape_t * new_shape = get_next_shape_internal (shape , id , SHAPE_IVAR , & variation_created , allow_new_shape );
811816
812817 // Check if we should update max_iv_count on the object's class
813- if (BUILTIN_TYPE (obj ) == T_OBJECT ) {
814- VALUE klass = rb_obj_class (obj );
815- if (new_shape -> next_field_index > RCLASS_MAX_IV_COUNT (klass )) {
816- RCLASS_SET_MAX_IV_COUNT (klass , new_shape -> next_field_index );
817- }
818-
819- if (variation_created ) {
820- RCLASS_VARIATION_COUNT (klass )++ ;
821- if (emit_warnings && rb_warning_category_enabled_p (RB_WARN_CATEGORY_PERFORMANCE )) {
822- if (RCLASS_VARIATION_COUNT (klass ) >= SHAPE_MAX_VARIATIONS ) {
823- rb_category_warn (
824- RB_WARN_CATEGORY_PERFORMANCE ,
825- "The class %" PRIsVALUE " reached %d shape variations, instance variables accesses will be slower and memory usage increased.\n"
826- "It is recommended to define instance variables in a consistent order, for instance by eagerly defining them all in the #initialize method." ,
827- rb_class_path (klass ),
828- SHAPE_MAX_VARIATIONS
829- );
830- }
818+ if (obj != klass && new_shape -> next_field_index > RCLASS_MAX_IV_COUNT (klass )) {
819+ RCLASS_SET_MAX_IV_COUNT (klass , new_shape -> next_field_index );
820+ }
821+
822+ if (variation_created ) {
823+ RCLASS_VARIATION_COUNT (klass )++ ;
824+
825+ if (emit_warnings && rb_warning_category_enabled_p (RB_WARN_CATEGORY_PERFORMANCE )) {
826+ if (RCLASS_VARIATION_COUNT (klass ) >= SHAPE_MAX_VARIATIONS ) {
827+ rb_category_warn (
828+ RB_WARN_CATEGORY_PERFORMANCE ,
829+ "The class %" PRIsVALUE " reached %d shape variations, instance variables accesses will be slower and memory usage increased.\n"
830+ "It is recommended to define instance variables in a consistent order, for instance by eagerly defining them all in the #initialize method." ,
831+ rb_class_path (klass ),
832+ SHAPE_MAX_VARIATIONS
833+ );
831834 }
832835 }
833836 }
0 commit comments