From: Eric Wong <normalperson@yhbt.net>
To: kgio@librelist.com
Subject: Re: [PATCH 3/3] add `#kgio_writev` and `#kgio_trywritev`
Date: Thu, 31 May 2012 14:19:23 -0700 [thread overview]
Message-ID: <20120531211923.GA21892@dcvr.yhbt.net> (raw)
In-Reply-To: 1338454833-11754-1-git-send-email-funny.falcon@gmail.com
Sokolov Yura 'funny-falcon <funny.falcon@gmail.com> wrote:
> +/* it seems that socket buffer is rarely exceed 1MB */
See other email about LFN. On the other hand, 1M may be a good size
anyways to fit into the CPU cache. Buffer size tuning is hard so
I usually avoid it :)
> +#define WRITEV_MEMLIMIT (1024*1024)
> +#define WRITEV_IMPL_THRESHOLD 512
I would like to see data/examples/benchmarks for how you arrived at the
WRITEV_IMPL_THRESHOLD.
This patch looks pretty good. More comments below.
> +static unsigned int iov_max = 1024; /* this could be overriden in init */
> +
> +struct io_args_v {
> + VALUE io;
> + VALUE buf;
> + VALUE vec_buf;
> + struct iovec *vec;
> + unsigned long iov_cnt;
> + size_t iov_len;
The "iov_len" name confused me, I think something with "total" is better.
Perhaps "total_len"?
> +static void prepare_writev(struct io_args_v *a, VALUE io, VALUE ary)
> +{
> + long vec_cnt;
> + static struct iovec stub = {NULL, 0};
> + a->io = io;
> + a->fd = my_fileno(io);
> + a->something_written = 0;
> +
> + /* rb_ary_subseq will not copy array unless it modified */
> + a->buf = rb_ary_subseq(ary, 0, RARRAY_LEN(ary));
> +
> + a->vec_buf = rb_str_new(0, 0);
> + a->vec = &stub;
Can we just leave a->vec unset? It's always set in fill_iovec anyways.
Static variables inside library function smells funny.
> +static void fill_iovec(struct io_args_v *a)
> +{
> + unsigned long i;
> + struct iovec *curvec;
> +
> + a->iov_cnt = RARRAY_LEN(a->buf);
> + a->iov_len = 0;
> + if (a->iov_cnt == 0) return;
> + if (a->iov_cnt > iov_max) a->iov_cnt = iov_max;
> + rb_str_resize(a->vec_buf, sizeof(struct iovec) * a->iov_cnt);
> + curvec = a->vec = (struct iovec*)RSTRING_PTR(a->vec_buf);
> + /* lets limit total memory to write,
> + * but always take first string */
> + next_len = a->iov_len + str_len;
> + if (i && next_len > WRITEV_MEMLIMIT) {
> + a->iov_cnt = i;
> + break;
Use hard tabs like the rest of the file
> +static VALUE my_writev(VALUE io, VALUE str, int io_wait)
> +{
> + struct io_args_v a;
> + long n;
> +
> + prepare_writev(&a, io, str);
> + set_nonblocking(a.fd);
> +
> + do {
> + fill_iovec(&a);
> +#ifdef HAVE_WRITEV
> + /* for big strings use library function */
> + if (a.iov_len / WRITEV_IMPL_THRESHOLD > a.iov_cnt)
> + n = (long)writev(a.fd, a.vec, a.iov_cnt);
> + else
> +#endif
> + n = (long)custom_writev(a.fd, a.vec, a.iov_cnt, a.iov_len);
Please no. CPP conditionals inside functions is confusing, but CPP
conditionals mixing with C conditionals makes my eyes bleed.
How about something like this?
#ifdef HAVE_WRITEV
static ssize_t
real_writev(int fd, const struct iovec *vec, int cnt, size_t total)
{
return writev(fd, vec, cnt);
}
#else
# define real_writev(fd,vec,cnt,total) custom_writev((fd),(vec),(cnt),(total))
#endif
next prev parent reply other threads:[~2012-05-31 21:19 UTC|newest]
Thread overview: 19+ messages / expand[flat|nested] mbox.gz Atom feed top
2012-05-30 13:56 [PATCH 1/3] Fix UnixClientReadServerWrite test class name Sokolov Yura 'funny-falcon
2012-05-30 13:56 ` [PATCH 2/3] use rb_str_subseq for tail string on write Sokolov Yura 'funny-falcon
2012-05-30 18:57 ` Eric Wong
2012-05-30 19:29 ` Юрий Соколов
2012-05-30 13:56 ` [PATCH 3/3] add `#kgio_writev` and `#kgio_trywritev` Sokolov Yura 'funny-falcon
2012-05-30 19:55 ` Юрий Соколов
2012-05-30 20:38 ` Eric Wong
2012-05-31 6:16 ` Юрий Соколов
2012-05-31 21:10 ` Eric Wong
2012-05-30 20:39 ` Eric Wong
2012-05-31 6:26 ` Юрий Соколов
2012-05-31 21:14 ` Eric Wong
2012-05-31 21:56 ` Eric Wong
2012-05-31 9:00 ` Sokolov Yura 'funny-falcon
2012-05-31 21:19 ` Eric Wong [this message]
2012-06-01 6:14 ` Юрий Соколов
2012-06-01 7:55 ` Eric Wong
2012-06-01 9:42 ` [PATCH] " Sokolov Yura 'funny-falcon
2012-06-01 19:20 ` 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=20120531211923.GA21892@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).