Skip to content

Commit 553e34e

Browse files
addaleaxrvagg
authored andcommitted
src: simplify http2 perf tracking code
Use `unique_ptr`s and use the resulting simplification to reduce indentation in these functions. PR-URL: #19470 Reviewed-By: Colin Ihrig <cjihrig@gmail.com> Reviewed-By: Tiancheng "Timothy" Gu <timothygu99@gmail.com> Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Tobias Nießen <tniessen@tnie.de>
1 parent a29cd25 commit 553e34e

File tree

1 file changed

+48
-47
lines changed

1 file changed

+48
-47
lines changed

src/node_http2.cc

Lines changed: 48 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -567,35 +567,35 @@ inline void Http2Stream::EmitStatistics() {
567567
Http2StreamPerformanceEntry* entry =
568568
new Http2StreamPerformanceEntry(env(), id_, statistics_);
569569
env()->SetImmediate([](Environment* env, void* data) {
570-
Http2StreamPerformanceEntry* entry =
571-
static_cast<Http2StreamPerformanceEntry*>(data);
572-
if (HasHttp2Observer(env)) {
573-
AliasedBuffer<double, v8::Float64Array>& buffer =
574-
env->http2_state()->stream_stats_buffer;
575-
buffer[IDX_STREAM_STATS_ID] = entry->id();
576-
if (entry->first_byte() != 0) {
577-
buffer[IDX_STREAM_STATS_TIMETOFIRSTBYTE] =
578-
(entry->first_byte() - entry->startTimeNano()) / 1e6;
579-
} else {
580-
buffer[IDX_STREAM_STATS_TIMETOFIRSTBYTE] = 0;
581-
}
582-
if (entry->first_header() != 0) {
583-
buffer[IDX_STREAM_STATS_TIMETOFIRSTHEADER] =
584-
(entry->first_header() - entry->startTimeNano()) / 1e6;
585-
} else {
586-
buffer[IDX_STREAM_STATS_TIMETOFIRSTHEADER] = 0;
587-
}
588-
if (entry->first_byte_sent() != 0) {
589-
buffer[IDX_STREAM_STATS_TIMETOFIRSTBYTESENT] =
590-
(entry->first_byte_sent() - entry->startTimeNano()) / 1e6;
591-
} else {
592-
buffer[IDX_STREAM_STATS_TIMETOFIRSTBYTESENT] = 0;
593-
}
594-
buffer[IDX_STREAM_STATS_SENTBYTES] = entry->sent_bytes();
595-
buffer[IDX_STREAM_STATS_RECEIVEDBYTES] = entry->received_bytes();
596-
entry->Notify(entry->ToObject());
570+
// This takes ownership, the entr is destroyed at the end of this scope.
571+
std::unique_ptr<Http2StreamPerformanceEntry> entry {
572+
static_cast<Http2StreamPerformanceEntry*>(data) };
573+
if (!HasHttp2Observer(env))
574+
return;
575+
AliasedBuffer<double, v8::Float64Array>& buffer =
576+
env->http2_state()->stream_stats_buffer;
577+
buffer[IDX_STREAM_STATS_ID] = entry->id();
578+
if (entry->first_byte() != 0) {
579+
buffer[IDX_STREAM_STATS_TIMETOFIRSTBYTE] =
580+
(entry->first_byte() - entry->startTimeNano()) / 1e6;
581+
} else {
582+
buffer[IDX_STREAM_STATS_TIMETOFIRSTBYTE] = 0;
597583
}
598-
delete entry;
584+
if (entry->first_header() != 0) {
585+
buffer[IDX_STREAM_STATS_TIMETOFIRSTHEADER] =
586+
(entry->first_header() - entry->startTimeNano()) / 1e6;
587+
} else {
588+
buffer[IDX_STREAM_STATS_TIMETOFIRSTHEADER] = 0;
589+
}
590+
if (entry->first_byte_sent() != 0) {
591+
buffer[IDX_STREAM_STATS_TIMETOFIRSTBYTESENT] =
592+
(entry->first_byte_sent() - entry->startTimeNano()) / 1e6;
593+
} else {
594+
buffer[IDX_STREAM_STATS_TIMETOFIRSTBYTESENT] = 0;
595+
}
596+
buffer[IDX_STREAM_STATS_SENTBYTES] = entry->sent_bytes();
597+
buffer[IDX_STREAM_STATS_RECEIVEDBYTES] = entry->received_bytes();
598+
entry->Notify(entry->ToObject());
599599
}, static_cast<void*>(entry));
600600
}
601601

@@ -605,25 +605,25 @@ inline void Http2Session::EmitStatistics() {
605605
Http2SessionPerformanceEntry* entry =
606606
new Http2SessionPerformanceEntry(env(), statistics_, session_type_);
607607
env()->SetImmediate([](Environment* env, void* data) {
608-
Http2SessionPerformanceEntry* entry =
609-
static_cast<Http2SessionPerformanceEntry*>(data);
610-
if (HasHttp2Observer(env)) {
611-
AliasedBuffer<double, v8::Float64Array>& buffer =
612-
env->http2_state()->session_stats_buffer;
613-
buffer[IDX_SESSION_STATS_TYPE] = entry->type();
614-
buffer[IDX_SESSION_STATS_PINGRTT] = entry->ping_rtt() / 1e6;
615-
buffer[IDX_SESSION_STATS_FRAMESRECEIVED] = entry->frame_count();
616-
buffer[IDX_SESSION_STATS_FRAMESSENT] = entry->frame_sent();
617-
buffer[IDX_SESSION_STATS_STREAMCOUNT] = entry->stream_count();
618-
buffer[IDX_SESSION_STATS_STREAMAVERAGEDURATION] =
619-
entry->stream_average_duration();
620-
buffer[IDX_SESSION_STATS_DATA_SENT] = entry->data_sent();
621-
buffer[IDX_SESSION_STATS_DATA_RECEIVED] = entry->data_received();
622-
buffer[IDX_SESSION_STATS_MAX_CONCURRENT_STREAMS] =
623-
entry->max_concurrent_streams();
624-
entry->Notify(entry->ToObject());
625-
}
626-
delete entry;
608+
// This takes ownership, the entr is destroyed at the end of this scope.
609+
std::unique_ptr<Http2SessionPerformanceEntry> entry {
610+
static_cast<Http2SessionPerformanceEntry*>(data) };
611+
if (!HasHttp2Observer(env))
612+
return;
613+
AliasedBuffer<double, v8::Float64Array>& buffer =
614+
env->http2_state()->session_stats_buffer;
615+
buffer[IDX_SESSION_STATS_TYPE] = entry->type();
616+
buffer[IDX_SESSION_STATS_PINGRTT] = entry->ping_rtt() / 1e6;
617+
buffer[IDX_SESSION_STATS_FRAMESRECEIVED] = entry->frame_count();
618+
buffer[IDX_SESSION_STATS_FRAMESSENT] = entry->frame_sent();
619+
buffer[IDX_SESSION_STATS_STREAMCOUNT] = entry->stream_count();
620+
buffer[IDX_SESSION_STATS_STREAMAVERAGEDURATION] =
621+
entry->stream_average_duration();
622+
buffer[IDX_SESSION_STATS_DATA_SENT] = entry->data_sent();
623+
buffer[IDX_SESSION_STATS_DATA_RECEIVED] = entry->data_received();
624+
buffer[IDX_SESSION_STATS_MAX_CONCURRENT_STREAMS] =
625+
entry->max_concurrent_streams();
626+
entry->Notify(entry->ToObject());
627627
}, static_cast<void*>(entry));
628628
}
629629

@@ -1410,6 +1410,7 @@ void Http2Session::MaybeScheduleWrite() {
14101410

14111411
// Sending data may call arbitrary JS code, so keep track of
14121412
// async context.
1413+
HandleScope handle_scope(env->isolate());
14131414
InternalCallbackScope callback_scope(session);
14141415
session->SendPendingData();
14151416
}, static_cast<void*>(this), object());

0 commit comments

Comments
 (0)