Skip to content
This repository has been archived by the owner on Aug 11, 2020. It is now read-only.

Commit

Permalink
src: cleanup histogram code
Browse files Browse the repository at this point in the history
Move methods that do not need to be inline into their own file,
replace `std::function` with a template variant for performance,
remove `inline` for functions with implicit inline linkage.

PR-URL: #126
Reviewed-By: Daniel Bevenius <[email protected]>
Reviewed-By: James M Snell <[email protected]>
  • Loading branch information
addaleax committed Sep 23, 2019
1 parent 30eb6e9 commit 930fb13
Show file tree
Hide file tree
Showing 4 changed files with 151 additions and 150 deletions.
1 change: 1 addition & 0 deletions node.gyp
Original file line number Diff line number Diff line change
Expand Up @@ -515,6 +515,7 @@
'src/fs_event_wrap.cc',
'src/handle_wrap.cc',
'src/heap_utils.cc',
'src/histogram.cc',
'src/js_native_api.h',
'src/js_native_api_types.h',
'src/js_native_api_v8.cc',
Expand Down
126 changes: 15 additions & 111 deletions src/histogram-inl.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,45 +9,46 @@

namespace node {

inline Histogram::Histogram(int64_t lowest, int64_t highest, int figures) {
Histogram::Histogram(int64_t lowest, int64_t highest, int figures) {
CHECK_EQ(0, hdr_init(lowest, highest, figures, &histogram_));
}

inline Histogram::~Histogram() {
Histogram::~Histogram() {
hdr_close(histogram_);
}

inline void Histogram::Reset() {
void Histogram::Reset() {
hdr_reset(histogram_);
}

inline bool Histogram::Record(int64_t value) {
bool Histogram::Record(int64_t value) {
return hdr_record_value(histogram_, value);
}

inline int64_t Histogram::Min() {
int64_t Histogram::Min() {
return hdr_min(histogram_);
}

inline int64_t Histogram::Max() {
int64_t Histogram::Max() {
return hdr_max(histogram_);
}

inline double Histogram::Mean() {
double Histogram::Mean() {
return hdr_mean(histogram_);
}

inline double Histogram::Stddev() {
double Histogram::Stddev() {
return hdr_stddev(histogram_);
}

inline double Histogram::Percentile(double percentile) {
double Histogram::Percentile(double percentile) {
CHECK_GT(percentile, 0);
CHECK_LE(percentile, 100);
return static_cast<double>(hdr_value_at_percentile(histogram_, percentile));
}

inline void Histogram::Percentiles(std::function<void(double, double)> fn) {
template <typename Iterator>
void Histogram::Percentiles(Iterator&& fn) {
hdr_iter iter;
hdr_iter_percentile_init(&iter, histogram_, 1);
while (hdr_iter_next(&iter)) {
Expand All @@ -57,7 +58,7 @@ inline void Histogram::Percentiles(std::function<void(double, double)> fn) {
}
}

inline HistogramBase::HistogramBase(
HistogramBase::HistogramBase(
Environment* env,
v8::Local<v8::Object> wrap,
int64_t lowest,
Expand All @@ -66,7 +67,7 @@ inline HistogramBase::HistogramBase(
BaseObject(env, wrap),
Histogram(lowest, highest, figures) {}

inline bool HistogramBase::RecordDelta() {
bool HistogramBase::RecordDelta() {
uint64_t time = uv_hrtime();
bool ret = true;
if (prev_ > 0) {
Expand All @@ -85,110 +86,13 @@ inline bool HistogramBase::RecordDelta() {
return ret;
}

inline void HistogramBase::ResetState() {
void HistogramBase::ResetState() {
Reset();
exceeds_ = 0;
prev_ = 0;
}

inline void HistogramBase::HistogramMin(
const v8::FunctionCallbackInfo<v8::Value>& args) {
HistogramBase* histogram;
ASSIGN_OR_RETURN_UNWRAP(&histogram, args.Holder());
double value = static_cast<double>(histogram->Min());
args.GetReturnValue().Set(value);
}

inline void HistogramBase::HistogramMax(
const v8::FunctionCallbackInfo<v8::Value>& args) {
HistogramBase* histogram;
ASSIGN_OR_RETURN_UNWRAP(&histogram, args.Holder());
double value = static_cast<double>(histogram->Max());
args.GetReturnValue().Set(value);
}

inline void HistogramBase::HistogramMean(
const v8::FunctionCallbackInfo<v8::Value>& args) {
HistogramBase* histogram;
ASSIGN_OR_RETURN_UNWRAP(&histogram, args.Holder());
args.GetReturnValue().Set(histogram->Mean());
}

inline void HistogramBase::HistogramExceeds(
const v8::FunctionCallbackInfo<v8::Value>& args) {
HistogramBase* histogram;
ASSIGN_OR_RETURN_UNWRAP(&histogram, args.Holder());
double value = static_cast<double>(histogram->Exceeds());
args.GetReturnValue().Set(value);
}

inline void HistogramBase::HistogramStddev(
const v8::FunctionCallbackInfo<v8::Value>& args) {
HistogramBase* histogram;
ASSIGN_OR_RETURN_UNWRAP(&histogram, args.Holder());
args.GetReturnValue().Set(histogram->Stddev());
}

inline void HistogramBase::HistogramPercentile(
const v8::FunctionCallbackInfo<v8::Value>& args) {
HistogramBase* histogram;
ASSIGN_OR_RETURN_UNWRAP(&histogram, args.Holder());
CHECK(args[0]->IsNumber());
double percentile = args[0].As<v8::Number>()->Value();
args.GetReturnValue().Set(histogram->Percentile(percentile));
}

inline void HistogramBase::HistogramPercentiles(
const v8::FunctionCallbackInfo<v8::Value>& args) {
Environment* env = Environment::GetCurrent(args);
HistogramBase* histogram;
ASSIGN_OR_RETURN_UNWRAP(&histogram, args.Holder());
CHECK(args[0]->IsMap());
v8::Local<v8::Map> map = args[0].As<v8::Map>();
histogram->Percentiles([&](double key, double value) {
map->Set(
env->context(),
v8::Number::New(env->isolate(), key),
v8::Number::New(env->isolate(), value)).IsEmpty();
});
}

inline void HistogramBase::HistogramReset(
const v8::FunctionCallbackInfo<v8::Value>& args) {
HistogramBase* histogram;
ASSIGN_OR_RETURN_UNWRAP(&histogram, args.Holder());
histogram->ResetState();
}

inline void HistogramBase::Initialize(Environment* env) {
// Guard against multiple initializations
if (!env->histogram_ctor_template().IsEmpty())
return;

v8::Local<v8::String> classname =
FIXED_ONE_BYTE_STRING(env->isolate(), "Histogram");

v8::Local<v8::FunctionTemplate> histogram =
v8::FunctionTemplate::New(env->isolate());
histogram->SetClassName(classname);

v8::Local<v8::ObjectTemplate> histogramt =
histogram->InstanceTemplate();

histogramt->SetInternalFieldCount(1);
env->SetProtoMethod(histogram, "exceeds", HistogramExceeds);
env->SetProtoMethod(histogram, "min", HistogramMin);
env->SetProtoMethod(histogram, "max", HistogramMax);
env->SetProtoMethod(histogram, "mean", HistogramMean);
env->SetProtoMethod(histogram, "stddev", HistogramStddev);
env->SetProtoMethod(histogram, "percentile", HistogramPercentile);
env->SetProtoMethod(histogram, "percentiles", HistogramPercentiles);
env->SetProtoMethod(histogram, "reset", HistogramReset);

env->set_histogram_ctor_template(histogramt);
}

inline HistogramBase* HistogramBase::New(
HistogramBase* HistogramBase::New(
Environment* env,
int64_t lowest,
int64_t highest,
Expand Down
111 changes: 111 additions & 0 deletions src/histogram.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
#include "histogram.h" // NOLINT(build/include_inline)
#include "histogram-inl.h"
#include "memory_tracker-inl.h"

namespace node {

using v8::FunctionCallbackInfo;
using v8::FunctionTemplate;
using v8::Local;
using v8::Map;
using v8::Number;
using v8::ObjectTemplate;
using v8::String;
using v8::Value;

void HistogramBase::MemoryInfo(MemoryTracker* tracker) const {
tracker->TrackFieldWithSize("histogram", GetMemorySize());
}

void HistogramBase::HistogramMin(const FunctionCallbackInfo<Value>& args) {
HistogramBase* histogram;
ASSIGN_OR_RETURN_UNWRAP(&histogram, args.Holder());
double value = static_cast<double>(histogram->Min());
args.GetReturnValue().Set(value);
}

void HistogramBase::HistogramMax(const FunctionCallbackInfo<Value>& args) {
HistogramBase* histogram;
ASSIGN_OR_RETURN_UNWRAP(&histogram, args.Holder());
double value = static_cast<double>(histogram->Max());
args.GetReturnValue().Set(value);
}

void HistogramBase::HistogramMean(const FunctionCallbackInfo<Value>& args) {
HistogramBase* histogram;
ASSIGN_OR_RETURN_UNWRAP(&histogram, args.Holder());
args.GetReturnValue().Set(histogram->Mean());
}

void HistogramBase::HistogramExceeds(const FunctionCallbackInfo<Value>& args) {
HistogramBase* histogram;
ASSIGN_OR_RETURN_UNWRAP(&histogram, args.Holder());
double value = static_cast<double>(histogram->Exceeds());
args.GetReturnValue().Set(value);
}

void HistogramBase::HistogramStddev(const FunctionCallbackInfo<Value>& args) {
HistogramBase* histogram;
ASSIGN_OR_RETURN_UNWRAP(&histogram, args.Holder());
args.GetReturnValue().Set(histogram->Stddev());
}

void HistogramBase::HistogramPercentile(
const FunctionCallbackInfo<Value>& args) {
HistogramBase* histogram;
ASSIGN_OR_RETURN_UNWRAP(&histogram, args.Holder());
CHECK(args[0]->IsNumber());
double percentile = args[0].As<Number>()->Value();
args.GetReturnValue().Set(histogram->Percentile(percentile));
}

void HistogramBase::HistogramPercentiles(
const FunctionCallbackInfo<Value>& args) {
Environment* env = Environment::GetCurrent(args);
HistogramBase* histogram;
ASSIGN_OR_RETURN_UNWRAP(&histogram, args.Holder());
CHECK(args[0]->IsMap());
Local<Map> map = args[0].As<Map>();
histogram->Percentiles([&](double key, double value) {
map->Set(
env->context(),
Number::New(env->isolate(), key),
Number::New(env->isolate(), value)).IsEmpty();
});
}

void HistogramBase::HistogramReset(const FunctionCallbackInfo<Value>& args) {
HistogramBase* histogram;
ASSIGN_OR_RETURN_UNWRAP(&histogram, args.Holder());
histogram->ResetState();
}

void HistogramBase::Initialize(Environment* env) {
// Guard against multiple initializations
if (!env->histogram_ctor_template().IsEmpty())
return;

Local<String> classname =
FIXED_ONE_BYTE_STRING(env->isolate(), "Histogram");

Local<FunctionTemplate> histogram =
FunctionTemplate::New(env->isolate());
histogram->SetClassName(classname);

Local<ObjectTemplate> histogramt =
histogram->InstanceTemplate();

histogramt->SetInternalFieldCount(1);
env->SetProtoMethod(histogram, "exceeds", HistogramExceeds);
env->SetProtoMethod(histogram, "min", HistogramMin);
env->SetProtoMethod(histogram, "max", HistogramMax);
env->SetProtoMethod(histogram, "mean", HistogramMean);
env->SetProtoMethod(histogram, "stddev", HistogramStddev);
env->SetProtoMethod(histogram, "percentile", HistogramPercentile);
env->SetProtoMethod(histogram, "percentiles", HistogramPercentiles);
env->SetProtoMethod(histogram, "reset", HistogramReset);

env->set_histogram_ctor_template(histogramt);
}

} // namespace node
Loading

0 comments on commit 930fb13

Please sign in to comment.