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

Side by Side Diff: src/runtime.cc

Issue 2989: Avoid the KMP overhead for simple indexOf() operations. (Closed)
Patch Set: Typo Created 12 years, 3 months 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 unified diff | Download patch
« no previous file with comments | « no previous file | src/string.js » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright 2006-2008 the V8 project authors. All rights reserved. 1 // Copyright 2006-2008 the V8 project authors. All rights reserved.
2 // Redistribution and use in source and binary forms, with or without 2 // Redistribution and use in source and binary forms, with or without
3 // modification, are permitted provided that the following conditions are 3 // modification, are permitted provided that the following conditions are
4 // met: 4 // met:
5 // 5 //
6 // * Redistributions of source code must retain the above copyright 6 // * Redistributions of source code must retain the above copyright
7 // notice, this list of conditions and the following disclaimer. 7 // notice, this list of conditions and the following disclaimer.
8 // * Redistributions in binary form must reproduce the above 8 // * Redistributions in binary form must reproduce the above
9 // copyright notice, this list of conditions and the following 9 // copyright notice, this list of conditions and the following
10 // disclaimer in the documentation and/or other materials provided 10 // disclaimer in the documentation and/or other materials provided
(...skipping 893 matching lines...) Expand 10 before | Expand all | Expand 10 after
904 904
905 905
906 static Object* Runtime_StringIndexOf(Arguments args) { 906 static Object* Runtime_StringIndexOf(Arguments args) {
907 NoHandleAllocation ha; 907 NoHandleAllocation ha;
908 ASSERT(args.length() == 3); 908 ASSERT(args.length() == 3);
909 909
910 CONVERT_CHECKED(String, sub, args[0]); 910 CONVERT_CHECKED(String, sub, args[0]);
911 CONVERT_CHECKED(String, pat, args[1]); 911 CONVERT_CHECKED(String, pat, args[1]);
912 Object* index = args[2]; 912 Object* index = args[2];
913 913
914 sub->TryFlatten();
915 pat->TryFlatten();
916
914 int subject_length = sub->length(); 917 int subject_length = sub->length();
915 int pattern_length = pat->length(); 918 int pattern_length = pat->length();
916 919
917 sub->TryFlatten();
918 pat->TryFlatten();
919
920 uint32_t start_index; 920 uint32_t start_index;
921 if (!Array::IndexFromObject(index, &start_index)) return Smi::FromInt(-1); 921 if (!Array::IndexFromObject(index, &start_index)) return Smi::FromInt(-1);
922 if (pattern_length == 0) return Smi::FromInt(start_index); 922 if (pattern_length == 0) return Smi::FromInt(start_index);
923 923
924 // Searching for one specific character is common. For one 924 // Searching for one specific character is common. For one
925 // character patterns the KMP algorithm is guaranteed to slow down 925 // character patterns the KMP algorithm is guaranteed to slow down
926 // the search, so we just run through the subject string. 926 // the search, so we just run through the subject string.
927 if (pattern_length == 1) { 927 if (pattern_length == 1) {
928 uint16_t pattern_char = pat->Get(0); 928 uint16_t pattern_char = pat->Get(0);
929 for (int i = start_index; i < subject_length; i++) { 929 for (int i = start_index; i < subject_length; i++) {
930 if (sub->Get(i) == pattern_char) { 930 if (sub->Get(i) == pattern_char) {
931 return Smi::FromInt(i); 931 return Smi::FromInt(i);
932 } 932 }
933 } 933 }
934 return Smi::FromInt(-1); 934 return Smi::FromInt(-1);
935 } 935 }
936 936
937 // For patterns with a length larger than one character we use the KMP 937 // For small searches, the ability to skip the search length is not worth
938 // algorithm. 938 // the overhead of setting up KMP. TODO get more accurate cutoffs here.
Mads Ager (chromium) 2008/09/19 12:20:22 Either leave out the TODO or report a bug and use
939 if (subject_length < 100) {
940 // We know our pattern is at least 2 characters, we cache the first so the
941 // the common case of the first character not matching is fast.
Mads Ager (chromium) 2008/09/19 12:20:22 the the -> that the
942 uint16_t pattern_first_char = pat->Get(0);
943 for (int i = start_index; i + pattern_length <= subject_length; i++) {
944 if (sub->Get(i) != pattern_first_char) continue;
945
946 for (int j = 1; j < pattern_length; ++j) {
Mads Ager (chromium) 2008/09/19 12:20:22 I think we use j++ consistently in the rest of the
947 if (pat->Get(j) != sub->Get(j + i)) break;
948 if (j == pattern_length - 1) return Smi::FromInt(i);
949 }
950 }
951 return Smi::FromInt(-1);
952 }
953
954 // For patterns with a larger length we use the KMP algorithm.
939 // 955 //
940 // Compute the 'next' table. 956 // Compute the 'next' table.
941 int* next_table = NewArray<int>(pattern_length); 957 int* next_table = NewArray<int>(pattern_length);
942 ComputeKMPNextTable(pat, next_table); 958 ComputeKMPNextTable(pat, next_table);
943 // Search using the 'next' table. 959 // Search using the 'next' table.
944 int pattern_index = 0; 960 int pattern_index = 0;
945 // We would like to use StringInputBuffer here, but it does not have 961 // We would like to use StringInputBuffer here, but it does not have
946 // the ability to start anywhere but the first character of a 962 // the ability to start anywhere but the first character of a
947 // string. It would be nice to have efficient forward-seeking 963 // string. It would be nice to have efficient forward-seeking
948 // support on StringInputBuffers. 964 // support on StringInputBuffers.
(...skipping 4012 matching lines...) Expand 10 before | Expand all | Expand 10 after
4961 4977
4962 void Runtime::PerformGC(Object* result) { 4978 void Runtime::PerformGC(Object* result) {
4963 Failure* failure = Failure::cast(result); 4979 Failure* failure = Failure::cast(result);
4964 // Try to do a garbage collection; ignore it if it fails. The C 4980 // Try to do a garbage collection; ignore it if it fails. The C
4965 // entry stub will throw an out-of-memory exception in that case. 4981 // entry stub will throw an out-of-memory exception in that case.
4966 Heap::CollectGarbage(failure->requested(), failure->allocation_space()); 4982 Heap::CollectGarbage(failure->requested(), failure->allocation_space());
4967 } 4983 }
4968 4984
4969 4985
4970 } } // namespace v8::internal 4986 } } // namespace v8::internal
OLDNEW
« no previous file with comments | « no previous file | src/string.js » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698