diff options
author | Takashi Kokubun <takashikkbn@gmail.com> | 2022-11-05 00:26:32 -0700 |
---|---|---|
committer | git <svn-admin@ruby-lang.org> | 2022-11-05 07:52:45 +0000 |
commit | e8873e01b67629f93ebbd83397f2454e16e0d864 (patch) | |
tree | 1e9820d472aeef3012b2b55480432372c6789d09 /ext | |
parent | 419d2fc14d2bedc6d5a7080ee80df8330884ea6c (diff) | |
download | ruby-e8873e01b67629f93ebbd83397f2454e16e0d864.tar.gz |
[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
Diffstat (limited to 'ext')
-rw-r--r-- | ext/erb/erb.c | 12 |
1 files changed, 7 insertions, 5 deletions
diff --git a/ext/erb/erb.c b/ext/erb/erb.c index 1e4842c793..1c3371d24e 100644 --- a/ext/erb/erb.c +++ b/ext/erb/erb.c @@ -38,9 +38,8 @@ escaped_length(VALUE str) static VALUE optimized_escape_html(VALUE str) { - // Optimize the most common, no-escape case with strpbrk(3). Not using it after - // this because calling a C function many times could be slower for some cases. - if (strpbrk(RSTRING_PTR(str), "'&\"<>") == NULL) { + // Use strpbrk to optimize the no-escape case when str is long enough for SIMD. + if (RSTRING_LEN(str) >= 16 && strpbrk(RSTRING_PTR(str), "'&\"<>") == NULL) { return str; } @@ -62,8 +61,11 @@ optimized_escape_html(VALUE str) } } - VALUE escaped = rb_str_new(buf, dest - buf); - preserve_original_state(str, escaped); + VALUE escaped = str; + if (RSTRING_LEN(str) < (dest - buf)) { + escaped = rb_str_new(buf, dest - buf); + preserve_original_state(str, escaped); + } ALLOCV_END(vbuf); return escaped; } |