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

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

Issue 2502323002: [builtins] Improve StringPrototypeEndsWith performance by adding a fastpath. (Closed)
Patch Set: Disallow heap alloc before flatcontent 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..4338676275f8828a4f0fe4a7b54cc1f7b9b0af8a 100644
--- a/src/builtins/builtins-string.cc
+++ b/src/builtins/builtins-string.cc
@@ -758,8 +758,28 @@ 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();
+ const char* str_ptr =
+ reinterpret_cast<const char*>(str_vector.start() + start);
+ const char* search_ptr =
+ reinterpret_cast<const char*>(search_vector.start());
+
+ if (strncmp(str_ptr, search_ptr, search_string->length()) == 0) {
+ return *isolate->factory()->true_value();
Yang 2016/11/16 12:13:24 isolate->heap()->true_value() also, why don't we
petermarshall 2016/11/16 12:19:15 Oops, yep.
+ }
+ }
+
+ 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