9
9
namespace node {
10
10
11
11
using v8::BigInt;
12
+ using v8::CFunction;
12
13
using v8::Context;
14
+ using v8::FastApiCallbackOptions;
13
15
using v8::FunctionCallbackInfo;
14
16
using v8::FunctionTemplate;
15
17
using v8::Integer;
@@ -42,7 +44,34 @@ HistogramImpl::HistogramImpl(const Histogram::Options& options)
42
44
HistogramImpl::HistogramImpl (std::shared_ptr<Histogram> histogram)
43
45
: histogram_(std::move(histogram)) {}
44
46
47
+ CFunction HistogramImpl::fast_reset_ (
48
+ CFunction::Make (&HistogramImpl::FastReset));
49
+ CFunction HistogramImpl::fast_get_count_ (
50
+ CFunction::Make (&HistogramImpl::FastGetCount));
51
+ CFunction HistogramImpl::fast_get_min_ (
52
+ CFunction::Make (&HistogramImpl::FastGetMin));
53
+ CFunction HistogramImpl::fast_get_max_ (
54
+ CFunction::Make (&HistogramImpl::FastGetMax));
55
+ CFunction HistogramImpl::fast_get_mean_ (
56
+ CFunction::Make (&HistogramImpl::FastGetMean));
57
+ CFunction HistogramImpl::fast_get_exceeds_ (
58
+ CFunction::Make (&HistogramImpl::FastGetExceeds));
59
+ CFunction HistogramImpl::fast_get_stddev_ (
60
+ CFunction::Make (&HistogramImpl::FastGetStddev));
61
+ CFunction HistogramImpl::fast_get_percentile_ (
62
+ CFunction::Make (&HistogramImpl::FastGetPercentile));
63
+ CFunction HistogramBase::fast_record_ (
64
+ CFunction::Make (&HistogramBase::FastRecord));
65
+ CFunction HistogramBase::fast_record_delta_ (
66
+ CFunction::Make (&HistogramBase::FastRecordDelta));
67
+ CFunction IntervalHistogram::fast_start_ (
68
+ CFunction::Make (&IntervalHistogram::FastStart));
69
+ CFunction IntervalHistogram::fast_stop_ (
70
+ CFunction::Make (&IntervalHistogram::FastStop));
71
+
45
72
void HistogramImpl::AddMethods (Isolate* isolate, Local<FunctionTemplate> tmpl) {
73
+ // TODO(@jasnell): The bigint API variations do not yet support fast
74
+ // variations since v8 will not return a bigint value from a fast method.
46
75
SetProtoMethodNoSideEffect (isolate, tmpl, " countBigInt" , GetCountBigInt);
47
76
SetProtoMethodNoSideEffect (isolate, tmpl, " exceedsBigInt" , GetExceedsBigInt);
48
77
SetProtoMethodNoSideEffect (isolate, tmpl, " minBigInt" , GetMinBigInt);
@@ -52,14 +81,20 @@ void HistogramImpl::AddMethods(Isolate* isolate, Local<FunctionTemplate> tmpl) {
52
81
SetProtoMethodNoSideEffect (isolate, tmpl, " percentiles" , GetPercentiles);
53
82
SetProtoMethodNoSideEffect (
54
83
isolate, tmpl, " percentilesBigInt" , GetPercentilesBigInt);
55
- SetProtoMethodNoSideEffect (isolate, tmpl, " count" , GetCount);
56
- SetProtoMethodNoSideEffect (isolate, tmpl, " exceeds" , GetExceeds);
57
- SetProtoMethodNoSideEffect (isolate, tmpl, " min" , GetMin);
58
- SetProtoMethodNoSideEffect (isolate, tmpl, " max" , GetMax);
59
- SetProtoMethodNoSideEffect (isolate, tmpl, " mean" , GetMean);
60
- SetProtoMethodNoSideEffect (isolate, tmpl, " stddev" , GetStddev);
61
- SetProtoMethodNoSideEffect (isolate, tmpl, " percentile" , GetPercentile);
62
- SetProtoMethod (isolate, tmpl, " reset" , DoReset);
84
+ auto instance = tmpl->InstanceTemplate ();
85
+ SetFastMethodNoSideEffect (
86
+ isolate, instance, " count" , GetCount, &fast_get_count_);
87
+ SetFastMethodNoSideEffect (
88
+ isolate, instance, " exceeds" , GetExceeds, &fast_get_exceeds_);
89
+ SetFastMethodNoSideEffect (isolate, instance, " min" , GetMin, &fast_get_min_);
90
+ SetFastMethodNoSideEffect (isolate, instance, " max" , GetMax, &fast_get_max_);
91
+ SetFastMethodNoSideEffect (
92
+ isolate, instance, " mean" , GetMean, &fast_get_mean_);
93
+ SetFastMethodNoSideEffect (
94
+ isolate, instance, " stddev" , GetStddev, &fast_get_stddev_);
95
+ SetFastMethodNoSideEffect (
96
+ isolate, instance, " percentile" , GetPercentile, &fast_get_percentile_);
97
+ SetFastMethod (isolate, instance, " reset" , DoReset, &fast_reset_);
63
98
}
64
99
65
100
void HistogramImpl::RegisterExternalReferences (
@@ -81,6 +116,22 @@ void HistogramImpl::RegisterExternalReferences(
81
116
registry->Register (GetPercentiles);
82
117
registry->Register (GetPercentilesBigInt);
83
118
registry->Register (DoReset);
119
+ registry->Register (fast_reset_.GetTypeInfo ());
120
+ registry->Register (fast_get_count_.GetTypeInfo ());
121
+ registry->Register (fast_get_min_.GetTypeInfo ());
122
+ registry->Register (fast_get_max_.GetTypeInfo ());
123
+ registry->Register (fast_get_mean_.GetTypeInfo ());
124
+ registry->Register (fast_get_exceeds_.GetTypeInfo ());
125
+ registry->Register (fast_get_stddev_.GetTypeInfo ());
126
+ registry->Register (fast_get_percentile_.GetTypeInfo ());
127
+ registry->Register (FastReset);
128
+ registry->Register (FastGetCount);
129
+ registry->Register (FastGetMin);
130
+ registry->Register (FastGetMax);
131
+ registry->Register (FastGetMean);
132
+ registry->Register (FastGetExceeds);
133
+ registry->Register (FastGetStddev);
134
+ registry->Register (FastGetPercentile);
84
135
is_registerd = true ;
85
136
}
86
137
@@ -118,6 +169,12 @@ void HistogramBase::RecordDelta(const FunctionCallbackInfo<Value>& args) {
118
169
(*histogram)->RecordDelta ();
119
170
}
120
171
172
+ void HistogramBase::FastRecordDelta (Local<Value> receiver) {
173
+ HistogramBase* histogram;
174
+ ASSIGN_OR_RETURN_UNWRAP (&histogram, receiver);
175
+ (*histogram)->RecordDelta ();
176
+ }
177
+
121
178
void HistogramBase::Record (const FunctionCallbackInfo<Value>& args) {
122
179
Environment* env = Environment::GetCurrent (args);
123
180
CHECK_IMPLIES (!args[0 ]->IsNumber (), args[0 ]->IsBigInt ());
@@ -132,6 +189,18 @@ void HistogramBase::Record(const FunctionCallbackInfo<Value>& args) {
132
189
(*histogram)->Record (value);
133
190
}
134
191
192
+ void HistogramBase::FastRecord (Local<Value> receiver,
193
+ const int64_t value,
194
+ FastApiCallbackOptions& options) {
195
+ if (value < 1 ) {
196
+ options.fallback = true ;
197
+ return ;
198
+ }
199
+ HistogramBase* histogram;
200
+ ASSIGN_OR_RETURN_UNWRAP (&histogram, receiver);
201
+ (*histogram)->Record (value);
202
+ }
203
+
135
204
void HistogramBase::Add (const FunctionCallbackInfo<Value>& args) {
136
205
Environment* env = Environment::GetCurrent (args);
137
206
HistogramBase* histogram;
@@ -213,8 +282,9 @@ Local<FunctionTemplate> HistogramBase::GetConstructorTemplate(
213
282
tmpl->SetClassName (classname);
214
283
auto instance = tmpl->InstanceTemplate ();
215
284
instance->SetInternalFieldCount (HistogramImpl::kInternalFieldCount );
216
- SetProtoMethod (isolate, tmpl, " record" , Record);
217
- SetProtoMethod (isolate, tmpl, " recordDelta" , RecordDelta);
285
+ SetFastMethod (isolate, instance, " record" , Record, &fast_record_);
286
+ SetFastMethod (
287
+ isolate, instance, " recordDelta" , RecordDelta, &fast_record_delta_);
218
288
SetProtoMethod (isolate, tmpl, " add" , Add);
219
289
HistogramImpl::AddMethods (isolate, tmpl);
220
290
isolate_data->set_histogram_ctor_template (tmpl);
@@ -228,6 +298,10 @@ void HistogramBase::RegisterExternalReferences(
228
298
registry->Register (Add);
229
299
registry->Register (Record);
230
300
registry->Register (RecordDelta);
301
+ registry->Register (fast_record_.GetTypeInfo ());
302
+ registry->Register (fast_record_delta_.GetTypeInfo ());
303
+ registry->Register (FastRecord);
304
+ registry->Register (FastRecordDelta);
231
305
HistogramImpl::RegisterExternalReferences (registry);
232
306
}
233
307
@@ -264,11 +338,11 @@ Local<FunctionTemplate> IntervalHistogram::GetConstructorTemplate(
264
338
tmpl = NewFunctionTemplate (isolate, nullptr );
265
339
tmpl->Inherit (HandleWrap::GetConstructorTemplate (env));
266
340
tmpl->SetClassName (OneByteString (isolate, " Histogram" ));
267
- tmpl->InstanceTemplate ()-> SetInternalFieldCount (
268
- HistogramImpl::kInternalFieldCount );
341
+ auto instance = tmpl->InstanceTemplate ();
342
+ instance-> SetInternalFieldCount ( HistogramImpl::kInternalFieldCount );
269
343
HistogramImpl::AddMethods (isolate, tmpl);
270
- SetProtoMethod (isolate, tmpl , " start" , Start);
271
- SetProtoMethod (isolate, tmpl , " stop" , Stop);
344
+ SetFastMethod (isolate, instance , " start" , Start, &fast_start_ );
345
+ SetFastMethod (isolate, instance , " stop" , Stop, &fast_stop_ );
272
346
env->set_intervalhistogram_constructor_template (tmpl);
273
347
}
274
348
return tmpl;
@@ -278,6 +352,10 @@ void IntervalHistogram::RegisterExternalReferences(
278
352
ExternalReferenceRegistry* registry) {
279
353
registry->Register (Start);
280
354
registry->Register (Stop);
355
+ registry->Register (fast_start_.GetTypeInfo ());
356
+ registry->Register (fast_stop_.GetTypeInfo ());
357
+ registry->Register (FastStart);
358
+ registry->Register (FastStop);
281
359
HistogramImpl::RegisterExternalReferences (registry);
282
360
}
283
361
@@ -358,12 +436,24 @@ void IntervalHistogram::Start(const FunctionCallbackInfo<Value>& args) {
358
436
histogram->OnStart (args[0 ]->IsTrue () ? StartFlags::RESET : StartFlags::NONE);
359
437
}
360
438
439
+ void IntervalHistogram::FastStart (Local<Value> receiver, bool reset) {
440
+ IntervalHistogram* histogram;
441
+ ASSIGN_OR_RETURN_UNWRAP (&histogram, receiver);
442
+ histogram->OnStart (reset ? StartFlags::RESET : StartFlags::NONE);
443
+ }
444
+
361
445
void IntervalHistogram::Stop (const FunctionCallbackInfo<Value>& args) {
362
446
IntervalHistogram* histogram;
363
447
ASSIGN_OR_RETURN_UNWRAP (&histogram, args.Holder ());
364
448
histogram->OnStop ();
365
449
}
366
450
451
+ void IntervalHistogram::FastStop (Local<Value> receiver) {
452
+ IntervalHistogram* histogram;
453
+ ASSIGN_OR_RETURN_UNWRAP (&histogram, receiver);
454
+ histogram->OnStop ();
455
+ }
456
+
367
457
void HistogramImpl::GetCount (const FunctionCallbackInfo<Value>& args) {
368
458
HistogramImpl* histogram = HistogramImpl::FromJSObject (args.Holder ());
369
459
double value = static_cast <double >((*histogram)->Count ());
@@ -474,6 +564,47 @@ void HistogramImpl::DoReset(const FunctionCallbackInfo<Value>& args) {
474
564
(*histogram)->Reset ();
475
565
}
476
566
567
+ void HistogramImpl::FastReset (Local<Value> receiver) {
568
+ HistogramImpl* histogram = HistogramImpl::FromJSObject (receiver);
569
+ (*histogram)->Reset ();
570
+ }
571
+
572
+ double HistogramImpl::FastGetCount (Local<Value> receiver) {
573
+ HistogramImpl* histogram = HistogramImpl::FromJSObject (receiver);
574
+ return static_cast <double >((*histogram)->Count ());
575
+ }
576
+
577
+ double HistogramImpl::FastGetMin (Local<Value> receiver) {
578
+ HistogramImpl* histogram = HistogramImpl::FromJSObject (receiver);
579
+ return static_cast <double >((*histogram)->Min ());
580
+ }
581
+
582
+ double HistogramImpl::FastGetMax (Local<Value> receiver) {
583
+ HistogramImpl* histogram = HistogramImpl::FromJSObject (receiver);
584
+ return static_cast <double >((*histogram)->Max ());
585
+ }
586
+
587
+ double HistogramImpl::FastGetMean (Local<Value> receiver) {
588
+ HistogramImpl* histogram = HistogramImpl::FromJSObject (receiver);
589
+ return (*histogram)->Mean ();
590
+ }
591
+
592
+ double HistogramImpl::FastGetExceeds (Local<Value> receiver) {
593
+ HistogramImpl* histogram = HistogramImpl::FromJSObject (receiver);
594
+ return static_cast <double >((*histogram)->Exceeds ());
595
+ }
596
+
597
+ double HistogramImpl::FastGetStddev (Local<Value> receiver) {
598
+ HistogramImpl* histogram = HistogramImpl::FromJSObject (receiver);
599
+ return (*histogram)->Stddev ();
600
+ }
601
+
602
+ double HistogramImpl::FastGetPercentile (Local<Value> receiver,
603
+ const double percentile) {
604
+ HistogramImpl* histogram = HistogramImpl::FromJSObject (receiver);
605
+ return static_cast <double >((*histogram)->Percentile (percentile));
606
+ }
607
+
477
608
HistogramImpl* HistogramImpl::FromJSObject (Local<Value> value) {
478
609
auto obj = value.As <Object>();
479
610
DCHECK_GE (obj->InternalFieldCount (), HistogramImpl::kInternalFieldCount );
0 commit comments