ruby-changes:39016
From: normal <ko1@a...>
Date: Thu, 2 Jul 2015 10:58:45 +0900 (JST)
Subject: [ruby-changes:39016] normal:r51097 (trunk): socket: memoize common socket families in fptr->mode
normal 2015-07-02 10:58:14 +0900 (Thu, 02 Jul 2015) New Revision: 51097 http://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=revision&revision=51097 Log: socket: memoize common socket families in fptr->mode This provides a minor speedup by avoiding an extra syscall require 'socket' require 'benchmark' nr = 100000 msg = 'hello world' buf = '' size = msg.bytesize puts(Benchmark.measure do UNIXSocket.pair(:SEQPACKET) do |a, b| nr.times do a.sendmsg_nonblock(msg, 0, exception: false) b.recv(size, 0, buf) end end end) user system total real before: 0.330000 0.340000 0.670000 ( 0.678235) after: 0.290000 0.240000 0.530000 ( 0.534527) * ext/socket/rubysocket.h: flags for common socket families (rsock_getfamily): update signature * include/ruby/io.h: comment socket FMODE flags * ext/socket/init.c (rsock_getfamily): memoize family * ext/socket/basicsocket.c: adjust rsock_getfamily calls * ext/socket/ancdata.c: ditto [ruby-core:69713] [Feature #11298] Modified files: trunk/ChangeLog trunk/ext/socket/ancdata.c trunk/ext/socket/basicsocket.c trunk/ext/socket/init.c trunk/ext/socket/rubysocket.h trunk/include/ruby/io.h Index: include/ruby/io.h =================================================================== --- include/ruby/io.h (revision 51096) +++ include/ruby/io.h (revision 51097) @@ -113,6 +113,9 @@ typedef struct rb_io_t { https://github.com/ruby/ruby/blob/trunk/include/ruby/io.h#L113 #define FMODE_TEXTMODE 0x00001000 /* #define FMODE_PREP 0x00010000 */ #define FMODE_SETENC_BY_BOM 0x00100000 +/* #define FMODE_UNIX 0x00200000 */ +/* #define FMODE_INET 0x00400000 */ +/* #define FMODE_INET6 0x00800000 */ #define GetOpenFile(obj,fp) rb_io_check_closed((fp) = RFILE(rb_io_taint_check(obj))->fptr) Index: ChangeLog =================================================================== --- ChangeLog (revision 51096) +++ ChangeLog (revision 51097) @@ -1,3 +1,13 @@ https://github.com/ruby/ruby/blob/trunk/ChangeLog#L1 +Thu Jul 2 10:43:36 2015 Eric Wong <e@8...> + + * ext/socket/rubysocket.h: flags for common socket families + (rsock_getfamily): update signature + * include/ruby/io.h: comment socket FMODE flags + * ext/socket/init.c (rsock_getfamily): memoize family + * ext/socket/basicsocket.c: adjust rsock_getfamily calls + * ext/socket/ancdata.c: ditto + [ruby-core:69713] [Feature #11298] + Thu Jul 2 10:30:01 2015 SHIBATA Hiroshi <hsbt@r...> * lib/rubygems/resolver.rb: fixed NameError of Gem::Util::NULL_DEVICE. Index: ext/socket/rubysocket.h =================================================================== --- ext/socket/rubysocket.h (revision 51096) +++ ext/socket/rubysocket.h (revision 51097) @@ -232,6 +232,12 @@ extern int rsock_do_not_reverse_lookup; https://github.com/ruby/ruby/blob/trunk/ext/socket/rubysocket.h#L232 extern int rsock_cmsg_cloexec_state; #define FMODE_NOREVLOOKUP 0x100 +/* common socket families only */ +#define FMODE_UNIX 0x00200000 +#define FMODE_INET 0x00400000 +#define FMODE_INET6 0x00800000 +#define FMODE_SOCK (FMODE_UNIX|FMODE_INET|FMODE_INET6) + extern VALUE rb_cBasicSocket; extern VALUE rb_cIPSocket; extern VALUE rb_cTCPSocket; @@ -279,7 +285,7 @@ int rsock_optname_arg(int family, int le https://github.com/ruby/ruby/blob/trunk/ext/socket/rubysocket.h#L285 int rsock_cmsg_type_arg(int family, int level, VALUE type); int rsock_shutdown_how_arg(VALUE how); -int rsock_getfamily(int sockfd); +int rsock_getfamily(rb_io_t *fptr); struct rb_addrinfo { struct addrinfo *ai; Index: ext/socket/init.c =================================================================== --- ext/socket/init.c (revision 51096) +++ ext/socket/init.c (revision 51097) @@ -624,15 +624,34 @@ rsock_s_accept(VALUE klass, int fd, stru https://github.com/ruby/ruby/blob/trunk/ext/socket/init.c#L624 } int -rsock_getfamily(int sockfd) +rsock_getfamily(rb_io_t *fptr) { union_sockaddr ss; socklen_t sslen = (socklen_t)sizeof(ss); + int cached = fptr->mode & FMODE_SOCK; + + if (cached) { + switch (cached) { +#ifdef AF_UNIX + case FMODE_UNIX: return AF_UNIX; +#endif + case FMODE_INET: return AF_INET; + case FMODE_INET6: return AF_INET6; + } + } ss.addr.sa_family = AF_UNSPEC; - if (getsockname(sockfd, &ss.addr, &sslen) < 0) + if (getsockname(fptr->fd, &ss.addr, &sslen) < 0) return AF_UNSPEC; + switch (ss.addr.sa_family) { +#ifdef AF_UNIX + case AF_UNIX: fptr->mode |= FMODE_UNIX; break; +#endif + case AF_INET: fptr->mode |= FMODE_INET; break; + case AF_INET6: fptr->mode |= FMODE_INET6; break; + } + return ss.addr.sa_family; } Index: ext/socket/basicsocket.c =================================================================== --- ext/socket/basicsocket.c (revision 51096) +++ ext/socket/basicsocket.c (revision 51097) @@ -214,7 +214,7 @@ bsock_setsockopt(int argc, VALUE *argv, https://github.com/ruby/ruby/blob/trunk/ext/socket/basicsocket.c#L214 } GetOpenFile(sock, fptr); - family = rsock_getfamily(fptr->fd); + family = rsock_getfamily(fptr); level = rsock_level_arg(family, lev); option = rsock_optname_arg(family, level, optname); @@ -311,7 +311,7 @@ bsock_getsockopt(VALUE sock, VALUE lev, https://github.com/ruby/ruby/blob/trunk/ext/socket/basicsocket.c#L311 int family; GetOpenFile(sock, fptr); - family = rsock_getfamily(fptr->fd); + family = rsock_getfamily(fptr); level = rsock_level_arg(family, lev); option = rsock_optname_arg(family, level, optname); len = 256; Index: ext/socket/ancdata.c =================================================================== --- ext/socket/ancdata.c (revision 51096) +++ ext/socket/ancdata.c (revision 51097) @@ -1146,7 +1146,7 @@ bsock_sendmsg_internal(int argc, VALUE * https://github.com/ruby/ruby/blob/trunk/ext/socket/ancdata.c#L1146 GetOpenFile(sock, fptr); #if defined(HAVE_STRUCT_MSGHDR_MSG_CONTROL) - family = rsock_getfamily(fptr->fd); + family = rsock_getfamily(fptr); #endif data = vflags = dest_sockaddr = Qnil; @@ -1692,7 +1692,7 @@ bsock_recvmsg_internal(int argc, VALUE * https://github.com/ruby/ruby/blob/trunk/ext/socket/ancdata.c#L1692 ); #if defined(HAVE_STRUCT_MSGHDR_MSG_CONTROL) - family = rsock_getfamily(fptr->fd); + family = rsock_getfamily(fptr); if (mh.msg_controllen) { char *msg_end = (char *)mh.msg_control + mh.msg_controllen; for (cmh = CMSG_FIRSTHDR(&mh); cmh != NULL; cmh = CMSG_NXTHDR(&mh, cmh)) { -- ML: ruby-changes@q... Info: http://www.atdot.net/~ko1/quickml/