From 0daf224c171c4fcd4f8ddb58a684a1f7c96525fc Mon Sep 17 00:00:00 2001 From: "loislo@chromium.org" Date: Mon, 16 Sep 2013 13:13:42 +0000 Subject: [PATCH] HeapSnapshot: replace O(N*ln(N)) algorithm of sorting with O(N) one. We have HashMap for the strings. They got id sequentially. So we could use index sort. BUG=none R=alph@chromium.org, yangguo@chromium.org Review URL: https://codereview.chromium.org/24174002 git-svn-id: http://v8.googlecode.com/svn/branches/bleeding_edge@16734 ce2b1a6d-e550-0410-aec6-3dcde31c8c00 --- src/heap-snapshot-generator.cc | 36 ++++++++++-------------------------- src/heap-snapshot-generator.h | 1 - 2 files changed, 10 insertions(+), 27 deletions(-) diff --git a/src/heap-snapshot-generator.cc b/src/heap-snapshot-generator.cc index 25b1526..279880f 100644 --- a/src/heap-snapshot-generator.cc +++ b/src/heap-snapshot-generator.cc @@ -2693,37 +2693,21 @@ void HeapSnapshotJSONSerializer::SerializeString(const unsigned char* s) { void HeapSnapshotJSONSerializer::SerializeStrings() { - List sorted_strings; - SortHashMap(&strings_, &sorted_strings); + ScopedVector sorted_strings( + strings_.occupancy() + 1); + for (HashMap::Entry* entry = strings_.Start(); + entry != NULL; + entry = strings_.Next(entry)) { + sorted_strings[reinterpret_cast(entry->value)] = + reinterpret_cast(entry->key); + } writer_->AddString("\"\""); - for (int i = 0; i < sorted_strings.length(); ++i) { + for (int i = 1; i < sorted_strings.length(); ++i) { writer_->AddCharacter(','); - SerializeString( - reinterpret_cast(sorted_strings[i]->key)); + SerializeString(sorted_strings[i]); if (writer_->aborted()) return; } } -template -inline static int SortUsingEntryValue(const T* x, const T* y) { - uintptr_t x_uint = reinterpret_cast((*x)->value); - uintptr_t y_uint = reinterpret_cast((*y)->value); - if (x_uint > y_uint) { - return 1; - } else if (x_uint == y_uint) { - return 0; - } else { - return -1; - } -} - - -void HeapSnapshotJSONSerializer::SortHashMap( - HashMap* map, List* sorted_entries) { - for (HashMap::Entry* p = map->Start(); p != NULL; p = map->Next(p)) - sorted_entries->Add(p); - sorted_entries->Sort(SortUsingEntryValue); -} - } } // namespace v8::internal diff --git a/src/heap-snapshot-generator.h b/src/heap-snapshot-generator.h index 11d094a..c323f3c 100644 --- a/src/heap-snapshot-generator.h +++ b/src/heap-snapshot-generator.h @@ -658,7 +658,6 @@ class HeapSnapshotJSONSerializer { void SerializeSnapshot(); void SerializeString(const unsigned char* s); void SerializeStrings(); - void SortHashMap(HashMap* map, List* sorted_entries); static const int kEdgeFieldsCount; static const int kNodeFieldsCount; -- 2.7.4