Commit 668d8927 authored by jgruber's avatar jgruber Committed by Commit Bot

[string] Fast-path in indexOf for subject == search

If the subject string is a string, it's pointer-equal to the search
string, and position <= 0, then we can simply return 0 and skip
all other logic in String.p.indexOf.

Further context at:
https://twitter.com/hashseed/status/893539117367271425

Bug: 
Change-Id: I93ce724f0ade6332599ba395fe8c662a28f05ade
Reviewed-on: https://chromium-review.googlesource.com/602214Reviewed-by: 's avatarYang Guo <yangguo@chromium.org>
Commit-Queue: Jakob Gruber <jgruber@chromium.org>
Cr-Commit-Position: refs/heads/master@{#47183}
parent 82202251
...@@ -793,6 +793,11 @@ void StringBuiltinsAssembler::StringIndexOf( ...@@ -793,6 +793,11 @@ void StringBuiltinsAssembler::StringIndexOf(
&return_minus_1); &return_minus_1);
} }
// If the string pointers are identical, we can just return 0. Note that this
// implies {start_position} == 0 since we've passed the check above.
Label return_zero(this);
GotoIf(WordEqual(subject_string, search_string), &return_zero);
// Try to unpack subject and search strings. Bail to runtime if either needs // Try to unpack subject and search strings. Bail to runtime if either needs
// to be flattened. // to be flattened.
ToDirectStringAssembler subject_to_direct(state(), subject_string); ToDirectStringAssembler subject_to_direct(state(), subject_string);
...@@ -914,6 +919,9 @@ void StringBuiltinsAssembler::StringIndexOf( ...@@ -914,6 +919,9 @@ void StringBuiltinsAssembler::StringIndexOf(
BIND(&return_minus_1); BIND(&return_minus_1);
f_return(SmiConstant(-1)); f_return(SmiConstant(-1));
BIND(&return_zero);
f_return(SmiConstant(0));
BIND(&zero_length_needle); BIND(&zero_length_needle);
{ {
Comment("0-length search_string"); Comment("0-length search_string");
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment