Commit 26a0f55
committed
Autoload IPAddr instead of requiring it eagerly
openssl/ssl.rb requires ipaddr at the top of the file. The only use is
in OpenSSL::SSL.verify_certificate_identity, and only for certificates
carrying an iPAddress SAN:
return true if san.value == IPAddr.new(hostname).hton
rescue IPAddr::InvalidAddressError
Autoload matches the shape of the change net/http is making for the
same constant and keeps IPAddr resolvable, so anything downstream that
relied on require "openssl" defining it is unaffected.
Note this does not save the socket require on the next line, which is
unconditional and unrelated. The saving is ipaddr.rb itself, 857 lines.
Measured with socket already loaded, which is openssl's actual
situation, on Ruby 4.0.6 (arm64-darwin), 15 runs:
marginal cost of require "ipaddr" min 2.47 ms / median 2.95 ms, 1 file
End-to-end require "openssl" moves from a min of 26.42 ms to 24.88 ms,
but that figure carries a lot of variance from loading openssl.so, so
the marginal number above is the honest one.
Small in absolute terms, but openssl is loaded in a large share of Ruby
processes, and the change is one line with no behavior difference.
The rescue clause still resolves correctly: IPAddr.new triggers the
autoload before it can raise, so IPAddr::InvalidAddressError is defined
by the time the rescue is evaluated.
Test suite: 630 tests, 0 failures, unchanged. The existing coverage
already exercises the IPAddr path directly, asserting
verify_certificate_identity against a cert with an IP:127.0.0.1 SAN.
The three added tests cover that requiring openssl does not load
ipaddr, that IPAddr still resolves afterwards, and that referencing it
pulls the library in. The first fails against the previous code.
Signed-off-by: Tim Smith <tsmith84@proton.me>1 parent baa28f5 commit 26a0f55
2 files changed
Lines changed: 29 additions & 1 deletion
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
15 | 15 | | |
16 | 16 | | |
17 | 17 | | |
18 | | - | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
19 | 23 | | |
20 | 24 | | |
21 | 25 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
| 24 | + | |
0 commit comments