[ruby/erb] Use strpbrk only when str is long enough for SIMD

This is the same trick used by https://github.com/k0kubun/hescape to
choose the best strategy for different scenarios.

https://github.com/ruby/erb/commit/af26da2858
This commit is contained in:
Takashi Kokubun 2022-11-05 00:26:32 -07:00 committed by git
parent 419d2fc14d
commit e8873e01b6

View File

@ -38,9 +38,8 @@ escaped_length(VALUE str)
static VALUE static VALUE
optimized_escape_html(VALUE str) optimized_escape_html(VALUE str)
{ {
// Optimize the most common, no-escape case with strpbrk(3). Not using it after // Use strpbrk to optimize the no-escape case when str is long enough for SIMD.
// this because calling a C function many times could be slower for some cases. if (RSTRING_LEN(str) >= 16 && strpbrk(RSTRING_PTR(str), "'&\"<>") == NULL) {
if (strpbrk(RSTRING_PTR(str), "'&\"<>") == NULL) {
return str; return str;
} }
@ -62,8 +61,11 @@ optimized_escape_html(VALUE str)
} }
} }
VALUE escaped = rb_str_new(buf, dest - buf); VALUE escaped = str;
preserve_original_state(str, escaped); if (RSTRING_LEN(str) < (dest - buf)) {
escaped = rb_str_new(buf, dest - buf);
preserve_original_state(str, escaped);
}
ALLOCV_END(vbuf); ALLOCV_END(vbuf);
return escaped; return escaped;
} }