Dear Marc and Gambit community,<div><br></div><div>Here comes a subject of meditation for the weekend. As to maximize clarity and discussability I'll split it in two emails. Here we go:</div>
<div><br></div><div><br></div><div><font size="4">How do we make Gambit port-based protocol wrapper Gambit ports with great performance and as solid abstractions?</font></div><div><br></div><div><br></div><div>Various forms of byte- and character-stream based protocols, such as communication protocols like SSL channels, or encodings like GZIP, probably accounts for in the range ~35% of all Gambit use, so it's central and even critical stuff.</div>




<div><br></div><div>If all of this can be not just whimsical special-purpose modules but be implemented in the united format of Gambit ports, *alot* would be won: Gambit's IO API is really neat and complete in nature: Being able to deliver also these completely central uses through it would be of enormous practical value.</div>




<div><br></div><div><br></div><div>Perhaps even everything needed for this is in Gambit already and we pretty much only need to make a bit of documentation and examples and this way we can all enjoy easily pluggable modules for the rich diversity of uses that are in this field.</div>




<div><br></div><div><br></div><div>To stimulate the conversation and add context, below I'll reflect my long experience of this kind of use and a proposal for qualities that such an abstraction mechanism needs.</div>



<div>
<br></div><div><div>I hope you share the enjoyment and appreciation of this topic, and look forward to any discussion that ensues.</div></div><div><br></div><div><br></div><div><br></div><div><b>Experience of protocol wrapper Gambit ports using <font face="courier new, monospace">open-u8vector-pipe</font></b><b>: Super easy to implement, and delivers beyond unacceptably badly in the respect that it's not suitable for real-world use ever.</b></div>




<div>Briefly, over the last years I used Gambit for doing SSL over TCP. A typical, elementary and critical use case for this is HTTPS client/server.</div><div><br></div><div>Til now I've essentially made two SSL modules for Gambit, both atop OpenSSL. Total time invested is somewhere in the range 100 - 150 man hours, a lot due great complexity of and lack of documentation in how the OpenSSL library is used in particular in nonblocking mode.</div>




<div><br></div><div><br></div><div>The first one was made based on a bidirectional byte channel Gambit port as generated by Gambit's |open-u8vector-pipe|.</div><div><br></div><div>Put very briefly, my experience with it was that </div>




<div><br></div><div> * It had a low transfer speed performance and high latency - probably because of the extra round through Gambit's scheduler needed for every piece of IO done, as compared with just using a native Gambit port such as a TCP client port directly -, and,</div>




<div><br></div><div> * It had all the pains of a leaky abstraction, such as, that when either party would close the channel, it was not necessarily propagated to this channel as the <font face="courier new, monospace">|read-subu8vector|</font> would not necessarily return straight away nor have a return value clearly reflecting that the connection was closed, so actual freeing of this port could take a while.</div>



<div>     I don't remember the exact details of this at the moment, but the constant feeling was like, uh oh, OMG what's that object on the heap doing now really plus the discomfort of knowing it's possible it's not quite right and of not having a clue - I believe you have similar experiences from your own encounters with leaky abstractions.</div>




<div><br></div><div>So briefly, my experience of <font face="courier new, monospace">|open-u8vector-pipe|</font> for providing a protocol wrapper atop Gambit ports was completely unsatisfactory, and provided motivation for exploring an alternative implementation path later. So..</div>




<div><br></div><div><br></div><div><b>Experience of protocol wrapper Gambit ports using custom "io-primitives" structures with a closure for each IO routine: Delivers great but will never be universal between projects</b></div>




<div>The second one, completed very recently, was built on the concept of that the Gambit port is wrapped into an "io-primitives" structure that contains one closure for each IO method: IO of u8vector chunks, and of ISO-8859-1/ANSI/8-bit encoded characters at the level of character, line and string, for complete definition see [1].</div>




<div><br></div><div>The benefits here are quite tremendously good: The io-primitives code itself accesses Gambit's IO routines directly, and thus</div><div><br></div><div> * Latency is ~zero, excellent.   Also, thanks to the same thing,</div>




<div><br></div><div> * Both the beneficiary code and the io-primitives code follows IO behavior directly, making the entire abstraction not leak but be solid. Introspection for instance is piece of cake as execution passes directly between the layers of port abstraction - that's easy to understand due to the sequential nature of the execution, it's easy to log meaningfully and to work with in the interactive debugger.</div>




