FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

src: shave about 20 bytes off each TLSWrap instance · nodejs/node@4018f3a · GitHub

/ node Public

Commit 4018f3a

Browse files
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
unsigned int 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
return SSL_TLSEXT_ERR_OK;
276276
}
277277

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

280280
if (alpn_protos.empty()) return SSL_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
void TLSWrap::InvokeQueued(int status, const char* 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
bool TLSWrap::OnEarlyClientHello(const unsigned char* 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-
return hello_answered_;
482+
return flags_.hello_answered;
484483
}
485484

486485
void TLSWrap::EmitClientHello(const std::vector<unsigned char>& 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
void TLSWrap::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
case SSL_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
int TLSWrap::ReadStart() {
10511050
Debug(this, "ReadStart()");
1052-
if (underlying_stream() != nullptr && !eof_)
1051+
if (underlying_stream() != nullptr && !flags_.eof)
10531052
return underlying_stream()->ReadStart();
10541053
return 0;
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
return 0;
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
return underlying_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
void TLSWrap::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
void TLSWrap::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
void TLSWrap::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-
inline bool is_cert_cb_running() const { return cert_cb_running_; }
65+
inline bool is_cert_cb_running() const { return flags_.cert_cb_running; }
6666
inline bool is_waiting_cert_cb() const { return cert_cb_ != nullptr; }
67-
inline bool has_session_callbacks() const { return session_callbacks_; }
67+
inline bool has_session_callbacks() const { return flags_.session_callbacks; }
6868
// We need to suspend the ClientHello only for server session id
6969
// callbacks, and only on the first pass.
7070
inline bool should_suspend_for_client_hello() const {
71-
return is_server() && session_callbacks_ && !hello_answered_;
71+
return is_server() && flags_.session_callbacks && !flags_.hello_answered;
72+
}
73+
inline void set_cert_cb_running(bool on = true) {
74+
flags_.cert_cb_running = on;
7275
}
73-
inline void set_cert_cb_running(bool on = true) { cert_cb_running_ = on; }
7476
inline void set_awaiting_new_session(bool on = true) {
75-
awaiting_new_session_ = on;
77+
flags_.awaiting_new_session = on;
7678
}
77-
inline void enable_session_callbacks() { session_callbacks_ = true; }
79+
inline void enable_session_callbacks() { flags_.session_callbacks = true; }
7880
inline bool is_server() const { return kind_ == Kind::kServer; }
7981
inline bool is_client() const { return kind_ == Kind::kClient; }
80-
inline bool is_awaiting_new_session() const { return awaiting_new_session_; }
82+
inline bool is_awaiting_new_session() const {
83+
return flags_.awaiting_new_session;
84+
}
8185

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

126130
std::string diagnostic_name() const override;
127131

132+
bool get_alpn_callback_enabled() const {
133+
return flags_.alpn_callback_enabled;
134+
}
135+
136+
const std::vector<unsigned char>& 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+
struct Flags {
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<unsigned char> alpn_protos_; // Accessed by SelectALPNCallback.
320-
bool alpn_callback_enabled_ = false; // Accessed by SelectALPNCallback.
321333
};
322334

323335
} // namespace crypto

0 commit comments

Comments
 (0)

Back | FazBrowse Home | New Git URL