Skip to content

Commit 54753f2

Browse files
bnoordhuistargos
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 <anna@addaleax.net> Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Sam Roberts <vieuxtech@gmail.com> Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
1 parent a20bf75 commit 54753f2

File tree

2 files changed

+16
-4
lines changed

2 files changed

+16
-4
lines changed

src/env.h

+2
Original file line numberDiff line numberDiff line change
@@ -188,11 +188,13 @@ constexpr size_t kFsStatsBufferLength = kFsStatsFieldsNumber * 2;
188188
V(get_data_clone_error_string, "_getDataCloneError") \
189189
V(get_shared_array_buffer_id_string, "_getSharedArrayBufferId") \
190190
V(gid_string, "gid") \
191+
V(h2_string, "h2") \
191192
V(handle_string, "handle") \
192193
V(help_text_string, "helpText") \
193194
V(homedir_string, "homedir") \
194195
V(host_string, "host") \
195196
V(hostmaster_string, "hostmaster") \
197+
V(http_1_1_string, "http/1.1") \
196198
V(ignore_string, "ignore") \
197199
V(infoaccess_string, "infoAccess") \
198200
V(inherit_string, "inherit") \

src/node_crypto.cc

+14-4
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ using v8::DontDelete;
6363
using v8::EscapableHandleScope;
6464
using v8::Exception;
6565
using v8::External;
66+
using v8::False;
6667
using v8::Function;
6768
using v8::FunctionCallback;
6869
using v8::FunctionCallbackInfo;
@@ -2400,11 +2401,20 @@ void SSLWrap<Base>::GetALPNNegotiatedProto(
24002401

24012402
SSL_get0_alpn_selected(w->ssl_.get(), &alpn_proto, &alpn_proto_len);
24022403

2403-
if (!alpn_proto)
2404-
return args.GetReturnValue().Set(false);
2404+
Local<Value> result;
2405+
if (alpn_proto_len == 0) {
2406+
result = False(args.GetIsolate());
2407+
} else if (alpn_proto_len == sizeof("h2") - 1 &&
2408+
0 == memcmp(alpn_proto, "h2", sizeof("h2") - 1)) {
2409+
result = w->env()->h2_string();
2410+
} else if (alpn_proto_len == sizeof("http/1.1") - 1 &&
2411+
0 == memcmp(alpn_proto, "http/1.1", sizeof("http/1.1") - 1)) {
2412+
result = w->env()->http_1_1_string();
2413+
} else {
2414+
result = OneByteString(args.GetIsolate(), alpn_proto, alpn_proto_len);
2415+
}
24052416

2406-
args.GetReturnValue().Set(
2407-
OneByteString(args.GetIsolate(), alpn_proto, alpn_proto_len));
2417+
args.GetReturnValue().Set(result);
24082418
}
24092419

24102420

0 commit comments

Comments
 (0)