diff options
author | Eric Wong <bofh@yhbt.net> | 2023-06-11 21:33:25 +0000 |
---|---|---|
committer | Eric Wong <bofh@yhbt.net> | 2023-06-26 09:16:36 +0000 |
commit | a8dfec2568830241a27fce10014b6151a7a47813 (patch) | |
tree | 9c31af5dbeffb046a23eb1fb4fe85ec68a7f49fc /ext/raindrops/my_fileno.h | |
parent | b3212417cc3e7cc44aa9e1ffe89b0d62ef3fdab5 (diff) | |
download | raindrops-a8dfec2568830241a27fce10014b6151a7a47813.tar.gz |
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.
Diffstat (limited to 'ext/raindrops/my_fileno.h')
-rw-r--r-- | ext/raindrops/my_fileno.h | 13 |
1 files changed, 4 insertions, 9 deletions
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 */ |