socket_helper: reduce constant lookups and caching
commit28bb0e47541e49f36b96725732f7a7ae260bd5e9
authorEric Wong <e@80x24.org>
Thu, 5 Feb 2015 01:17:51 +0000 (5 01:17 +0000)
committerEric Wong <e@80x24.org>
Thu, 5 Feb 2015 17:18:11 +0000 (5 17:18 +0000)
treed3997bef4d5912bff580b916c1dce2905d6b7821
parentfe83ead4eae6f011fa15f506cd80cb4256813a92
socket_helper: reduce constant lookups and caching

In Ruby 1.9.2+, socket options may be specified using symbols
instead of constants to avoid the need to import Socket::Constants
into the namespace.  This also has a nice side-effect of reducing
the size of the bytecode by trading 3 instructions (getinlinecache,
getconstant, setinlinecache) for one "putobject" instruction.

Nowadays, we may also avoid defining OS-specific constants ourselves
since 1.9+ versions of Ruby already provide them to further reduce
bytecode size.

getsockopt also returns Socket::Option objects in 1.9.2+,
allowing us to avoid the larger "unpack('i')" method dispatch
for an operand-free "int" method call.

Finally, favor Object#nil? calls rather than "== nil" comparisons
to reduce bytecode size even more.

Since this code is only called at startup time, it does not benefit
from inline caching of constant lookups in current mainline Ruby.

Combined, these changes reduce YARV bytecode size by around 2K on a
64-bit system.
lib/unicorn/socket_helper.rb
test/unit/test_socket_helper.rb