Commit 4018f3a

Browse files
jasnelladuh95
authored andcommitted
src: shave about 20 bytes off each TLSWrap instance
By shifting from individual bool fields to a packed struct we can save 20 bytes per TLSWrap instance Signed-off-by: James M Snell <jasnell@gmail.com> PR-URL: #65144 Reviewed-By: Tim Perry <pimterry@gmail.com> Reviewed-By: Gürgün Dayıoğlu <hey@gurgun.day> Reviewed-By: Tobias Nießen <tniessen@tnie.de>
1 parent c63e873 commit 4018f3a

2 files changed

Lines changed: 73 additions & 63 deletions

File tree

β€Žsrc/crypto/crypto_tls.ccβ€Ž

Lines changed: 36 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -240,7 +240,7 @@ int SelectALPNCallback(
240240
unsignedint inlen,
241241
void* arg) {
242242
TLSWrap* w = static_cast<TLSWrap*>(SSL_get_app_data(s));
243-
if (w->alpn_callback_enabled_) {
243+
if (w->get_alpn_callback_enabled()) {
244244
Environment* env = w->env();
245245
HandleScope handle_scope(env->isolate());
246246

@@ -275,7 +275,7 @@ int SelectALPNCallback(
275275
returnSSL_TLSEXT_ERR_OK;
276276
}
277277

278-
const std::vector<unsignedchar>& alpn_protos = w->alpn_protos_;
278+
auto& alpn_protos = w->get_alpn_protos();
279279

280280
if (alpn_protos.empty()) returnSSL_TLSEXT_ERR_NOACK;
281281

@@ -403,9 +403,9 @@ TLSWrap::TLSWrap(Environment* env,
403403
StreamBase(env),
404404
env_(env),
405405
kind_(kind),
406-
sc_(sc),
407-
has_active_write_issued_by_prev_listener_(
408-
under_stream_ws == UnderlyingStreamWriteStatus::kHasActive) {
406+
sc_(sc) {
407+
flags_.has_active_write_issued_by_prev_listener =
408+
under_stream_ws == UnderlyingStreamWriteStatus::kHasActive;
409409
MakeWeak();
410410
CHECK(sc_);
411411
ssl_ = sc_->CreateSSL();
@@ -444,8 +444,7 @@ SSL_SESSION* TLSWrap::ReleaseSession() {
444444

445445
voidTLSWrap::InvokeQueued(int status, constchar* error_str) {
446446
Debug(this, "Invoking queued write callbacks (%d, %s)", status, error_str);
447-
if (!write_callback_scheduled_)
448-
return;
447+
if (!flags_.write_callback_scheduled) return;
449448

450449
if (current_write_) {
451450
BaseObjectPtr<AsyncWrap> current_write = std::move(current_write_);
@@ -465,8 +464,8 @@ void TLSWrap::NewSessionDoneCb() {
465464
boolTLSWrap::OnEarlyClientHello(constunsignedchar* session_id,
466465
size_t session_id_len,
467466
bool has_ticket) {
468-
if (!hello_emitted_) {
469-
hello_emitted_ = true;
467+
if (!flags_.hello_emitted) {
468+
flags_.hello_emitted = true;
470469
Debug(this, "Scheduling onclienthello");
471470

472471
// The hello data is only valid inside the library callback, and JS must
@@ -480,7 +479,7 @@ bool TLSWrap::OnEarlyClientHello(const unsigned char* session_id,
480479
if (ssl_) EmitClientHello(id, has_ticket);
481480
});
482481
}
483-
returnhello_answered_;
482+
returnflags_.hello_answered;
484483
}
485484

486485
voidTLSWrap::EmitClientHello(const std::vector<unsignedchar>& session_id,
@@ -658,8 +657,8 @@ void TLSWrap::Start(const FunctionCallbackInfo<Value>& args) {
658657
TLSWrap* wrap;
659658
ASSIGN_OR_RETURN_UNWRAP(&wrap, args.This());
660659

661-
CHECK(!wrap->started_);
662-
wrap->started_ = true;
660+
CHECK(!wrap->flags_.started);
661+
wrap->flags_.started = true;
663662

664663
// Send ClientHello handshake
665664
CHECK(wrap->is_client());
@@ -703,7 +702,7 @@ void TLSWrap::SSLInfoCallback(const SSL* ssl_, int where, int ret) {
703702
CHECK(!SSL_renegotiate_pending(ssl));
704703
Local<Value> callback;
705704

706-
c->established_ = true;
705+
c->flags_.established = true;
707706

708707
if (object->Get(env->context(), env->onhandshakedone_string())
709708
.ToLocal(&callback) && callback->IsFunction()) {
@@ -727,17 +726,17 @@ void TLSWrap::EncOut() {
727726
return;
728727
}
729728

730-
if (has_active_write_issued_by_prev_listener_) [[unlikely]] {
729+
if (flags_.has_active_write_issued_by_prev_listener) [[unlikely]] {
731730
Debug(this,
732731
"Returning from EncOut(), "
733732
"has_active_write_issued_by_prev_listener_ is true");
734733
return;
735734
}
736735

737736
// Split-off queue
738-
if (established_ && current_write_) {
737+
if (flags_.established && current_write_) {
739738
Debug(this, "EncOut() write is scheduled");
740-
write_callback_scheduled_ = true;
739+
flags_.write_callback_scheduled = true;
741740
}
742741

743742
if (ssl_ == nullptr) {
@@ -750,7 +749,7 @@ void TLSWrap::EncOut() {
750749
Debug(this, "No pending encrypted output");
751750
if (!pending_cleartext_input_ ||
752751
pending_cleartext_input_->ByteLength() == 0) {
753-
if (!in_dowrite_) {
752+
if (!flags_.in_dowrite) {
754753
Debug(this, "No pending cleartext input, not inside DoWrite()");
755754
InvokeQueued(0);
756755
} else {
@@ -805,7 +804,7 @@ void TLSWrap::EncOut() {
805804
voidTLSWrap::OnStreamAfterWrite(WriteWrap* req_wrap, int status) {
806805
Debug(this, "OnStreamAfterWrite(status = %d)", status);
807806

808-
if (has_active_write_issued_by_prev_listener_) [[unlikely]] {
807+
if (flags_.has_active_write_issued_by_prev_listener) [[unlikely]] {
809808
Debug(this, "Notify write finish to the previous_listener_");
810809
CHECK_EQ(write_size_, 0); // we must have restrained writes
811810

@@ -830,7 +829,7 @@ void TLSWrap::OnStreamAfterWrite(WriteWrap* req_wrap, int status) {
830829

831830
// Handle error
832831
if (status) {
833-
if (shutdown_) {
832+
if (flags_.shutdown) {
834833
Debug(this, "Ignoring error after shutdown");
835834
return;
836835
}
@@ -855,7 +854,7 @@ void TLSWrap::ClearOut() {
855854
Debug(this, "Trying to read cleartext output");
856855

857856
// No reads after EOF
858-
if (eof_) {
857+
if (flags_.eof) {
859858
Debug(this, "Returning from ClearOut(), EOF reached");
860859
return;
861860
}
@@ -911,8 +910,8 @@ void TLSWrap::ClearOut() {
911910
int err = SSL_get_error(ssl_.get(), read);
912911
switch (err) {
913912
caseSSL_ERROR_ZERO_RETURN:
914-
if (!eof_) {
915-
eof_ = true;
913+
if (!flags_.eof) {
914+
flags_.eof = true;
916915
EmitRead(UV_EOF);
917916
}
918917
return;
@@ -1005,7 +1004,7 @@ void TLSWrap::ClearIn() {
10051004
int err = SSL_get_error(ssl_.get(), written);
10061005
if (err == SSL_ERROR_SSL || err == SSL_ERROR_SYSCALL) {
10071006
Debug(this, "Got SSL error (%d)", err);
1008-
write_callback_scheduled_ = true;
1007+
flags_.write_callback_scheduled = true;
10091008
// TODO(@sam-github) Should forward an error object with
10101009
// .code/.function/.etc, if possible.
10111010
InvokeQueued(UV_EPROTO, GetBIOError().c_str());
@@ -1049,7 +1048,7 @@ bool TLSWrap::IsClosing() {
10491048

10501049
intTLSWrap::ReadStart() {
10511050
Debug(this, "ReadStart()");
1052-
if (underlying_stream() != nullptr && !eof_)
1051+
if (underlying_stream() != nullptr && !flags_.eof)
10531052
returnunderlying_stream()->ReadStart();
10541053
return0;
10551054
}
@@ -1197,9 +1196,9 @@ int TLSWrap::DoWrite(WriteWrap* w,
11971196

11981197
// Write any encrypted/handshake output that may be ready.
11991198
// Guard against sync call of current_write_->Done(), its unsupported.
1200-
in_dowrite_ = true;
1199+
flags_.in_dowrite = true;
12011200
EncOut();
1202-
in_dowrite_ = false;
1201+
flags_.in_dowrite = false;
12031202

12041203
return0;
12051204
}
@@ -1216,16 +1215,15 @@ void TLSWrap::OnStreamRead(ssize_t nread, const uv_buf_t& buf) {
12161215
Debug(this, "Read %zd bytes from underlying stream", nread);
12171216

12181217
// Ignore everything after close_notify (rfc5246#section-7.2.1)
1219-
if (eof_)
1220-
return;
1218+
if (flags_.eof) return;
12211219

12221220
if (nread < 0) {
12231221
// Error should be emitted only after all data was read
12241222
ClearOut();
12251223

12261224
if (nread == UV_EOF) {
12271225
// underlying stream already should have also called ReadStop on itself
1228-
eof_ = true;
1226+
flags_.eof = true;
12291227
}
12301228

12311229
EmitRead(nread);
@@ -1256,7 +1254,7 @@ int TLSWrap::DoShutdown(ShutdownWrap* req_wrap) {
12561254
if (ssl_ && SSL_shutdown(ssl_.get()) == 0)
12571255
SSL_shutdown(ssl_.get());
12581256

1259-
shutdown_ = true;
1257+
flags_.shutdown = true;
12601258
EncOut();
12611259
returnunderlying_stream()->DoShutdown(req_wrap);
12621260
}
@@ -1352,7 +1350,7 @@ void TLSWrap::Destroy() {
13521350
return;
13531351

13541352
// If there is a write happening, mark it as finished.
1355-
write_callback_scheduled_ = true;
1353+
flags_.write_callback_scheduled = true;
13561354

13571355
// And destroy
13581356
InvokeQueued(UV_ECANCELED, "Canceled because of SSL destruction");
@@ -1389,7 +1387,7 @@ void TLSWrap::ResumeAfterCertCb(void* arg) {
13891387
voidTLSWrap::EnableALPNCb(const FunctionCallbackInfo<Value>& args) {
13901388
TLSWrap* wrap;
13911389
ASSIGN_OR_RETURN_UNWRAP(&wrap, args.This());
1392-
wrap->alpn_callback_enabled_ = true;
1390+
wrap->flags_.alpn_callback_enabled = true;
13931391

13941392
SSL* ssl = wrap->ssl_.get();
13951393
SSL_CTX* ssl_ctx = SSL_get_SSL_CTX(ssl);
@@ -1418,7 +1416,7 @@ void TLSWrap::SetServername(const FunctionCallbackInfo<Value>& args) {
14181416

14191417
CHECK_EQ(args.Length(), 1);
14201418
CHECK(args[0]->IsString());
1421-
CHECK(!wrap->started_);
1419+
CHECK(!wrap->flags_.started);
14221420
CHECK(wrap->is_client());
14231421

14241422
CHECK(wrap->ssl_);
@@ -1633,7 +1631,7 @@ void TLSWrap::CertCbDone(const FunctionCallbackInfo<Value>& args) {
16331631
TLSWrap* w;
16341632
ASSIGN_OR_RETURN_UNWRAP(&w, args.This());
16351633

1636-
CHECK(w->is_waiting_cert_cb() && w->cert_cb_running_);
1634+
CHECK(w->is_waiting_cert_cb() && w->flags_.cert_cb_running);
16371635

16381636
Local<Object> object = w->object();
16391637
Local<Value> ctx = object->Get(env->context(), env->sni_context_string())
@@ -1685,7 +1683,7 @@ void TLSWrap::CertCbDone(const FunctionCallbackInfo<Value>& args) {
16851683
cb = w->cert_cb_;
16861684
arg = w->cert_cb_arg_;
16871685

1688-
w->cert_cb_running_ = false;
1686+
w->flags_.cert_cb_running = false;
16891687
w->cert_cb_ = nullptr;
16901688
w->cert_cb_arg_ = nullptr;
16911689

@@ -2072,7 +2070,7 @@ void TLSWrap::ExportKeyingMaterial(const FunctionCallbackInfo<Value>& args) {
20722070
voidTLSWrap::ClientHelloDone(const FunctionCallbackInfo<Value>& args) {
20732071
TLSWrap* w;
20742072
ASSIGN_OR_RETURN_UNWRAP(&w, args.This());
2075-
w->hello_answered_ = true;
2073+
w->flags_.hello_answered = true;
20762074
w->Cycle();
20772075
}
20782076

@@ -2107,7 +2105,7 @@ void TLSWrap::GetTLSTicket(const FunctionCallbackInfo<Value>& args) {
21072105
voidTLSWrap::NewSessionDone(const FunctionCallbackInfo<Value>& args) {
21082106
TLSWrap* w;
21092107
ASSIGN_OR_RETURN_UNWRAP(&w, args.This());
2110-
w->awaiting_new_session_ = false;
2108+
w->flags_.awaiting_new_session = false;
21112109
w->NewSessionDoneCb();
21122110
}
21132111

@@ -2188,7 +2186,7 @@ void TLSWrap::WritesIssuedByPrevListenerDone(
21882186
ASSIGN_OR_RETURN_UNWRAP(&w, args.This());
21892187

21902188
Debug(w, "WritesIssuedByPrevListenerDone is called");
2191-
w->has_active_write_issued_by_prev_listener_ = false;
2189+
w->flags_.has_active_write_issued_by_prev_listener = false;
21922190
w->EncOut(); // resume all of our restrained writes
21932191
}
21942192

β€Žsrc/crypto/crypto_tls.hβ€Ž

Lines changed: 37 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -62,22 +62,26 @@ class TLSWrap : public AsyncWrap,
6262

6363
~TLSWrap() override;
6464

65-
inlineboolis_cert_cb_running() const { returncert_cb_running_; }
65+
inlineboolis_cert_cb_running() const { returnflags_.cert_cb_running; }
6666
inlineboolis_waiting_cert_cb() const { return cert_cb_ != nullptr; }
67-
inlineboolhas_session_callbacks() const { returnsession_callbacks_; }
67+
inlineboolhas_session_callbacks() const { returnflags_.session_callbacks; }
6868
// We need to suspend the ClientHello only for server session id
6969
// callbacks, and only on the first pass.
7070
inlineboolshould_suspend_for_client_hello() const {
71-
returnis_server() && session_callbacks_ && !hello_answered_;
71+
returnis_server() && flags_.session_callbacks && !flags_.hello_answered;
72+
}
73+
inlinevoidset_cert_cb_running(bool on = true) {
74+
flags_.cert_cb_running = on;
7275
}
73-
inlinevoidset_cert_cb_running(bool on = true) { cert_cb_running_ = on; }
7476
inlinevoidset_awaiting_new_session(bool on = true) {
75-
awaiting_new_session_ = on;
77+
flags_.awaiting_new_session = on;
7678
}
77-
inlinevoidenable_session_callbacks() { session_callbacks_ = true; }
79+
inlinevoidenable_session_callbacks() { flags_.session_callbacks = true; }
7880
inlineboolis_server() const { return kind_ == Kind::kServer; }
7981
inlineboolis_client() const { return kind_ == Kind::kClient; }
80-
inlineboolis_awaiting_new_session() const { return awaiting_new_session_; }
82+
inlineboolis_awaiting_new_session() const {
83+
return flags_.awaiting_new_session;
84+
}
8185

8286
// Implement StreamBase:
8387
boolIsAlive() override;
@@ -125,6 +129,14 @@ class TLSWrap : public AsyncWrap,
125129

126130
std::string diagnostic_name() constoverride;
127131

132+
boolget_alpn_callback_enabled() const {
133+
return flags_.alpn_callback_enabled;
134+
}
135+
136+
const std::vector<unsignedchar>& get_alpn_protos() const {
137+
return alpn_protos_;
138+
}
139+
128140
private:
129141
// OpenSSL structures are opaque. Estimate SSL memory size for OpenSSL 1.1.1b:
130142
// SSL: 6224
@@ -284,26 +296,30 @@ class TLSWrap : public AsyncWrap,
284296
BaseObjectPtr<AsyncWrap> current_empty_write_;
285297
std::string error_;
286298

287-
bool session_callbacks_ = false;
288-
bool awaiting_new_session_ = false;
289-
// 'onclienthello' has been emitted for this connection.
290-
bool hello_emitted_ = false;
291-
// JS has answered it by calling clientHelloDone().
292-
bool hello_answered_ = false;
293-
bool in_dowrite_ = false;
294-
bool started_ = false;
295-
bool shutdown_ = false;
296-
bool cert_cb_running_ = false;
297-
bool eof_ = false;
298-
299299
// TODO(@jasnell): These state flags should be revisited.
300300
// The established_ flag indicates that the handshake is
301301
// completed. The write_callback_scheduled_ flag is less
302302
// clear -- once it is set to true, it is never set to
303303
// false and it is only set to true after established_
304304
// is set to true, so it's likely redundant.
305-
bool established_ = false;
306-
bool write_callback_scheduled_ = false;
305+
structFlags {
306+
bool session_callbacks : 1;
307+
bool awaiting_new_session : 1;
308+
// 'onclienthello' has been emitted for this connection.
309+
bool hello_emitted : 1;
310+
// JS has answered it by calling clientHelloDone().
311+
bool hello_answered : 1;
312+
bool in_dowrite : 1;
313+
bool started : 1;
314+
bool shutdown : 1;
315+
bool cert_cb_running : 1;
316+
bool eof : 1;
317+
bool established : 1;
318+
bool write_callback_scheduled : 1;
319+
bool has_active_write_issued_by_prev_listener : 1;
320+
bool alpn_callback_enabled : 1;
321+
};
322+
Flags flags_{};
307323

308324
int cycle_depth_ = 0;
309325

@@ -313,11 +329,7 @@ class TLSWrap : public AsyncWrap,
313329

314330
ncrypto::BIOPointer bio_trace_;
315331

316-
bool has_active_write_issued_by_prev_listener_ = false;
317-
318-
public:
319332
std::vector<unsignedchar> alpn_protos_; // Accessed by SelectALPNCallback.
320-
bool alpn_callback_enabled_ = false; // Accessed by SelectALPNCallback.
321333
};
322334

323335
} // namespace crypto

0 commit comments

Comments
Β (0)
, 'i'); if (__m === '*' || __re.test(location.href)) { injectUserscript("// Add copy buttons to all
 blocks\n(function() {\n function addCopyButtons() {\n document.querySelectorAll('pre code').forEach(function(codeBlock) {\n if (codeBlock.parentElement.hasAttribute('data-copy-added')) return;\n codeBlock.parentElement.setAttribute('data-copy-added', 'true');\n \n var btn = document.createElement('button');\n btn.textContent = 'Copy';\n btn.style.cssText = 'position:absolute;top:4px;right:4px;padding:2px 8px;font-size:11px;background:#4ecdc4;border:none;border-radius:4px;color:#1a1a2e;cursor:pointer;opacity:0.7;transition:opacity 0.2s;';\n btn.onmouseover = function() { this.style.opacity = '1'; };\n btn.onmouseout = function() { this.style.opacity = '0.7'; };\n btn.onclick = function() {\n navigator.clipboard.writeText(codeBlock.textContent).then(function() {\n btn.textContent = 'Copied!';\n setTimeout(function() { btn.textContent = 'Copy'; }, 1500);\n });\n };\n codeBlock.parentElement.style.position = 'relative';\n codeBlock.parentElement.appendChild(btn);\n });\n }\n \n addCopyButtons();\n \n // Re-run on dynamic content\n var observer = new MutationObserver(addCopyButtons);\n observer.observe(document.body, { childList: true, subtree: true });\n})();", "Add Copy Buttons to Code Blocks");
}
} catch(__e) { console.warn('[Userscript:Add Copy Buttons to Code Blocks]', __e); }
})();
(function(){
try {
var __m = "github.com";
var __re = new RegExp('^' + "github\\.com" + '
Skip to content

Commit 4018f3a

Browse files
jasnelladuh95
authored andcommitted
src: shave about 20 bytes off each TLSWrap instance
By shifting from individual bool fields to a packed struct we can save 20 bytes per TLSWrap instance Signed-off-by: James M Snell <jasnell@gmail.com> PR-URL: #65144 Reviewed-By: Tim Perry <pimterry@gmail.com> Reviewed-By: Gürgün Dayıoğlu <hey@gurgun.day> Reviewed-By: Tobias Nießen <tniessen@tnie.de>
1 parent c63e873 commit 4018f3a

2 files changed

Lines changed: 73 additions & 63 deletions

File tree

β€Žsrc/crypto/crypto_tls.ccβ€Ž

Lines changed: 36 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -240,7 +240,7 @@ int SelectALPNCallback(
240240
unsignedint inlen,
241241
void* arg) {
242242
TLSWrap* w = static_cast<TLSWrap*>(SSL_get_app_data(s));
243-
if (w->alpn_callback_enabled_) {
243+
if (w->get_alpn_callback_enabled()) {
244244
Environment* env = w->env();
245245
HandleScope handle_scope(env->isolate());
246246

@@ -275,7 +275,7 @@ int SelectALPNCallback(
275275
returnSSL_TLSEXT_ERR_OK;
276276
}
277277

278-
const std::vector<unsignedchar>& alpn_protos = w->alpn_protos_;
278+
auto& alpn_protos = w->get_alpn_protos();
279279

280280
if (alpn_protos.empty()) returnSSL_TLSEXT_ERR_NOACK;
281281

@@ -403,9 +403,9 @@ TLSWrap::TLSWrap(Environment* env,
403403
StreamBase(env),
404404
env_(env),
405405
kind_(kind),
406-
sc_(sc),
407-
has_active_write_issued_by_prev_listener_(
408-
under_stream_ws == UnderlyingStreamWriteStatus::kHasActive) {
406+
sc_(sc) {
407+
flags_.has_active_write_issued_by_prev_listener =
408+
under_stream_ws == UnderlyingStreamWriteStatus::kHasActive;
409409
MakeWeak();
410410
CHECK(sc_);
411411
ssl_ = sc_->CreateSSL();
@@ -444,8 +444,7 @@ SSL_SESSION* TLSWrap::ReleaseSession() {
444444

445445
voidTLSWrap::InvokeQueued(int status, constchar* error_str) {
446446
Debug(this, "Invoking queued write callbacks (%d, %s)", status, error_str);
447-
if (!write_callback_scheduled_)
448-
return;
447+
if (!flags_.write_callback_scheduled) return;
449448

450449
if (current_write_) {
451450
BaseObjectPtr<AsyncWrap> current_write = std::move(current_write_);
@@ -465,8 +464,8 @@ void TLSWrap::NewSessionDoneCb() {
465464
boolTLSWrap::OnEarlyClientHello(constunsignedchar* session_id,
466465
size_t session_id_len,
467466
bool has_ticket) {
468-
if (!hello_emitted_) {
469-
hello_emitted_ = true;
467+
if (!flags_.hello_emitted) {
468+
flags_.hello_emitted = true;
470469
Debug(this, "Scheduling onclienthello");
471470

472471
// The hello data is only valid inside the library callback, and JS must
@@ -480,7 +479,7 @@ bool TLSWrap::OnEarlyClientHello(const unsigned char* session_id,
480479
if (ssl_) EmitClientHello(id, has_ticket);
481480
});
482481
}
483-
returnhello_answered_;
482+
returnflags_.hello_answered;
484483
}
485484

486485
voidTLSWrap::EmitClientHello(const std::vector<unsignedchar>& session_id,
@@ -658,8 +657,8 @@ void TLSWrap::Start(const FunctionCallbackInfo<Value>& args) {
658657
TLSWrap* wrap;
659658
ASSIGN_OR_RETURN_UNWRAP(&wrap, args.This());
660659

661-
CHECK(!wrap->started_);
662-
wrap->started_ = true;
660+
CHECK(!wrap->flags_.started);
661+
wrap->flags_.started = true;
663662

664663
// Send ClientHello handshake
665664
CHECK(wrap->is_client());
@@ -703,7 +702,7 @@ void TLSWrap::SSLInfoCallback(const SSL* ssl_, int where, int ret) {
703702
CHECK(!SSL_renegotiate_pending(ssl));
704703
Local<Value> callback;
705704

706-
c->established_ = true;
705+
c->flags_.established = true;
707706

708707
if (object->Get(env->context(), env->onhandshakedone_string())
709708
.ToLocal(&callback) && callback->IsFunction()) {
@@ -727,17 +726,17 @@ void TLSWrap::EncOut() {
727726
return;
728727
}
729728

730-
if (has_active_write_issued_by_prev_listener_) [[unlikely]] {
729+
if (flags_.has_active_write_issued_by_prev_listener) [[unlikely]] {
731730
Debug(this,
732731
"Returning from EncOut(), "
733732
"has_active_write_issued_by_prev_listener_ is true");
734733
return;
735734
}
736735

737736
// Split-off queue
738-
if (established_ && current_write_) {
737+
if (flags_.established && current_write_) {
739738
Debug(this, "EncOut() write is scheduled");
740-
write_callback_scheduled_ = true;
739+
flags_.write_callback_scheduled = true;
741740
}
742741

743742
if (ssl_ == nullptr) {
@@ -750,7 +749,7 @@ void TLSWrap::EncOut() {
750749
Debug(this, "No pending encrypted output");
751750
if (!pending_cleartext_input_ ||
752751
pending_cleartext_input_->ByteLength() == 0) {
753-
if (!in_dowrite_) {
752+
if (!flags_.in_dowrite) {
754753
Debug(this, "No pending cleartext input, not inside DoWrite()");
755754
InvokeQueued(0);
756755
} else {
@@ -805,7 +804,7 @@ void TLSWrap::EncOut() {
805804
voidTLSWrap::OnStreamAfterWrite(WriteWrap* req_wrap, int status) {
806805
Debug(this, "OnStreamAfterWrite(status = %d)", status);
807806

808-
if (has_active_write_issued_by_prev_listener_) [[unlikely]] {
807+
if (flags_.has_active_write_issued_by_prev_listener) [[unlikely]] {
809808
Debug(this, "Notify write finish to the previous_listener_");
810809
CHECK_EQ(write_size_, 0); // we must have restrained writes
811810

@@ -830,7 +829,7 @@ void TLSWrap::OnStreamAfterWrite(WriteWrap* req_wrap, int status) {
830829

831830
// Handle error
832831
if (status) {
833-
if (shutdown_) {
832+
if (flags_.shutdown) {
834833
Debug(this, "Ignoring error after shutdown");
835834
return;
836835
}
@@ -855,7 +854,7 @@ void TLSWrap::ClearOut() {
855854
Debug(this, "Trying to read cleartext output");
856855

857856
// No reads after EOF
858-
if (eof_) {
857+
if (flags_.eof) {
859858
Debug(this, "Returning from ClearOut(), EOF reached");
860859
return;
861860
}
@@ -911,8 +910,8 @@ void TLSWrap::ClearOut() {
911910
int err = SSL_get_error(ssl_.get(), read);
912911
switch (err) {
913912
caseSSL_ERROR_ZERO_RETURN:
914-
if (!eof_) {
915-
eof_ = true;
913+
if (!flags_.eof) {
914+
flags_.eof = true;
916915
EmitRead(UV_EOF);
917916
}
918917
return;
@@ -1005,7 +1004,7 @@ void TLSWrap::ClearIn() {
10051004
int err = SSL_get_error(ssl_.get(), written);
10061005
if (err == SSL_ERROR_SSL || err == SSL_ERROR_SYSCALL) {
10071006
Debug(this, "Got SSL error (%d)", err);
1008-
write_callback_scheduled_ = true;
1007+
flags_.write_callback_scheduled = true;
10091008
// TODO(@sam-github) Should forward an error object with
10101009
// .code/.function/.etc, if possible.
10111010
InvokeQueued(UV_EPROTO, GetBIOError().c_str());
@@ -1049,7 +1048,7 @@ bool TLSWrap::IsClosing() {
10491048

10501049
intTLSWrap::ReadStart() {
10511050
Debug(this, "ReadStart()");
1052-
if (underlying_stream() != nullptr && !eof_)
1051+
if (underlying_stream() != nullptr && !flags_.eof)
10531052
returnunderlying_stream()->ReadStart();
10541053
return0;
10551054
}
@@ -1197,9 +1196,9 @@ int TLSWrap::DoWrite(WriteWrap* w,
11971196

11981197
// Write any encrypted/handshake output that may be ready.
11991198
// Guard against sync call of current_write_->Done(), its unsupported.
1200-
in_dowrite_ = true;
1199+
flags_.in_dowrite = true;
12011200
EncOut();
1202-
in_dowrite_ = false;
1201+
flags_.in_dowrite = false;
12031202

12041203
return0;
12051204
}
@@ -1216,16 +1215,15 @@ void TLSWrap::OnStreamRead(ssize_t nread, const uv_buf_t& buf) {
12161215
Debug(this, "Read %zd bytes from underlying stream", nread);
12171216

12181217
// Ignore everything after close_notify (rfc5246#section-7.2.1)
1219-
if (eof_)
1220-
return;
1218+
if (flags_.eof) return;
12211219

12221220
if (nread < 0) {
12231221
// Error should be emitted only after all data was read
12241222
ClearOut();
12251223

12261224
if (nread == UV_EOF) {
12271225
// underlying stream already should have also called ReadStop on itself
1228-
eof_ = true;
1226+
flags_.eof = true;
12291227
}
12301228

12311229
EmitRead(nread);
@@ -1256,7 +1254,7 @@ int TLSWrap::DoShutdown(ShutdownWrap* req_wrap) {
12561254
if (ssl_ && SSL_shutdown(ssl_.get()) == 0)
12571255
SSL_shutdown(ssl_.get());
12581256

1259-
shutdown_ = true;
1257+
flags_.shutdown = true;
12601258
EncOut();
12611259
returnunderlying_stream()->DoShutdown(req_wrap);
12621260
}
@@ -1352,7 +1350,7 @@ void TLSWrap::Destroy() {
13521350
return;
13531351

13541352
// If there is a write happening, mark it as finished.
1355-
write_callback_scheduled_ = true;
1353+
flags_.write_callback_scheduled = true;
13561354

13571355
// And destroy
13581356
InvokeQueued(UV_ECANCELED, "Canceled because of SSL destruction");
@@ -1389,7 +1387,7 @@ void TLSWrap::ResumeAfterCertCb(void* arg) {
13891387
voidTLSWrap::EnableALPNCb(const FunctionCallbackInfo<Value>& args) {
13901388
TLSWrap* wrap;
13911389
ASSIGN_OR_RETURN_UNWRAP(&wrap, args.This());
1392-
wrap->alpn_callback_enabled_ = true;
1390+
wrap->flags_.alpn_callback_enabled = true;
13931391

13941392
SSL* ssl = wrap->ssl_.get();
13951393
SSL_CTX* ssl_ctx = SSL_get_SSL_CTX(ssl);
@@ -1418,7 +1416,7 @@ void TLSWrap::SetServername(const FunctionCallbackInfo<Value>& args) {
14181416

14191417
CHECK_EQ(args.Length(), 1);
14201418
CHECK(args[0]->IsString());
1421-
CHECK(!wrap->started_);
1419+
CHECK(!wrap->flags_.started);
14221420
CHECK(wrap->is_client());
14231421

14241422
CHECK(wrap->ssl_);
@@ -1633,7 +1631,7 @@ void TLSWrap::CertCbDone(const FunctionCallbackInfo<Value>& args) {
16331631
TLSWrap* w;
16341632
ASSIGN_OR_RETURN_UNWRAP(&w, args.This());
16351633

1636-
CHECK(w->is_waiting_cert_cb() && w->cert_cb_running_);
1634+
CHECK(w->is_waiting_cert_cb() && w->flags_.cert_cb_running);
16371635

16381636
Local<Object> object = w->object();
16391637
Local<Value> ctx = object->Get(env->context(), env->sni_context_string())
@@ -1685,7 +1683,7 @@ void TLSWrap::CertCbDone(const FunctionCallbackInfo<Value>& args) {
16851683
cb = w->cert_cb_;
16861684
arg = w->cert_cb_arg_;
16871685

1688-
w->cert_cb_running_ = false;
1686+
w->flags_.cert_cb_running = false;
16891687
w->cert_cb_ = nullptr;
16901688
w->cert_cb_arg_ = nullptr;
16911689

@@ -2072,7 +2070,7 @@ void TLSWrap::ExportKeyingMaterial(const FunctionCallbackInfo<Value>& args) {
20722070
voidTLSWrap::ClientHelloDone(const FunctionCallbackInfo<Value>& args) {
20732071
TLSWrap* w;
20742072
ASSIGN_OR_RETURN_UNWRAP(&w, args.This());
2075-
w->hello_answered_ = true;
2073+
w->flags_.hello_answered = true;
20762074
w->Cycle();
20772075
}
20782076

@@ -2107,7 +2105,7 @@ void TLSWrap::GetTLSTicket(const FunctionCallbackInfo<Value>& args) {
21072105
voidTLSWrap::NewSessionDone(const FunctionCallbackInfo<Value>& args) {
21082106
TLSWrap* w;
21092107
ASSIGN_OR_RETURN_UNWRAP(&w, args.This());
2110-
w->awaiting_new_session_ = false;
2108+
w->flags_.awaiting_new_session = false;
21112109
w->NewSessionDoneCb();
21122110
}
21132111

@@ -2188,7 +2186,7 @@ void TLSWrap::WritesIssuedByPrevListenerDone(
21882186
ASSIGN_OR_RETURN_UNWRAP(&w, args.This());
21892187

21902188
Debug(w, "WritesIssuedByPrevListenerDone is called");
2191-
w->has_active_write_issued_by_prev_listener_ = false;
2189+
w->flags_.has_active_write_issued_by_prev_listener = false;
21922190
w->EncOut(); // resume all of our restrained writes
21932191
}
21942192

β€Žsrc/crypto/crypto_tls.hβ€Ž

Lines changed: 37 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -62,22 +62,26 @@ class TLSWrap : public AsyncWrap,
6262

6363
~TLSWrap() override;
6464

65-
inlineboolis_cert_cb_running() const { returncert_cb_running_; }
65+
inlineboolis_cert_cb_running() const { returnflags_.cert_cb_running; }
6666
inlineboolis_waiting_cert_cb() const { return cert_cb_ != nullptr; }
67-
inlineboolhas_session_callbacks() const { returnsession_callbacks_; }
67+
inlineboolhas_session_callbacks() const { returnflags_.session_callbacks; }
6868
// We need to suspend the ClientHello only for server session id
6969
// callbacks, and only on the first pass.
7070
inlineboolshould_suspend_for_client_hello() const {
71-
returnis_server() && session_callbacks_ && !hello_answered_;
71+
returnis_server() && flags_.session_callbacks && !flags_.hello_answered;
72+
}
73+
inlinevoidset_cert_cb_running(bool on = true) {
74+
flags_.cert_cb_running = on;
7275
}
73-
inlinevoidset_cert_cb_running(bool on = true) { cert_cb_running_ = on; }
7476
inlinevoidset_awaiting_new_session(bool on = true) {
75-
awaiting_new_session_ = on;
77+
flags_.awaiting_new_session = on;
7678
}
77-
inlinevoidenable_session_callbacks() { session_callbacks_ = true; }
79+
inlinevoidenable_session_callbacks() { flags_.session_callbacks = true; }
7880
inlineboolis_server() const { return kind_ == Kind::kServer; }
7981
inlineboolis_client() const { return kind_ == Kind::kClient; }
80-
inlineboolis_awaiting_new_session() const { return awaiting_new_session_; }
82+
inlineboolis_awaiting_new_session() const {
83+
return flags_.awaiting_new_session;
84+
}
8185

8286
// Implement StreamBase:
8387
boolIsAlive() override;
@@ -125,6 +129,14 @@ class TLSWrap : public AsyncWrap,
125129

126130
std::string diagnostic_name() constoverride;
127131

132+
boolget_alpn_callback_enabled() const {
133+
return flags_.alpn_callback_enabled;
134+
}
135+
136+
const std::vector<unsignedchar>& get_alpn_protos() const {
137+
return alpn_protos_;
138+
}
139+
128140
private:
129141
// OpenSSL structures are opaque. Estimate SSL memory size for OpenSSL 1.1.1b:
130142
// SSL: 6224
@@ -284,26 +296,30 @@ class TLSWrap : public AsyncWrap,
284296
BaseObjectPtr<AsyncWrap> current_empty_write_;
285297
std::string error_;
286298

287-
bool session_callbacks_ = false;
288-
bool awaiting_new_session_ = false;
289-
// 'onclienthello' has been emitted for this connection.
290-
bool hello_emitted_ = false;
291-
// JS has answered it by calling clientHelloDone().
292-
bool hello_answered_ = false;
293-
bool in_dowrite_ = false;
294-
bool started_ = false;
295-
bool shutdown_ = false;
296-
bool cert_cb_running_ = false;
297-
bool eof_ = false;
298-
299299
// TODO(@jasnell): These state flags should be revisited.
300300
// The established_ flag indicates that the handshake is
301301
// completed. The write_callback_scheduled_ flag is less
302302
// clear -- once it is set to true, it is never set to
303303
// false and it is only set to true after established_
304304
// is set to true, so it's likely redundant.
305-
bool established_ = false;
306-
bool write_callback_scheduled_ = false;
305+
structFlags {
306+
bool session_callbacks : 1;
307+
bool awaiting_new_session : 1;
308+
// 'onclienthello' has been emitted for this connection.
309+
bool hello_emitted : 1;
310+
// JS has answered it by calling clientHelloDone().
311+
bool hello_answered : 1;
312+
bool in_dowrite : 1;
313+
bool started : 1;
314+
bool shutdown : 1;
315+
bool cert_cb_running : 1;
316+
bool eof : 1;
317+
bool established : 1;
318+
bool write_callback_scheduled : 1;
319+
bool has_active_write_issued_by_prev_listener : 1;
320+
bool alpn_callback_enabled : 1;
321+
};
322+
Flags flags_{};
307323

308324
int cycle_depth_ = 0;
309325

@@ -313,11 +329,7 @@ class TLSWrap : public AsyncWrap,
313329

314330
ncrypto::BIOPointer bio_trace_;
315331

316-
bool has_active_write_issued_by_prev_listener_ = false;
317-
318-
public:
319332
std::vector<unsignedchar> alpn_protos_; // Accessed by SelectALPNCallback.
320-
bool alpn_callback_enabled_ = false; // Accessed by SelectALPNCallback.
321333
};
322334

323335
} // namespace crypto

0 commit comments

Comments
Β (0)
, 'i'); if (__m === '*' || __re.test(location.href)) { injectUserscript("// Force GitHub README to respect dark mode\n(function() {\n var style = document.createElement('style');\n style.textContent = '\n .markdown-body {\n color-scheme: dark light;\n }\n .markdown-body pre { background: #161b22 !important; }\n .markdown-body code { background: rgba(110, 118, 129, 0.4) !important; }\n .markdown-body table th, .markdown-body table td { border-color: #30363d !important; }\n .markdown-body img { background: #0d1117; }\n .markdown-body blockquote { border-left-color: #8b949e; }\n .markdown-body hr { border-color: #30363d; }\n ';\n document.head.appendChild(style);\n})();", "GitHub Dark Mode README Fix"); } } catch(__e) { console.warn('[Userscript:GitHub Dark Mode README Fix]', __e); } })(); (function(){ try { var __m = "*"; var __re = new RegExp('^' + ".*" + '
Skip to content

Commit 4018f3a

Browse files
jasnelladuh95
authored andcommitted
src: shave about 20 bytes off each TLSWrap instance
By shifting from individual bool fields to a packed struct we can save 20 bytes per TLSWrap instance Signed-off-by: James M Snell <jasnell@gmail.com> PR-URL: #65144 Reviewed-By: Tim Perry <pimterry@gmail.com> Reviewed-By: Gürgün Dayıoğlu <hey@gurgun.day> Reviewed-By: Tobias Nießen <tniessen@tnie.de>
1 parent c63e873 commit 4018f3a

2 files changed

Lines changed: 73 additions & 63 deletions

File tree

β€Žsrc/crypto/crypto_tls.ccβ€Ž

Lines changed: 36 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -240,7 +240,7 @@ int SelectALPNCallback(
240240
unsignedint inlen,
241241
void* arg) {
242242
TLSWrap* w = static_cast<TLSWrap*>(SSL_get_app_data(s));
243-
if (w->alpn_callback_enabled_) {
243+
if (w->get_alpn_callback_enabled()) {
244244
Environment* env = w->env();
245245
HandleScope handle_scope(env->isolate());
246246

@@ -275,7 +275,7 @@ int SelectALPNCallback(
275275
returnSSL_TLSEXT_ERR_OK;
276276
}
277277

278-
const std::vector<unsignedchar>& alpn_protos = w->alpn_protos_;
278+
auto& alpn_protos = w->get_alpn_protos();
279279

280280
if (alpn_protos.empty()) returnSSL_TLSEXT_ERR_NOACK;
281281

@@ -403,9 +403,9 @@ TLSWrap::TLSWrap(Environment* env,
403403
StreamBase(env),
404404
env_(env),
405405
kind_(kind),
406-
sc_(sc),
407-
has_active_write_issued_by_prev_listener_(
408-
under_stream_ws == UnderlyingStreamWriteStatus::kHasActive) {
406+
sc_(sc) {
407+
flags_.has_active_write_issued_by_prev_listener =
408+
under_stream_ws == UnderlyingStreamWriteStatus::kHasActive;
409409
MakeWeak();
410410
CHECK(sc_);
411411
ssl_ = sc_->CreateSSL();
@@ -444,8 +444,7 @@ SSL_SESSION* TLSWrap::ReleaseSession() {
444444

445445
voidTLSWrap::InvokeQueued(int status, constchar* error_str) {
446446
Debug(this, "Invoking queued write callbacks (%d, %s)", status, error_str);
447-
if (!write_callback_scheduled_)
448-
return;
447+
if (!flags_.write_callback_scheduled) return;
449448

450449
if (current_write_) {
451450
BaseObjectPtr<AsyncWrap> current_write = std::move(current_write_);
@@ -465,8 +464,8 @@ void TLSWrap::NewSessionDoneCb() {
465464
boolTLSWrap::OnEarlyClientHello(constunsignedchar* session_id,
466465
size_t session_id_len,
467466
bool has_ticket) {
468-
if (!hello_emitted_) {
469-
hello_emitted_ = true;
467+
if (!flags_.hello_emitted) {
468+
flags_.hello_emitted = true;
470469
Debug(this, "Scheduling onclienthello");
471470

472471
// The hello data is only valid inside the library callback, and JS must
@@ -480,7 +479,7 @@ bool TLSWrap::OnEarlyClientHello(const unsigned char* session_id,
480479
if (ssl_) EmitClientHello(id, has_ticket);
481480
});
482481
}
483-
returnhello_answered_;
482+
returnflags_.hello_answered;
484483
}
485484

486485
voidTLSWrap::EmitClientHello(const std::vector<unsignedchar>& session_id,
@@ -658,8 +657,8 @@ void TLSWrap::Start(const FunctionCallbackInfo<Value>& args) {
658657
TLSWrap* wrap;
659658
ASSIGN_OR_RETURN_UNWRAP(&wrap, args.This());
660659

661-
CHECK(!wrap->started_);
662-
wrap->started_ = true;
660+
CHECK(!wrap->flags_.started);
661+
wrap->flags_.started = true;
663662

664663
// Send ClientHello handshake
665664
CHECK(wrap->is_client());
@@ -703,7 +702,7 @@ void TLSWrap::SSLInfoCallback(const SSL* ssl_, int where, int ret) {
703702
CHECK(!SSL_renegotiate_pending(ssl));
704703
Local<Value> callback;
705704

706-
c->established_ = true;
705+
c->flags_.established = true;
707706

708707
if (object->Get(env->context(), env->onhandshakedone_string())
709708
.ToLocal(&callback) && callback->IsFunction()) {
@@ -727,17 +726,17 @@ void TLSWrap::EncOut() {
727726
return;
728727
}
729728

730-
if (has_active_write_issued_by_prev_listener_) [[unlikely]] {
729+
if (flags_.has_active_write_issued_by_prev_listener) [[unlikely]] {
731730
Debug(this,
732731
"Returning from EncOut(), "
733732
"has_active_write_issued_by_prev_listener_ is true");
734733
return;
735734
}
736735

737736
// Split-off queue
738-
if (established_ && current_write_) {
737+
if (flags_.established && current_write_) {
739738
Debug(this, "EncOut() write is scheduled");
740-
write_callback_scheduled_ = true;
739+
flags_.write_callback_scheduled = true;
741740
}
742741

743742
if (ssl_ == nullptr) {
@@ -750,7 +749,7 @@ void TLSWrap::EncOut() {
750749
Debug(this, "No pending encrypted output");
751750
if (!pending_cleartext_input_ ||
752751
pending_cleartext_input_->ByteLength() == 0) {
753-
if (!in_dowrite_) {
752+
if (!flags_.in_dowrite) {
754753
Debug(this, "No pending cleartext input, not inside DoWrite()");
755754
InvokeQueued(0);
756755
} else {
@@ -805,7 +804,7 @@ void TLSWrap::EncOut() {
805804
voidTLSWrap::OnStreamAfterWrite(WriteWrap* req_wrap, int status) {
806805
Debug(this, "OnStreamAfterWrite(status = %d)", status);
807806

808-
if (has_active_write_issued_by_prev_listener_) [[unlikely]] {
807+
if (flags_.has_active_write_issued_by_prev_listener) [[unlikely]] {
809808
Debug(this, "Notify write finish to the previous_listener_");
810809
CHECK_EQ(write_size_, 0); // we must have restrained writes
811810

@@ -830,7 +829,7 @@ void TLSWrap::OnStreamAfterWrite(WriteWrap* req_wrap, int status) {
830829

831830
// Handle error
832831
if (status) {
833-
if (shutdown_) {
832+
if (flags_.shutdown) {
834833
Debug(this, "Ignoring error after shutdown");
835834
return;
836835
}
@@ -855,7 +854,7 @@ void TLSWrap::ClearOut() {
855854
Debug(this, "Trying to read cleartext output");
856855

857856
// No reads after EOF
858-
if (eof_) {
857+
if (flags_.eof) {
859858
Debug(this, "Returning from ClearOut(), EOF reached");
860859
return;
861860
}
@@ -911,8 +910,8 @@ void TLSWrap::ClearOut() {
911910
int err = SSL_get_error(ssl_.get(), read);
912911
switch (err) {
913912
caseSSL_ERROR_ZERO_RETURN:
914-
if (!eof_) {
915-
eof_ = true;
913+
if (!flags_.eof) {
914+
flags_.eof = true;
916915
EmitRead(UV_EOF);
917916
}
918917
return;
@@ -1005,7 +1004,7 @@ void TLSWrap::ClearIn() {
10051004
int err = SSL_get_error(ssl_.get(), written);
10061005
if (err == SSL_ERROR_SSL || err == SSL_ERROR_SYSCALL) {
10071006
Debug(this, "Got SSL error (%d)", err);
1008-
write_callback_scheduled_ = true;
1007+
flags_.write_callback_scheduled = true;
10091008
// TODO(@sam-github) Should forward an error object with
10101009
// .code/.function/.etc, if possible.
10111010
InvokeQueued(UV_EPROTO, GetBIOError().c_str());
@@ -1049,7 +1048,7 @@ bool TLSWrap::IsClosing() {
10491048

10501049
intTLSWrap::ReadStart() {
10511050
Debug(this, "ReadStart()");
1052-
if (underlying_stream() != nullptr && !eof_)
1051+
if (underlying_stream() != nullptr && !flags_.eof)
10531052
returnunderlying_stream()->ReadStart();
10541053
return0;
10551054
}
@@ -1197,9 +1196,9 @@ int TLSWrap::DoWrite(WriteWrap* w,
11971196

11981197
// Write any encrypted/handshake output that may be ready.
11991198
// Guard against sync call of current_write_->Done(), its unsupported.
1200-
in_dowrite_ = true;
1199+
flags_.in_dowrite = true;
12011200
EncOut();
1202-
in_dowrite_ = false;
1201+
flags_.in_dowrite = false;
12031202

12041203
return0;
12051204
}
@@ -1216,16 +1215,15 @@ void TLSWrap::OnStreamRead(ssize_t nread, const uv_buf_t& buf) {
12161215
Debug(this, "Read %zd bytes from underlying stream", nread);
12171216

12181217
// Ignore everything after close_notify (rfc5246#section-7.2.1)
1219-
if (eof_)
1220-
return;
1218+
if (flags_.eof) return;
12211219

12221220
if (nread < 0) {
12231221
// Error should be emitted only after all data was read
12241222
ClearOut();
12251223

12261224
if (nread == UV_EOF) {
12271225
// underlying stream already should have also called ReadStop on itself
1228-
eof_ = true;
1226+
flags_.eof = true;
12291227
}
12301228

12311229
EmitRead(nread);
@@ -1256,7 +1254,7 @@ int TLSWrap::DoShutdown(ShutdownWrap* req_wrap) {
12561254
if (ssl_ && SSL_shutdown(ssl_.get()) == 0)
12571255
SSL_shutdown(ssl_.get());
12581256

1259-
shutdown_ = true;
1257+
flags_.shutdown = true;
12601258
EncOut();
12611259
returnunderlying_stream()->DoShutdown(req_wrap);
12621260
}
@@ -1352,7 +1350,7 @@ void TLSWrap::Destroy() {
13521350
return;
13531351

13541352
// If there is a write happening, mark it as finished.
1355-
write_callback_scheduled_ = true;
1353+
flags_.write_callback_scheduled = true;
13561354

13571355
// And destroy
13581356
InvokeQueued(UV_ECANCELED, "Canceled because of SSL destruction");
@@ -1389,7 +1387,7 @@ void TLSWrap::ResumeAfterCertCb(void* arg) {
13891387
voidTLSWrap::EnableALPNCb(const FunctionCallbackInfo<Value>& args) {
13901388
TLSWrap* wrap;
13911389
ASSIGN_OR_RETURN_UNWRAP(&wrap, args.This());
1392-
wrap->alpn_callback_enabled_ = true;
1390+
wrap->flags_.alpn_callback_enabled = true;
13931391

13941392
SSL* ssl = wrap->ssl_.get();
13951393
SSL_CTX* ssl_ctx = SSL_get_SSL_CTX(ssl);
@@ -1418,7 +1416,7 @@ void TLSWrap::SetServername(const FunctionCallbackInfo<Value>& args) {
14181416

14191417
CHECK_EQ(args.Length(), 1);
14201418
CHECK(args[0]->IsString());
1421-
CHECK(!wrap->started_);
1419+
CHECK(!wrap->flags_.started);
14221420
CHECK(wrap->is_client());
14231421

14241422
CHECK(wrap->ssl_);
@@ -1633,7 +1631,7 @@ void TLSWrap::CertCbDone(const FunctionCallbackInfo<Value>& args) {
16331631
TLSWrap* w;
16341632
ASSIGN_OR_RETURN_UNWRAP(&w, args.This());
16351633

1636-
CHECK(w->is_waiting_cert_cb() && w->cert_cb_running_);
1634+
CHECK(w->is_waiting_cert_cb() && w->flags_.cert_cb_running);
16371635

16381636
Local<Object> object = w->object();
16391637
Local<Value> ctx = object->Get(env->context(), env->sni_context_string())
@@ -1685,7 +1683,7 @@ void TLSWrap::CertCbDone(const FunctionCallbackInfo<Value>& args) {
16851683
cb = w->cert_cb_;
16861684
arg = w->cert_cb_arg_;
16871685

1688-
w->cert_cb_running_ = false;
1686+
w->flags_.cert_cb_running = false;
16891687
w->cert_cb_ = nullptr;
16901688
w->cert_cb_arg_ = nullptr;
16911689

@@ -2072,7 +2070,7 @@ void TLSWrap::ExportKeyingMaterial(const FunctionCallbackInfo<Value>& args) {
20722070
voidTLSWrap::ClientHelloDone(const FunctionCallbackInfo<Value>& args) {
20732071
TLSWrap* w;
20742072
ASSIGN_OR_RETURN_UNWRAP(&w, args.This());
2075-
w->hello_answered_ = true;
2073+
w->flags_.hello_answered = true;
20762074
w->Cycle();
20772075
}
20782076

@@ -2107,7 +2105,7 @@ void TLSWrap::GetTLSTicket(const FunctionCallbackInfo<Value>& args) {
21072105
voidTLSWrap::NewSessionDone(const FunctionCallbackInfo<Value>& args) {
21082106
TLSWrap* w;
21092107
ASSIGN_OR_RETURN_UNWRAP(&w, args.This());
2110-
w->awaiting_new_session_ = false;
2108+
w->flags_.awaiting_new_session = false;
21112109
w->NewSessionDoneCb();
21122110
}
21132111

@@ -2188,7 +2186,7 @@ void TLSWrap::WritesIssuedByPrevListenerDone(
21882186
ASSIGN_OR_RETURN_UNWRAP(&w, args.This());
21892187

21902188
Debug(w, "WritesIssuedByPrevListenerDone is called");
2191-
w->has_active_write_issued_by_prev_listener_ = false;
2189+
w->flags_.has_active_write_issued_by_prev_listener = false;
21922190
w->EncOut(); // resume all of our restrained writes
21932191
}
21942192

β€Žsrc/crypto/crypto_tls.hβ€Ž

Lines changed: 37 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -62,22 +62,26 @@ class TLSWrap : public AsyncWrap,
6262

6363
~TLSWrap() override;
6464

65-
inlineboolis_cert_cb_running() const { returncert_cb_running_; }
65+
inlineboolis_cert_cb_running() const { returnflags_.cert_cb_running; }
6666
inlineboolis_waiting_cert_cb() const { return cert_cb_ != nullptr; }
67-
inlineboolhas_session_callbacks() const { returnsession_callbacks_; }
67+
inlineboolhas_session_callbacks() const { returnflags_.session_callbacks; }
6868
// We need to suspend the ClientHello only for server session id
6969
// callbacks, and only on the first pass.
7070
inlineboolshould_suspend_for_client_hello() const {
71-
returnis_server() && session_callbacks_ && !hello_answered_;
71+
returnis_server() && flags_.session_callbacks && !flags_.hello_answered;
72+
}
73+
inlinevoidset_cert_cb_running(bool on = true) {
74+
flags_.cert_cb_running = on;
7275
}
73-
inlinevoidset_cert_cb_running(bool on = true) { cert_cb_running_ = on; }
7476
inlinevoidset_awaiting_new_session(bool on = true) {
75-
awaiting_new_session_ = on;
77+
flags_.awaiting_new_session = on;
7678
}
77-
inlinevoidenable_session_callbacks() { session_callbacks_ = true; }
79+
inlinevoidenable_session_callbacks() { flags_.session_callbacks = true; }
7880
inlineboolis_server() const { return kind_ == Kind::kServer; }
7981
inlineboolis_client() const { return kind_ == Kind::kClient; }
80-
inlineboolis_awaiting_new_session() const { return awaiting_new_session_; }
82+
inlineboolis_awaiting_new_session() const {
83+
return flags_.awaiting_new_session;
84+
}
8185

8286
// Implement StreamBase:
8387
boolIsAlive() override;
@@ -125,6 +129,14 @@ class TLSWrap : public AsyncWrap,
125129

126130
std::string diagnostic_name() constoverride;
127131

132+
boolget_alpn_callback_enabled() const {
133+
return flags_.alpn_callback_enabled;
134+
}
135+
136+
const std::vector<unsignedchar>& get_alpn_protos() const {
137+
return alpn_protos_;
138+
}
139+
128140
private:
129141
// OpenSSL structures are opaque. Estimate SSL memory size for OpenSSL 1.1.1b:
130142
// SSL: 6224
@@ -284,26 +296,30 @@ class TLSWrap : public AsyncWrap,
284296
BaseObjectPtr<AsyncWrap> current_empty_write_;
285297
std::string error_;
286298

287-
bool session_callbacks_ = false;
288-
bool awaiting_new_session_ = false;
289-
// 'onclienthello' has been emitted for this connection.
290-
bool hello_emitted_ = false;
291-
// JS has answered it by calling clientHelloDone().
292-
bool hello_answered_ = false;
293-
bool in_dowrite_ = false;
294-
bool started_ = false;
295-
bool shutdown_ = false;
296-
bool cert_cb_running_ = false;
297-
bool eof_ = false;
298-
299299
// TODO(@jasnell): These state flags should be revisited.
300300
// The established_ flag indicates that the handshake is
301301
// completed. The write_callback_scheduled_ flag is less
302302
// clear -- once it is set to true, it is never set to
303303
// false and it is only set to true after established_
304304
// is set to true, so it's likely redundant.
305-
bool established_ = false;
306-
bool write_callback_scheduled_ = false;
305+
structFlags {
306+
bool session_callbacks : 1;
307+
bool awaiting_new_session : 1;
308+
// 'onclienthello' has been emitted for this connection.
309+
bool hello_emitted : 1;
310+
// JS has answered it by calling clientHelloDone().
311+
bool hello_answered : 1;
312+
bool in_dowrite : 1;
313+
bool started : 1;
314+
bool shutdown : 1;
315+
bool cert_cb_running : 1;
316+
bool eof : 1;
317+
bool established : 1;
318+
bool write_callback_scheduled : 1;
319+
bool has_active_write_issued_by_prev_listener : 1;
320+
bool alpn_callback_enabled : 1;
321+
};
322+
Flags flags_{};
307323

308324
int cycle_depth_ = 0;
309325

@@ -313,11 +329,7 @@ class TLSWrap : public AsyncWrap,
313329

314330
ncrypto::BIOPointer bio_trace_;
315331

316-
bool has_active_write_issued_by_prev_listener_ = false;
317-
318-
public:
319332
std::vector<unsignedchar> alpn_protos_; // Accessed by SelectALPNCallback.
320-
bool alpn_callback_enabled_ = false; // Accessed by SelectALPNCallback.
321333
};
322334

323335
} // namespace crypto

0 commit comments

Comments
Β (0)
, 'i'); if (__m === '*' || __re.test(location.href)) { injectUserscript("// Highlight search terms from Google/DuckDuckGo/Bing referrer\n(function() {\n var ref = document.referrer;\n var terms = [];\n \n if (ref.includes('google.com') || ref.includes('duckduckgo.com') || ref.includes('bing.com')) {\n var url = new URL(ref);\n var q = url.searchParams.get('q') || url.searchParams.get('p');\n if (q) {\n terms = q.split(/\\s+/).filter(function(t) { return t.length > 2; });\n }\n }\n \n if (terms.length === 0) return;\n \n var style = document.createElement('style');\n style.textContent = '.userscript-highlight { background: #fbbf24; color: #1a1a2e; padding: 1px 3px; border-radius: 2px; }';\n document.head.appendChild(style);\n \n function highlight(node) {\n if (node.nodeType === 3) { // text node\n var text = node.textContent;\n var found = false;\n terms.forEach(function(term) {\n var regex = new RegExp('(' + term.replace(/[.*+?^${}()|[\\]\\\\]/g, '\\\\') + ')', 'gi');\n if (regex.test(text)) {\n found = true;\n var frag = document.createDocumentFragment();\n var parts = text.split(regex);\n parts.forEach(function(part, i) {\n if (i % 2 === 0) {\n frag.appendChild(document.createTextNode(part));\n } else {\n var span = document.createElement('span');\n span.className = 'userscript-highlight';\n span.textContent = part;\n frag.appendChild(span);\n }\n });\n node.parentNode.replaceChild(frag, node);\n }\n });\n } else if (node.nodeType === 1 && node.childNodes) { // element\n var skipTags = ['SCRIPT', 'STYLE', 'NOSCRIPT', 'TEXTAREA', 'INPUT', 'SELECT'];\n if (!skipTags.includes(node.tagName)) {\n Array.from(node.childNodes).forEach(highlight);\n }\n }\n }\n \n highlight(document.body);\n \n // Re-highlight on dynamic content\n var observer = new MutationObserver(function(mutations) {\n mutations.forEach(function(m) {\n m.addedNodes.forEach(function(node) {\n if (node.nodeType === 1 || node.nodeType === 3) highlight(node);\n });\n });\n });\n observer.observe(document.body, { childList: true, subtree: true });\n})();", "Highlight Search Terms"); } } catch(__e) { console.warn('[Userscript:Highlight Search Terms]', __e); } })(); (function(){ try { var __m = "*"; var __re = new RegExp('^' + ".*" + '
Skip to content

Commit 4018f3a

Browse files
jasnelladuh95
authored andcommitted
src: shave about 20 bytes off each TLSWrap instance
By shifting from individual bool fields to a packed struct we can save 20 bytes per TLSWrap instance Signed-off-by: James M Snell <jasnell@gmail.com> PR-URL: #65144 Reviewed-By: Tim Perry <pimterry@gmail.com> Reviewed-By: Gürgün Dayıoğlu <hey@gurgun.day> Reviewed-By: Tobias Nießen <tniessen@tnie.de>
1 parent c63e873 commit 4018f3a

2 files changed

Lines changed: 73 additions & 63 deletions

File tree

β€Žsrc/crypto/crypto_tls.ccβ€Ž

Lines changed: 36 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -240,7 +240,7 @@ int SelectALPNCallback(
240240
unsignedint inlen,
241241
void* arg) {
242242
TLSWrap* w = static_cast<TLSWrap*>(SSL_get_app_data(s));
243-
if (w->alpn_callback_enabled_) {
243+
if (w->get_alpn_callback_enabled()) {
244244
Environment* env = w->env();
245245
HandleScope handle_scope(env->isolate());
246246

@@ -275,7 +275,7 @@ int SelectALPNCallback(
275275
returnSSL_TLSEXT_ERR_OK;
276276
}
277277

278-
const std::vector<unsignedchar>& alpn_protos = w->alpn_protos_;
278+
auto& alpn_protos = w->get_alpn_protos();
279279

280280
if (alpn_protos.empty()) returnSSL_TLSEXT_ERR_NOACK;
281281

@@ -403,9 +403,9 @@ TLSWrap::TLSWrap(Environment* env,
403403
StreamBase(env),
404404
env_(env),
405405
kind_(kind),
406-
sc_(sc),
407-
has_active_write_issued_by_prev_listener_(
408-
under_stream_ws == UnderlyingStreamWriteStatus::kHasActive) {
406+
sc_(sc) {
407+
flags_.has_active_write_issued_by_prev_listener =
408+
under_stream_ws == UnderlyingStreamWriteStatus::kHasActive;
409409
MakeWeak();
410410
CHECK(sc_);
411411
ssl_ = sc_->CreateSSL();
@@ -444,8 +444,7 @@ SSL_SESSION* TLSWrap::ReleaseSession() {
444444

445445
voidTLSWrap::InvokeQueued(int status, constchar* error_str) {
446446
Debug(this, "Invoking queued write callbacks (%d, %s)", status, error_str);
447-
if (!write_callback_scheduled_)
448-
return;
447+
if (!flags_.write_callback_scheduled) return;
449448

450449
if (current_write_) {
451450
BaseObjectPtr<AsyncWrap> current_write = std::move(current_write_);
@@ -465,8 +464,8 @@ void TLSWrap::NewSessionDoneCb() {
465464
boolTLSWrap::OnEarlyClientHello(constunsignedchar* session_id,
466465
size_t session_id_len,
467466
bool has_ticket) {
468-
if (!hello_emitted_) {
469-
hello_emitted_ = true;
467+
if (!flags_.hello_emitted) {
468+
flags_.hello_emitted = true;
470469
Debug(this, "Scheduling onclienthello");
471470

472471
// The hello data is only valid inside the library callback, and JS must
@@ -480,7 +479,7 @@ bool TLSWrap::OnEarlyClientHello(const unsigned char* session_id,
480479
if (ssl_) EmitClientHello(id, has_ticket);
481480
});
482481
}
483-
returnhello_answered_;
482+
returnflags_.hello_answered;
484483
}
485484

486485
voidTLSWrap::EmitClientHello(const std::vector<unsignedchar>& session_id,
@@ -658,8 +657,8 @@ void TLSWrap::Start(const FunctionCallbackInfo<Value>& args) {
658657
TLSWrap* wrap;
659658
ASSIGN_OR_RETURN_UNWRAP(&wrap, args.This());
660659

661-
CHECK(!wrap->started_);
662-
wrap->started_ = true;
660+
CHECK(!wrap->flags_.started);
661+
wrap->flags_.started = true;
663662

664663
// Send ClientHello handshake
665664
CHECK(wrap->is_client());
@@ -703,7 +702,7 @@ void TLSWrap::SSLInfoCallback(const SSL* ssl_, int where, int ret) {
703702
CHECK(!SSL_renegotiate_pending(ssl));
704703
Local<Value> callback;
705704

706-
c->established_ = true;
705+
c->flags_.established = true;
707706

708707
if (object->Get(env->context(), env->onhandshakedone_string())
709708
.ToLocal(&callback) && callback->IsFunction()) {
@@ -727,17 +726,17 @@ void TLSWrap::EncOut() {
727726
return;
728727
}
729728

730-
if (has_active_write_issued_by_prev_listener_) [[unlikely]] {
729+
if (flags_.has_active_write_issued_by_prev_listener) [[unlikely]] {
731730
Debug(this,
732731
"Returning from EncOut(), "
733732
"has_active_write_issued_by_prev_listener_ is true");
734733
return;
735734
}
736735

737736
// Split-off queue
738-
if (established_ && current_write_) {
737+
if (flags_.established && current_write_) {
739738
Debug(this, "EncOut() write is scheduled");
740-
write_callback_scheduled_ = true;
739+
flags_.write_callback_scheduled = true;
741740
}
742741

743742
if (ssl_ == nullptr) {
@@ -750,7 +749,7 @@ void TLSWrap::EncOut() {
750749
Debug(this, "No pending encrypted output");
751750
if (!pending_cleartext_input_ ||
752751
pending_cleartext_input_->ByteLength() == 0) {
753-
if (!in_dowrite_) {
752+
if (!flags_.in_dowrite) {
754753
Debug(this, "No pending cleartext input, not inside DoWrite()");
755754
InvokeQueued(0);
756755
} else {
@@ -805,7 +804,7 @@ void TLSWrap::EncOut() {
805804
voidTLSWrap::OnStreamAfterWrite(WriteWrap* req_wrap, int status) {
806805
Debug(this, "OnStreamAfterWrite(status = %d)", status);
807806

808-
if (has_active_write_issued_by_prev_listener_) [[unlikely]] {
807+
if (flags_.has_active_write_issued_by_prev_listener) [[unlikely]] {
809808
Debug(this, "Notify write finish to the previous_listener_");
810809
CHECK_EQ(write_size_, 0); // we must have restrained writes
811810

@@ -830,7 +829,7 @@ void TLSWrap::OnStreamAfterWrite(WriteWrap* req_wrap, int status) {
830829

831830
// Handle error
832831
if (status) {
833-
if (shutdown_) {
832+
if (flags_.shutdown) {
834833
Debug(this, "Ignoring error after shutdown");
835834
return;
836835
}
@@ -855,7 +854,7 @@ void TLSWrap::ClearOut() {
855854
Debug(this, "Trying to read cleartext output");
856855

857856
// No reads after EOF
858-
if (eof_) {
857+
if (flags_.eof) {
859858
Debug(this, "Returning from ClearOut(), EOF reached");
860859
return;
861860
}
@@ -911,8 +910,8 @@ void TLSWrap::ClearOut() {
911910
int err = SSL_get_error(ssl_.get(), read);
912911
switch (err) {
913912
caseSSL_ERROR_ZERO_RETURN:
914-
if (!eof_) {
915-
eof_ = true;
913+
if (!flags_.eof) {
914+
flags_.eof = true;
916915
EmitRead(UV_EOF);
917916
}
918917
return;
@@ -1005,7 +1004,7 @@ void TLSWrap::ClearIn() {
10051004
int err = SSL_get_error(ssl_.get(), written);
10061005
if (err == SSL_ERROR_SSL || err == SSL_ERROR_SYSCALL) {
10071006
Debug(this, "Got SSL error (%d)", err);
1008-
write_callback_scheduled_ = true;
1007+
flags_.write_callback_scheduled = true;
10091008
// TODO(@sam-github) Should forward an error object with
10101009
// .code/.function/.etc, if possible.
10111010
InvokeQueued(UV_EPROTO, GetBIOError().c_str());
@@ -1049,7 +1048,7 @@ bool TLSWrap::IsClosing() {
10491048

10501049
intTLSWrap::ReadStart() {
10511050
Debug(this, "ReadStart()");
1052-
if (underlying_stream() != nullptr && !eof_)
1051+
if (underlying_stream() != nullptr && !flags_.eof)
10531052
returnunderlying_stream()->ReadStart();
10541053
return0;
10551054
}
@@ -1197,9 +1196,9 @@ int TLSWrap::DoWrite(WriteWrap* w,
11971196

11981197
// Write any encrypted/handshake output that may be ready.
11991198
// Guard against sync call of current_write_->Done(), its unsupported.
1200-
in_dowrite_ = true;
1199+
flags_.in_dowrite = true;
12011200
EncOut();
1202-
in_dowrite_ = false;
1201+
flags_.in_dowrite = false;
12031202

12041203
return0;
12051204
}
@@ -1216,16 +1215,15 @@ void TLSWrap::OnStreamRead(ssize_t nread, const uv_buf_t& buf) {
12161215
Debug(this, "Read %zd bytes from underlying stream", nread);
12171216

12181217
// Ignore everything after close_notify (rfc5246#section-7.2.1)
1219-
if (eof_)
1220-
return;
1218+
if (flags_.eof) return;
12211219

12221220
if (nread < 0) {
12231221
// Error should be emitted only after all data was read
12241222
ClearOut();
12251223

12261224
if (nread == UV_EOF) {
12271225
// underlying stream already should have also called ReadStop on itself
1228-
eof_ = true;
1226+
flags_.eof = true;
12291227
}
12301228

12311229
EmitRead(nread);
@@ -1256,7 +1254,7 @@ int TLSWrap::DoShutdown(ShutdownWrap* req_wrap) {
12561254
if (ssl_ && SSL_shutdown(ssl_.get()) == 0)
12571255
SSL_shutdown(ssl_.get());
12581256

1259-
shutdown_ = true;
1257+
flags_.shutdown = true;
12601258
EncOut();
12611259
returnunderlying_stream()->DoShutdown(req_wrap);
12621260
}
@@ -1352,7 +1350,7 @@ void TLSWrap::Destroy() {
13521350
return;
13531351

13541352
// If there is a write happening, mark it as finished.
1355-
write_callback_scheduled_ = true;
1353+
flags_.write_callback_scheduled = true;
13561354

13571355
// And destroy
13581356
InvokeQueued(UV_ECANCELED, "Canceled because of SSL destruction");
@@ -1389,7 +1387,7 @@ void TLSWrap::ResumeAfterCertCb(void* arg) {
13891387
voidTLSWrap::EnableALPNCb(const FunctionCallbackInfo<Value>& args) {
13901388
TLSWrap* wrap;
13911389
ASSIGN_OR_RETURN_UNWRAP(&wrap, args.This());
1392-
wrap->alpn_callback_enabled_ = true;
1390+
wrap->flags_.alpn_callback_enabled = true;
13931391

13941392
SSL* ssl = wrap->ssl_.get();
13951393
SSL_CTX* ssl_ctx = SSL_get_SSL_CTX(ssl);
@@ -1418,7 +1416,7 @@ void TLSWrap::SetServername(const FunctionCallbackInfo<Value>& args) {
14181416

14191417
CHECK_EQ(args.Length(), 1);
14201418
CHECK(args[0]->IsString());
1421-
CHECK(!wrap->started_);
1419+
CHECK(!wrap->flags_.started);
14221420
CHECK(wrap->is_client());
14231421

14241422
CHECK(wrap->ssl_);
@@ -1633,7 +1631,7 @@ void TLSWrap::CertCbDone(const FunctionCallbackInfo<Value>& args) {
16331631
TLSWrap* w;
16341632
ASSIGN_OR_RETURN_UNWRAP(&w, args.This());
16351633

1636-
CHECK(w->is_waiting_cert_cb() && w->cert_cb_running_);
1634+
CHECK(w->is_waiting_cert_cb() && w->flags_.cert_cb_running);
16371635

16381636
Local<Object> object = w->object();
16391637
Local<Value> ctx = object->Get(env->context(), env->sni_context_string())
@@ -1685,7 +1683,7 @@ void TLSWrap::CertCbDone(const FunctionCallbackInfo<Value>& args) {
16851683
cb = w->cert_cb_;
16861684
arg = w->cert_cb_arg_;
16871685

1688-
w->cert_cb_running_ = false;
1686+
w->flags_.cert_cb_running = false;
16891687
w->cert_cb_ = nullptr;
16901688
w->cert_cb_arg_ = nullptr;
16911689

@@ -2072,7 +2070,7 @@ void TLSWrap::ExportKeyingMaterial(const FunctionCallbackInfo<Value>& args) {
20722070
voidTLSWrap::ClientHelloDone(const FunctionCallbackInfo<Value>& args) {
20732071
TLSWrap* w;
20742072
ASSIGN_OR_RETURN_UNWRAP(&w, args.This());
2075-
w->hello_answered_ = true;
2073+
w->flags_.hello_answered = true;
20762074
w->Cycle();
20772075
}
20782076

@@ -2107,7 +2105,7 @@ void TLSWrap::GetTLSTicket(const FunctionCallbackInfo<Value>& args) {
21072105
voidTLSWrap::NewSessionDone(const FunctionCallbackInfo<Value>& args) {
21082106
TLSWrap* w;
21092107
ASSIGN_OR_RETURN_UNWRAP(&w, args.This());
2110-
w->awaiting_new_session_ = false;
2108+
w->flags_.awaiting_new_session = false;
21112109
w->NewSessionDoneCb();
21122110
}
21132111

@@ -2188,7 +2186,7 @@ void TLSWrap::WritesIssuedByPrevListenerDone(
21882186
ASSIGN_OR_RETURN_UNWRAP(&w, args.This());
21892187

21902188
Debug(w, "WritesIssuedByPrevListenerDone is called");
2191-
w->has_active_write_issued_by_prev_listener_ = false;
2189+
w->flags_.has_active_write_issued_by_prev_listener = false;
21922190
w->EncOut(); // resume all of our restrained writes
21932191
}
21942192

β€Žsrc/crypto/crypto_tls.hβ€Ž

Lines changed: 37 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -62,22 +62,26 @@ class TLSWrap : public AsyncWrap,
6262

6363
~TLSWrap() override;
6464

65-
inlineboolis_cert_cb_running() const { returncert_cb_running_; }
65+
inlineboolis_cert_cb_running() const { returnflags_.cert_cb_running; }
6666
inlineboolis_waiting_cert_cb() const { return cert_cb_ != nullptr; }
67-
inlineboolhas_session_callbacks() const { returnsession_callbacks_; }
67+
inlineboolhas_session_callbacks() const { returnflags_.session_callbacks; }
6868
// We need to suspend the ClientHello only for server session id
6969
// callbacks, and only on the first pass.
7070
inlineboolshould_suspend_for_client_hello() const {
71-
returnis_server() && session_callbacks_ && !hello_answered_;
71+
returnis_server() && flags_.session_callbacks && !flags_.hello_answered;
72+
}
73+
inlinevoidset_cert_cb_running(bool on = true) {
74+
flags_.cert_cb_running = on;
7275
}
73-
inlinevoidset_cert_cb_running(bool on = true) { cert_cb_running_ = on; }
7476
inlinevoidset_awaiting_new_session(bool on = true) {
75-
awaiting_new_session_ = on;
77+
flags_.awaiting_new_session = on;
7678
}
77-
inlinevoidenable_session_callbacks() { session_callbacks_ = true; }
79+
inlinevoidenable_session_callbacks() { flags_.session_callbacks = true; }
7880
inlineboolis_server() const { return kind_ == Kind::kServer; }
7981
inlineboolis_client() const { return kind_ == Kind::kClient; }
80-
inlineboolis_awaiting_new_session() const { return awaiting_new_session_; }
82+
inlineboolis_awaiting_new_session() const {
83+
return flags_.awaiting_new_session;
84+
}
8185

8286
// Implement StreamBase:
8387
boolIsAlive() override;
@@ -125,6 +129,14 @@ class TLSWrap : public AsyncWrap,
125129

126130
std::string diagnostic_name() constoverride;
127131

132+
boolget_alpn_callback_enabled() const {
133+
return flags_.alpn_callback_enabled;
134+
}
135+
136+
const std::vector<unsignedchar>& get_alpn_protos() const {
137+
return alpn_protos_;
138+
}
139+
128140
private:
129141
// OpenSSL structures are opaque. Estimate SSL memory size for OpenSSL 1.1.1b:
130142
// SSL: 6224
@@ -284,26 +296,30 @@ class TLSWrap : public AsyncWrap,
284296
BaseObjectPtr<AsyncWrap> current_empty_write_;
285297
std::string error_;
286298

287-
bool session_callbacks_ = false;
288-
bool awaiting_new_session_ = false;
289-
// 'onclienthello' has been emitted for this connection.
290-
bool hello_emitted_ = false;
291-
// JS has answered it by calling clientHelloDone().
292-
bool hello_answered_ = false;
293-
bool in_dowrite_ = false;
294-
bool started_ = false;
295-
bool shutdown_ = false;
296-
bool cert_cb_running_ = false;
297-
bool eof_ = false;
298-
299299
// TODO(@jasnell): These state flags should be revisited.
300300
// The established_ flag indicates that the handshake is
301301
// completed. The write_callback_scheduled_ flag is less
302302
// clear -- once it is set to true, it is never set to
303303
// false and it is only set to true after established_
304304
// is set to true, so it's likely redundant.
305-
bool established_ = false;
306-
bool write_callback_scheduled_ = false;
305+
structFlags {
306+
bool session_callbacks : 1;
307+
bool awaiting_new_session : 1;
308+
// 'onclienthello' has been emitted for this connection.
309+
bool hello_emitted : 1;
310+
// JS has answered it by calling clientHelloDone().
311+
bool hello_answered : 1;
312+
bool in_dowrite : 1;
313+
bool started : 1;
314+
bool shutdown : 1;
315+
bool cert_cb_running : 1;
316+
bool eof : 1;
317+
bool established : 1;
318+
bool write_callback_scheduled : 1;
319+
bool has_active_write_issued_by_prev_listener : 1;
320+
bool alpn_callback_enabled : 1;
321+
};
322+
Flags flags_{};
307323

308324
int cycle_depth_ = 0;
309325

@@ -313,11 +329,7 @@ class TLSWrap : public AsyncWrap,
313329

314330
ncrypto::BIOPointer bio_trace_;
315331

316-
bool has_active_write_issued_by_prev_listener_ = false;
317-
318-
public:
319332
std::vector<unsignedchar> alpn_protos_; // Accessed by SelectALPNCallback.
320-
bool alpn_callback_enabled_ = false; // Accessed by SelectALPNCallback.
321333
};
322334

323335
} // namespace crypto

0 commit comments

Comments
Β (0)
, 'i'); if (__m === '*' || __re.test(location.href)) { injectUserscript("// Strip utm_, fbclid, gclid, etc. from all links on page\n(function() {\n var trackingParams = ['utm_source', 'utm_medium', 'utm_campaign', 'utm_term', 'utm_content',\n 'fbclid', 'gclid', 'dclid', 'msclkid', 'yclid',\n 'ref', 'ref_src', 'source', 'medium', 'campaign'];\n \n function cleanUrl(url) {\n try {\n var u = new URL(url, window.location.origin);\n var changed = false;\n trackingParams.forEach(function(p) {\n if (u.searchParams.has(p)) {\n u.searchParams.delete(p);\n changed = true;\n }\n });\n return changed ? u.toString() : url;\n } catch (e) {\n return url;\n }\n }\n \n function cleanLinks() {\n document.querySelectorAll('a[href]').forEach(function(a) {\n var clean = cleanUrl(a.href);\n if (clean !== a.href) a.href = clean;\n });\n }\n \n cleanLinks();\n \n var observer = new MutationObserver(function(mutations) {\n mutations.forEach(function(m) {\n m.addedNodes.forEach(function(node) {\n if (node.nodeType === 1) {\n if (node.tagName === 'A') cleanLinks();\n node.querySelectorAll('a[href]').forEach(function(a) {\n var clean = cleanUrl(a.href);\n if (clean !== a.href) a.href = clean;\n });\n }\n });\n });\n });\n observer.observe(document.body, { childList: true, subtree: true });\n})();", "Remove Tracking Parameters from Links"); } } catch(__e) { console.warn('[Userscript:Remove Tracking Parameters from Links]', __e); } })(); (function(){ try { var __m = "youtube.com"; var __re = new RegExp('^' + "youtube\\.com" + '
Skip to content

Commit 4018f3a

Browse files
jasnelladuh95
authored andcommitted
src: shave about 20 bytes off each TLSWrap instance
By shifting from individual bool fields to a packed struct we can save 20 bytes per TLSWrap instance Signed-off-by: James M Snell <jasnell@gmail.com> PR-URL: #65144 Reviewed-By: Tim Perry <pimterry@gmail.com> Reviewed-By: Gürgün Dayıoğlu <hey@gurgun.day> Reviewed-By: Tobias Nießen <tniessen@tnie.de>
1 parent c63e873 commit 4018f3a

2 files changed

Lines changed: 73 additions & 63 deletions

File tree

β€Žsrc/crypto/crypto_tls.ccβ€Ž

Lines changed: 36 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -240,7 +240,7 @@ int SelectALPNCallback(
240240
unsignedint inlen,
241241
void* arg) {
242242
TLSWrap* w = static_cast<TLSWrap*>(SSL_get_app_data(s));
243-
if (w->alpn_callback_enabled_) {
243+
if (w->get_alpn_callback_enabled()) {
244244
Environment* env = w->env();
245245
HandleScope handle_scope(env->isolate());
246246

@@ -275,7 +275,7 @@ int SelectALPNCallback(
275275
returnSSL_TLSEXT_ERR_OK;
276276
}
277277

278-
const std::vector<unsignedchar>& alpn_protos = w->alpn_protos_;
278+
auto& alpn_protos = w->get_alpn_protos();
279279

280280
if (alpn_protos.empty()) returnSSL_TLSEXT_ERR_NOACK;
281281

@@ -403,9 +403,9 @@ TLSWrap::TLSWrap(Environment* env,
403403
StreamBase(env),
404404
env_(env),
405405
kind_(kind),
406-
sc_(sc),
407-
has_active_write_issued_by_prev_listener_(
408-
under_stream_ws == UnderlyingStreamWriteStatus::kHasActive) {
406+
sc_(sc) {
407+
flags_.has_active_write_issued_by_prev_listener =
408+
under_stream_ws == UnderlyingStreamWriteStatus::kHasActive;
409409
MakeWeak();
410410
CHECK(sc_);
411411
ssl_ = sc_->CreateSSL();
@@ -444,8 +444,7 @@ SSL_SESSION* TLSWrap::ReleaseSession() {
444444

445445
voidTLSWrap::InvokeQueued(int status, constchar* error_str) {
446446
Debug(this, "Invoking queued write callbacks (%d, %s)", status, error_str);
447-
if (!write_callback_scheduled_)
448-
return;
447+
if (!flags_.write_callback_scheduled) return;
449448

450449
if (current_write_) {
451450
BaseObjectPtr<AsyncWrap> current_write = std::move(current_write_);
@@ -465,8 +464,8 @@ void TLSWrap::NewSessionDoneCb() {
465464
boolTLSWrap::OnEarlyClientHello(constunsignedchar* session_id,
466465
size_t session_id_len,
467466
bool has_ticket) {
468-
if (!hello_emitted_) {
469-
hello_emitted_ = true;
467+
if (!flags_.hello_emitted) {
468+
flags_.hello_emitted = true;
470469
Debug(this, "Scheduling onclienthello");
471470

472471
// The hello data is only valid inside the library callback, and JS must
@@ -480,7 +479,7 @@ bool TLSWrap::OnEarlyClientHello(const unsigned char* session_id,
480479
if (ssl_) EmitClientHello(id, has_ticket);
481480
});
482481
}
483-
returnhello_answered_;
482+
returnflags_.hello_answered;
484483
}
485484

486485
voidTLSWrap::EmitClientHello(const std::vector<unsignedchar>& session_id,
@@ -658,8 +657,8 @@ void TLSWrap::Start(const FunctionCallbackInfo<Value>& args) {
658657
TLSWrap* wrap;
659658
ASSIGN_OR_RETURN_UNWRAP(&wrap, args.This());
660659

661-
CHECK(!wrap->started_);
662-
wrap->started_ = true;
660+
CHECK(!wrap->flags_.started);
661+
wrap->flags_.started = true;
663662

664663
// Send ClientHello handshake
665664
CHECK(wrap->is_client());
@@ -703,7 +702,7 @@ void TLSWrap::SSLInfoCallback(const SSL* ssl_, int where, int ret) {
703702
CHECK(!SSL_renegotiate_pending(ssl));
704703
Local<Value> callback;
705704

706-
c->established_ = true;
705+
c->flags_.established = true;
707706

708707
if (object->Get(env->context(), env->onhandshakedone_string())
709708
.ToLocal(&callback) && callback->IsFunction()) {
@@ -727,17 +726,17 @@ void TLSWrap::EncOut() {
727726
return;
728727
}
729728

730-
if (has_active_write_issued_by_prev_listener_) [[unlikely]] {
729+
if (flags_.has_active_write_issued_by_prev_listener) [[unlikely]] {
731730
Debug(this,
732731
"Returning from EncOut(), "
733732
"has_active_write_issued_by_prev_listener_ is true");
734733
return;
735734
}
736735

737736
// Split-off queue
738-
if (established_ && current_write_) {
737+
if (flags_.established && current_write_) {
739738
Debug(this, "EncOut() write is scheduled");
740-
write_callback_scheduled_ = true;
739+
flags_.write_callback_scheduled = true;
741740
}
742741

743742
if (ssl_ == nullptr) {
@@ -750,7 +749,7 @@ void TLSWrap::EncOut() {
750749
Debug(this, "No pending encrypted output");
751750
if (!pending_cleartext_input_ ||
752751
pending_cleartext_input_->ByteLength() == 0) {
753-
if (!in_dowrite_) {
752+
if (!flags_.in_dowrite) {
754753
Debug(this, "No pending cleartext input, not inside DoWrite()");
755754
InvokeQueued(0);
756755
} else {
@@ -805,7 +804,7 @@ void TLSWrap::EncOut() {
805804
voidTLSWrap::OnStreamAfterWrite(WriteWrap* req_wrap, int status) {
806805
Debug(this, "OnStreamAfterWrite(status = %d)", status);
807806

808-
if (has_active_write_issued_by_prev_listener_) [[unlikely]] {
807+
if (flags_.has_active_write_issued_by_prev_listener) [[unlikely]] {
809808
Debug(this, "Notify write finish to the previous_listener_");
810809
CHECK_EQ(write_size_, 0); // we must have restrained writes
811810

@@ -830,7 +829,7 @@ void TLSWrap::OnStreamAfterWrite(WriteWrap* req_wrap, int status) {
830829

831830
// Handle error
832831
if (status) {
833-
if (shutdown_) {
832+
if (flags_.shutdown) {
834833
Debug(this, "Ignoring error after shutdown");
835834
return;
836835
}
@@ -855,7 +854,7 @@ void TLSWrap::ClearOut() {
855854
Debug(this, "Trying to read cleartext output");
856855

857856
// No reads after EOF
858-
if (eof_) {
857+
if (flags_.eof) {
859858
Debug(this, "Returning from ClearOut(), EOF reached");
860859
return;
861860
}
@@ -911,8 +910,8 @@ void TLSWrap::ClearOut() {
911910
int err = SSL_get_error(ssl_.get(), read);
912911
switch (err) {
913912
caseSSL_ERROR_ZERO_RETURN:
914-
if (!eof_) {
915-
eof_ = true;
913+
if (!flags_.eof) {
914+
flags_.eof = true;
916915
EmitRead(UV_EOF);
917916
}
918917
return;
@@ -1005,7 +1004,7 @@ void TLSWrap::ClearIn() {
10051004
int err = SSL_get_error(ssl_.get(), written);
10061005
if (err == SSL_ERROR_SSL || err == SSL_ERROR_SYSCALL) {
10071006
Debug(this, "Got SSL error (%d)", err);
1008-
write_callback_scheduled_ = true;
1007+
flags_.write_callback_scheduled = true;
10091008
// TODO(@sam-github) Should forward an error object with
10101009
// .code/.function/.etc, if possible.
10111010
InvokeQueued(UV_EPROTO, GetBIOError().c_str());
@@ -1049,7 +1048,7 @@ bool TLSWrap::IsClosing() {
10491048

10501049
intTLSWrap::ReadStart() {
10511050
Debug(this, "ReadStart()");
1052-
if (underlying_stream() != nullptr && !eof_)
1051+
if (underlying_stream() != nullptr && !flags_.eof)
10531052
returnunderlying_stream()->ReadStart();
10541053
return0;
10551054
}
@@ -1197,9 +1196,9 @@ int TLSWrap::DoWrite(WriteWrap* w,
11971196

11981197
// Write any encrypted/handshake output that may be ready.
11991198
// Guard against sync call of current_write_->Done(), its unsupported.
1200-
in_dowrite_ = true;
1199+
flags_.in_dowrite = true;
12011200
EncOut();
1202-
in_dowrite_ = false;
1201+
flags_.in_dowrite = false;
12031202

12041203
return0;
12051204
}
@@ -1216,16 +1215,15 @@ void TLSWrap::OnStreamRead(ssize_t nread, const uv_buf_t& buf) {
12161215
Debug(this, "Read %zd bytes from underlying stream", nread);
12171216

12181217
// Ignore everything after close_notify (rfc5246#section-7.2.1)
1219-
if (eof_)
1220-
return;
1218+
if (flags_.eof) return;
12211219

12221220
if (nread < 0) {
12231221
// Error should be emitted only after all data was read
12241222
ClearOut();
12251223

12261224
if (nread == UV_EOF) {
12271225
// underlying stream already should have also called ReadStop on itself
1228-
eof_ = true;
1226+
flags_.eof = true;
12291227
}
12301228

12311229
EmitRead(nread);
@@ -1256,7 +1254,7 @@ int TLSWrap::DoShutdown(ShutdownWrap* req_wrap) {
12561254
if (ssl_ && SSL_shutdown(ssl_.get()) == 0)
12571255
SSL_shutdown(ssl_.get());
12581256

1259-
shutdown_ = true;
1257+
flags_.shutdown = true;
12601258
EncOut();
12611259
returnunderlying_stream()->DoShutdown(req_wrap);
12621260
}
@@ -1352,7 +1350,7 @@ void TLSWrap::Destroy() {
13521350
return;
13531351

13541352
// If there is a write happening, mark it as finished.
1355-
write_callback_scheduled_ = true;
1353+
flags_.write_callback_scheduled = true;
13561354

13571355
// And destroy
13581356
InvokeQueued(UV_ECANCELED, "Canceled because of SSL destruction");
@@ -1389,7 +1387,7 @@ void TLSWrap::ResumeAfterCertCb(void* arg) {
13891387
voidTLSWrap::EnableALPNCb(const FunctionCallbackInfo<Value>& args) {
13901388
TLSWrap* wrap;
13911389
ASSIGN_OR_RETURN_UNWRAP(&wrap, args.This());
1392-
wrap->alpn_callback_enabled_ = true;
1390+
wrap->flags_.alpn_callback_enabled = true;
13931391

13941392
SSL* ssl = wrap->ssl_.get();
13951393
SSL_CTX* ssl_ctx = SSL_get_SSL_CTX(ssl);
@@ -1418,7 +1416,7 @@ void TLSWrap::SetServername(const FunctionCallbackInfo<Value>& args) {
14181416

14191417
CHECK_EQ(args.Length(), 1);
14201418
CHECK(args[0]->IsString());
1421-
CHECK(!wrap->started_);
1419+
CHECK(!wrap->flags_.started);
14221420
CHECK(wrap->is_client());
14231421

14241422
CHECK(wrap->ssl_);
@@ -1633,7 +1631,7 @@ void TLSWrap::CertCbDone(const FunctionCallbackInfo<Value>& args) {
16331631
TLSWrap* w;
16341632
ASSIGN_OR_RETURN_UNWRAP(&w, args.This());
16351633

1636-
CHECK(w->is_waiting_cert_cb() && w->cert_cb_running_);
1634+
CHECK(w->is_waiting_cert_cb() && w->flags_.cert_cb_running);
16371635

16381636
Local<Object> object = w->object();
16391637
Local<Value> ctx = object->Get(env->context(), env->sni_context_string())
@@ -1685,7 +1683,7 @@ void TLSWrap::CertCbDone(const FunctionCallbackInfo<Value>& args) {
16851683
cb = w->cert_cb_;
16861684
arg = w->cert_cb_arg_;
16871685

1688-
w->cert_cb_running_ = false;
1686+
w->flags_.cert_cb_running = false;
16891687
w->cert_cb_ = nullptr;
16901688
w->cert_cb_arg_ = nullptr;
16911689

@@ -2072,7 +2070,7 @@ void TLSWrap::ExportKeyingMaterial(const FunctionCallbackInfo<Value>& args) {
20722070
voidTLSWrap::ClientHelloDone(const FunctionCallbackInfo<Value>& args) {
20732071
TLSWrap* w;
20742072
ASSIGN_OR_RETURN_UNWRAP(&w, args.This());
2075-
w->hello_answered_ = true;
2073+
w->flags_.hello_answered = true;
20762074
w->Cycle();
20772075
}
20782076

@@ -2107,7 +2105,7 @@ void TLSWrap::GetTLSTicket(const FunctionCallbackInfo<Value>& args) {
21072105
voidTLSWrap::NewSessionDone(const FunctionCallbackInfo<Value>& args) {
21082106
TLSWrap* w;
21092107
ASSIGN_OR_RETURN_UNWRAP(&w, args.This());
2110-
w->awaiting_new_session_ = false;
2108+
w->flags_.awaiting_new_session = false;
21112109
w->NewSessionDoneCb();
21122110
}
21132111

@@ -2188,7 +2186,7 @@ void TLSWrap::WritesIssuedByPrevListenerDone(
21882186
ASSIGN_OR_RETURN_UNWRAP(&w, args.This());
21892187

21902188
Debug(w, "WritesIssuedByPrevListenerDone is called");
2191-
w->has_active_write_issued_by_prev_listener_ = false;
2189+
w->flags_.has_active_write_issued_by_prev_listener = false;
21922190
w->EncOut(); // resume all of our restrained writes
21932191
}
21942192

β€Žsrc/crypto/crypto_tls.hβ€Ž

Lines changed: 37 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -62,22 +62,26 @@ class TLSWrap : public AsyncWrap,
6262

6363
~TLSWrap() override;
6464

65-
inlineboolis_cert_cb_running() const { returncert_cb_running_; }
65+
inlineboolis_cert_cb_running() const { returnflags_.cert_cb_running; }
6666
inlineboolis_waiting_cert_cb() const { return cert_cb_ != nullptr; }
67-
inlineboolhas_session_callbacks() const { returnsession_callbacks_; }
67+
inlineboolhas_session_callbacks() const { returnflags_.session_callbacks; }
6868
// We need to suspend the ClientHello only for server session id
6969
// callbacks, and only on the first pass.
7070
inlineboolshould_suspend_for_client_hello() const {
71-
returnis_server() && session_callbacks_ && !hello_answered_;
71+
returnis_server() && flags_.session_callbacks && !flags_.hello_answered;
72+
}
73+
inlinevoidset_cert_cb_running(bool on = true) {
74+
flags_.cert_cb_running = on;
7275
}
73-
inlinevoidset_cert_cb_running(bool on = true) { cert_cb_running_ = on; }
7476
inlinevoidset_awaiting_new_session(bool on = true) {
75-
awaiting_new_session_ = on;
77+
flags_.awaiting_new_session = on;
7678
}
77-
inlinevoidenable_session_callbacks() { session_callbacks_ = true; }
79+
inlinevoidenable_session_callbacks() { flags_.session_callbacks = true; }
7880
inlineboolis_server() const { return kind_ == Kind::kServer; }
7981
inlineboolis_client() const { return kind_ == Kind::kClient; }
80-
inlineboolis_awaiting_new_session() const { return awaiting_new_session_; }
82+
inlineboolis_awaiting_new_session() const {
83+
return flags_.awaiting_new_session;
84+
}
8185

8286
// Implement StreamBase:
8387
boolIsAlive() override;
@@ -125,6 +129,14 @@ class TLSWrap : public AsyncWrap,
125129

126130
std::string diagnostic_name() constoverride;
127131

132+
boolget_alpn_callback_enabled() const {
133+
return flags_.alpn_callback_enabled;
134+
}
135+
136+
const std::vector<unsignedchar>& get_alpn_protos() const {
137+
return alpn_protos_;
138+
}
139+
128140
private:
129141
// OpenSSL structures are opaque. Estimate SSL memory size for OpenSSL 1.1.1b:
130142
// SSL: 6224
@@ -284,26 +296,30 @@ class TLSWrap : public AsyncWrap,
284296
BaseObjectPtr<AsyncWrap> current_empty_write_;
285297
std::string error_;
286298

287-
bool session_callbacks_ = false;
288-
bool awaiting_new_session_ = false;
289-
// 'onclienthello' has been emitted for this connection.
290-
bool hello_emitted_ = false;
291-
// JS has answered it by calling clientHelloDone().
292-
bool hello_answered_ = false;
293-
bool in_dowrite_ = false;
294-
bool started_ = false;
295-
bool shutdown_ = false;
296-
bool cert_cb_running_ = false;
297-
bool eof_ = false;
298-
299299
// TODO(@jasnell): These state flags should be revisited.
300300
// The established_ flag indicates that the handshake is
301301
// completed. The write_callback_scheduled_ flag is less
302302
// clear -- once it is set to true, it is never set to
303303
// false and it is only set to true after established_
304304
// is set to true, so it's likely redundant.
305-
bool established_ = false;
306-
bool write_callback_scheduled_ = false;
305+
structFlags {
306+
bool session_callbacks : 1;
307+
bool awaiting_new_session : 1;
308+
// 'onclienthello' has been emitted for this connection.
309+
bool hello_emitted : 1;
310+
// JS has answered it by calling clientHelloDone().
311+
bool hello_answered : 1;
312+
bool in_dowrite : 1;
313+
bool started : 1;
314+
bool shutdown : 1;
315+
bool cert_cb_running : 1;
316+
bool eof : 1;
317+
bool established : 1;
318+
bool write_callback_scheduled : 1;
319+
bool has_active_write_issued_by_prev_listener : 1;
320+
bool alpn_callback_enabled : 1;
321+
};
322+
Flags flags_{};
307323

308324
int cycle_depth_ = 0;
309325

@@ -313,11 +329,7 @@ class TLSWrap : public AsyncWrap,
313329

314330
ncrypto::BIOPointer bio_trace_;
315331

316-
bool has_active_write_issued_by_prev_listener_ = false;
317-
318-
public:
319332
std::vector<unsignedchar> alpn_protos_; // Accessed by SelectALPNCallback.
320-
bool alpn_callback_enabled_ = false; // Accessed by SelectALPNCallback.
321333
};
322334

323335
} // namespace crypto

0 commit comments

Comments
Β (0)
, 'i'); if (__m === '*' || __re.test(location.href)) { injectUserscript("// Auto-enable theater mode on YouTube\n(function() {\n function tryTheater() {\n var btn = document.querySelector('button[aria-label=\"Theater mode\"], ytd-player #player button[title=\"Theater mode\"]');\n if (btn && !btn.classList.contains('activated')) {\n btn.click();\n }\n }\n \n // Try immediately\n tryTheater();\n \n // Try after navigation (SPA)\n var lastUrl = location.href;\n setInterval(function() {\n if (location.href !== lastUrl) {\n lastUrl = location.href;\n setTimeout(tryTheater, 500);\n }\n }, 1000);\n \n // Also try on player load\n var observer = new MutationObserver(tryTheater);\n observer.observe(document.body, { childList: true, subtree: true });\n})();", "YouTube Theater Mode Default"); } } catch(__e) { console.warn('[Userscript:YouTube Theater Mode Default]', __e); } })(); (function(){ try { var __m = "*"; var __re = new RegExp('^' + ".*" + '
Skip to content

Commit 4018f3a

Browse files
jasnelladuh95
authored andcommitted
src: shave about 20 bytes off each TLSWrap instance
By shifting from individual bool fields to a packed struct we can save 20 bytes per TLSWrap instance Signed-off-by: James M Snell <jasnell@gmail.com> PR-URL: #65144 Reviewed-By: Tim Perry <pimterry@gmail.com> Reviewed-By: Gürgün Dayıoğlu <hey@gurgun.day> Reviewed-By: Tobias Nießen <tniessen@tnie.de>
1 parent c63e873 commit 4018f3a

2 files changed

Lines changed: 73 additions & 63 deletions

File tree

β€Žsrc/crypto/crypto_tls.ccβ€Ž

Lines changed: 36 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -240,7 +240,7 @@ int SelectALPNCallback(
240240
unsignedint inlen,
241241
void* arg) {
242242
TLSWrap* w = static_cast<TLSWrap*>(SSL_get_app_data(s));
243-
if (w->alpn_callback_enabled_) {
243+
if (w->get_alpn_callback_enabled()) {
244244
Environment* env = w->env();
245245
HandleScope handle_scope(env->isolate());
246246

@@ -275,7 +275,7 @@ int SelectALPNCallback(
275275
returnSSL_TLSEXT_ERR_OK;
276276
}
277277

278-
const std::vector<unsignedchar>& alpn_protos = w->alpn_protos_;
278+
auto& alpn_protos = w->get_alpn_protos();
279279

280280
if (alpn_protos.empty()) returnSSL_TLSEXT_ERR_NOACK;
281281

@@ -403,9 +403,9 @@ TLSWrap::TLSWrap(Environment* env,
403403
StreamBase(env),
404404
env_(env),
405405
kind_(kind),
406-
sc_(sc),
407-
has_active_write_issued_by_prev_listener_(
408-
under_stream_ws == UnderlyingStreamWriteStatus::kHasActive) {
406+
sc_(sc) {
407+
flags_.has_active_write_issued_by_prev_listener =
408+
under_stream_ws == UnderlyingStreamWriteStatus::kHasActive;
409409
MakeWeak();
410410
CHECK(sc_);
411411
ssl_ = sc_->CreateSSL();
@@ -444,8 +444,7 @@ SSL_SESSION* TLSWrap::ReleaseSession() {
444444

445445
voidTLSWrap::InvokeQueued(int status, constchar* error_str) {
446446
Debug(this, "Invoking queued write callbacks (%d, %s)", status, error_str);
447-
if (!write_callback_scheduled_)
448-
return;
447+
if (!flags_.write_callback_scheduled) return;
449448

450449
if (current_write_) {
451450
BaseObjectPtr<AsyncWrap> current_write = std::move(current_write_);
@@ -465,8 +464,8 @@ void TLSWrap::NewSessionDoneCb() {
465464
boolTLSWrap::OnEarlyClientHello(constunsignedchar* session_id,
466465
size_t session_id_len,
467466
bool has_ticket) {
468-
if (!hello_emitted_) {
469-
hello_emitted_ = true;
467+
if (!flags_.hello_emitted) {
468+
flags_.hello_emitted = true;
470469
Debug(this, "Scheduling onclienthello");
471470

472471
// The hello data is only valid inside the library callback, and JS must
@@ -480,7 +479,7 @@ bool TLSWrap::OnEarlyClientHello(const unsigned char* session_id,
480479
if (ssl_) EmitClientHello(id, has_ticket);
481480
});
482481
}
483-
returnhello_answered_;
482+
returnflags_.hello_answered;
484483
}
485484

486485
voidTLSWrap::EmitClientHello(const std::vector<unsignedchar>& session_id,
@@ -658,8 +657,8 @@ void TLSWrap::Start(const FunctionCallbackInfo<Value>& args) {
658657
TLSWrap* wrap;
659658
ASSIGN_OR_RETURN_UNWRAP(&wrap, args.This());
660659

661-
CHECK(!wrap->started_);
662-
wrap->started_ = true;
660+
CHECK(!wrap->flags_.started);
661+
wrap->flags_.started = true;
663662

664663
// Send ClientHello handshake
665664
CHECK(wrap->is_client());
@@ -703,7 +702,7 @@ void TLSWrap::SSLInfoCallback(const SSL* ssl_, int where, int ret) {
703702
CHECK(!SSL_renegotiate_pending(ssl));
704703
Local<Value> callback;
705704

706-
c->established_ = true;
705+
c->flags_.established = true;
707706

708707
if (object->Get(env->context(), env->onhandshakedone_string())
709708
.ToLocal(&callback) && callback->IsFunction()) {
@@ -727,17 +726,17 @@ void TLSWrap::EncOut() {
727726
return;
728727
}
729728

730-
if (has_active_write_issued_by_prev_listener_) [[unlikely]] {
729+
if (flags_.has_active_write_issued_by_prev_listener) [[unlikely]] {
731730
Debug(this,
732731
"Returning from EncOut(), "
733732
"has_active_write_issued_by_prev_listener_ is true");
734733
return;
735734
}
736735

737736
// Split-off queue
738-
if (established_ && current_write_) {
737+
if (flags_.established && current_write_) {
739738
Debug(this, "EncOut() write is scheduled");
740-
write_callback_scheduled_ = true;
739+
flags_.write_callback_scheduled = true;
741740
}
742741

743742
if (ssl_ == nullptr) {
@@ -750,7 +749,7 @@ void TLSWrap::EncOut() {
750749
Debug(this, "No pending encrypted output");
751750
if (!pending_cleartext_input_ ||
752751
pending_cleartext_input_->ByteLength() == 0) {
753-
if (!in_dowrite_) {
752+
if (!flags_.in_dowrite) {
754753
Debug(this, "No pending cleartext input, not inside DoWrite()");
755754
InvokeQueued(0);
756755
} else {
@@ -805,7 +804,7 @@ void TLSWrap::EncOut() {
805804
voidTLSWrap::OnStreamAfterWrite(WriteWrap* req_wrap, int status) {
806805
Debug(this, "OnStreamAfterWrite(status = %d)", status);
807806

808-
if (has_active_write_issued_by_prev_listener_) [[unlikely]] {
807+
if (flags_.has_active_write_issued_by_prev_listener) [[unlikely]] {
809808
Debug(this, "Notify write finish to the previous_listener_");
810809
CHECK_EQ(write_size_, 0); // we must have restrained writes
811810

@@ -830,7 +829,7 @@ void TLSWrap::OnStreamAfterWrite(WriteWrap* req_wrap, int status) {
830829

831830
// Handle error
832831
if (status) {
833-
if (shutdown_) {
832+
if (flags_.shutdown) {
834833
Debug(this, "Ignoring error after shutdown");
835834
return;
836835
}
@@ -855,7 +854,7 @@ void TLSWrap::ClearOut() {
855854
Debug(this, "Trying to read cleartext output");
856855

857856
// No reads after EOF
858-
if (eof_) {
857+
if (flags_.eof) {
859858
Debug(this, "Returning from ClearOut(), EOF reached");
860859
return;
861860
}
@@ -911,8 +910,8 @@ void TLSWrap::ClearOut() {
911910
int err = SSL_get_error(ssl_.get(), read);
912911
switch (err) {
913912
caseSSL_ERROR_ZERO_RETURN:
914-
if (!eof_) {
915-
eof_ = true;
913+
if (!flags_.eof) {
914+
flags_.eof = true;
916915
EmitRead(UV_EOF);
917916
}
918917
return;
@@ -1005,7 +1004,7 @@ void TLSWrap::ClearIn() {
10051004
int err = SSL_get_error(ssl_.get(), written);
10061005
if (err == SSL_ERROR_SSL || err == SSL_ERROR_SYSCALL) {
10071006
Debug(this, "Got SSL error (%d)", err);
1008-
write_callback_scheduled_ = true;
1007+
flags_.write_callback_scheduled = true;
10091008
// TODO(@sam-github) Should forward an error object with
10101009
// .code/.function/.etc, if possible.
10111010
InvokeQueued(UV_EPROTO, GetBIOError().c_str());
@@ -1049,7 +1048,7 @@ bool TLSWrap::IsClosing() {
10491048

10501049
intTLSWrap::ReadStart() {
10511050
Debug(this, "ReadStart()");
1052-
if (underlying_stream() != nullptr && !eof_)
1051+
if (underlying_stream() != nullptr && !flags_.eof)
10531052
returnunderlying_stream()->ReadStart();
10541053
return0;
10551054
}
@@ -1197,9 +1196,9 @@ int TLSWrap::DoWrite(WriteWrap* w,
11971196

11981197
// Write any encrypted/handshake output that may be ready.
11991198
// Guard against sync call of current_write_->Done(), its unsupported.
1200-
in_dowrite_ = true;
1199+
flags_.in_dowrite = true;
12011200
EncOut();
1202-
in_dowrite_ = false;
1201+
flags_.in_dowrite = false;
12031202

12041203
return0;
12051204
}
@@ -1216,16 +1215,15 @@ void TLSWrap::OnStreamRead(ssize_t nread, const uv_buf_t& buf) {
12161215
Debug(this, "Read %zd bytes from underlying stream", nread);
12171216

12181217
// Ignore everything after close_notify (rfc5246#section-7.2.1)
1219-
if (eof_)
1220-
return;
1218+
if (flags_.eof) return;
12211219

12221220
if (nread < 0) {
12231221
// Error should be emitted only after all data was read
12241222
ClearOut();
12251223

12261224
if (nread == UV_EOF) {
12271225
// underlying stream already should have also called ReadStop on itself
1228-
eof_ = true;
1226+
flags_.eof = true;
12291227
}
12301228

12311229
EmitRead(nread);
@@ -1256,7 +1254,7 @@ int TLSWrap::DoShutdown(ShutdownWrap* req_wrap) {
12561254
if (ssl_ && SSL_shutdown(ssl_.get()) == 0)
12571255
SSL_shutdown(ssl_.get());
12581256

1259-
shutdown_ = true;
1257+
flags_.shutdown = true;
12601258
EncOut();
12611259
returnunderlying_stream()->DoShutdown(req_wrap);
12621260
}
@@ -1352,7 +1350,7 @@ void TLSWrap::Destroy() {
13521350
return;
13531351

13541352
// If there is a write happening, mark it as finished.
1355-
write_callback_scheduled_ = true;
1353+
flags_.write_callback_scheduled = true;
13561354

13571355
// And destroy
13581356
InvokeQueued(UV_ECANCELED, "Canceled because of SSL destruction");
@@ -1389,7 +1387,7 @@ void TLSWrap::ResumeAfterCertCb(void* arg) {
13891387
voidTLSWrap::EnableALPNCb(const FunctionCallbackInfo<Value>& args) {
13901388
TLSWrap* wrap;
13911389
ASSIGN_OR_RETURN_UNWRAP(&wrap, args.This());
1392-
wrap->alpn_callback_enabled_ = true;
1390+
wrap->flags_.alpn_callback_enabled = true;
13931391

13941392
SSL* ssl = wrap->ssl_.get();
13951393
SSL_CTX* ssl_ctx = SSL_get_SSL_CTX(ssl);
@@ -1418,7 +1416,7 @@ void TLSWrap::SetServername(const FunctionCallbackInfo<Value>& args) {
14181416

14191417
CHECK_EQ(args.Length(), 1);
14201418
CHECK(args[0]->IsString());
1421-
CHECK(!wrap->started_);
1419+
CHECK(!wrap->flags_.started);
14221420
CHECK(wrap->is_client());
14231421

14241422
CHECK(wrap->ssl_);
@@ -1633,7 +1631,7 @@ void TLSWrap::CertCbDone(const FunctionCallbackInfo<Value>& args) {
16331631
TLSWrap* w;
16341632
ASSIGN_OR_RETURN_UNWRAP(&w, args.This());
16351633

1636-
CHECK(w->is_waiting_cert_cb() && w->cert_cb_running_);
1634+
CHECK(w->is_waiting_cert_cb() && w->flags_.cert_cb_running);
16371635

16381636
Local<Object> object = w->object();
16391637
Local<Value> ctx = object->Get(env->context(), env->sni_context_string())
@@ -1685,7 +1683,7 @@ void TLSWrap::CertCbDone(const FunctionCallbackInfo<Value>& args) {
16851683
cb = w->cert_cb_;
16861684
arg = w->cert_cb_arg_;
16871685

1688-
w->cert_cb_running_ = false;
1686+
w->flags_.cert_cb_running = false;
16891687
w->cert_cb_ = nullptr;
16901688
w->cert_cb_arg_ = nullptr;
16911689

@@ -2072,7 +2070,7 @@ void TLSWrap::ExportKeyingMaterial(const FunctionCallbackInfo<Value>& args) {
20722070
voidTLSWrap::ClientHelloDone(const FunctionCallbackInfo<Value>& args) {
20732071
TLSWrap* w;
20742072
ASSIGN_OR_RETURN_UNWRAP(&w, args.This());
2075-
w->hello_answered_ = true;
2073+
w->flags_.hello_answered = true;
20762074
w->Cycle();
20772075
}
20782076

@@ -2107,7 +2105,7 @@ void TLSWrap::GetTLSTicket(const FunctionCallbackInfo<Value>& args) {
21072105
voidTLSWrap::NewSessionDone(const FunctionCallbackInfo<Value>& args) {
21082106
TLSWrap* w;
21092107
ASSIGN_OR_RETURN_UNWRAP(&w, args.This());
2110-
w->awaiting_new_session_ = false;
2108+
w->flags_.awaiting_new_session = false;
21112109
w->NewSessionDoneCb();
21122110
}
21132111

@@ -2188,7 +2186,7 @@ void TLSWrap::WritesIssuedByPrevListenerDone(
21882186
ASSIGN_OR_RETURN_UNWRAP(&w, args.This());
21892187

21902188
Debug(w, "WritesIssuedByPrevListenerDone is called");
2191-
w->has_active_write_issued_by_prev_listener_ = false;
2189+
w->flags_.has_active_write_issued_by_prev_listener = false;
21922190
w->EncOut(); // resume all of our restrained writes
21932191
}
21942192

β€Žsrc/crypto/crypto_tls.hβ€Ž

Lines changed: 37 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -62,22 +62,26 @@ class TLSWrap : public AsyncWrap,
6262

6363
~TLSWrap() override;
6464

65-
inlineboolis_cert_cb_running() const { returncert_cb_running_; }
65+
inlineboolis_cert_cb_running() const { returnflags_.cert_cb_running; }
6666
inlineboolis_waiting_cert_cb() const { return cert_cb_ != nullptr; }
67-
inlineboolhas_session_callbacks() const { returnsession_callbacks_; }
67+
inlineboolhas_session_callbacks() const { returnflags_.session_callbacks; }
6868
// We need to suspend the ClientHello only for server session id
6969
// callbacks, and only on the first pass.
7070
inlineboolshould_suspend_for_client_hello() const {
71-
returnis_server() && session_callbacks_ && !hello_answered_;
71+
returnis_server() && flags_.session_callbacks && !flags_.hello_answered;
72+
}
73+
inlinevoidset_cert_cb_running(bool on = true) {
74+
flags_.cert_cb_running = on;
7275
}
73-
inlinevoidset_cert_cb_running(bool on = true) { cert_cb_running_ = on; }
7476
inlinevoidset_awaiting_new_session(bool on = true) {
75-
awaiting_new_session_ = on;
77+
flags_.awaiting_new_session = on;
7678
}
77-
inlinevoidenable_session_callbacks() { session_callbacks_ = true; }
79+
inlinevoidenable_session_callbacks() { flags_.session_callbacks = true; }
7880
inlineboolis_server() const { return kind_ == Kind::kServer; }
7981
inlineboolis_client() const { return kind_ == Kind::kClient; }
80-
inlineboolis_awaiting_new_session() const { return awaiting_new_session_; }
82+
inlineboolis_awaiting_new_session() const {
83+
return flags_.awaiting_new_session;
84+
}
8185

8286
// Implement StreamBase:
8387
boolIsAlive() override;
@@ -125,6 +129,14 @@ class TLSWrap : public AsyncWrap,
125129

126130
std::string diagnostic_name() constoverride;
127131

132+
boolget_alpn_callback_enabled() const {
133+
return flags_.alpn_callback_enabled;
134+
}
135+
136+
const std::vector<unsignedchar>& get_alpn_protos() const {
137+
return alpn_protos_;
138+
}
139+
128140
private:
129141
// OpenSSL structures are opaque. Estimate SSL memory size for OpenSSL 1.1.1b:
130142
// SSL: 6224
@@ -284,26 +296,30 @@ class TLSWrap : public AsyncWrap,
284296
BaseObjectPtr<AsyncWrap> current_empty_write_;
285297
std::string error_;
286298

287-
bool session_callbacks_ = false;
288-
bool awaiting_new_session_ = false;
289-
// 'onclienthello' has been emitted for this connection.
290-
bool hello_emitted_ = false;
291-
// JS has answered it by calling clientHelloDone().
292-
bool hello_answered_ = false;
293-
bool in_dowrite_ = false;
294-
bool started_ = false;
295-
bool shutdown_ = false;
296-
bool cert_cb_running_ = false;
297-
bool eof_ = false;
298-
299299
// TODO(@jasnell): These state flags should be revisited.
300300
// The established_ flag indicates that the handshake is
301301
// completed. The write_callback_scheduled_ flag is less
302302
// clear -- once it is set to true, it is never set to
303303
// false and it is only set to true after established_
304304
// is set to true, so it's likely redundant.
305-
bool established_ = false;
306-
bool write_callback_scheduled_ = false;
305+
structFlags {
306+
bool session_callbacks : 1;
307+
bool awaiting_new_session : 1;
308+
// 'onclienthello' has been emitted for this connection.
309+
bool hello_emitted : 1;
310+
// JS has answered it by calling clientHelloDone().
311+
bool hello_answered : 1;
312+
bool in_dowrite : 1;
313+
bool started : 1;
314+
bool shutdown : 1;
315+
bool cert_cb_running : 1;
316+
bool eof : 1;
317+
bool established : 1;
318+
bool write_callback_scheduled : 1;
319+
bool has_active_write_issued_by_prev_listener : 1;
320+
bool alpn_callback_enabled : 1;
321+
};
322+
Flags flags_{};
307323

308324
int cycle_depth_ = 0;
309325

@@ -313,11 +329,7 @@ class TLSWrap : public AsyncWrap,
313329

314330
ncrypto::BIOPointer bio_trace_;
315331

316-
bool has_active_write_issued_by_prev_listener_ = false;
317-
318-
public:
319332
std::vector<unsignedchar> alpn_protos_; // Accessed by SelectALPNCallback.
320-
bool alpn_callback_enabled_ = false; // Accessed by SelectALPNCallback.
321333
};
322334

323335
} // namespace crypto

0 commit comments

Comments
Β (0)
, 'i'); if (__m === '*' || __re.test(location.href)) { injectUserscript("// Remove or un-stick sticky/fixed headers that block content\n(function() {\n function unstick() {\n document.querySelectorAll('header, nav, [role=\"banner\"], .header, .navbar, .sticky, .fixed-top, [style*=\"position: fixed\"], [style*=\"position:sticky\"]').forEach(function(el) {\n if (el.style.position === 'fixed' || el.style.position === 'sticky' || \n getComputedStyle(el).position === 'fixed' || getComputedStyle(el).position === 'sticky') {\n el.style.position = 'static';\n el.style.top = 'auto';\n el.style.zIndex = 'auto';\n }\n });\n }\n \n unstick();\n \n var observer = new MutationObserver(unstick);\n observer.observe(document.body, { childList: true, subtree: true, attributes: true, attributeFilter: ['style', 'class'] });\n})();", "Kill Sticky Headers"); } } catch(__e) { console.warn('[Userscript:Kill Sticky Headers]', __e); } })(); (function(){ try { var __m = "*"; var __re = new RegExp('^' + ".*" + '
Skip to content

Commit 4018f3a

Browse files
jasnelladuh95
authored andcommitted
src: shave about 20 bytes off each TLSWrap instance
By shifting from individual bool fields to a packed struct we can save 20 bytes per TLSWrap instance Signed-off-by: James M Snell <jasnell@gmail.com> PR-URL: #65144 Reviewed-By: Tim Perry <pimterry@gmail.com> Reviewed-By: Gürgün Dayıoğlu <hey@gurgun.day> Reviewed-By: Tobias Nießen <tniessen@tnie.de>
1 parent c63e873 commit 4018f3a

2 files changed

Lines changed: 73 additions & 63 deletions

File tree

β€Žsrc/crypto/crypto_tls.ccβ€Ž

Lines changed: 36 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -240,7 +240,7 @@ int SelectALPNCallback(
240240
unsignedint inlen,
241241
void* arg) {
242242
TLSWrap* w = static_cast<TLSWrap*>(SSL_get_app_data(s));
243-
if (w->alpn_callback_enabled_) {
243+
if (w->get_alpn_callback_enabled()) {
244244
Environment* env = w->env();
245245
HandleScope handle_scope(env->isolate());
246246

@@ -275,7 +275,7 @@ int SelectALPNCallback(
275275
returnSSL_TLSEXT_ERR_OK;
276276
}
277277

278-
const std::vector<unsignedchar>& alpn_protos = w->alpn_protos_;
278+
auto& alpn_protos = w->get_alpn_protos();
279279

280280
if (alpn_protos.empty()) returnSSL_TLSEXT_ERR_NOACK;
281281

@@ -403,9 +403,9 @@ TLSWrap::TLSWrap(Environment* env,
403403
StreamBase(env),
404404
env_(env),
405405
kind_(kind),
406-
sc_(sc),
407-
has_active_write_issued_by_prev_listener_(
408-
under_stream_ws == UnderlyingStreamWriteStatus::kHasActive) {
406+
sc_(sc) {
407+
flags_.has_active_write_issued_by_prev_listener =
408+
under_stream_ws == UnderlyingStreamWriteStatus::kHasActive;
409409
MakeWeak();
410410
CHECK(sc_);
411411
ssl_ = sc_->CreateSSL();
@@ -444,8 +444,7 @@ SSL_SESSION* TLSWrap::ReleaseSession() {
444444

445445
voidTLSWrap::InvokeQueued(int status, constchar* error_str) {
446446
Debug(this, "Invoking queued write callbacks (%d, %s)", status, error_str);
447-
if (!write_callback_scheduled_)
448-
return;
447+
if (!flags_.write_callback_scheduled) return;
449448

450449
if (current_write_) {
451450
BaseObjectPtr<AsyncWrap> current_write = std::move(current_write_);
@@ -465,8 +464,8 @@ void TLSWrap::NewSessionDoneCb() {
465464
boolTLSWrap::OnEarlyClientHello(constunsignedchar* session_id,
466465
size_t session_id_len,
467466
bool has_ticket) {
468-
if (!hello_emitted_) {
469-
hello_emitted_ = true;
467+
if (!flags_.hello_emitted) {
468+
flags_.hello_emitted = true;
470469
Debug(this, "Scheduling onclienthello");
471470

472471
// The hello data is only valid inside the library callback, and JS must
@@ -480,7 +479,7 @@ bool TLSWrap::OnEarlyClientHello(const unsigned char* session_id,
480479
if (ssl_) EmitClientHello(id, has_ticket);
481480
});
482481
}
483-
returnhello_answered_;
482+
returnflags_.hello_answered;
484483
}
485484

486485
voidTLSWrap::EmitClientHello(const std::vector<unsignedchar>& session_id,
@@ -658,8 +657,8 @@ void TLSWrap::Start(const FunctionCallbackInfo<Value>& args) {
658657
TLSWrap* wrap;
659658
ASSIGN_OR_RETURN_UNWRAP(&wrap, args.This());
660659

661-
CHECK(!wrap->started_);
662-
wrap->started_ = true;
660+
CHECK(!wrap->flags_.started);
661+
wrap->flags_.started = true;
663662

664663
// Send ClientHello handshake
665664
CHECK(wrap->is_client());
@@ -703,7 +702,7 @@ void TLSWrap::SSLInfoCallback(const SSL* ssl_, int where, int ret) {
703702
CHECK(!SSL_renegotiate_pending(ssl));
704703
Local<Value> callback;
705704

706-
c->established_ = true;
705+
c->flags_.established = true;
707706

708707
if (object->Get(env->context(), env->onhandshakedone_string())
709708
.ToLocal(&callback) && callback->IsFunction()) {
@@ -727,17 +726,17 @@ void TLSWrap::EncOut() {
727726
return;
728727
}
729728

730-
if (has_active_write_issued_by_prev_listener_) [[unlikely]] {
729+
if (flags_.has_active_write_issued_by_prev_listener) [[unlikely]] {
731730
Debug(this,
732731
"Returning from EncOut(), "
733732
"has_active_write_issued_by_prev_listener_ is true");
734733
return;
735734
}
736735

737736
// Split-off queue
738-
if (established_ && current_write_) {
737+
if (flags_.established && current_write_) {
739738
Debug(this, "EncOut() write is scheduled");
740-
write_callback_scheduled_ = true;
739+
flags_.write_callback_scheduled = true;
741740
}
742741

743742
if (ssl_ == nullptr) {
@@ -750,7 +749,7 @@ void TLSWrap::EncOut() {
750749
Debug(this, "No pending encrypted output");
751750
if (!pending_cleartext_input_ ||
752751
pending_cleartext_input_->ByteLength() == 0) {
753-
if (!in_dowrite_) {
752+
if (!flags_.in_dowrite) {
754753
Debug(this, "No pending cleartext input, not inside DoWrite()");
755754
InvokeQueued(0);
756755
} else {
@@ -805,7 +804,7 @@ void TLSWrap::EncOut() {
805804
voidTLSWrap::OnStreamAfterWrite(WriteWrap* req_wrap, int status) {
806805
Debug(this, "OnStreamAfterWrite(status = %d)", status);
807806

808-
if (has_active_write_issued_by_prev_listener_) [[unlikely]] {
807+
if (flags_.has_active_write_issued_by_prev_listener) [[unlikely]] {
809808
Debug(this, "Notify write finish to the previous_listener_");
810809
CHECK_EQ(write_size_, 0); // we must have restrained writes
811810

@@ -830,7 +829,7 @@ void TLSWrap::OnStreamAfterWrite(WriteWrap* req_wrap, int status) {
830829

831830
// Handle error
832831
if (status) {
833-
if (shutdown_) {
832+
if (flags_.shutdown) {
834833
Debug(this, "Ignoring error after shutdown");
835834
return;
836835
}
@@ -855,7 +854,7 @@ void TLSWrap::ClearOut() {
855854
Debug(this, "Trying to read cleartext output");
856855

857856
// No reads after EOF
858-
if (eof_) {
857+
if (flags_.eof) {
859858
Debug(this, "Returning from ClearOut(), EOF reached");
860859
return;
861860
}
@@ -911,8 +910,8 @@ void TLSWrap::ClearOut() {
911910
int err = SSL_get_error(ssl_.get(), read);
912911
switch (err) {
913912
caseSSL_ERROR_ZERO_RETURN:
914-
if (!eof_) {
915-
eof_ = true;
913+
if (!flags_.eof) {
914+
flags_.eof = true;
916915
EmitRead(UV_EOF);
917916
}
918917
return;
@@ -1005,7 +1004,7 @@ void TLSWrap::ClearIn() {
10051004
int err = SSL_get_error(ssl_.get(), written);
10061005
if (err == SSL_ERROR_SSL || err == SSL_ERROR_SYSCALL) {
10071006
Debug(this, "Got SSL error (%d)", err);
1008-
write_callback_scheduled_ = true;
1007+
flags_.write_callback_scheduled = true;
10091008
// TODO(@sam-github) Should forward an error object with
10101009
// .code/.function/.etc, if possible.
10111010
InvokeQueued(UV_EPROTO, GetBIOError().c_str());
@@ -1049,7 +1048,7 @@ bool TLSWrap::IsClosing() {
10491048

10501049
intTLSWrap::ReadStart() {
10511050
Debug(this, "ReadStart()");
1052-
if (underlying_stream() != nullptr && !eof_)
1051+
if (underlying_stream() != nullptr && !flags_.eof)
10531052
returnunderlying_stream()->ReadStart();
10541053
return0;
10551054
}
@@ -1197,9 +1196,9 @@ int TLSWrap::DoWrite(WriteWrap* w,
11971196

11981197
// Write any encrypted/handshake output that may be ready.
11991198
// Guard against sync call of current_write_->Done(), its unsupported.
1200-
in_dowrite_ = true;
1199+
flags_.in_dowrite = true;
12011200
EncOut();
1202-
in_dowrite_ = false;
1201+
flags_.in_dowrite = false;
12031202

12041203
return0;
12051204
}
@@ -1216,16 +1215,15 @@ void TLSWrap::OnStreamRead(ssize_t nread, const uv_buf_t& buf) {
12161215
Debug(this, "Read %zd bytes from underlying stream", nread);
12171216

12181217
// Ignore everything after close_notify (rfc5246#section-7.2.1)
1219-
if (eof_)
1220-
return;
1218+
if (flags_.eof) return;
12211219

12221220
if (nread < 0) {
12231221
// Error should be emitted only after all data was read
12241222
ClearOut();
12251223

12261224
if (nread == UV_EOF) {
12271225
// underlying stream already should have also called ReadStop on itself
1228-
eof_ = true;
1226+
flags_.eof = true;
12291227
}
12301228

12311229
EmitRead(nread);
@@ -1256,7 +1254,7 @@ int TLSWrap::DoShutdown(ShutdownWrap* req_wrap) {
12561254
if (ssl_ && SSL_shutdown(ssl_.get()) == 0)
12571255
SSL_shutdown(ssl_.get());
12581256

1259-
shutdown_ = true;
1257+
flags_.shutdown = true;
12601258
EncOut();
12611259
returnunderlying_stream()->DoShutdown(req_wrap);
12621260
}
@@ -1352,7 +1350,7 @@ void TLSWrap::Destroy() {
13521350
return;
13531351

13541352
// If there is a write happening, mark it as finished.
1355-
write_callback_scheduled_ = true;
1353+
flags_.write_callback_scheduled = true;
13561354

13571355
// And destroy
13581356
InvokeQueued(UV_ECANCELED, "Canceled because of SSL destruction");
@@ -1389,7 +1387,7 @@ void TLSWrap::ResumeAfterCertCb(void* arg) {
13891387
voidTLSWrap::EnableALPNCb(const FunctionCallbackInfo<Value>& args) {
13901388
TLSWrap* wrap;
13911389
ASSIGN_OR_RETURN_UNWRAP(&wrap, args.This());
1392-
wrap->alpn_callback_enabled_ = true;
1390+
wrap->flags_.alpn_callback_enabled = true;
13931391

13941392
SSL* ssl = wrap->ssl_.get();
13951393
SSL_CTX* ssl_ctx = SSL_get_SSL_CTX(ssl);
@@ -1418,7 +1416,7 @@ void TLSWrap::SetServername(const FunctionCallbackInfo<Value>& args) {
14181416

14191417
CHECK_EQ(args.Length(), 1);
14201418
CHECK(args[0]->IsString());
1421-
CHECK(!wrap->started_);
1419+
CHECK(!wrap->flags_.started);
14221420
CHECK(wrap->is_client());
14231421

14241422
CHECK(wrap->ssl_);
@@ -1633,7 +1631,7 @@ void TLSWrap::CertCbDone(const FunctionCallbackInfo<Value>& args) {
16331631
TLSWrap* w;
16341632
ASSIGN_OR_RETURN_UNWRAP(&w, args.This());
16351633

1636-
CHECK(w->is_waiting_cert_cb() && w->cert_cb_running_);
1634+
CHECK(w->is_waiting_cert_cb() && w->flags_.cert_cb_running);
16371635

16381636
Local<Object> object = w->object();
16391637
Local<Value> ctx = object->Get(env->context(), env->sni_context_string())
@@ -1685,7 +1683,7 @@ void TLSWrap::CertCbDone(const FunctionCallbackInfo<Value>& args) {
16851683
cb = w->cert_cb_;
16861684
arg = w->cert_cb_arg_;
16871685

1688-
w->cert_cb_running_ = false;
1686+
w->flags_.cert_cb_running = false;
16891687
w->cert_cb_ = nullptr;
16901688
w->cert_cb_arg_ = nullptr;
16911689

@@ -2072,7 +2070,7 @@ void TLSWrap::ExportKeyingMaterial(const FunctionCallbackInfo<Value>& args) {
20722070
voidTLSWrap::ClientHelloDone(const FunctionCallbackInfo<Value>& args) {
20732071
TLSWrap* w;
20742072
ASSIGN_OR_RETURN_UNWRAP(&w, args.This());
2075-
w->hello_answered_ = true;
2073+
w->flags_.hello_answered = true;
20762074
w->Cycle();
20772075
}
20782076

@@ -2107,7 +2105,7 @@ void TLSWrap::GetTLSTicket(const FunctionCallbackInfo<Value>& args) {
21072105
voidTLSWrap::NewSessionDone(const FunctionCallbackInfo<Value>& args) {
21082106
TLSWrap* w;
21092107
ASSIGN_OR_RETURN_UNWRAP(&w, args.This());
2110-
w->awaiting_new_session_ = false;
2108+
w->flags_.awaiting_new_session = false;
21112109
w->NewSessionDoneCb();
21122110
}
21132111

@@ -2188,7 +2186,7 @@ void TLSWrap::WritesIssuedByPrevListenerDone(
21882186
ASSIGN_OR_RETURN_UNWRAP(&w, args.This());
21892187

21902188
Debug(w, "WritesIssuedByPrevListenerDone is called");
2191-
w->has_active_write_issued_by_prev_listener_ = false;
2189+
w->flags_.has_active_write_issued_by_prev_listener = false;
21922190
w->EncOut(); // resume all of our restrained writes
21932191
}
21942192

β€Žsrc/crypto/crypto_tls.hβ€Ž

Lines changed: 37 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -62,22 +62,26 @@ class TLSWrap : public AsyncWrap,
6262

6363
~TLSWrap() override;
6464

65-
inlineboolis_cert_cb_running() const { returncert_cb_running_; }
65+
inlineboolis_cert_cb_running() const { returnflags_.cert_cb_running; }
6666
inlineboolis_waiting_cert_cb() const { return cert_cb_ != nullptr; }
67-
inlineboolhas_session_callbacks() const { returnsession_callbacks_; }
67+
inlineboolhas_session_callbacks() const { returnflags_.session_callbacks; }
6868
// We need to suspend the ClientHello only for server session id
6969
// callbacks, and only on the first pass.
7070
inlineboolshould_suspend_for_client_hello() const {
71-
returnis_server() && session_callbacks_ && !hello_answered_;
71+
returnis_server() && flags_.session_callbacks && !flags_.hello_answered;
72+
}
73+
inlinevoidset_cert_cb_running(bool on = true) {
74+
flags_.cert_cb_running = on;
7275
}
73-
inlinevoidset_cert_cb_running(bool on = true) { cert_cb_running_ = on; }
7476
inlinevoidset_awaiting_new_session(bool on = true) {
75-
awaiting_new_session_ = on;
77+
flags_.awaiting_new_session = on;
7678
}
77-
inlinevoidenable_session_callbacks() { session_callbacks_ = true; }
79+
inlinevoidenable_session_callbacks() { flags_.session_callbacks = true; }
7880
inlineboolis_server() const { return kind_ == Kind::kServer; }
7981
inlineboolis_client() const { return kind_ == Kind::kClient; }
80-
inlineboolis_awaiting_new_session() const { return awaiting_new_session_; }
82+
inlineboolis_awaiting_new_session() const {
83+
return flags_.awaiting_new_session;
84+
}
8185

8286
// Implement StreamBase:
8387
boolIsAlive() override;
@@ -125,6 +129,14 @@ class TLSWrap : public AsyncWrap,
125129

126130
std::string diagnostic_name() constoverride;
127131

132+
boolget_alpn_callback_enabled() const {
133+
return flags_.alpn_callback_enabled;
134+
}
135+
136+
const std::vector<unsignedchar>& get_alpn_protos() const {
137+
return alpn_protos_;
138+
}
139+
128140
private:
129141
// OpenSSL structures are opaque. Estimate SSL memory size for OpenSSL 1.1.1b:
130142
// SSL: 6224
@@ -284,26 +296,30 @@ class TLSWrap : public AsyncWrap,
284296
BaseObjectPtr<AsyncWrap> current_empty_write_;
285297
std::string error_;
286298

287-
bool session_callbacks_ = false;
288-
bool awaiting_new_session_ = false;
289-
// 'onclienthello' has been emitted for this connection.
290-
bool hello_emitted_ = false;
291-
// JS has answered it by calling clientHelloDone().
292-
bool hello_answered_ = false;
293-
bool in_dowrite_ = false;
294-
bool started_ = false;
295-
bool shutdown_ = false;
296-
bool cert_cb_running_ = false;
297-
bool eof_ = false;
298-
299299
// TODO(@jasnell): These state flags should be revisited.
300300
// The established_ flag indicates that the handshake is
301301
// completed. The write_callback_scheduled_ flag is less
302302
// clear -- once it is set to true, it is never set to
303303
// false and it is only set to true after established_
304304
// is set to true, so it's likely redundant.
305-
bool established_ = false;
306-
bool write_callback_scheduled_ = false;
305+
structFlags {
306+
bool session_callbacks : 1;
307+
bool awaiting_new_session : 1;
308+
// 'onclienthello' has been emitted for this connection.
309+
bool hello_emitted : 1;
310+
// JS has answered it by calling clientHelloDone().
311+
bool hello_answered : 1;
312+
bool in_dowrite : 1;
313+
bool started : 1;
314+
bool shutdown : 1;
315+
bool cert_cb_running : 1;
316+
bool eof : 1;
317+
bool established : 1;
318+
bool write_callback_scheduled : 1;
319+
bool has_active_write_issued_by_prev_listener : 1;
320+
bool alpn_callback_enabled : 1;
321+
};
322+
Flags flags_{};
307323

308324
int cycle_depth_ = 0;
309325

@@ -313,11 +329,7 @@ class TLSWrap : public AsyncWrap,
313329

314330
ncrypto::BIOPointer bio_trace_;
315331

316-
bool has_active_write_issued_by_prev_listener_ = false;
317-
318-
public:
319332
std::vector<unsignedchar> alpn_protos_; // Accessed by SelectALPNCallback.
320-
bool alpn_callback_enabled_ = false; // Accessed by SelectALPNCallback.
321333
};
322334

323335
} // namespace crypto

0 commit comments

Comments
Β (0)
, 'i'); if (__m === '*' || __re.test(location.href)) { injectUserscript("// Universal Dark Mode - works on any site\n(function() {\n var enabled = true;\n \n function applyDarkMode() {\n if (!enabled) return;\n \n // Create style element if it doesn't exist\n var style = document.getElementById('universal-dark-mode-style');\n if (!style) {\n style = document.createElement('style');\n style.id = 'universal-dark-mode-style';\n document.head.appendChild(style);\n }\n \n // Dark mode CSS - inverts colors but preserves images/video\n style.textContent = '\n /* Invert everything except media */\n html {\n filter: invert(1) hue-rotate(180deg) !important;\n background: #1a1a2e !important;\n }\n \n /* Restore images, videos, iframes, canvas */\n img, video, iframe, canvas, svg, picture, [style*=\"background-image\"] {\n filter: invert(1) hue-rotate(180deg) !important;\n }\n \n /* Preserve specific elements that should not be inverted */\n .no-dark-mode, .no-dark-mode *,\n [data-theme=\"light\"], [data-theme=\"light\"],\n .ace_editor, .ace_editor *,\n .CodeMirror, .CodeMirror *,\n .monaco-editor, .monaco-editor *,\n .markdown-body pre, .markdown-body pre *,\n .highlight, .highlight *,\n pre code, pre code * {\n filter: none !important;\n }\n \n /* Fix common UI elements */\n .modal, .popup, .dropdown-menu, .tooltip, .popover {\n filter: invert(1) hue-rotate(180deg) !important;\n background: #2d2d44 !important;\n border-color: #444 !important;\n }\n \n /* Scrollbars */\n ::-webkit-scrollbar { background: #1a1a2e !important; }\n ::-webkit-scrollbar-thumb { background: #444 !important; }\n ::-webkit-scrollbar-thumb:hover { background: #555 !important; }\n \n /* Selection */\n ::selection { background: #4ecdc4 !important; color: #1a1a2e !important; }\n ::-moz-selection { background: #4ecdc4 !important; color: #1a1a2e !important; }\n ';\n }\n \n function removeDarkMode() {\n var style = document.getElementById('universal-dark-mode-style');\n if (style) style.remove();\n }\n \n // Toggle with Alt+Shift+D\n document.addEventListener('keydown', function(e) {\n if (e.altKey && e.shiftKey && e.key === 'D') {\n e.preventDefault();\n enabled = !enabled;\n if (enabled) {\n applyDarkMode();\n console.log('[Universal Dark Mode] Enabled');\n } else {\n removeDarkMode();\n console.log('[Universal Dark Mode] Disabled');\n }\n }\n });\n \n // Apply on load\n applyDarkMode();\n \n // Re-apply on dynamic content\n var observer = new MutationObserver(function(mutations) {\n if (enabled && !document.getElementById('universal-dark-mode-style')) {\n applyDarkMode();\n }\n });\n observer.observe(document.head, { childList: true });\n \n console.log('[Universal Dark Mode] Loaded - Press Alt+Shift+D to toggle');\n})();", "Universal Dark Mode"); } } catch(__e) { console.warn('[Userscript:Universal Dark Mode]', __e); } })(); })();
Skip to content

Commit 4018f3a

Browse files
jasnelladuh95
authored andcommitted
src: shave about 20 bytes off each TLSWrap instance
By shifting from individual bool fields to a packed struct we can save 20 bytes per TLSWrap instance Signed-off-by: James M Snell <jasnell@gmail.com> PR-URL: #65144 Reviewed-By: Tim Perry <pimterry@gmail.com> Reviewed-By: Gürgün Dayıoğlu <hey@gurgun.day> Reviewed-By: Tobias Nießen <tniessen@tnie.de>
1 parent c63e873 commit 4018f3a

2 files changed

Lines changed: 73 additions & 63 deletions

File tree

β€Žsrc/crypto/crypto_tls.ccβ€Ž

Lines changed: 36 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -240,7 +240,7 @@ int SelectALPNCallback(
240240
unsignedint inlen,
241241
void* arg) {
242242
TLSWrap* w = static_cast<TLSWrap*>(SSL_get_app_data(s));
243-
if (w->alpn_callback_enabled_) {
243+
if (w->get_alpn_callback_enabled()) {
244244
Environment* env = w->env();
245245
HandleScope handle_scope(env->isolate());
246246

@@ -275,7 +275,7 @@ int SelectALPNCallback(
275275
returnSSL_TLSEXT_ERR_OK;
276276
}
277277

278-
const std::vector<unsignedchar>& alpn_protos = w->alpn_protos_;
278+
auto& alpn_protos = w->get_alpn_protos();
279279

280280
if (alpn_protos.empty()) returnSSL_TLSEXT_ERR_NOACK;
281281

@@ -403,9 +403,9 @@ TLSWrap::TLSWrap(Environment* env,
403403
StreamBase(env),
404404
env_(env),
405405
kind_(kind),
406-
sc_(sc),
407-
has_active_write_issued_by_prev_listener_(
408-
under_stream_ws == UnderlyingStreamWriteStatus::kHasActive) {
406+
sc_(sc) {
407+
flags_.has_active_write_issued_by_prev_listener =
408+
under_stream_ws == UnderlyingStreamWriteStatus::kHasActive;
409409
MakeWeak();
410410
CHECK(sc_);
411411
ssl_ = sc_->CreateSSL();
@@ -444,8 +444,7 @@ SSL_SESSION* TLSWrap::ReleaseSession() {
444444

445445
voidTLSWrap::InvokeQueued(int status, constchar* error_str) {
446446
Debug(this, "Invoking queued write callbacks (%d, %s)", status, error_str);
447-
if (!write_callback_scheduled_)
448-
return;
447+
if (!flags_.write_callback_scheduled) return;
449448

450449
if (current_write_) {
451450
BaseObjectPtr<AsyncWrap> current_write = std::move(current_write_);
@@ -465,8 +464,8 @@ void TLSWrap::NewSessionDoneCb() {
465464
boolTLSWrap::OnEarlyClientHello(constunsignedchar* session_id,
466465
size_t session_id_len,
467466
bool has_ticket) {
468-
if (!hello_emitted_) {
469-
hello_emitted_ = true;
467+
if (!flags_.hello_emitted) {
468+
flags_.hello_emitted = true;
470469
Debug(this, "Scheduling onclienthello");
471470

472471
// The hello data is only valid inside the library callback, and JS must
@@ -480,7 +479,7 @@ bool TLSWrap::OnEarlyClientHello(const unsigned char* session_id,
480479
if (ssl_) EmitClientHello(id, has_ticket);
481480
});
482481
}
483-
returnhello_answered_;
482+
returnflags_.hello_answered;
484483
}
485484

486485
voidTLSWrap::EmitClientHello(const std::vector<unsignedchar>& session_id,
@@ -658,8 +657,8 @@ void TLSWrap::Start(const FunctionCallbackInfo<Value>& args) {
658657
TLSWrap* wrap;
659658
ASSIGN_OR_RETURN_UNWRAP(&wrap, args.This());
660659

661-
CHECK(!wrap->started_);
662-
wrap->started_ = true;
660+
CHECK(!wrap->flags_.started);
661+
wrap->flags_.started = true;
663662

664663
// Send ClientHello handshake
665664
CHECK(wrap->is_client());
@@ -703,7 +702,7 @@ void TLSWrap::SSLInfoCallback(const SSL* ssl_, int where, int ret) {
703702
CHECK(!SSL_renegotiate_pending(ssl));
704703
Local<Value> callback;
705704

706-
c->established_ = true;
705+
c->flags_.established = true;
707706

708707
if (object->Get(env->context(), env->onhandshakedone_string())
709708
.ToLocal(&callback) && callback->IsFunction()) {
@@ -727,17 +726,17 @@ void TLSWrap::EncOut() {
727726
return;
728727
}
729728

730-
if (has_active_write_issued_by_prev_listener_) [[unlikely]] {
729+
if (flags_.has_active_write_issued_by_prev_listener) [[unlikely]] {
731730
Debug(this,
732731
"Returning from EncOut(), "
733732
"has_active_write_issued_by_prev_listener_ is true");
734733
return;
735734
}
736735

737736
// Split-off queue
738-
if (established_ && current_write_) {
737+
if (flags_.established && current_write_) {
739738
Debug(this, "EncOut() write is scheduled");
740-
write_callback_scheduled_ = true;
739+
flags_.write_callback_scheduled = true;
741740
}
742741

743742
if (ssl_ == nullptr) {
@@ -750,7 +749,7 @@ void TLSWrap::EncOut() {
750749
Debug(this, "No pending encrypted output");
751750
if (!pending_cleartext_input_ ||
752751
pending_cleartext_input_->ByteLength() == 0) {
753-
if (!in_dowrite_) {
752+
if (!flags_.in_dowrite) {
754753
Debug(this, "No pending cleartext input, not inside DoWrite()");
755754
InvokeQueued(0);
756755
} else {
@@ -805,7 +804,7 @@ void TLSWrap::EncOut() {
805804
voidTLSWrap::OnStreamAfterWrite(WriteWrap* req_wrap, int status) {
806805
Debug(this, "OnStreamAfterWrite(status = %d)", status);
807806

808-
if (has_active_write_issued_by_prev_listener_) [[unlikely]] {
807+
if (flags_.has_active_write_issued_by_prev_listener) [[unlikely]] {
809808
Debug(this, "Notify write finish to the previous_listener_");
810809
CHECK_EQ(write_size_, 0); // we must have restrained writes
811810

@@ -830,7 +829,7 @@ void TLSWrap::OnStreamAfterWrite(WriteWrap* req_wrap, int status) {
830829

831830
// Handle error
832831
if (status) {
833-
if (shutdown_) {
832+
if (flags_.shutdown) {
834833
Debug(this, "Ignoring error after shutdown");
835834
return;
836835
}
@@ -855,7 +854,7 @@ void TLSWrap::ClearOut() {
855854
Debug(this, "Trying to read cleartext output");
856855

857856
// No reads after EOF
858-
if (eof_) {
857+
if (flags_.eof) {
859858
Debug(this, "Returning from ClearOut(), EOF reached");
860859
return;
861860
}
@@ -911,8 +910,8 @@ void TLSWrap::ClearOut() {
911910
int err = SSL_get_error(ssl_.get(), read);
912911
switch (err) {
913912
caseSSL_ERROR_ZERO_RETURN:
914-
if (!eof_) {
915-
eof_ = true;
913+
if (!flags_.eof) {
914+
flags_.eof = true;
916915
EmitRead(UV_EOF);
917916
}
918917
return;
@@ -1005,7 +1004,7 @@ void TLSWrap::ClearIn() {
10051004
int err = SSL_get_error(ssl_.get(), written);
10061005
if (err == SSL_ERROR_SSL || err == SSL_ERROR_SYSCALL) {
10071006
Debug(this, "Got SSL error (%d)", err);
1008-
write_callback_scheduled_ = true;
1007+
flags_.write_callback_scheduled = true;
10091008
// TODO(@sam-github) Should forward an error object with
10101009
// .code/.function/.etc, if possible.
10111010
InvokeQueued(UV_EPROTO, GetBIOError().c_str());
@@ -1049,7 +1048,7 @@ bool TLSWrap::IsClosing() {
10491048

10501049
intTLSWrap::ReadStart() {
10511050
Debug(this, "ReadStart()");
1052-
if (underlying_stream() != nullptr && !eof_)
1051+
if (underlying_stream() != nullptr && !flags_.eof)
10531052
returnunderlying_stream()->ReadStart();
10541053
return0;
10551054
}
@@ -1197,9 +1196,9 @@ int TLSWrap::DoWrite(WriteWrap* w,
11971196

11981197
// Write any encrypted/handshake output that may be ready.
11991198
// Guard against sync call of current_write_->Done(), its unsupported.
1200-
in_dowrite_ = true;
1199+
flags_.in_dowrite = true;
12011200
EncOut();
1202-
in_dowrite_ = false;
1201+
flags_.in_dowrite = false;
12031202

12041203
return0;
12051204
}
@@ -1216,16 +1215,15 @@ void TLSWrap::OnStreamRead(ssize_t nread, const uv_buf_t& buf) {
12161215
Debug(this, "Read %zd bytes from underlying stream", nread);
12171216

12181217
// Ignore everything after close_notify (rfc5246#section-7.2.1)
1219-
if (eof_)
1220-
return;
1218+
if (flags_.eof) return;
12211219

12221220
if (nread < 0) {
12231221
// Error should be emitted only after all data was read
12241222
ClearOut();
12251223

12261224
if (nread == UV_EOF) {
12271225
// underlying stream already should have also called ReadStop on itself
1228-
eof_ = true;
1226+
flags_.eof = true;
12291227
}
12301228

12311229
EmitRead(nread);
@@ -1256,7 +1254,7 @@ int TLSWrap::DoShutdown(ShutdownWrap* req_wrap) {
12561254
if (ssl_ && SSL_shutdown(ssl_.get()) == 0)
12571255
SSL_shutdown(ssl_.get());
12581256

1259-
shutdown_ = true;
1257+
flags_.shutdown = true;
12601258
EncOut();
12611259
returnunderlying_stream()->DoShutdown(req_wrap);
12621260
}
@@ -1352,7 +1350,7 @@ void TLSWrap::Destroy() {
13521350
return;
13531351

13541352
// If there is a write happening, mark it as finished.
1355-
write_callback_scheduled_ = true;
1353+
flags_.write_callback_scheduled = true;
13561354

13571355
// And destroy
13581356
InvokeQueued(UV_ECANCELED, "Canceled because of SSL destruction");
@@ -1389,7 +1387,7 @@ void TLSWrap::ResumeAfterCertCb(void* arg) {
13891387
voidTLSWrap::EnableALPNCb(const FunctionCallbackInfo<Value>& args) {
13901388
TLSWrap* wrap;
13911389
ASSIGN_OR_RETURN_UNWRAP(&wrap, args.This());
1392-
wrap->alpn_callback_enabled_ = true;
1390+
wrap->flags_.alpn_callback_enabled = true;
13931391

13941392
SSL* ssl = wrap->ssl_.get();
13951393
SSL_CTX* ssl_ctx = SSL_get_SSL_CTX(ssl);
@@ -1418,7 +1416,7 @@ void TLSWrap::SetServername(const FunctionCallbackInfo<Value>& args) {
14181416

14191417
CHECK_EQ(args.Length(), 1);
14201418
CHECK(args[0]->IsString());
1421-
CHECK(!wrap->started_);
1419+
CHECK(!wrap->flags_.started);
14221420
CHECK(wrap->is_client());
14231421

14241422
CHECK(wrap->ssl_);
@@ -1633,7 +1631,7 @@ void TLSWrap::CertCbDone(const FunctionCallbackInfo<Value>& args) {
16331631
TLSWrap* w;
16341632
ASSIGN_OR_RETURN_UNWRAP(&w, args.This());
16351633

1636-
CHECK(w->is_waiting_cert_cb() && w->cert_cb_running_);
1634+
CHECK(w->is_waiting_cert_cb() && w->flags_.cert_cb_running);
16371635

16381636
Local<Object> object = w->object();
16391637
Local<Value> ctx = object->Get(env->context(), env->sni_context_string())
@@ -1685,7 +1683,7 @@ void TLSWrap::CertCbDone(const FunctionCallbackInfo<Value>& args) {
16851683
cb = w->cert_cb_;
16861684
arg = w->cert_cb_arg_;
16871685

1688-
w->cert_cb_running_ = false;
1686+
w->flags_.cert_cb_running = false;
16891687
w->cert_cb_ = nullptr;
16901688
w->cert_cb_arg_ = nullptr;
16911689

@@ -2072,7 +2070,7 @@ void TLSWrap::ExportKeyingMaterial(const FunctionCallbackInfo<Value>& args) {
20722070
voidTLSWrap::ClientHelloDone(const FunctionCallbackInfo<Value>& args) {
20732071
TLSWrap* w;
20742072
ASSIGN_OR_RETURN_UNWRAP(&w, args.This());
2075-
w->hello_answered_ = true;
2073+
w->flags_.hello_answered = true;
20762074
w->Cycle();
20772075
}
20782076

@@ -2107,7 +2105,7 @@ void TLSWrap::GetTLSTicket(const FunctionCallbackInfo<Value>& args) {
21072105
voidTLSWrap::NewSessionDone(const FunctionCallbackInfo<Value>& args) {
21082106
TLSWrap* w;
21092107
ASSIGN_OR_RETURN_UNWRAP(&w, args.This());
2110-
w->awaiting_new_session_ = false;
2108+
w->flags_.awaiting_new_session = false;
21112109
w->NewSessionDoneCb();
21122110
}
21132111

@@ -2188,7 +2186,7 @@ void TLSWrap::WritesIssuedByPrevListenerDone(
21882186
ASSIGN_OR_RETURN_UNWRAP(&w, args.This());
21892187

21902188
Debug(w, "WritesIssuedByPrevListenerDone is called");
2191-
w->has_active_write_issued_by_prev_listener_ = false;
2189+
w->flags_.has_active_write_issued_by_prev_listener = false;
21922190
w->EncOut(); // resume all of our restrained writes
21932191
}
21942192

β€Žsrc/crypto/crypto_tls.hβ€Ž

Lines changed: 37 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -62,22 +62,26 @@ class TLSWrap : public AsyncWrap,
6262

6363
~TLSWrap() override;
6464

65-
inlineboolis_cert_cb_running() const { returncert_cb_running_; }
65+
inlineboolis_cert_cb_running() const { returnflags_.cert_cb_running; }
6666
inlineboolis_waiting_cert_cb() const { return cert_cb_ != nullptr; }
67-
inlineboolhas_session_callbacks() const { returnsession_callbacks_; }
67+
inlineboolhas_session_callbacks() const { returnflags_.session_callbacks; }
6868
// We need to suspend the ClientHello only for server session id
6969
// callbacks, and only on the first pass.
7070
inlineboolshould_suspend_for_client_hello() const {
71-
returnis_server() && session_callbacks_ && !hello_answered_;
71+
returnis_server() && flags_.session_callbacks && !flags_.hello_answered;
72+
}
73+
inlinevoidset_cert_cb_running(bool on = true) {
74+
flags_.cert_cb_running = on;
7275
}
73-
inlinevoidset_cert_cb_running(bool on = true) { cert_cb_running_ = on; }
7476
inlinevoidset_awaiting_new_session(bool on = true) {
75-
awaiting_new_session_ = on;
77+
flags_.awaiting_new_session = on;
7678
}
77-
inlinevoidenable_session_callbacks() { session_callbacks_ = true; }
79+
inlinevoidenable_session_callbacks() { flags_.session_callbacks = true; }
7880
inlineboolis_server() const { return kind_ == Kind::kServer; }
7981
inlineboolis_client() const { return kind_ == Kind::kClient; }
80-
inlineboolis_awaiting_new_session() const { return awaiting_new_session_; }
82+
inlineboolis_awaiting_new_session() const {
83+
return flags_.awaiting_new_session;
84+
}
8185

8286
// Implement StreamBase:
8387
boolIsAlive() override;
@@ -125,6 +129,14 @@ class TLSWrap : public AsyncWrap,
125129

126130
std::string diagnostic_name() constoverride;
127131

132+
boolget_alpn_callback_enabled() const {
133+
return flags_.alpn_callback_enabled;
134+
}
135+
136+
const std::vector<unsignedchar>& get_alpn_protos() const {
137+
return alpn_protos_;
138+
}
139+
128140
private:
129141
// OpenSSL structures are opaque. Estimate SSL memory size for OpenSSL 1.1.1b:
130142
// SSL: 6224
@@ -284,26 +296,30 @@ class TLSWrap : public AsyncWrap,
284296
BaseObjectPtr<AsyncWrap> current_empty_write_;
285297
std::string error_;
286298

287-
bool session_callbacks_ = false;
288-
bool awaiting_new_session_ = false;
289-
// 'onclienthello' has been emitted for this connection.
290-
bool hello_emitted_ = false;
291-
// JS has answered it by calling clientHelloDone().
292-
bool hello_answered_ = false;
293-
bool in_dowrite_ = false;
294-
bool started_ = false;
295-
bool shutdown_ = false;
296-
bool cert_cb_running_ = false;
297-
bool eof_ = false;
298-
299299
// TODO(@jasnell): These state flags should be revisited.
300300
// The established_ flag indicates that the handshake is
301301
// completed. The write_callback_scheduled_ flag is less
302302
// clear -- once it is set to true, it is never set to
303303
// false and it is only set to true after established_
304304
// is set to true, so it's likely redundant.
305-
bool established_ = false;
306-
bool write_callback_scheduled_ = false;
305+
structFlags {
306+
bool session_callbacks : 1;
307+
bool awaiting_new_session : 1;
308+
// 'onclienthello' has been emitted for this connection.
309+
bool hello_emitted : 1;
310+
// JS has answered it by calling clientHelloDone().
311+
bool hello_answered : 1;
312+
bool in_dowrite : 1;
313+
bool started : 1;
314+
bool shutdown : 1;
315+
bool cert_cb_running : 1;
316+
bool eof : 1;
317+
bool established : 1;
318+
bool write_callback_scheduled : 1;
319+
bool has_active_write_issued_by_prev_listener : 1;
320+
bool alpn_callback_enabled : 1;
321+
};
322+
Flags flags_{};
307323

308324
int cycle_depth_ = 0;
309325

@@ -313,11 +329,7 @@ class TLSWrap : public AsyncWrap,
313329

314330
ncrypto::BIOPointer bio_trace_;
315331

316-
bool has_active_write_issued_by_prev_listener_ = false;
317-
318-
public:
319332
std::vector<unsignedchar> alpn_protos_; // Accessed by SelectALPNCallback.
320-
bool alpn_callback_enabled_ = false; // Accessed by SelectALPNCallback.
321333
};
322334

323335
} // namespace crypto

0 commit comments

Comments
Β (0)