From a8dfec2568830241a27fce10014b6151a7a47813 Mon Sep 17 00:00:00 2001 From: Eric Wong Date: Sun, 11 Jun 2023 21:33:25 +0000 Subject: avoid unnecessary #to_io calls Calling `#to_io' is only necessary when we're handling an argument from user code where the user could pass a non-IO object. `#to_io' calls are a waste of time when we create the IO object ourselves (in `Raindrops::InetDiagSock.new'). This allows us to define the `my_fileno' macro for Ruby 3.1+ users to call the new `rb_io_descriptor' function directly without an extra C stack frame. This also allows us to get rid of nesting CPP directives inside C functions which (IMHO) improves readability. Furthermore, any necessary #to_io calls using `rb_convert_type' can be replaced with `rb_io_get_io' to decrease code size. `rb_io_get_io' has been in ruby/io.h since Ruby 1.9.2 and there's no expectation that it'd be deprecated since it only deals with opaque `VALUE' types. --- ext/raindrops/linux_inet_diag.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'ext/raindrops/linux_inet_diag.c') diff --git a/ext/raindrops/linux_inet_diag.c b/ext/raindrops/linux_inet_diag.c index 2a2360c..e1ae62a 100644 --- a/ext/raindrops/linux_inet_diag.c +++ b/ext/raindrops/linux_inet_diag.c @@ -627,8 +627,8 @@ static VALUE tcp_listener_stats(int argc, VALUE *argv, VALUE self) args.iov[2].iov_len = OPLEN; args.iov[2].iov_base = alloca(page_size); args.table = NULL; - if (NIL_P(sock)) - sock = rb_funcall(cIDSock, id_new, 0); + sock = NIL_P(sock) ? rb_funcall(cIDSock, id_new, 0) + : rb_io_get_io(sock); args.fd = my_fileno(sock); switch (TYPE(addrs)) { -- cgit v1.2.3-24-ge0c7