From 28bb0e47541e49f36b96725732f7a7ae260bd5e9 Mon Sep 17 00:00:00 2001 From: Eric Wong Date: Thu, 5 Feb 2015 01:17:51 +0000 Subject: 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. --- test/unit/test_socket_helper.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'test') diff --git a/test/unit/test_socket_helper.rb b/test/unit/test_socket_helper.rb index 8992757..dd6881c 100644 --- a/test/unit/test_socket_helper.rb +++ b/test/unit/test_socket_helper.rb @@ -189,7 +189,7 @@ class TestSocketHelper < Test::Unit::TestCase port = unused_port @test_addr name = "#@test_addr:#{port}" sock = bind_listen(name, :reuseport => true) - cur = sock.getsockopt(Socket::SOL_SOCKET, SO_REUSEPORT).unpack('i')[0] + cur = sock.getsockopt(:SOL_SOCKET, :SO_REUSEPORT).int assert_operator cur, :>, 0 rescue Errno::ENOPROTOOPT # kernel does not support SO_REUSEPORT (older Linux) -- cgit v1.2.3-24-ge0c7