Improve inlinability of rb_define_class and rb_define_module

By moving the `rb_intern` to the caller, we allow compilers to
precompute the `strlen` part of `rb_intern` which isn't a huge win,
but happens quite a lot as part of Ruby's boot process and when
requiring native gems.
This commit is contained in:
Jean Boussier authored and Jean Boussier committed 2026-06-19 17:49:17 +02:00
1 parent 143f28bed5
commit cd7e59d45b
2 files changed
+20
Notes: git 2026-08-07 15:21:37 +00:00

No files matched your search

+12
View File
@@ -1402,12 +1402,18 @@ rb_class_inherited(VALUE super, VALUE klass)
return rb_funcall(super, inherited, 1, klass);
}
#ifdef rb_define_class
#undef rb_define_class
#endif
VALUE
rb_define_class(const char *name, VALUE super)
{
return rb_define_class_under(rb_cObject, name, super);
}
#ifdef rb_define_class_under
#undef rb_define_class_under
#endif
VALUE
rb_define_class_under(VALUE outer, const char *name, VALUE super)
{
@@ -1506,12 +1512,18 @@ rb_define_module_id(ID id)
return rb_module_new();
}
#ifdef rb_define_module
#undef rb_define_module
#endif
VALUE
rb_define_module(const char *name)
{
return rb_define_module_id_under(rb_cObject, rb_intern(name));
}
#ifdef rb_define_module_under
#undef rb_define_module_under
#endif
VALUE
rb_define_module_under(VALUE outer, const char *name)
{
+8
View File
@@ -66,6 +66,8 @@ RBIMPL_ATTR_NONNULL(())
*/
VALUE rb_define_class(const char *name, VALUE super);
#define rb_define_class(name, super) rb_define_class_id_under(rb_cObject, rb_intern_const((name)), (super))
RBIMPL_ATTR_NONNULL(())
/**
* Defines a top-level module.
@@ -85,6 +87,8 @@ RBIMPL_ATTR_NONNULL(())
*/
VALUE rb_define_module(const char *name);
#define rb_define_module(name) rb_define_module_id_under(rb_cObject, rb_intern_const((name)))
RBIMPL_ATTR_NONNULL(())
/**
* Defines a class under the namespace of `outer`.
@@ -108,6 +112,8 @@ RBIMPL_ATTR_NONNULL(())
*/
VALUE rb_define_class_under(VALUE outer, const char *name, VALUE super);
#define rb_define_class_under(outer, name, super) rb_define_class_id_under((outer), rb_intern_const((name)), (super))
RBIMPL_ATTR_NONNULL(())
/**
* Defines a module under the namespace of `outer`.
@@ -123,6 +129,8 @@ RBIMPL_ATTR_NONNULL(())
*/
VALUE rb_define_module_under(VALUE outer, const char *name);
#define rb_define_module_under(outer, name) rb_define_module_id_under((outer), rb_intern_const((name)))
/**
* Includes a module to a class.
*