caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
From: "Daniel Bünzli" <daniel.buenzli@erratique.ch>
To: Anil Madhavapeddy <anil@recoil.org>
Cc: caml-list <caml-list@inria.fr>
Subject: Re: [Caml-list] Non-blocking IO interface design
Date: Tue, 10 Apr 2012 10:21:06 +0200	[thread overview]
Message-ID: <8CE84D3ACD2A43659ABB1EF6BAFE8A9A@erratique.ch> (raw)
In-Reply-To: <1341A6A9-0273-434A-BCA9-819DACA53FFF@recoil.org>

Anil,

Thanks for the analysis. 

> The I/O loop is being called twice for the non-blocking version, as it receives
> the `Await signal, does the Unix syscall, and then jumps into decode_src. Presumably
> a full non-blocking version would have to register with a select handler if it
> gets an EAGAIN at this point,


Yes.

> In terms of the number of system calls, the non-blocking one is more efficient,
> as it uses a 16KB buffer versus the 4K reads done by the blocking version.


Yes, the 4K reads are a limitation of pervasives channels. For each mechanism I used the largest buffer that the OCaml runtime uses. 

> Looking at the two decoders in src/se.ml, it looks like the non-blocking one
> allocates closures on every loop, which the blocking one doesn't. This is so it
> can store the continuation in d.k for the next loop. 


Yes, that's a side effect of writing in continuation passing style in general since continuations are often partially applied functions. 

> So to summarise, instead of storing a continuation closure, it would probably be better
> to explicitly store the state in d.k to minimise allocation?


Maybe, but keep in mind that s-expressions are very simple to parse. It may be obvious in this case but depending on what you decode defining/storing the state may become complex. Cps is an easy and general way to solve the problem while keeping the whole thing reasonably readable. But do you maybe see another pattern that I don't ?

> The library looks very useful by the way: I have exactly the same issue with several
> Lwt-only protocol libraries we're developing at the moment. Would love to use yours before
> the first release of them to make them more independent of the underlying I/O mechanism...


That would be nice, I'm glad if you can somehow reuse the pattern.


Best,

Daniel

  reply	other threads:[~2012-04-10  8:22 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2012-04-08 20:37 Daniel Bünzli
2012-04-09 19:28 ` Anil Madhavapeddy
2012-04-10  8:21   ` Daniel Bünzli [this message]
2012-04-10 12:40     ` Yaron Minsky
2012-04-14  9:46       ` Daniel Bünzli

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

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

  git send-email \
    --in-reply-to=8CE84D3ACD2A43659ABB1EF6BAFE8A9A@erratique.ch \
    --to=daniel.buenzli@erratique.ch \
    --cc=anil@recoil.org \
    --cc=caml-list@inria.fr \
    /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.
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 NNTP newsgroup(s).