Revision: 11252
Author:   [email protected]
Date:     Tue Apr 10 04:24:09 2012
Log:      Use SortedListBSearch instead of custom one in heap profiler
Review URL: https://chromiumcodereview.appspot.com/10006032
http://code.google.com/p/v8/source/detail?r=11252

Modified:
 /branches/bleeding_edge/src/list-inl.h
 /branches/bleeding_edge/src/list.h
 /branches/bleeding_edge/src/profile-generator.cc

=======================================
--- /branches/bleeding_edge/src/list-inl.h      Mon Feb  6 08:23:40 2012
+++ /branches/bleeding_edge/src/list-inl.h      Tue Apr 10 04:24:09 2012
@@ -207,20 +207,19 @@
 }


-template <typename T>
-int SortedListBSearch(
-    const List<T>& list, T elem, int (*cmp)(const T* x, const T* y)) {
+template <typename T, typename P>
+int SortedListBSearch(const List<T>& list, P cmp) {
   int low = 0;
   int high = list.length() - 1;
   while (low <= high) {
     int mid = (low + high) / 2;
     T mid_elem = list[mid];

-    if (cmp(&mid_elem, &elem) > 0) {
+    if (cmp(&mid_elem) > 0) {
       high = mid - 1;
       continue;
     }
-    if (cmp(&mid_elem, &elem) < 0) {
+    if (cmp(&mid_elem) < 0) {
       low = mid + 1;
       continue;
     }
@@ -229,11 +228,23 @@
   }
   return -1;
 }
+
+
+template<typename T>
+class ElementCmp {
+ public:
+  explicit ElementCmp(T e) : elem_(e) {}
+  int operator()(const T* other) {
+    return PointerValueCompare(other, &elem_);
+  }
+ private:
+  T elem_;
+};


 template <typename T>
 int SortedListBSearch(const List<T>& list, T elem) {
-  return SortedListBSearch<T>(list, elem, PointerValueCompare<T>);
+  return SortedListBSearch<T, ElementCmp<T> > (list, ElementCmp<T>(elem));
 }


=======================================
--- /branches/bleeding_edge/src/list.h  Mon Jan 16 04:38:59 2012
+++ /branches/bleeding_edge/src/list.h  Tue Apr 10 04:24:09 2012
@@ -173,9 +173,11 @@

 // Perform binary search for an element in an already sorted
 // list. Returns the index of the element of -1 if it was not found.
-template <typename T>
-int SortedListBSearch(
-    const List<T>& list, T elem, int (*cmp)(const T* x, const T* y));
+// |cmp| is a predicate that takes a pointer to an element of the List
+// and returns +1 if it is greater, -1 if it is less than the element
+// being searched.
+template <typename T, class P>
+int SortedListBSearch(const List<T>& list, P cmp);
 template <typename T>
 int SortedListBSearch(const List<T>& list, T elem);

=======================================
--- /branches/bleeding_edge/src/profile-generator.cc Tue Apr 10 04:07:16 2012 +++ /branches/bleeding_edge/src/profile-generator.cc Tue Apr 10 04:24:09 2012
@@ -1256,24 +1256,25 @@
 }


+class FindEntryById {
+ public:
+  explicit FindEntryById(SnapshotObjectId id) : id_(id) { }
+  int operator()(HeapEntry* const* entry) {
+    if ((*entry)->id() == id_) return 0;
+    return (*entry)->id() < id_ ? -1 : 1;
+  }
+ private:
+  SnapshotObjectId id_;
+};
+
+
 HeapEntry* HeapSnapshot::GetEntryById(SnapshotObjectId id) {
   List<HeapEntry*>* entries_by_id = GetSortedEntriesList();
-
   // Perform a binary search by id.
-  int low = 0;
-  int high = entries_by_id->length() - 1;
-  while (low <= high) {
-    int mid =
- (static_cast<unsigned int>(low) + static_cast<unsigned int>(high))
1;
-    SnapshotObjectId mid_id = entries_by_id->at(mid)->id();
-    if (mid_id > id)
-      high = mid - 1;
-    else if (mid_id < id)
-      low = mid + 1;
-    else
-      return entries_by_id->at(mid);
-  }
-  return NULL;
+  int index = SortedListBSearch(*entries_by_id, FindEntryById(id));
+  if (index == -1)
+    return NULL;
+  return entries_by_id->at(index);
 }


--
v8-dev mailing list
[email protected]
http://groups.google.com/group/v8-dev

Reply via email to