Skip to content

Commit b4f58c2

Browse files
bnoordhuisTrott
authored andcommitted
src: micro-optimize ALPN negotiation
99 out of a 100 times (conservative estimate!) the negotiated protocol will be either "h2" or "http/1.1" so reuse an existing JS string for those instead of creating a new one every time. PR-URL: #26836 Reviewed-By: Anna Henningsen <[email protected]> Reviewed-By: James M Snell <[email protected]> Reviewed-By: Sam Roberts <[email protected]> Reviewed-By: Colin Ihrig <[email protected]>
1 parent 3f41fcb commit b4f58c2

File tree

2 files changed

+16
-4
lines changed

2 files changed

+16
-4
lines changed

src/env.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -193,11 +193,13 @@ constexpr size_t kFsStatsBufferLength = kFsStatsFieldsNumber * 2;
193193
V(get_data_clone_error_string, "_getDataCloneError") \
194194
V(get_shared_array_buffer_id_string, "_getSharedArrayBufferId") \
195195
V(gid_string, "gid") \
196+
V(h2_string, "h2") \
196197
V(handle_string, "handle") \
197198
V(help_text_string, "helpText") \
198199
V(homedir_string, "homedir") \
199200
V(host_string, "host") \
200201
V(hostmaster_string, "hostmaster") \
202+
V(http_1_1_string, "http/1.1") \
201203
V(ignore_string, "ignore") \
202204
V(infoaccess_string, "infoAccess") \
203205
V(inherit_string, "inherit") \

src/node_crypto.cc

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ using v8::DontDelete;
6565
using v8::EscapableHandleScope;
6666
using v8::Exception;
6767
using v8::External;
68+
using v8::False;
6869
using v8::Function;
6970
using v8::FunctionCallback;
7071
using v8::FunctionCallbackInfo;
@@ -2503,11 +2504,20 @@ void SSLWrap<Base>::GetALPNNegotiatedProto(
25032504

25042505
SSL_get0_alpn_selected(w->ssl_.get(), &alpn_proto, &alpn_proto_len);
25052506

2506-
if (!alpn_proto)
2507-
return args.GetReturnValue().Set(false);
2507+
Local<Value> result;
2508+
if (alpn_proto_len == 0) {
2509+
result = False(args.GetIsolate());
2510+
} else if (alpn_proto_len == sizeof("h2") - 1 &&
2511+
0 == memcmp(alpn_proto, "h2", sizeof("h2") - 1)) {
2512+
result = w->env()->h2_string();
2513+
} else if (alpn_proto_len == sizeof("http/1.1") - 1 &&
2514+
0 == memcmp(alpn_proto, "http/1.1", sizeof("http/1.1") - 1)) {
2515+
result = w->env()->http_1_1_string();
2516+
} else {
2517+
result = OneByteString(args.GetIsolate(), alpn_proto, alpn_proto_len);
2518+
}
25082519

2509-
args.GetReturnValue().Set(
2510-
OneByteString(args.GetIsolate(), alpn_proto, alpn_proto_len));
2520+
args.GetReturnValue().Set(result);
25112521
}
25122522

25132523

0 commit comments

Comments
 (0)