99namespace node {
1010
1111using v8::BigInt;
12+ using v8::CFunction;
1213using v8::Context;
14+ using v8::FastApiCallbackOptions;
1315using v8::FunctionCallbackInfo;
1416using v8::FunctionTemplate;
1517using v8::Integer;
@@ -42,7 +44,34 @@ HistogramImpl::HistogramImpl(const Histogram::Options& options)
4244HistogramImpl::HistogramImpl(std::shared_ptr<Histogram> histogram)
4345 : histogram_(std::move(histogram)) {}
4446
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+
4572void 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.
4675 SetProtoMethodNoSideEffect(isolate, tmpl, "countBigInt", GetCountBigInt);
4776 SetProtoMethodNoSideEffect(isolate, tmpl, "exceedsBigInt", GetExceedsBigInt);
4877 SetProtoMethodNoSideEffect(isolate, tmpl, "minBigInt", GetMinBigInt);
@@ -52,14 +81,20 @@ void HistogramImpl::AddMethods(Isolate* isolate, Local<FunctionTemplate> tmpl) {
5281 SetProtoMethodNoSideEffect(isolate, tmpl, "percentiles", GetPercentiles);
5382 SetProtoMethodNoSideEffect(
5483 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_);
6398}
6499
65100void HistogramImpl::RegisterExternalReferences(
@@ -81,6 +116,22 @@ void HistogramImpl::RegisterExternalReferences(
81116 registry->Register(GetPercentiles);
82117 registry->Register(GetPercentilesBigInt);
83118 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);
84135 is_registerd = true;
85136}
86137
@@ -118,6 +169,12 @@ void HistogramBase::RecordDelta(const FunctionCallbackInfo<Value>& args) {
118169 (*histogram)->RecordDelta();
119170}
120171
172+ void HistogramBase::FastRecordDelta(Local<Value> receiver) {
173+ HistogramBase* histogram;
174+ ASSIGN_OR_RETURN_UNWRAP(&histogram, receiver);
175+ (*histogram)->RecordDelta();
176+ }
177+
121178void HistogramBase::Record(const FunctionCallbackInfo<Value>& args) {
122179 Environment* env = Environment::GetCurrent(args);
123180 CHECK_IMPLIES(!args[0]->IsNumber(), args[0]->IsBigInt());
@@ -132,6 +189,18 @@ void HistogramBase::Record(const FunctionCallbackInfo<Value>& args) {
132189 (*histogram)->Record(value);
133190}
134191
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+
135204void HistogramBase::Add(const FunctionCallbackInfo<Value>& args) {
136205 Environment* env = Environment::GetCurrent(args);
137206 HistogramBase* histogram;
@@ -213,8 +282,9 @@ Local<FunctionTemplate> HistogramBase::GetConstructorTemplate(
213282 tmpl->SetClassName(classname);
214283 auto instance = tmpl->InstanceTemplate();
215284 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_);
218288 SetProtoMethod(isolate, tmpl, "add", Add);
219289 HistogramImpl::AddMethods(isolate, tmpl);
220290 isolate_data->set_histogram_ctor_template(tmpl);
@@ -228,6 +298,10 @@ void HistogramBase::RegisterExternalReferences(
228298 registry->Register(Add);
229299 registry->Register(Record);
230300 registry->Register(RecordDelta);
301+ registry->Register(fast_record_.GetTypeInfo());
302+ registry->Register(fast_record_delta_.GetTypeInfo());
303+ registry->Register(FastRecord);
304+ registry->Register(FastRecordDelta);
231305 HistogramImpl::RegisterExternalReferences(registry);
232306}
233307
@@ -264,11 +338,11 @@ Local<FunctionTemplate> IntervalHistogram::GetConstructorTemplate(
264338 tmpl = NewFunctionTemplate(isolate, nullptr);
265339 tmpl->Inherit(HandleWrap::GetConstructorTemplate(env));
266340 tmpl->SetClassName(OneByteString(isolate, "Histogram"));
267- tmpl->InstanceTemplate()->SetInternalFieldCount(
268- HistogramImpl::kInternalFieldCount);
341+ auto instance = tmpl->InstanceTemplate();
342+ instance->SetInternalFieldCount( HistogramImpl::kInternalFieldCount);
269343 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_ );
272346 env->set_intervalhistogram_constructor_template(tmpl);
273347 }
274348 return tmpl;
@@ -278,6 +352,10 @@ void IntervalHistogram::RegisterExternalReferences(
278352 ExternalReferenceRegistry* registry) {
279353 registry->Register(Start);
280354 registry->Register(Stop);
355+ registry->Register(fast_start_.GetTypeInfo());
356+ registry->Register(fast_stop_.GetTypeInfo());
357+ registry->Register(FastStart);
358+ registry->Register(FastStop);
281359 HistogramImpl::RegisterExternalReferences(registry);
282360}
283361
@@ -358,12 +436,24 @@ void IntervalHistogram::Start(const FunctionCallbackInfo<Value>& args) {
358436 histogram->OnStart(args[0]->IsTrue() ? StartFlags::RESET : StartFlags::NONE);
359437}
360438
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+
361445void IntervalHistogram::Stop(const FunctionCallbackInfo<Value>& args) {
362446 IntervalHistogram* histogram;
363447 ASSIGN_OR_RETURN_UNWRAP(&histogram, args.Holder());
364448 histogram->OnStop();
365449}
366450
451+ void IntervalHistogram::FastStop(Local<Value> receiver) {
452+ IntervalHistogram* histogram;
453+ ASSIGN_OR_RETURN_UNWRAP(&histogram, receiver);
454+ histogram->OnStop();
455+ }
456+
367457void HistogramImpl::GetCount(const FunctionCallbackInfo<Value>& args) {
368458 HistogramImpl* histogram = HistogramImpl::FromJSObject(args.Holder());
369459 double value = static_cast<double>((*histogram)->Count());
@@ -474,6 +564,47 @@ void HistogramImpl::DoReset(const FunctionCallbackInfo<Value>& args) {
474564 (*histogram)->Reset();
475565}
476566
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+
477608HistogramImpl* HistogramImpl::FromJSObject(Local<Value> value) {
478609 auto obj = value.As<Object>();
479610 DCHECK_GE(obj->InternalFieldCount(), HistogramImpl::kInternalFieldCount);
0 commit comments