Skip to content

Commit

Permalink
http2: use static allocated arrays
Browse files Browse the repository at this point in the history
Backport-PR-URL: #14813
Backport-Reviewed-By: Anna Henningsen <[email protected]>
Backport-Reviewed-By: Timothy Gu <[email protected]>

PR-URL: #14239
Reviewed-By: Anna Henningsen <[email protected]>
Reviewed-By: Colin Ihrig <[email protected]>
Reviewed-By: Matteo Collina <[email protected]>
  • Loading branch information
jasnell authored and addaleax committed Aug 14, 2017
1 parent 9a4be4a commit 174ab6f
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 127 deletions.
55 changes: 0 additions & 55 deletions src/env-inl.h
Original file line number Diff line number Diff line change
Expand Up @@ -331,11 +331,6 @@ inline Environment::~Environment() {
delete[] heap_space_statistics_buffer_;
delete[] http_parser_buffer_;
delete[] http2_socket_buffer_;
delete[] http2_settings_buffer_;
delete[] http2_options_buffer_;
delete[] http2_session_state_buffer_;
delete[] http2_stream_state_buffer_;
delete[] http2_padding_buffer_;
}

inline v8::Isolate* Environment::isolate() const {
Expand Down Expand Up @@ -476,56 +471,6 @@ inline void Environment::set_heap_space_statistics_buffer(double* pointer) {
heap_space_statistics_buffer_ = pointer;
}

inline uint32_t* Environment::http2_settings_buffer() const {
CHECK_NE(http2_settings_buffer_, nullptr);
return http2_settings_buffer_;
}

inline void Environment::set_http2_settings_buffer(uint32_t* pointer) {
CHECK_EQ(http2_settings_buffer_, nullptr); // Should be set only once
http2_settings_buffer_ = pointer;
}

inline uint32_t* Environment::http2_options_buffer() const {
CHECK_NE(http2_options_buffer_, nullptr);
return http2_options_buffer_;
}

inline void Environment::set_http2_options_buffer(uint32_t* pointer) {
CHECK_EQ(http2_options_buffer_, nullptr); // Should be set only once
http2_options_buffer_ = pointer;
}

inline double* Environment::http2_session_state_buffer() const {
CHECK_NE(http2_session_state_buffer_, nullptr);
return http2_session_state_buffer_;
}

inline void Environment::set_http2_session_state_buffer(double* pointer) {
CHECK_EQ(http2_session_state_buffer_, nullptr);
http2_session_state_buffer_ = pointer;
}

inline double* Environment::http2_stream_state_buffer() const {
CHECK_NE(http2_stream_state_buffer_, nullptr);
return http2_stream_state_buffer_;
}

inline void Environment::set_http2_stream_state_buffer(double* pointer) {
CHECK_EQ(http2_stream_state_buffer_, nullptr);
http2_stream_state_buffer_ = pointer;
}

inline uint32_t* Environment::http2_padding_buffer() const {
CHECK_NE(http2_padding_buffer_, nullptr);
return http2_padding_buffer_;
}

inline void Environment::set_http2_padding_buffer(uint32_t* pointer) {
CHECK_EQ(http2_padding_buffer_, nullptr);
http2_padding_buffer_ = pointer;
}

inline char* Environment::http_parser_buffer() const {
return http_parser_buffer_;
}
Expand Down
20 changes: 0 additions & 20 deletions src/env.h
Original file line number Diff line number Diff line change
Expand Up @@ -596,21 +596,6 @@ class Environment {
inline double* heap_space_statistics_buffer() const;
inline void set_heap_space_statistics_buffer(double* pointer);

inline uint32_t* http2_settings_buffer() const;
inline void set_http2_settings_buffer(uint32_t* pointer);

inline uint32_t* http2_options_buffer() const;
inline void set_http2_options_buffer(uint32_t* pointer);

inline double* http2_session_state_buffer() const;
inline void set_http2_session_state_buffer(double* pointer);

inline double* http2_stream_state_buffer() const;
inline void set_http2_stream_state_buffer(double* pointer);

inline uint32_t* http2_padding_buffer() const;
inline void set_http2_padding_buffer(uint32_t* pointer);

inline char* http_parser_buffer() const;
inline void set_http_parser_buffer(char* buffer);
inline char* http2_socket_buffer() const;
Expand Down Expand Up @@ -720,11 +705,6 @@ class Environment {

double* heap_statistics_buffer_ = nullptr;
double* heap_space_statistics_buffer_ = nullptr;
uint32_t* http2_settings_buffer_ = nullptr;
uint32_t* http2_options_buffer_ = nullptr;
double* http2_session_state_buffer_ = nullptr;
double* http2_stream_state_buffer_ = nullptr;
uint32_t* http2_padding_buffer_ = nullptr;

char* http_parser_buffer_;
char* http2_socket_buffer_;
Expand Down
82 changes: 30 additions & 52 deletions src/node_http2.cc
Original file line number Diff line number Diff line change
Expand Up @@ -55,10 +55,27 @@ enum Http2OptionsIndex {
IDX_OPTIONS_FLAGS
};

static uint32_t http2_padding_buffer[3];
static uint32_t http2_options_buffer[IDX_OPTIONS_FLAGS + 1];
static uint32_t http2_settings_buffer[IDX_SETTINGS_COUNT + 1];
static double http2_session_state_buffer[IDX_SESSION_STATE_COUNT];
static double http2_stream_state_buffer[IDX_STREAM_STATE_COUNT];

static const size_t http2_options_buffer_byte_length =
sizeof(http2_options_buffer) * (IDX_OPTIONS_FLAGS + 1);
static const size_t http2_settings_buffer_byte_length =
sizeof(http2_settings_buffer) * (IDX_SETTINGS_COUNT + 1);
static const size_t http2_padding_buffer_byte_length =
sizeof(http2_padding_buffer) * 3;
static const size_t http2_stream_state_buffer_byte_length =
sizeof(http2_stream_state_buffer) * IDX_STREAM_STATE_COUNT;
static const size_t http2_session_state_buffer_byte_length =
sizeof(http2_session_state_buffer) * IDX_SESSION_STATE_COUNT;

Http2Options::Http2Options(Environment* env) {
nghttp2_option_new(&options_);

uint32_t* buffer = env->http2_options_buffer();
uint32_t* buffer = http2_options_buffer;
uint32_t flags = buffer[IDX_OPTIONS_FLAGS];

if (flags & (1 << IDX_OPTIONS_MAX_DEFLATE_DYNAMIC_TABLE_SIZE)) {
Expand Down Expand Up @@ -148,7 +165,7 @@ ssize_t Http2Session::OnCallbackPadding(size_t frameLen,
Context::Scope context_scope(context);

if (object()->Has(context, env()->ongetpadding_string()).FromJust()) {
uint32_t* buffer = env()->http2_padding_buffer();
uint32_t* buffer = http2_padding_buffer;
buffer[0] = frameLen;
buffer[1] = maxPayloadLen;
MakeCallback(env()->ongetpadding_string(), 0, nullptr);
Expand Down Expand Up @@ -189,7 +206,7 @@ void PackSettings(const FunctionCallbackInfo<Value>& args) {
std::vector<nghttp2_settings_entry> entries;
entries.reserve(6);

uint32_t* const buffer = env->http2_settings_buffer();
uint32_t* buffer = http2_settings_buffer;
uint32_t flags = buffer[IDX_SETTINGS_COUNT];

if (flags & (1 << IDX_SETTINGS_HEADER_TABLE_SIZE)) {
Expand Down Expand Up @@ -248,8 +265,7 @@ void PackSettings(const FunctionCallbackInfo<Value>& args) {
// Used to fill in the spec defined initial values for each setting.
void RefreshDefaultSettings(const FunctionCallbackInfo<Value>& args) {
DEBUG_HTTP2("Http2Session: refreshing default settings\n");
Environment* env = Environment::GetCurrent(args);
uint32_t* const buffer = env->http2_settings_buffer();
uint32_t* buffer = http2_settings_buffer;
buffer[IDX_SETTINGS_HEADER_TABLE_SIZE] =
DEFAULT_SETTINGS_HEADER_TABLE_SIZE;
buffer[IDX_SETTINGS_ENABLE_PUSH] =
Expand All @@ -272,10 +288,9 @@ void RefreshSettings(const FunctionCallbackInfo<Value>& args) {
CHECK(args[0]->IsObject());
Http2Session* session;
ASSIGN_OR_RETURN_UNWRAP(&session, args[0].As<Object>());
Environment* env = session->env();
nghttp2_session* s = session->session();

uint32_t* const buffer = env->http2_settings_buffer();
uint32_t* buffer = http2_settings_buffer;
buffer[IDX_SETTINGS_HEADER_TABLE_SIZE] =
fn(s, NGHTTP2_SETTINGS_HEADER_TABLE_SIZE);
buffer[IDX_SETTINGS_MAX_CONCURRENT_STREAMS] =
Expand All @@ -295,8 +310,7 @@ void RefreshSessionState(const FunctionCallbackInfo<Value>& args) {
DEBUG_HTTP2("Http2Session: refreshing session state\n");
CHECK_EQ(args.Length(), 1);
CHECK(args[0]->IsObject());
Environment* env = Environment::GetCurrent(args);
double* const buffer = env->http2_session_state_buffer();
double* buffer = http2_session_state_buffer;
Http2Session* session;
ASSIGN_OR_RETURN_UNWRAP(&session, args[0].As<Object>());
nghttp2_session* s = session->session();
Expand Down Expand Up @@ -333,7 +347,7 @@ void RefreshStreamState(const FunctionCallbackInfo<Value>& args) {
nghttp2_session* s = session->session();
Nghttp2Stream* stream;

double* const buffer = env->http2_stream_state_buffer();
double* buffer = http2_stream_state_buffer;

if ((stream = session->FindStream(id)) == nullptr) {
buffer[IDX_STREAM_STATE] = NGHTTP2_STREAM_STATE_IDLE;
Expand Down Expand Up @@ -438,9 +452,7 @@ void Http2Session::SubmitSettings(const FunctionCallbackInfo<Value>& args) {
Http2Session* session;
ASSIGN_OR_RETURN_UNWRAP(&session, args.Holder());

Environment* env = session->env();

uint32_t* const buffer = env->http2_settings_buffer();
uint32_t* buffer = http2_settings_buffer;
uint32_t flags = buffer[IDX_SETTINGS_COUNT];

std::vector<nghttp2_settings_entry> entries;
Expand Down Expand Up @@ -1107,74 +1119,40 @@ void Initialize(Local<Object> target,
HandleScope scope(isolate);

// Initialize the buffer used for padding callbacks
env->set_http2_padding_buffer(new uint32_t[3]);
const size_t http2_padding_buffer_byte_length =
sizeof(*env->http2_padding_buffer()) * 3;

target->Set(context,
FIXED_ONE_BYTE_STRING(env->isolate(), "paddingArrayBuffer"),
ArrayBuffer::New(env->isolate(),
env->http2_padding_buffer(),
&http2_padding_buffer,
http2_padding_buffer_byte_length))
.FromJust();

// Initialize the buffer used to store the session state
env->set_http2_session_state_buffer(
new double[IDX_SESSION_STATE_COUNT]);

const size_t http2_session_state_buffer_byte_length =
sizeof(*env->http2_session_state_buffer()) *
IDX_SESSION_STATE_COUNT;

target->Set(context,
FIXED_ONE_BYTE_STRING(env->isolate(), "sessionStateArrayBuffer"),
ArrayBuffer::New(env->isolate(),
env->http2_session_state_buffer(),
&http2_session_state_buffer,
http2_session_state_buffer_byte_length))
.FromJust();

// Initialize the buffer used to store the stream state
env->set_http2_stream_state_buffer(
new double[IDX_STREAM_STATE_COUNT]);

const size_t http2_stream_state_buffer_byte_length =
sizeof(*env->http2_stream_state_buffer()) *
IDX_STREAM_STATE_COUNT;

target->Set(context,
FIXED_ONE_BYTE_STRING(env->isolate(), "streamStateArrayBuffer"),
ArrayBuffer::New(env->isolate(),
env->http2_stream_state_buffer(),
&http2_stream_state_buffer,
http2_stream_state_buffer_byte_length))
.FromJust();

// Initialize the buffer used to store the current settings
env->set_http2_settings_buffer(
new uint32_t[IDX_SETTINGS_COUNT + 1]);

const size_t http2_settings_buffer_byte_length =
sizeof(*env->http2_settings_buffer()) *
(IDX_SETTINGS_COUNT + 1);

target->Set(context,
FIXED_ONE_BYTE_STRING(env->isolate(), "settingsArrayBuffer"),
ArrayBuffer::New(env->isolate(),
env->http2_settings_buffer(),
&http2_settings_buffer,
http2_settings_buffer_byte_length))
.FromJust();

// Initialize the buffer used to store the options
env->set_http2_options_buffer(
new uint32_t[IDX_OPTIONS_FLAGS + 1]);

const size_t http2_options_buffer_byte_length =
sizeof(*env->http2_options_buffer()) *
(IDX_OPTIONS_FLAGS + 1);

target->Set(context,
FIXED_ONE_BYTE_STRING(env->isolate(), "optionsArrayBuffer"),
ArrayBuffer::New(env->isolate(),
env->http2_options_buffer(),
&http2_options_buffer,
http2_options_buffer_byte_length))
.FromJust();

Expand Down

0 comments on commit 174ab6f

Please sign in to comment.