<div><br></div><div><br></div><div>Now, after having implemented this and had these basically very good experiences, it became/was completely evident that this is and will remain a special purpose code, as Gambit's built-in IO API is so good in itself. And really, these io-primitives could be mapped to Gambit's IO exports, it's quite the same thing really, so if that can be done in a way that really delivers it's completely preferable. Thus this proposition:</div>




<div><br></div><div><br></div><div><b>Proposed characteristics for Gambit protocol wrapper port functionality</b></div><div>So, now here are the primary things that come to my mind about what protocol wrapper support ought to include:</div>




<div><br></div><div> * Provision of application-layer ports exported through Gambit's IO routines</div><div><br></div><div> * These ports can export individual Gambit IO routines i.e. can provide a specific <font face="courier new, monospace">read-char</font>, <font face="courier new, monospace">read-u8</font>, <font face="courier new, monospace">read-subu8vector</font>, <font face="courier new, monospace">read-line</font> behavior, so that a call e.g. <font face="courier new, monospace">(read-subu8vector u8v start end port)</font> leads directly to <font face="courier new, monospace">|mycustomimplementation-read-subu8vector|</font>. Part of the point with this is that,</div>




<div><br></div><div> * Double-buffering and copying must be kept to a minimum as it brings all kinds of expense and complexity - a "protocol wrapper" like this, for example an SSL wrapper, must not be required to work with the beneficiary code via a buffer, but must be let to deal with buffering as it wants to. Furthermore I'd say that,</div>




<div><br></div><div> * Due to the dual nature of a lot of protocols, the API must be in a hybrid binary-character mode i.e. binary IO (i.e. read/write-subu8vector) must be combinable with character IO, at least for 8bit-fixedsize-character i.e. ISO-8859-1 encoding.</div>



<div><br></div><div>   This is not a must though - even if a bit of work, binary mode only would be fine, and the user can just wrap <font face="courier new, monospace">read-u8</font> to <font face="courier new, monospace">read-char</font> etc., this is easy enough to do really.</div>



<div><br></div><div>   And last, I'd guess that</div>
<div><br></div><div> * There must be a way to implement these ports in such a way that any IO made of them does not use mutexes at all (i.e. can be run in non-threadsafe mode), and, can be done with not so many trampolines happening.   The reason for this is performance:</div>




<div><br></div><div>    The default <font face="courier new, monospace">|read-char|</font> implementation whose design involves both mutex locking and trampolines on each call, delivers unacceptably low performance (approximately 200KB/second on laptop CPU core), while a direct implementation approach would deliver approx 25MB/second on the same core which is really good.     (Disclaimer: these two numbers are taken off the top of my head from 5 years ago, though should apply now.)</div>




<div><br></div><div>    Mutexes can be addressed now, and trampolines probably better wait til the native backend is around, where module-to-module calls don't involve trampolines anymore anyhow, and I guess these parts of Gambit's runtime can be run under that backend at that time.</div>



<div><br></div><div><br>
</div><div><b>Rough proposition of API, draft</b></div><div><br></div><div><font face="courier new, monospace">(make-custom-port #!key read-subu8vector read-substring read-u8 read-char peek-char read-line</font></div><div>




<font face="courier new, monospace">                        write-subu8vector write-substring write-u8 write-char</font></div><div><font face="courier new, monospace">                        force-output close-input-port close-output-port close-port</font></div>




<div><font face="courier new, monospace">                        input-port-timeout-set! output-port-timeout-set!                     )</font></div><div><font face="courier new, monospace">=> gambit-port</font></div><div>



<br></div><div>Note the absence of <font face="courier new, monospace">read</font> and <font face="courier new, monospace">write</font> here - those deal with objects and are not in the scope of this port abstraction mechanism.</div>




<div><br></div><div>It would not be an issue that a particular protocol wrapper port implementation would only implement some of the involved functionality, such as only byte and no character IO; if anything is needed it can be implemented later, the important thing here is for Gambit to provide a format for this that encompasses all this area of use in a performant and qualitative way and thus is suitable for cross-module, cross-project use.</div>




<div><br></div><div><br><b>Example</b></div><div><div>With this approach it'd be something like</div><div><br></div><div><font face="courier new, monospace">(let* ((client (open-tcp-client "<a href="http://wikipedia.org:443">wikipedia.org:443</a>"))</font></div>

