Reviewers: Mikhail Naganov (Chromium), loislo, alexeif,
Description:
Use SortedListBSearch instead of custom one in heap profiler
Please review this at https://chromiumcodereview.appspot.com/10006032/
SVN Base: http://v8.googlecode.com/svn/branches/bleeding_edge/
Affected files:
M src/list-inl.h
M src/list.h
M src/profile-generator.cc
Index: src/list-inl.h
===================================================================
--- src/list-inl.h (revision 11245)
+++ src/list-inl.h (working copy)
@@ -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;
}
@@ -231,9 +230,21 @@
}
+template<typename T>
+class ElementCmp {
+ public:
+ explicit ElementCmp(T e) : elem_(e) {}
+ int operator()(const T* elem) {
+ return PointerValueCompare(elem, &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));
}
Index: src/list.h
===================================================================
--- src/list.h (revision 11245)
+++ src/list.h (working copy)
@@ -173,10 +173,9 @@
// 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, typename P>
+int SortedListBSearch(const List<T>& list, P cmp);
template <typename T>
-int SortedListBSearch(
- const List<T>& list, T elem, int (*cmp)(const T* x, const T* y));
-template <typename T>
int SortedListBSearch(const List<T>& list, T elem);
Index: src/profile-generator.cc
===================================================================
--- src/profile-generator.cc (revision 11246)
+++ src/profile-generator.cc (working copy)
@@ -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