str_independent: add a fastpath with a single flag check
If we assume that most strings we modify are not frozen and are independent, then we can optimize this case by replacing multiple flag checks by a single mask check.
This commit is contained in:
parent
f57167d338
commit
af44af238b
Notes:
git
2024-08-09 13:21:16 +00:00
27
string.c
27
string.c
@ -2500,32 +2500,43 @@ rb_check_lockedtmp(VALUE str)
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// If none of these flags are set, we know we have an modifiable string.
|
||||||
|
// If any is set, we need to do more detailed checks.
|
||||||
|
#define STR_UNMODIFIABLE_MASK (FL_FREEZE | STR_TMPLOCK | STR_CHILLED)
|
||||||
static inline void
|
static inline void
|
||||||
str_modifiable(VALUE str)
|
str_modifiable(VALUE str)
|
||||||
{
|
{
|
||||||
if (CHILLED_STRING_P(str)) {
|
if (RB_UNLIKELY(FL_ANY_RAW(str, STR_UNMODIFIABLE_MASK))) {
|
||||||
CHILLED_STRING_MUTATED(str);
|
if (CHILLED_STRING_P(str)) {
|
||||||
|
CHILLED_STRING_MUTATED(str);
|
||||||
|
}
|
||||||
|
rb_check_lockedtmp(str);
|
||||||
|
rb_check_frozen(str);
|
||||||
}
|
}
|
||||||
rb_check_lockedtmp(str);
|
|
||||||
rb_check_frozen(str);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
static inline int
|
static inline int
|
||||||
str_dependent_p(VALUE str)
|
str_dependent_p(VALUE str)
|
||||||
{
|
{
|
||||||
if (STR_EMBED_P(str) || !FL_TEST(str, STR_SHARED|STR_NOFREE)) {
|
if (STR_EMBED_P(str) || !FL_TEST(str, STR_SHARED|STR_NOFREE)) {
|
||||||
return 0;
|
return FALSE;
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
return 1;
|
return TRUE;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// If none of these flags are set, we know we have an independent string.
|
||||||
|
// If any is set, we need to do more detailed checks.
|
||||||
|
#define STR_DEPENDANT_MASK (STR_UNMODIFIABLE_MASK | STR_SHARED | STR_NOFREE)
|
||||||
static inline int
|
static inline int
|
||||||
str_independent(VALUE str)
|
str_independent(VALUE str)
|
||||||
{
|
{
|
||||||
str_modifiable(str);
|
if (RB_UNLIKELY(FL_ANY_RAW(str, STR_DEPENDANT_MASK))) {
|
||||||
return !str_dependent_p(str);
|
str_modifiable(str);
|
||||||
|
return !str_dependent_p(str);
|
||||||
|
}
|
||||||
|
return TRUE;
|
||||||
}
|
}
|
||||||
|
|
||||||
static void
|
static void
|
||||||
|
Loading…
x
Reference in New Issue
Block a user