Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(30)

Unified Diff: src/builtins/builtins-string.cc

Issue 2502323002: [builtins] Improve StringPrototypeEndsWith performance by adding a fastpath. (Closed)
Patch Set: Use memcmp instead to avoid casting Created 4 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « no previous file | no next file » | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: src/builtins/builtins-string.cc
diff --git a/src/builtins/builtins-string.cc b/src/builtins/builtins-string.cc
index 4ccccbc85970cb89ab3977d9ac9c7791d918cdc6..a5439017c9fb76d9aa6e5ce50581b12b5b813806 100644
--- a/src/builtins/builtins-string.cc
+++ b/src/builtins/builtins-string.cc
@@ -758,8 +758,24 @@ BUILTIN(StringPrototypeEndsWith) {
int start = end - search_string->length();
if (start < 0) return isolate->heap()->false_value();
- FlatStringReader str_reader(isolate, String::Flatten(str));
- FlatStringReader search_reader(isolate, String::Flatten(search_string));
+ str = String::Flatten(str);
+ search_string = String::Flatten(search_string);
+
+ DisallowHeapAllocation no_gc; // ensure vectors stay valid
+ String::FlatContent str_content = str->GetFlatContent();
+ String::FlatContent search_content = search_string->GetFlatContent();
+
+ if (str_content.IsOneByte() && search_content.IsOneByte()) {
+ Vector<const uint8_t> str_vector = str_content.ToOneByteVector();
+ Vector<const uint8_t> search_vector = search_content.ToOneByteVector();
+
+ return isolate->heap()->ToBoolean(memcmp(str_vector.start() + start,
+ search_vector.start(),
+ search_string->length()) == 0);
+ }
+
+ FlatStringReader str_reader(isolate, str);
+ FlatStringReader search_reader(isolate, search_string);
for (int i = 0; i < search_string->length(); i++) {
if (str_reader.Get(start + i) != search_reader.Get(i)) {
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698