From f63aaee99001f440474953a9d525df62e1350f93 Mon Sep 17 00:00:00 2001 From: Franziska Hinkelmann Date: Fri, 28 Apr 2017 14:07:45 +0200 Subject: [PATCH] [cleanup] Replace List::BinarySearch with std::lower_bound. Also-by:ahaas@chromium.org R:ahaas@chromium.org Bug:v8:6325 Change-Id: I5fc7891a2201ac9a889bceec668b23b46e402545 Reviewed-on: https://chromium-review.googlesource.com/490109 Reviewed-by: Andreas Haas Commit-Queue: Franziska Hinkelmann Cr-Commit-Position: refs/heads/master@{#44981} --- src/list-inl.h | 35 ------------------------- src/list.h | 9 ------- src/profiler/heap-snapshot-generator.cc | 12 +++++---- src/utils.h | 7 ----- 4 files changed, 7 insertions(+), 56 deletions(-) diff --git a/src/list-inl.h b/src/list-inl.h index cc9933fd41..c29a420169 100644 --- a/src/list-inl.h +++ b/src/list-inl.h @@ -245,41 +245,6 @@ void List::StableSort() { ToVector().StableSort(); } - -template -int SortedListBSearch(const List& list, P cmp) { - int low = 0; - int high = list.length() - 1; - while (low <= high) { - int mid = low + (high - low) / 2; - T mid_elem = list[mid]; - - if (cmp(&mid_elem) > 0) { - high = mid - 1; - continue; - } - if (cmp(&mid_elem) < 0) { - low = mid + 1; - continue; - } - // Found the element. - return mid; - } - return -1; -} - - -template -class ElementCmp { - public: - explicit ElementCmp(T e) : elem_(e) {} - int operator()(const T* other) { - return PointerValueCompare(other, &elem_); - } - private: - T elem_; -}; - } // namespace internal } // namespace v8 diff --git a/src/list.h b/src/list.h index c7b35877c4..24b96e31a5 100644 --- a/src/list.h +++ b/src/list.h @@ -217,15 +217,6 @@ typedef List > MapHandleList; typedef List > TypeHandleList; typedef List > CodeHandleList; -// Perform binary search for an element in an already sorted -// list. Returns the index of the element of -1 if it was not found. -// |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 -int SortedListBSearch(const List& list, P cmp); - - } // namespace internal } // namespace v8 diff --git a/src/profiler/heap-snapshot-generator.cc b/src/profiler/heap-snapshot-generator.cc index 0fcc4dd686..ddbb0cb99f 100644 --- a/src/profiler/heap-snapshot-generator.cc +++ b/src/profiler/heap-snapshot-generator.cc @@ -306,11 +306,13 @@ class FindEntryById { HeapEntry* HeapSnapshot::GetEntryById(SnapshotObjectId id) { List* entries_by_id = GetSortedEntriesList(); - // Perform a binary search by id. - int index = SortedListBSearch(*entries_by_id, FindEntryById(id)); - if (index == -1) - return NULL; - return entries_by_id->at(index); + + auto it = std::lower_bound( + entries_by_id->begin(), entries_by_id->end(), id, + [](HeapEntry* first, SnapshotObjectId val) { return first->id() < val; }); + + if (it == entries_by_id->end() || (*it)->id() != id) return NULL; + return *it; } diff --git a/src/utils.h b/src/utils.h index 07ab118459..f00674df55 100644 --- a/src/utils.h +++ b/src/utils.h @@ -161,13 +161,6 @@ int Compare(const T& a, const T& b) { return 1; } - -template -int PointerValueCompare(const T* a, const T* b) { - return Compare(*a, *b); -} - - // Compare function to compare the object pointer value of two // handlified objects. The handles are passed as pointers to the // handles.