Fork of the Go standard TLS library, providing low-level access to the ClientHello for mimicry purposes.
Find a file
David Taylor 6060472b5c crypto/tls: avoid extra allocations in steady-state Handshake calls
The Read and Write methods of *tls.Conn call Handshake
unconditionally, every time they are called, expecting it to only
perform a new handshake if required.

However in go 1.17 handshakeContext was extended to set up a
cancelable Context, and importantly did so prior to checking if a
handshake is required. This thus causes it to allocate on every call,
even in those that are no-ops when called in a Read or Write on an
established connection, sometimes leading to very large numbers of
allocations during reads.

This change adds an early return, prior to setting up the context or
proceeding into the handshakeMutex and checking the handshake error, if
the handshake status atomic indicates handshake is already complete.

name                                     old allocs/op  new allocs/op  delta
Throughput/MaxPacket/1MB/TLSv12-10          1.07k ± 0%     0.62k ± 0%  -42.16%  (p=0.000 n=4+5)
Throughput/MaxPacket/1MB/TLSv13-10          1.70k ± 0%     1.25k ± 0%     ~     (p=0.079 n=4+5)
Throughput/MaxPacket/2MB/TLSv12-10          1.62k ± 0%     0.73k ± 0%  -55.18%  (p=0.008 n=5+5)
Throughput/MaxPacket/2MB/TLSv13-10          2.26k ± 0%     1.36k ± 0%  -39.64%  (p=0.008 n=5+5)
Throughput/MaxPacket/4MB/TLSv12-10          2.74k ± 0%     0.95k ± 0%  -65.35%  (p=0.008 n=5+5)
Throughput/MaxPacket/4MB/TLSv13-10          3.37k ± 0%     1.58k ± 0%  -53.15%  (p=0.008 n=5+5)
Throughput/MaxPacket/8MB/TLSv12-10          4.96k ± 0%     1.39k ± 0%  -72.06%  (p=0.016 n=4+5)
Throughput/MaxPacket/8MB/TLSv13-10          5.60k ± 0%     2.01k ± 0%  -64.05%  (p=0.008 n=5+5)
Throughput/MaxPacket/16MB/TLSv12-10         9.42k ± 0%     2.27k ± 1%  -75.92%  (p=0.016 n=4+5)
Throughput/MaxPacket/16MB/TLSv13-10         10.0k ± 0%      2.9k ± 0%  -71.39%  (p=0.008 n=5+5)
Throughput/MaxPacket/32MB/TLSv12-10         18.3k ± 0%      4.0k ± 0%  -77.97%  (p=0.008 n=5+5)
Throughput/MaxPacket/32MB/TLSv13-10         18.9k ± 0%      4.6k ± 0%  -75.62%  (p=0.008 n=5+5)
Throughput/MaxPacket/64MB/TLSv12-10         36.2k ± 0%      7.5k ± 0%  -79.15%  (p=0.008 n=5+5)
Throughput/MaxPacket/64MB/TLSv13-10         36.7k ± 0%      8.1k ± 0%  -78.06%  (p=0.008 n=5+5)
Throughput/DynamicPacket/1MB/TLSv12-10      1.12k ± 0%     0.63k ± 0%  -44.20%  (p=0.008 n=5+5)
Throughput/DynamicPacket/1MB/TLSv13-10      1.76k ± 0%     1.26k ± 0%  -28.22%  (p=0.016 n=5+4)
Throughput/DynamicPacket/2MB/TLSv12-10      1.68k ± 0%     0.74k ± 0%  -56.11%  (p=0.008 n=5+5)
Throughput/DynamicPacket/2MB/TLSv13-10      2.32k ± 0%     1.37k ± 0%  -40.80%  (p=0.008 n=5+5)
Throughput/DynamicPacket/4MB/TLSv12-10      2.80k ± 0%     0.96k ± 0%  -65.81%  (p=0.008 n=5+5)
Throughput/DynamicPacket/4MB/TLSv13-10      3.43k ± 0%     1.59k ± 0%  -53.57%  (p=0.008 n=5+5)
Throughput/DynamicPacket/8MB/TLSv12-10      5.03k ± 0%     1.39k ± 0%  -72.27%  (p=0.008 n=5+5)
Throughput/DynamicPacket/8MB/TLSv13-10      5.66k ± 0%     2.02k ± 0%  -64.27%  (p=0.008 n=5+5)
Throughput/DynamicPacket/16MB/TLSv12-10     9.48k ± 0%     2.28k ± 1%  -75.98%  (p=0.008 n=5+5)
Throughput/DynamicPacket/16MB/TLSv13-10     10.1k ± 0%      2.9k ± 0%  -71.34%  (p=0.008 n=5+5)
Throughput/DynamicPacket/32MB/TLSv12-10     18.4k ± 0%      4.0k ± 0%  -78.13%  (p=0.008 n=5+5)
Throughput/DynamicPacket/32MB/TLSv13-10     19.0k ± 0%      4.6k ± 0%  -75.54%  (p=0.008 n=5+5)
Throughput/DynamicPacket/64MB/TLSv12-10     36.2k ± 0%      7.6k ± 1%  -79.02%  (p=0.008 n=5+5)
Throughput/DynamicPacket/64MB/TLSv13-10     36.8k ± 0%      8.2k ± 1%  -77.76%  (p=0.008 n=5+5)

