From: Eric Wong <normalperson@yhbt.net>
To: kgio@librelist.com
Subject: Re: [PATCH 1/2] add `#kgio_writev` and `#kgio_trywritev`
Date: Tue, 29 May 2012 19:11:27 +0000 [thread overview]
Message-ID: <20120529191127.GA25401@dcvr.yhbt.net> (raw)
In-Reply-To: 1338303629-12277-1-git-send-email-funny.falcon@gmail.com
Sokolov Yura 'funny-falcon <funny.falcon@gmail.com> wrote:
> Add methods for using writev(2) syscall for sending array of string in
> a single syscall. This is more efficient than concatenating strings on
> Ruby side.
> `#kgio_trywritev` returns array of strings which are not sent to the
> socket.
Thanks. Comments inline.
> Since both methods dups array and strings in it, `#kgio_writev` semantic
> a bit different from `#kgio_write`: it does not react on changes to
> array/strings that made in other thread. But I think, this way is more
> correct.
Perhaps we should use rb_str_locktmp()?
> --- a/ext/kgio/read_write.c
> +++ b/ext/kgio/read_write.c
> @@ -1,6 +1,9 @@
> #include "kgio.h"
> #include "my_fileno.h"
> #include "nonblock.h"
> +#ifdef HAVE_WRITEV
> +# include "sys/uio.h"
> +#endif
Use angle braces for system headers: <sys/uio.h>
> static VALUE sym_wait_readable, sym_wait_writable;
> static VALUE eErrno_EPIPE, eErrno_ECONNRESET;
> static ID id_set_backtrace;
> @@ -403,6 +406,158 @@ static VALUE kgio_trywrite(VALUE io, VALUE str)
> return my_write(io, str, 0);
> }
>
> +struct io_args_v {
> + VALUE io;
> + VALUE buf;
> + VALUE vec_buf;
> + struct iovec *vec;
> + int rest_len;
> + int total_len;
size_t is better for lengths. Or long if you want RSTRING compat
but definitely not int.
Also, use hard tabs for all indentations.
> +#ifdef HAVE_WRITEV
> +static void prepare_writev(struct io_args_v *a, VALUE io, VALUE ary)
> +{
> + long i;
> + a->io = io;
> + a->fd = my_fileno(io);
> + a->buf = rb_ary_dup(ary);
> + a->vec_buf = rb_str_tmp_new(sizeof(struct iovec) * RARRAY_LEN(a->buf));
> + a->vec = (struct iovec*)RSTRING_PTR(a->vec_buf);
> + a->total_len = 0;
> + for(i=0; i < RARRAY_LEN(a->buf); i++) {
Formatting:
for (i = 0; ...
> + VALUE str = RARRAY_PTR(a->buf)[i];
> + if (TYPE(str) != T_STRING) {
> + str = rb_obj_as_string(str);
> + } else {
> + str = rb_str_dup_frozen(str);
> + }
> + RARRAY_PTR(a->buf)[i] = str;
> + a->vec[i].iov_base = RSTRING_PTR(str);
> + a->vec[i].iov_len = RSTRING_LEN(str);
> + a->total_len += RSTRING_LEN(str);
Avoid repeatedly calling RARRAY_*/RSTRING_* macros on the same object.
It adds noise and makes the code harder to read (especially with CAPS).
There's also a small size reduction because these macros branch in MRI
1.9+
> + }
> + a->rest_len = a->total_len;
> +}
> +
> +static int writev_check(struct io_args_v *a, long n, const char *msg, int io_wait)
> +{
> + if (a->rest_len == n) {
> +done:
> + a->buf = Qnil;
> + } else if (n == -1) {
> + if (errno == EINTR) {
> + a->fd = my_fileno(a->io);
> + return -1;
> + }
> + if (errno == EAGAIN) {
> + if (io_wait) {
> + (void)kgio_call_wait_writable(a->io);
> + return -1;
> + } else if (a->total_len == a->rest_len) {
> + a->buf = sym_wait_writable;
> + }
> + return 0;
> + }
> + wr_sys_fail(msg);
> + } else {
> + assert(n >= 0 && n < a->rest_len && "writev syscall broken?");
> + a->rest_len -= n;
> + while (n > 0) {
> + VALUE str = RARRAY_PTR(a->buf)[0];
> + if (RSTRING_LEN(str) > n) {
> + str = rb_str_subseq(str, n, RSTRING_LEN(str) - n);
> + RARRAY_PTR(a->buf)[0] = str;
> + a->vec->iov_base = RSTRING_PTR(str);
> + a->vec->iov_len = RSTRING_LEN(str);
> + n = 0;
> + } else {
> + n -= RSTRING_LEN(str);
> + rb_ary_shift(a->buf);
> + a->vec++;
Probably better to store the array offset instead of shifting
Array#shift is O(n).
> + }
> + }
> + return -1;
> + }
> + return 0;
> +}
> +
> +static VALUE my_writev(VALUE io, VALUE str, int io_wait)
> +{
> + struct io_args_v a;
> + long n, iov_cnt, iov_max;
> +
> + prepare_writev(&a, io, str);
> + set_nonblocking(a.fd);
> + iov_max = sysconf(_SC_IOV_MAX);
> +retry:
> + iov_cnt = RARRAY_LEN(a.buf);
> + if (iov_cnt > iov_max) iov_cnt = iov_max;
> + n = (long)writev(a.fd, a.vec, iov_cnt);
> + if (writev_check(&a, n, "writev", io_wait) != 0)
> + goto retry;
> + if (TYPE(a.buf) != T_SYMBOL)
> + kgio_autopush_write(io);
> + return a.buf;
(I'm not fully awake): You do retry when truncating to iov_max, right?
> +}
> +#endif
> +
> +/*
> + * call-seq:
> + *
> + * io.kgio_writev(array) -> nil
> + *
> + * Returns nil when the write completes.
> + *
> + * This may block and call any method defined to +kgio_wait_writable+
> + * for the class.
> + *
> + * It fallbacks to kgio_write when writev(2) syscall is missing
> + */
> +static VALUE kgio_writev(VALUE io, VALUE ary)
> +{
> + VALUE array = rb_check_array_type(ary);
> +#ifdef HAVE_WRITEV
> + return my_writev(io, array, 1);
> +#else
> + VALUE str = rb_ary_join(array, Qnil);
> + return my_write(io, str, 1);
> +#endif
> +}
I don't like #ifdef inside function definitions, so I'd rather have
something like this:
#ifdef HAVE_WRITEV
static VALUE kgio_writev(VALUE io, VALUE ary)
{
...
}
#else
# include "compat_kgio_writev.h"
#endif
> +static VALUE kgio_trywritev(VALUE io, VALUE ary)
> +{
> + VALUE array = rb_check_array_type(ary);
> +#ifdef HAVE_WRITEV
> + return my_writev(io, array, 0);
> +#else
> + VALUE str = rb_ary_join(array, Qnil);
> + VALUE result = my_write(io, str, 0);
> + if (TYPE(result) == T_STRING) {
> + return rb_ary_new4(1, &result);
> + }
> + return result;
> +#endif
Ditto.
next prev parent reply other threads:[~2012-05-29 19:11 UTC|newest]
Thread overview: 6+ messages / expand[flat|nested] mbox.gz Atom feed top
2012-05-29 15:00 [PATCH 1/2] add `#kgio_writev` and `#kgio_trywritev` Sokolov Yura 'funny-falcon
2012-05-29 15:00 ` [PATCH 2/2] tests for " Sokolov Yura 'funny-falcon
2012-05-29 19:13 ` Eric Wong
2012-05-29 19:11 ` Eric Wong [this message]
2012-05-30 4:30 ` [PATCH 1/2] add " Yura Sokolov
2012-05-30 4:55 ` 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/kgio/
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20120529191127.GA25401@dcvr.yhbt.net \
--to=normalperson@yhbt.net \
--cc=kgio@librelist.com \
/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/kgio.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).