From: verwaest@chromium.org Date: Wed, 10 Oct 2012 14:48:07 +0000 (+0000) Subject: Also use binary search to search through valid entries. X-Git-Tag: upstream/4.7.83~15879 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=e5620d5a588a7638bc6484bdc9613ae63eecefde;p=platform%2Fupstream%2Fv8.git Also use binary search to search through valid entries. Review URL: https://chromiumcodereview.appspot.com/11028056 git-svn-id: http://v8.googlecode.com/svn/branches/bleeding_edge@12690 ce2b1a6d-e550-0410-aec6-3dcde31c8c00 --- diff --git a/src/objects-inl.h b/src/objects-inl.h index fb3fe64..e47cec7 100644 --- a/src/objects-inl.h +++ b/src/objects-inl.h @@ -1915,8 +1915,8 @@ void DescriptorArray::SetNumberOfDescriptors(int number_of_descriptors) { // Perform a binary search in a fixed array. Low and high are entry indices. If // there are three entries in this array it should be called with low=0 and // high=2. -template -int BinarySearch(T* array, String* name, int low, int high) { +template +int BinarySearch(T* array, String* name, int low, int high, int valid_entries) { uint32_t hash = name->Hash(); int limit = high; @@ -1938,12 +1938,18 @@ int BinarySearch(T* array, String* name, int low, int high) { int sort_index = array->GetSortedKeyIndex(low); String* entry = array->GetKey(sort_index); if (entry->Hash() != hash) break; - if (entry->Equals(name)) return sort_index; + if (entry->Equals(name)) { + if (search_mode == ALL_ENTRIES || sort_index < valid_entries) { + return sort_index; + } + return T::kNotFound; + } } return T::kNotFound; } + // Perform a linear search in this fixed array. len is the number of entry // indices that are valid. template @@ -1982,13 +1988,15 @@ int Search(T* array, String* name, int valid_entries) { // Fast case: do linear search for small arrays. const int kMaxElementsForLinearSearch = 8; - if (search_mode == VALID_ENTRIES || - (search_mode == ALL_ENTRIES && nof < kMaxElementsForLinearSearch)) { + if ((search_mode == ALL_ENTRIES && + nof <= kMaxElementsForLinearSearch) || + (search_mode == VALID_ENTRIES && + valid_entries <= (kMaxElementsForLinearSearch * 3))) { return LinearSearch(array, name, nof, valid_entries); } // Slow case: perform binary search. - return BinarySearch(array, name, 0, nof - 1); + return BinarySearch(array, name, 0, nof - 1, valid_entries); }