Fixes #50657

Change-Id: Iacb1f9bf7802022960d9dbce141b8c0587a614d4
Reviewed-on: https://go-review.googlesource.com/c/go/+/379034
Reviewed-by: David Chase <drchase@google.com>
TryBot-Result: Gopher Robot <gobot@golang.org>
Reviewed-by: Filippo Valsorda <valsorda@google.com>
Auto-Submit: Filippo Valsorda <filippo@golang.org>
Run-TryBot: Filippo Valsorda <filippo@golang.org>
2022-05-06 20:17:52 +00:00
fipsonly [dev.boringcrypto] all: add boringcrypto build tags 2022-04-29 14:23:22 +00:00
testdata crypto/tls: let HTTP/1.1 clients connect to servers with NextProtos "h2" 2021-06-10 12:41:37 +00:00
alert.go crypto/tls: add missing alert values 2020-04-01 19:32:57 +00:00
auth.go [dev.boringcrypto] all: merge commit 9d0819b27c (CL 314609) into dev.boringcrypto 2021-05-13 12:59:22 -04:00
auth_test.go [dev.boringcrypto] all: merge master into dev.boringcrypto 2020-04-08 17:48:41 -04:00
boring.go [dev.boringcrypto] crypto/x509: remove VerifyOptions.IsBoring 2022-04-29 14:23:29 +00:00
boring_test.go [dev.boringcrypto] crypto/x509: remove VerifyOptions.IsBoring 2022-04-29 14:23:29 +00:00
cipher_suites.go [dev.boringcrypto] all: merge master into dev.boringcrypto 2022-04-20 16:57:46 +02:00
common.go [dev.boringcrypto] all: merge master into dev.boringcrypto 2022-04-27 20:09:28 +02:00
common_string.go crypto/tls: add {SignatureScheme,CurveID,ClientAuthType}.String() 2020-03-11 20:02:18 +00:00
conn.go crypto/tls: avoid extra allocations in steady-state Handshake calls 2022-05-06 20:17:52 +00:00
conn_test.go crypto/tls: select only compatible chains from Certificates 2019-11-12 01:08:57 +00:00
example_test.go crypto/tls: replace VerifyPeerCertificate example with VerifyConnection 2020-06-24 20:48:12 +00:00
generate_cert.go all: gofmt -w -r 'interface{} -> any' src 2021-12-13 18:45:54 +00:00
handshake_client.go [dev.boringcrypto] crypto/x509: remove VerifyOptions.IsBoring 2022-04-29 14:23:29 +00:00
handshake_client_test.go all: consistently use US spelling of present participles 2022-04-08 13:44:41 +00:00
handshake_client_tls13.go [dev.boringcrypto] all: merge commit 57c115e1 into dev.boringcrypto 2021-08-16 10:33:17 -07:00
handshake_messages.go crypto/tls: reject duplicate extensions 2022-04-21 16:18:13 +00:00
handshake_messages_test.go [dev.boringcrypto] all: merge master into dev.boringcrypto 2022-04-27 20:09:28 +02:00
handshake_server.go [dev.boringcrypto] crypto/x509: remove VerifyOptions.IsBoring 2022-04-29 14:23:29 +00:00
handshake_server_test.go crypto/tls: remove tls10default GODEBUG flag 2022-04-27 17:20:34 +00:00
handshake_server_tls13.go [dev.boringcrypto] all: merge master into dev.boringcrypto 2021-11-08 14:46:41 -05:00
handshake_test.go crypto/tls: set default minimum client version to TLS 1.2 2021-11-05 22:03:24 +00:00
handshake_unix_test.go all: use new "unix" build tag where appropriate 2022-03-29 16:24:51 +00:00
key_agreement.go crypto/tls: test key type when casting 2021-07-12 20:58:00 +00:00
key_schedule.go math/big: add (*Int).FillBytes 2020-05-05 00:36:44 +00:00
key_schedule_test.go crypto/tls: implement TLS 1.3 cryptographic computations 2018-11-02 21:54:52 +00:00
link_test.go all: update to use os.ReadFile, os.WriteFile, os.CreateTemp, os.MkdirTemp 2020-12-09 19:12:23 +00:00
notboring.go [dev.boringcrypto] crypto/x509: remove VerifyOptions.IsBoring 2022-04-29 14:23:29 +00:00
prf.go crypto/tls: improve error messages for invalid certificates and signatures 2019-10-30 20:18:59 +00:00
prf_test.go crypto/tls: remove SSLv3 support 2019-08-27 22:24:05 +00:00
ticket.go crypto/tls: remove version check when unmarshaling sessionState 2020-05-18 23:09:31 +00:00
tls.go crypto/tls: add HandshakeContext method to Conn 2021-03-16 14:05:45 +00:00
tls_test.go crypto/tls: make cipher suite preference ordering automatic 2021-05-08 05:15:48 +00:00