bug 661574: Use a hashtable for histogram lookup r=glandium

This commit is contained in:
Taras Glek 2011-06-20 14:48:03 -07:00
Родитель 909283a871
Коммит 3b12d3039e
1 изменённых файлов: 29 добавлений и 18 удалений

Просмотреть файл

@ -47,20 +47,14 @@
#include "nsStringGlue.h" #include "nsStringGlue.h"
#include "nsITelemetry.h" #include "nsITelemetry.h"
#include "Telemetry.h" #include "Telemetry.h"
#include <map> #include "nsTHashtable.h"
#include <string.h> #include "nsHashKeys.h"
#include "nsBaseHashtable.h"
namespace { namespace {
using namespace base; using namespace base;
using namespace mozilla; using namespace mozilla;
using namespace std;
struct ltstr {
bool operator()(const char* s1, const char* s2) const {
return strcmp(s1, s2) < 0;
}
};
class TelemetryImpl : public nsITelemetry class TelemetryImpl : public nsITelemetry
{ {
@ -68,11 +62,14 @@ class TelemetryImpl : public nsITelemetry
NS_DECL_NSITELEMETRY NS_DECL_NSITELEMETRY
public: public:
TelemetryImpl();
~TelemetryImpl();
private: private:
// This is used to cache JS string->Telemetry::ID conversions // This is used for speedy JS string->Telemetry::ID conversions
typedef map<const char*, Telemetry::ID, ltstr> NameHistogramMap; typedef nsBaseHashtableET<nsCharPtrHashKey, Telemetry::ID> CharPtrEntryType;
NameHistogramMap mHistogramMap; typedef nsTHashtable<CharPtrEntryType> HistogramMapType;
HistogramMapType mHistogramMap;
}; };
// A initializer to initialize histogram collection // A initializer to initialize histogram collection
@ -238,6 +235,14 @@ WrapAndReturnHistogram(Histogram *h, JSContext *cx, jsval *ret)
&& JS_DefineFunction (cx, obj, "snapshot", JSHistogram_Snapshot, 1, 0)) ? NS_OK : NS_ERROR_FAILURE; && JS_DefineFunction (cx, obj, "snapshot", JSHistogram_Snapshot, 1, 0)) ? NS_OK : NS_ERROR_FAILURE;
} }
TelemetryImpl::TelemetryImpl() {
mHistogramMap.Init(Telemetry::HistogramCount);
}
TelemetryImpl::~TelemetryImpl() {
mHistogramMap.Clear();
}
NS_IMETHODIMP NS_IMETHODIMP
TelemetryImpl::NewHistogram(const nsACString &name, PRUint32 min, PRUint32 max, PRUint32 bucketCount, PRUint32 histogramType, JSContext *cx, jsval *ret) TelemetryImpl::NewHistogram(const nsACString &name, PRUint32 min, PRUint32 max, PRUint32 bucketCount, PRUint32 histogramType, JSContext *cx, jsval *ret)
{ {
@ -275,20 +280,26 @@ TelemetryImpl::GetHistogramSnapshots(JSContext *cx, jsval *ret)
NS_IMETHODIMP NS_IMETHODIMP
TelemetryImpl::GetHistogramById(const nsACString &name, JSContext *cx, jsval *ret) TelemetryImpl::GetHistogramById(const nsACString &name, JSContext *cx, jsval *ret)
{ {
// Cache names for log(N) lookup // Cache names
// Note the histogram names are statically allocated // Note the histogram names are statically allocated
if (Telemetry::HistogramCount && !mHistogramMap.size()) { if (!mHistogramMap.Count()) {
for (PRUint32 i = 0; i < Telemetry::HistogramCount; i++) { for (PRUint32 i = 0; i < Telemetry::HistogramCount; i++) {
mHistogramMap[gHistograms[i].id] = (Telemetry::ID) i; CharPtrEntryType *entry = mHistogramMap.PutEntry(gHistograms[i].id);
if (NS_UNLIKELY(!entry)) {
mHistogramMap.Clear();
return NS_ERROR_OUT_OF_MEMORY;
}
entry->mData = (Telemetry::ID) i;
} }
} }
NameHistogramMap::iterator it = mHistogramMap.find(PromiseFlatCString(name).get()); CharPtrEntryType *entry = mHistogramMap.GetEntry(PromiseFlatCString(name).get());
if (it == mHistogramMap.end()) if (!entry)
return NS_ERROR_FAILURE; return NS_ERROR_FAILURE;
Histogram *h; Histogram *h;
nsresult rv = GetHistogramByEnumId(it->second, &h);
nsresult rv = GetHistogramByEnumId(entry->mData, &h);
if (NS_FAILED(rv)) if (NS_FAILED(rv))
return rv; return rv;