raindrops RubyGem user+dev discussion/patches/pulls/bugs/help
 help / color / mirror / code / Atom feed
* [PATCH 0/4] minor modernizations and cleanups
@ 2023-06-11 21:33 Eric Wong
  2023-06-11 21:33 ` [PATCH 1/4] avoid unnecessary #to_io calls Eric Wong
                   ` (3 more replies)
  0 siblings, 4 replies; 5+ messages in thread
From: Eric Wong @ 2023-06-11 21:33 UTC (permalink / raw)
  To: raindrops-public

alloca use was dangerous for architectures with >4K pages,
and we can avoid some Ruby #to_io dispatches in some places.

Eric Wong (4):
  avoid unnecessary #to_io calls
  my_fileno: use rb_io_check_closed for Ruby <3.1
  linux_inet_diag: get rid of alloca usage
  test_linux_reuseport_tcp_listen_stats: skip w/o unicorn

 ext/raindrops/linux_inet_diag.c               | 23 +++++++++++--------
 ext/raindrops/my_fileno.h                     | 16 ++++---------
 ext/raindrops/tcp_info.c                      |  2 +-
 test/test_linux_reuseport_tcp_listen_stats.rb |  2 +-
 4 files changed, 20 insertions(+), 23 deletions(-)

^ permalink raw reply	[flat|nested] 5+ messages in thread

* [PATCH 1/4] avoid unnecessary #to_io calls
  2023-06-11 21:33 [PATCH 0/4] minor modernizations and cleanups Eric Wong
@ 2023-06-11 21:33 ` Eric Wong
  2023-06-11 21:33 ` [PATCH 2/4] my_fileno: use rb_io_check_closed for Ruby <3.1 Eric Wong
                   ` (2 subsequent siblings)
  3 siblings, 0 replies; 5+ messages in thread
From: Eric Wong @ 2023-06-11 21:33 UTC (permalink / raw)
  To: raindrops-public

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);

^ permalink raw reply related	[flat|nested] 5+ messages in thread

* [PATCH 2/4] my_fileno: use rb_io_check_closed for Ruby <3.1
  2023-06-11 21:33 [PATCH 0/4] minor modernizations and cleanups Eric Wong
  2023-06-11 21:33 ` [PATCH 1/4] avoid unnecessary #to_io calls Eric Wong
@ 2023-06-11 21:33 ` 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
  3 siblings, 0 replies; 5+ messages in thread
From: Eric Wong @ 2023-06-11 21:33 UTC (permalink / raw)
  To: raindrops-public

This is less code and hopefully smaller binaries.
`rb_io_check_closed' has been in Ruby since the pre-CVS of
decades ago, and it doesn't matter if it's removed or not
in the future since Ruby 3.1+ doesn't see this code path
and calls `rb_io_descriptor' directly.
---
 ext/raindrops/my_fileno.h | 3 +--
 1 file changed, 1 insertion(+), 2 deletions(-)

diff --git a/ext/raindrops/my_fileno.h b/ext/raindrops/my_fileno.h
index 646f31b..3a0100f 100644
--- a/ext/raindrops/my_fileno.h
+++ b/ext/raindrops/my_fileno.h
@@ -9,9 +9,8 @@ static int my_fileno(VALUE io)
 	rb_io_t *fptr;
 
 	GetOpenFile(io, fptr);
+	rb_io_check_closed(fptr);
 
-	if (fptr->fd < 0)
-		rb_raise(rb_eIOError, "closed stream");
 	return fptr->fd;
 }
 #endif /* Ruby <3.1 !HAVE_RB_IO_DESCRIPTOR */

^ permalink raw reply related	[flat|nested] 5+ messages in thread

* [PATCH 3/4] linux_inet_diag: get rid of alloca usage
  2023-06-11 21:33 [PATCH 0/4] minor modernizations and cleanups Eric Wong
  2023-06-11 21:33 ` [PATCH 1/4] avoid unnecessary #to_io calls Eric Wong
  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 ` Eric Wong
  2023-06-11 21:33 ` [PATCH 4/4] test_linux_reuseport_tcp_listen_stats: skip w/o unicorn Eric Wong
  3 siblings, 0 replies; 5+ messages in thread
From: Eric Wong @ 2023-06-11 21:33 UTC (permalink / raw)
  To: raindrops-public

alloca makes stack usage unpredictable and life difficult for
static analysis tools and compilers.  The 46 bytes of
INET6_ADDRSTRLEN is fine to keep on stack, but page size can be
several MB large in some architectures (but typically 4K on
common architectures).