<div><font face="courier new, monospace">       (ssl-channel (open-ssl-channel client))</font><span style="font-family:'courier new',monospace">)</span></div><div><font face="courier new, monospace">  (display "POST /someform HTTP/1.1\nHost: <a href="http://wikipedia.org">wikipedia.org</a>\nContent-Length: 100\n\n" ssl-channel)</font></div>

<div><font face="courier new, monospace">  (write-subu8vector postdata 0 (u8vector-length postdata) </font><span style="font-family:'courier new',monospace">ssl-channel</span><span style="font-family:'courier new',monospace">)</span></div>

<div><span style="font-family:'courier new',monospace">  (force-output </span><span style="font-family:'courier new',monospace">ssl-channel 1)</span></div><div><font face="courier new, monospace">  (let ((hdr (read-line </font><span style="font-family:'courier new',monospace">ssl-channel</span><font face="courier new, monospace">))</font></div>

<div><font face="courier new, monospace">    [..]</font></div></div><div><font face="courier new, monospace"><br></font></div><div><br></div><div><b>Related implementation/code example</b></div><div>For an example of an implementation of a set of IO operation closures akin to what's suggested in this email, see line 61 - 89 of openssl-sack.scm and row 1621 to 1969 of openssl.scm , both attached.</div>

<div><br></div><div>This suits as an example, as as it exports an API extremely similar to Gambit's IO, it is essentially built atop Gambit's IO API in itself, it adds no latency, delivers high performance and performs minimum buffering and copying while providing some hybrid byte and character IO, and can be run in a thread-unsafe mode.</div>

<div><br></div><div><br></div><div><b>What do you say?</b></div><div>Now I wish to query you about your take on this:</div><div><br></div><div> * What are your thoughts on this topic?</div><div><br></div><div>and:</div><div>




<br></div><div> * Marc, how usable is Gambit's included IO abstraction for this kind of use today?</div><div><br></div><div>    Spontaneously, would you say this can be done right away just given people know how to do it (i.e. documentation), or would adaptations of the IO code be required, if so what kind of adaptations would it be?</div>




<div><br></div><div>    Feel free to reflect at high detail what of this can be done already now, perhaps all is ready and it's just a question of getting going with it.</div><div><br></div><div><br></div><div><br></div>




<div>With best regards,</div><div>Mikael</div><div><br></div><div><br></div><div><br></div><div><br></div><div><br></div><div><br></div><div><br></div><div>[1] io-primitives definition used in the particular wrapper described above:</div>




<div><br></div><div><div><font face="courier new, monospace">;      Name                      Arguments & Result                  Behavior</font></div><div><font face="courier new, monospace">;      read-subu8vector          (u8v start end #!optional (need 1)) Read u8vector data</font></div>




<div><font face="courier new, monospace">;                                => bytes read or #!eof</font></div><div><font face="courier new, monospace">;      read-substring            (str start end #!optional (need 1)) Read string data as ISO-8859-1</font></div>




<div><font face="courier new, monospace">;                                => bytes read or #!eof</font></div><div><font face="courier new, monospace">;      read-u8                   () => byte or #!eof                 Read a byte</font></div>




<div><font face="courier new, monospace">;      write-subu8vector         (u8v start end)                     Write u8vector data</font></div><div><font face="courier new, monospace">;      display                   (v)                                 Write string, number or symbol as ISO-8859-1</font></div>




<div><font face="courier new, monospace">;      force-output              ()                                  Flush any write buffer to network</font></div><div><font face="courier new, monospace">;      close-port                ()                                  Close port</font></div>




<div><font face="courier new, monospace">;      make-read-line-until-crlf (#!optional return-eof-on-eof?)     Read line until reaching crlf. Permissive behavior.</font></div><div><font face="courier new, monospace">;                                => procedure taking no args         The procedure returned may be used in a single thread only and thus</font></div>




<div><font face="courier new, monospace">;                                => string or maybe #!eof, see desc  reuse the same buffers across invocations.</font></div><div><font face="courier new, monospace">;                                                                    If return-eof-on-eof? is set then #!eof is returned in case of EOF.</font></div>




<div><font face="courier new, monospace">;                                                                    If not, string reading is just ended and result is returned.</font></div></div><div><br></div><div><br></div><div>




<br></div><div><br></div><div><br></div><div><br></div>