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

Commit 930fb13

Browse files
committed
src: cleanup histogram code
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]>
1 parent 30eb6e9 commit 930fb13

File tree

4 files changed

+151
-150
lines changed

4 files changed

+151
-150
lines changed

node.gyp

+1
Original file line numberDiff line numberDiff line change
@@ -515,6 +515,7 @@
515515
'src/fs_event_wrap.cc',
516516
'src/handle_wrap.cc',
517517
'src/heap_utils.cc',
518+
'src/histogram.cc',
518519
'src/js_native_api.h',
519520
'src/js_native_api_types.h',
520521
'src/js_native_api_v8.cc',

src/histogram-inl.h

+15-111
Original file line numberDiff line numberDiff line change
@@ -9,45 +9,46 @@
99

1010
namespace node {
1111

12-
inline Histogram::Histogram(int64_t lowest, int64_t highest, int figures) {
12+
Histogram::Histogram(int64_t lowest, int64_t highest, int figures) {
1313
CHECK_EQ(0, hdr_init(lowest, highest, figures, &histogram_));
1414
}
1515

16-
inline Histogram::~Histogram() {
16+
Histogram::~Histogram() {
1717
hdr_close(histogram_);
1818
}
1919

20-
inline void Histogram::Reset() {
20+
void Histogram::Reset() {
2121
hdr_reset(histogram_);
2222
}
2323

24-
inline bool Histogram::Record(int64_t value) {
24+
bool Histogram::Record(int64_t value) {
2525
return hdr_record_value(histogram_, value);
2626
}
2727

28-
inline int64_t Histogram::Min() {
28+
int64_t Histogram::Min() {
2929
return hdr_min(histogram_);
3030
}
3131

32-
inline int64_t Histogram::Max() {
32+
int64_t Histogram::Max() {
3333
return hdr_max(histogram_);
3434
}
3535

36-
inline double Histogram::Mean() {
36+
double Histogram::Mean() {
3737
return hdr_mean(histogram_);
3838
}
3939

40-
inline double Histogram::Stddev() {
40+
double Histogram::Stddev() {
4141
return hdr_stddev(histogram_);
4242
}
4343

44-
inline double Histogram::Percentile(double percentile) {
44+
double Histogram::Percentile(double percentile) {
4545
CHECK_GT(percentile, 0);
4646
CHECK_LE(percentile, 100);
4747
return static_cast<double>(hdr_value_at_percentile(histogram_, percentile));
4848
}
4949

50-
inline void Histogram::Percentiles(std::function<void(double, double)> fn) {
50+
template <typename Iterator>
51+
void Histogram::Percentiles(Iterator&& fn) {
5152
hdr_iter iter;
5253
hdr_iter_percentile_init(&iter, histogram_, 1);
5354
while (hdr_iter_next(&iter)) {
@@ -57,7 +58,7 @@ inline void Histogram::Percentiles(std::function<void(double, double)> fn) {
5758
}
5859
}
5960

60-
inline HistogramBase::HistogramBase(
61+
HistogramBase::HistogramBase(
6162
Environment* env,
6263
v8::Local<v8::Object> wrap,
6364
int64_t lowest,
@@ -66,7 +67,7 @@ inline HistogramBase::HistogramBase(
6667
BaseObject(env, wrap),
6768
Histogram(lowest, highest, figures) {}
6869

69-
inline bool HistogramBase::RecordDelta() {
70+
bool HistogramBase::RecordDelta() {
7071
uint64_t time = uv_hrtime();
7172
bool ret = true;
7273
if (prev_ > 0) {
@@ -85,110 +86,13 @@ inline bool HistogramBase::RecordDelta() {
8586
return ret;
8687
}
8788

88-
inline void HistogramBase::ResetState() {
89+
void HistogramBase::ResetState() {
8990
Reset();
9091
exceeds_ = 0;
9192
prev_ = 0;
9293
}
9394

94-
inline void HistogramBase::HistogramMin(
95-
const v8::FunctionCallbackInfo<v8::Value>& args) {
96-
HistogramBase* histogram;
97-
ASSIGN_OR_RETURN_UNWRAP(&histogram, args.Holder());
98-
double value = static_cast<double>(histogram->Min());
99-
args.GetReturnValue().Set(value);
100-
}
101-
102-
inline void HistogramBase::HistogramMax(
103-
const v8::FunctionCallbackInfo<v8::Value>& args) {
104-
HistogramBase* histogram;
105-
ASSIGN_OR_RETURN_UNWRAP(&histogram, args.Holder());
106-
double value = static_cast<double>(histogram->Max());
107-
args.GetReturnValue().Set(value);
108-
}
109-
110-
inline void HistogramBase::HistogramMean(
111-
const v8::FunctionCallbackInfo<v8::Value>& args) {
112-
HistogramBase* histogram;
113-
ASSIGN_OR_RETURN_UNWRAP(&histogram, args.Holder());
114-
args.GetReturnValue().Set(histogram->Mean());
115-
}
116-
117-
inline void HistogramBase::HistogramExceeds(
118-
const v8::FunctionCallbackInfo<v8::Value>& args) {
119-
HistogramBase* histogram;
120-
ASSIGN_OR_RETURN_UNWRAP(&histogram, args.Holder());
121-
double value = static_cast<double>(histogram->Exceeds());
122-
args.GetReturnValue().Set(value);
123-
}
124-
125-
inline void HistogramBase::HistogramStddev(
126-
const v8::FunctionCallbackInfo<v8::Value>& args) {
127-
HistogramBase* histogram;
128-
ASSIGN_OR_RETURN_UNWRAP(&histogram, args.Holder());
129-
args.GetReturnValue().Set(histogram->Stddev());
130-
}
131-
132-
inline void HistogramBase::HistogramPercentile(
133-
const v8::FunctionCallbackInfo<v8::Value>& args) {
134-
HistogramBase* histogram;
135-
ASSIGN_OR_RETURN_UNWRAP(&histogram, args.Holder());
136-
CHECK(args[0]->IsNumber());
137-
double percentile = args[0].As<v8::Number>()->Value();
138-
args.GetReturnValue().Set(histogram->Percentile(percentile));
139-
}
140-
141-
inline void HistogramBase::HistogramPercentiles(
142-
const v8::FunctionCallbackInfo<v8::Value>& args) {
143-
Environment* env = Environment::GetCurrent(args);
144-
HistogramBase* histogram;
145-
ASSIGN_OR_RETURN_UNWRAP(&histogram, args.Holder());
146-
CHECK(args[0]->IsMap());
147-
v8::Local<v8::Map> map = args[0].As<v8::Map>();
148-
histogram->Percentiles([&](double key, double value) {
149-
map->Set(
150-
env->context(),
151-
v8::Number::New(env->isolate(), key),
152-
v8::Number::New(env->isolate(), value)).IsEmpty();
153-
});
154-
}
155-
156-
inline void HistogramBase::HistogramReset(
157-
const v8::FunctionCallbackInfo<v8::Value>& args) {
158-
HistogramBase* histogram;
159-
ASSIGN_OR_RETURN_UNWRAP(&histogram, args.Holder());
160-
histogram->ResetState();
161-
}
162-
163-
inline void HistogramBase::Initialize(Environment* env) {
164-
// Guard against multiple initializations
165-
if (!env->histogram_ctor_template().IsEmpty())
166-
return;
167-
168-
v8::Local<v8::String> classname =
169-
FIXED_ONE_BYTE_STRING(env->isolate(), "Histogram");
170-
171-
v8::Local<v8::FunctionTemplate> histogram =
172-
v8::FunctionTemplate::New(env->isolate());
173-
histogram->SetClassName(classname);
174-
175-
v8::Local<v8::ObjectTemplate> histogramt =
176-
histogram->InstanceTemplate();
177-
178-
histogramt->SetInternalFieldCount(1);
179-
env->SetProtoMethod(histogram, "exceeds", HistogramExceeds);
180-
env->SetProtoMethod(histogram, "min", HistogramMin);
181-
env->SetProtoMethod(histogram, "max", HistogramMax);
182-
env->SetProtoMethod(histogram, "mean", HistogramMean);
183-
env->SetProtoMethod(histogram, "stddev", HistogramStddev);
184-
env->SetProtoMethod(histogram, "percentile", HistogramPercentile);
185-
env->SetProtoMethod(histogram, "percentiles", HistogramPercentiles);
186-
env->SetProtoMethod(histogram, "reset", HistogramReset);
187-
188-
env->set_histogram_ctor_template(histogramt);
189-
}
190-
191-
inline HistogramBase* HistogramBase::New(
95+
HistogramBase* HistogramBase::New(
19296
Environment* env,
19397
int64_t lowest,
19498
int64_t highest,

src/histogram.cc

+111
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
#include "histogram.h" // NOLINT(build/include_inline)
2+
#include "histogram-inl.h"
3+
#include "memory_tracker-inl.h"
4+
5+
namespace node {
6+
7+
using v8::FunctionCallbackInfo;
8+
using v8::FunctionTemplate;
9+
using v8::Local;
10+
using v8::Map;
11+
using v8::Number;
12+
using v8::ObjectTemplate;
13+
using v8::String;
14+
using v8::Value;
15+
16+
void HistogramBase::MemoryInfo(MemoryTracker* tracker) const {
17+
tracker->TrackFieldWithSize("histogram", GetMemorySize());
18+
}
19+
20+
void HistogramBase::HistogramMin(const FunctionCallbackInfo<Value>& args) {
21+
HistogramBase* histogram;
22+
ASSIGN_OR_RETURN_UNWRAP(&histogram, args.Holder());
23+
double value = static_cast<double>(histogram->Min());
24+
args.GetReturnValue().Set(value);
25+
}
26+
27+
void HistogramBase::HistogramMax(const FunctionCallbackInfo<Value>& args) {
28+
HistogramBase* histogram;
29+
ASSIGN_OR_RETURN_UNWRAP(&histogram, args.Holder());
30+
double value = static_cast<double>(histogram->Max());
31+
args.GetReturnValue().Set(value);
32+
}
33+
34+
void HistogramBase::HistogramMean(const FunctionCallbackInfo<Value>& args) {
35+
HistogramBase* histogram;
36+
ASSIGN_OR_RETURN_UNWRAP(&histogram, args.Holder());
37+
args.GetReturnValue().Set(histogram->Mean());
38+
}
39+
40+
void HistogramBase::HistogramExceeds(const FunctionCallbackInfo<Value>& args) {
41+
HistogramBase* histogram;
42+
ASSIGN_OR_RETURN_UNWRAP(&histogram, args.Holder());
43+
double value = static_cast<double>(histogram->Exceeds());
44+
args.GetReturnValue().Set(value);
45+
}
46+
47+
void HistogramBase::HistogramStddev(const FunctionCallbackInfo<Value>& args) {
48+
HistogramBase* histogram;
49+
ASSIGN_OR_RETURN_UNWRAP(&histogram, args.Holder());
50+
args.GetReturnValue().Set(histogram->Stddev());
51+
}
52+
53+
void HistogramBase::HistogramPercentile(
54+
const FunctionCallbackInfo<Value>& args) {
55+
HistogramBase* histogram;
56+
ASSIGN_OR_RETURN_UNWRAP(&histogram, args.Holder());
57+
CHECK(args[0]->IsNumber());
58+
double percentile = args[0].As<Number>()->Value();
59+
args.GetReturnValue().Set(histogram->Percentile(percentile));
60+
}
61+
62+
void HistogramBase::HistogramPercentiles(
63+
const FunctionCallbackInfo<Value>& args) {
64+
Environment* env = Environment::GetCurrent(args);
65+
HistogramBase* histogram;
66+
ASSIGN_OR_RETURN_UNWRAP(&histogram, args.Holder());
67+
CHECK(args[0]->IsMap());
68+
Local<Map> map = args[0].As<Map>();
69+
histogram->Percentiles([&](double key, double value) {
70+
map->Set(
71+
env->context(),
72+
Number::New(env->isolate(), key),
73+
Number::New(env->isolate(), value)).IsEmpty();
74+
});
75+
}
76+
77+
void HistogramBase::HistogramReset(const FunctionCallbackInfo<Value>& args) {
78+
HistogramBase* histogram;
79+
ASSIGN_OR_RETURN_UNWRAP(&histogram, args.Holder());
80+
histogram->ResetState();
81+
}
82+
83+
void HistogramBase::Initialize(Environment* env) {
84+
// Guard against multiple initializations
85+
if (!env->histogram_ctor_template().IsEmpty())
86+
return;
87+
88+
Local<String> classname =
89+
FIXED_ONE_BYTE_STRING(env->isolate(), "Histogram");
90+
91+
Local<FunctionTemplate> histogram =
92+
FunctionTemplate::New(env->isolate());
93+
histogram->SetClassName(classname);
94+
95+
Local<ObjectTemplate> histogramt =
96+
histogram->InstanceTemplate();
97+
98+
histogramt->SetInternalFieldCount(1);
99+
env->SetProtoMethod(histogram, "exceeds", HistogramExceeds);
100+
env->SetProtoMethod(histogram, "min", HistogramMin);
101+
env->SetProtoMethod(histogram, "max", HistogramMax);
102+
env->SetProtoMethod(histogram, "mean", HistogramMean);
103+
env->SetProtoMethod(histogram, "stddev", HistogramStddev);
104+
env->SetProtoMethod(histogram, "percentile", HistogramPercentile);
105+
env->SetProtoMethod(histogram, "percentiles", HistogramPercentiles);
106+
env->SetProtoMethod(histogram, "reset", HistogramReset);
107+
108+
env->set_histogram_ctor_template(histogramt);
109+
}
110+
111+
} // namespace node

0 commit comments

Comments
 (0)