Switch to a multimap for the hash->function map.

This commit is contained in:
Unknown W. Brackets 2014-11-03 07:59:15 -08:00
parent 8a57e37f33
commit 67a7205bdd

View file

@ -16,6 +16,15 @@
// https://github.com/hrydgard/ppsspp and http://www.ppsspp.org/.
#include <map>
#ifdef IOS
#include <tr1/unordered_map>
namespace std {
using std::tr1:unordered_map;
using std::tr1:unordered_multimap;
};
#else
#include <unordered_map>
#endif
#include <set>
#include "base/mutex.h"
#include "ext/cityhash/city.h"
@ -40,10 +49,9 @@ typedef std::vector<MIPSAnalyst::AnalyzedFunction> FunctionsVector;
static FunctionsVector functions;
recursive_mutex functions_lock;
// TODO: Try multimap instead
// One function can appear in multiple copies in memory, and they will all have
// the same hash and should all be replaced if possible.
static std::map<u64, std::vector<MIPSAnalyst::AnalyzedFunction*>> hashToFunction;
static std::unordered_multimap<u64, MIPSAnalyst::AnalyzedFunction *> hashToFunction;
struct HashMapFunc {
char name[64];
@ -654,10 +662,11 @@ namespace MIPSAnalyst {
void UpdateHashToFunctionMap() {
lock_guard guard(functions_lock);
hashToFunction.clear();
hashToFunction.reserve(functions.size());
for (auto iter = functions.begin(); iter != functions.end(); iter++) {
AnalyzedFunction &f = *iter;
if (f.hasHash && f.size > 16) {
hashToFunction[f.hash].push_back(&f);
hashToFunction.emplace(f.hash, &f);
}
}
}
@ -1124,15 +1133,14 @@ skip:
UpdateHashToFunctionMap();
for (auto mf = hashMap.begin(), end = hashMap.end(); mf != end; ++mf) {
auto iter = hashToFunction.find(mf->hash);
if (iter == hashToFunction.end()) {
auto range = hashToFunction.equal_range(mf->hash);
if (range.first == range.second) {
continue;
}
// Yay, found a function.
for (unsigned int i = 0; i < iter->second.size(); i++) {
AnalyzedFunction &f = *(iter->second[i]);
for (auto iter = range.first; iter != range.second; ++iter) {
AnalyzedFunction &f = *iter->second;
if (f.hash == mf->hash && f.size == mf->size) {
strncpy(f.name, mf->name, sizeof(mf->name) - 1);