Thus we handle page size-ed allocations via `rb_str_tmp_new'.
`rb_str_tmp_new' has been in public Ruby headers since the 1.9
days and used by the core `zlib', `digest', and `zlib'
extensions, so it should be safe to use (and `rb_str_resize' is
used in many more C extensions).
---
 ext/raindrops/linux_inet_diag.c | 19 +++++++++++--------
 1 file changed, 11 insertions(+), 8 deletions(-)

diff --git a/ext/raindrops/linux_inet_diag.c b/ext/raindrops/linux_inet_diag.c
index e1ae62a..2d4f503 100644
--- a/ext/raindrops/linux_inet_diag.c
+++ b/ext/raindrops/linux_inet_diag.c
@@ -212,24 +212,25 @@ static void bug_warn_nogvl(const char *fmt, ...)
 static struct listen_stats *stats_for(st_table *table, struct inet_diag_msg *r)
 {
 	char *host, *key, *port, *old_key;
-	size_t alloca_len;
 	struct listen_stats *stats;
 	socklen_t hostlen;
 	socklen_t portlen = (socklen_t)sizeof("65535");
 	int n;
 	const void *src = r->id.idiag_src;
+	char buf[INET6_ADDRSTRLEN];
+	size_t buf_len;
 
 	switch (r->idiag_family) {
 	case AF_INET: {
 		hostlen = INET_ADDRSTRLEN;
-		alloca_len = hostlen + portlen;
-		host = key = alloca(alloca_len);
+		buf_len = hostlen + portlen;
+		host = key = buf;
 		break;
 		}
 	case AF_INET6: {
 		hostlen = INET6_ADDRSTRLEN;
-		alloca_len = 1 + hostlen + 1 + portlen;
-		key = alloca(alloca_len);
+		buf_len = 1 + hostlen + 1 + portlen;
+		key = buf;
 		host = key + 1;
 		break;
 		}
@@ -269,7 +270,7 @@ static struct listen_stats *stats_for(st_table *table, struct inet_diag_msg *r)
 	old_key = key;
 
 	if (r->idiag_state == TCP_ESTABLISHED) {
-		n = snprintf(key, alloca_len, "%s:%u",
+		n = snprintf(key, buf_len, "%s:%u",
 				 addr_any(r->idiag_family),
 				 ntohs(r->id.idiag_sport));
 		if (n <= 0) {
@@ -615,7 +616,7 @@ static VALUE tcp_listener_stats(int argc, VALUE *argv, VALUE self)
 {
 	VALUE rv = rb_hash_new();
 	struct nogvl_args args;
-	VALUE addrs, sock;
+	VALUE addrs, sock, buf;
 
 	rb_scan_args(argc, argv, "02", &addrs, &sock);
 
@@ -624,8 +625,9 @@ static VALUE tcp_listener_stats(int argc, VALUE *argv, VALUE self)
 	 * buffer for recvmsg() later, we already checked for
 	 * OPLEN <= page_size at initialization
 	 */
+	buf = rb_str_buf_new(page_size);
 	args.iov[2].iov_len = OPLEN;
-	args.iov[2].iov_base = alloca(page_size);
+	args.iov[2].iov_base = RSTRING_PTR(buf);
 	args.table = NULL;
 	sock = NIL_P(sock) ? rb_funcall(cIDSock, id_new, 0)
 			: rb_io_get_io(sock);
@@ -672,6 +674,7 @@ static VALUE tcp_listener_stats(int argc, VALUE *argv, VALUE self)
 		rb_hash_foreach(rv, drop_placeholders, Qfalse);
 
 	/* let GC deal with corner cases */
+	rb_str_resize(buf, 0);
 	if (argc < 2) rb_io_close(sock);
 	return rv;
 }

^ permalink raw reply related	[flat|nested] 5+ messages in thread

* [PATCH 4/4] test_linux_reuseport_tcp_listen_stats: skip w/o unicorn
  2023-06-11 21:33 [PATCH 0/4] minor modernizations and cleanups Eric Wong
                   ` (2 preceding siblings ...)
  2023-06-11 21:33 ` [PATCH 3/4] linux_inet_diag: get rid of alloca usage Eric Wong
@ 2023-06-11 21:33 ` Eric Wong
  3 siblings, 0 replies; 5+ messages in thread
From: Eric Wong @ 2023-06-11 21:33 UTC (permalink / raw)
  To: raindrops-public

We don't list `unicorn' as a development dependency since
unicorn currently depends on this project.  While unicorn might
drop us as a dependency, don't waste disk space and bandwidth
of potential raindrops hackers who don't have unicorn.
---
 test/test_linux_reuseport_tcp_listen_stats.rb | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/test/test_linux_reuseport_tcp_listen_stats.rb b/test/test_linux_reuseport_tcp_listen_stats.rb
index 73995ea..4fda218 100644
--- a/test/test_linux_reuseport_tcp_listen_stats.rb
+++ b/test/test_linux_reuseport_tcp_listen_stats.rb
@@ -48,4 +48,4 @@ def test_reuseport_queue_stats
       assert_equal [0, i+1], all[addr].to_a
     end
   end
-end if RUBY_PLATFORM =~ /linux/
+end if RUBY_PLATFORM =~ /linux/ && Object.const_defined?(:Unicorn)

^ permalink raw reply related	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2023-06-11 21:33 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-06-11 21:33 [PATCH 0/4] minor modernizations and cleanups Eric Wong
2023-06-11 21:33 ` [PATCH 1/4] avoid unnecessary #to_io calls Eric Wong
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

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).