Skip to content

Commit af26da2

Browse files
committed
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.
1 parent 0a70bbf commit af26da2

File tree

1 file changed

+7
-5
lines changed

1 file changed

+7
-5
lines changed

ext/erb/erb.c

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -38,9 +38,8 @@ escaped_length(VALUE str)
3838
static VALUE
3939
optimized_escape_html(VALUE str)
4040
{
41-
// Optimize the most common, no-escape case with strpbrk(3). Not using it after
42-
// this because calling a C function many times could be slower for some cases.
43-
if (strpbrk(RSTRING_PTR(str), "'&\"<>") == NULL) {
41+
// Use strpbrk to optimize the no-escape case when str is long enough for SIMD.
42+
if (RSTRING_LEN(str) >= 16 && strpbrk(RSTRING_PTR(str), "'&\"<>") == NULL) {
4443
return str;
4544
}
4645

@@ -62,8 +61,11 @@ optimized_escape_html(VALUE str)
6261
}
6362
}
6463

65-
VALUE escaped = rb_str_new(buf, dest - buf);
66-
preserve_original_state(str, escaped);
64+
VALUE escaped = str;
65+
if (RSTRING_LEN(str) < (dest - buf)) {
66+
escaped = rb_str_new(buf, dest - buf);
67+
preserve_original_state(str, escaped);
68+
}
6769
ALLOCV_END(vbuf);
6870
return escaped;
6971
}

0 commit comments

Comments
 (0)