Skip to content

Commit

Permalink
Make rb_check_frozen_inline() static inline again
Browse files Browse the repository at this point in the history
Since 730e3b2
("Stop exposing `rb_str_chilled_p`"), we noticed a speed loss on a few
benchmarks that are string operations-heavy. This is partially due to
routines no longer having the options to inline rb_check_frozen_inline()
in non-LTO builds. Make it an inlining candidate again to recover speed.

On the fannkuchredux benchmark, this patch gives a 1.15 speed-up with
YJIT and 1.03 without YJIT.
  • Loading branch information
XrXr committed Jul 19, 2024
1 parent e801fa5 commit d0aa2a2
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 15 deletions.
13 changes: 0 additions & 13 deletions error.c
Original file line number Diff line number Diff line change
Expand Up @@ -3997,19 +3997,6 @@ rb_error_frozen_object(VALUE frozen_obj)
rb_exc_raise(exc);
}

#undef rb_check_frozen
void
rb_check_frozen(VALUE obj)
{
if (RB_UNLIKELY(RB_OBJ_FROZEN(obj))) {
rb_error_frozen_object(obj);
}

if (RB_UNLIKELY(CHILLED_STRING_P(obj))) {
rb_str_modify(obj);
}
}

void
rb_check_copyable(VALUE obj, VALUE orig)
{
Expand Down
19 changes: 17 additions & 2 deletions include/ruby/internal/intern/error.h
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,7 @@ void rb_error_frozen_object(VALUE what);
* @exception rb_eFrozenError It is frozen.
* @post Upon successful return it is guaranteed _not_ frozen.
*/
void rb_check_frozen(VALUE obj);
#define rb_check_frozen rb_check_frozen_inline

/**
* Ensures that the passed object can be `initialize_copy` relationship. When
Expand Down Expand Up @@ -237,6 +237,9 @@ RBIMPL_ATTR_NORETURN()
*/
void rb_error_arity(int argc, int min, int max);

bool rb_str_chilled_p(VALUE str);
void rb_str_modify(VALUE str);

RBIMPL_SYMBOL_EXPORT_END()

/**
Expand All @@ -247,7 +250,19 @@ RBIMPL_SYMBOL_EXPORT_END()
#define rb_check_frozen_internal rb_check_frozen

/** @alias{rb_check_frozen} */
#define rb_check_frozen_inline rb_check_frozen
static inline void
rb_check_frozen_inline(VALUE obj)
{
if (RB_UNLIKELY(RB_OBJ_FROZEN(obj))) {
rb_error_frozen_object(obj);
}

// ref: internal CHILLED_STRING_P()
// This is implementation details subject to change.
if (RB_UNLIKELY(RB_TYPE_P(obj, T_STRING) && FL_TEST_RAW(obj, RUBY_FL_USER3))) {
rb_str_modify(obj);
}
}

/**
* Ensures that the passed integer is in the passed range. When you can use
Expand Down

0 comments on commit d0aa2a2

Please sign in to comment.