raindrops RubyGem user+dev discussion/patches/pulls/bugs/help
 help / color / mirror / code / Atom feed
From: Eric Wong <bofh@yhbt.net>
To: raindrops-public@yhbt.net
Subject: [PATCH 1/4] avoid unnecessary #to_io calls
Date: Sun, 11 Jun 2023 21:33:25 +0000	[thread overview]
Message-ID: <20230611213328.379546-2-bofh@yhbt.net> (raw)
In-Reply-To: <20230611213328.379546-1-bofh@yhbt.net>

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 ++--
 ext/raindrops/my_fileno.h       | 13 ++++---------
 ext/raindrops/tcp_info.c        |  2 +-
 3 files changed, 7 insertions(+), 12 deletions(-)

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)) {
diff --git a/ext/raindrops/my_fileno.h b/ext/raindrops/my_fileno.h
index 00c5d29..646f31b 100644
--- a/ext/raindrops/my_fileno.h
+++ b/ext/raindrops/my_fileno.h
@@ -1,22 +1,17 @@
 #include <ruby.h>
 #include <ruby/io.h>
 
+#ifdef HAVE_RB_IO_DESCRIPTOR
+#	define my_fileno(io) rb_io_descriptor(io)
+#else /* Ruby <3.1 */
 static int my_fileno(VALUE io)
 {
-#ifdef HAVE_RB_IO_DESCRIPTOR
-	if (TYPE(io) != T_FILE)
-		io = rb_convert_type(io, T_FILE, "IO", "to_io");
-
-	return rb_io_descriptor(io);
-#else
 	rb_io_t *fptr;
 
-	if (TYPE(io) != T_FILE)
-		io = rb_convert_type(io, T_FILE, "IO", "to_io");
 	GetOpenFile(io, fptr);
 
 	if (fptr->fd < 0)
 		rb_raise(rb_eIOError, "closed stream");
 	return fptr->fd;
-#endif
 }
+#endif /* Ruby <3.1 !HAVE_RB_IO_DESCRIPTOR */
diff --git a/ext/raindrops/tcp_info.c b/ext/raindrops/tcp_info.c
index b82f705..c0d34e0 100644
--- a/ext/raindrops/tcp_info.c
+++ b/ext/raindrops/tcp_info.c
@@ -76,7 +76,7 @@ static VALUE alloc(VALUE klass)
  */
 static VALUE init(VALUE self, VALUE io)
 {
-	int fd = my_fileno(io);
+	int fd = my_fileno(rb_io_get_io(io));
 	struct tcp_info *info = DATA_PTR(self);
 	socklen_t len = (socklen_t)sizeof(struct tcp_info);
 	int rc = getsockopt(fd, IPPROTO_TCP, TCP_INFO, info, &len);

  reply	other threads:[~2023-06-11 21:33 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-06-11 21:33 [PATCH 0/4] minor modernizations and cleanups Eric Wong
2023-06-11 21:33 ` Eric Wong [this message]
2023-06-11 21:33 ` [PATCH 2/4] my_fileno: use rb_io_check_closed for Ruby <3.1 Eric Wong
2023-06-11 21:33 ` [PATCH 3/4] linux_inet_diag: get rid of alloca usage Eric Wong
2023-06-11 21:33 ` [PATCH 4/4] test_linux_reuseport_tcp_listen_stats: skip w/o unicorn Eric Wong

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://yhbt.net/raindrops/

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20230611213328.379546-2-bofh@yhbt.net \
    --to=bofh@yhbt.net \
    --cc=raindrops-public@yhbt.net \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

	https://yhbt.net/raindrops.git/

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).