Revision: 12690
Author:   verwa...@chromium.org
Date:     Wed Oct 10 07:48:07 2012
Log:      Also use binary search to search through valid entries.

Review URL: https://chromiumcodereview.appspot.com/11028056
http://code.google.com/p/v8/source/detail?r=12690

Modified:
 /branches/bleeding_edge/src/objects-inl.h

=======================================
--- /branches/bleeding_edge/src/objects-inl.h   Mon Sep 24 07:23:46 2012
+++ /branches/bleeding_edge/src/objects-inl.h   Wed Oct 10 07:48:07 2012
@@ -1915,8 +1915,8 @@
// 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<typename T>
-int BinarySearch(T* array, String* name, int low, int high) {
+template<SearchMode search_mode, typename T>
+int BinarySearch(T* array, String* name, int low, int high, int valid_entries) {
   uint32_t hash = name->Hash();
   int limit = high;

@@ -1938,11 +1938,17 @@
     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.
@@ -1982,13 +1988,15 @@

   // 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<search_mode>(array, name, nof, valid_entries);
   }

   // Slow case: perform binary search.
-  return BinarySearch(array, name, 0, nof - 1);
+  return BinarySearch<search_mode>(array, name, 0, nof - 1, valid_entries);
 }


--
v8-dev mailing list
v8-dev@googlegroups.com
http://groups.google.com/group/v8-dev

Reply via email to