{"id":459,"date":"2016-10-21T13:31:10","date_gmt":"2016-10-21T13:31:10","guid":{"rendered":"http:\/\/dlang.org\/blog\/?p=459"},"modified":"2021-10-08T11:09:42","modified_gmt":"2021-10-08T11:09:42","slug":"project-highlight-libasync","status":"publish","type":"post","link":"https:\/\/dlang.org\/blog\/2016\/10\/21\/project-highlight-libasync\/","title":{"rendered":"Project Highlight: libasync"},"content":{"rendered":"<p><a href=\"https:\/\/github.com\/etcimon\/libasync\"><img loading=\"lazy\" class=\"alignleft size-full wp-image-181\" src=\"http:\/\/dlang.org\/blog\/wp-content\/uploads\/2016\/08\/d6.png\" alt=\"d6\" width=\"200\" height=\"200\" \/>libasync <\/a>is a cross-platform event loop library written completely in D. \u00a0It was created, and continues to be maintained, by Etienne Cimon, who started it as a native driver for <a href=\"http:\/\/vibed.org\/\">vibe.d<\/a>, a modular asynchronous I\/O framework most often used for web app development in D.<\/p>\n<blockquote><p>In 2014 or so, I was looking for a framework to power my future web development projects. I wasn&#8217;t going to use an interpreted language, as binary executables were too attractive. I found vibe.d appealing because, coming from C++, it was relatively simple and featureful. So I studied it, along with the D programming language and <a href=\"http:\/\/dlang.org\/phobos\/index.html\">the Phobos standard library<\/a>.<\/p><\/blockquote>\n<p>vibe.d has always used libevent under the hood by default. This is where Etienne ran into a problem that bothered him.<\/p>\n<blockquote><p>I stumbled on some workflow issues when deploying vibe.d apps to other operating systems which may or may not have the right version of libevent in the package repository. I didn&#8217;t want to package a DLL with my server, or have to go through dependency hell with my software, and I wanted everything to be consistently written in D to reduce the mental complexity of switching programming languages or to debug other issues.<\/p><\/blockquote>\n<p>So he decided to study up on the system APIs across the platforms supported by DMD (Windows, Linux, *BSD and OS X) and create his own event loop library in D. Now he, and anyone using libasync, can issue a single command with <a href=\"https:\/\/code.dlang.org\/getting_started\">DUB<\/a> to compile and execute a web application without needing to worry about external event loop dependencies.<\/p>\n<p>libasync takes advantage of D&#8217;s delegates to provide a very intuitive interface.<\/p>\n<pre class=\"prettyprint lang-d\" data-start-line=\"1\" data-visibility=\"visible\" data-highlight=\"\" data-caption=\"\">void testDNS() {\r\n\tauto dns = new shared AsyncDNS(g_evl);\r\n\tdns.handler((NetworkAddress addr) {\r\n\t\twriteln(\"Resolved to: \", addr.toString(), \", it took: \", g_swDns.peek().usecs, \" usecs\");\r\n\t}).resolveHost(\"127.0.0.1\");\r\n}<\/pre>\n<p>Etienne says of the code snippet above:<\/p>\n<blockquote><p>The D garbage collector will keep the <code>AsyncDNS<\/code> object in <code>dns<\/code> alive for as long as the delegate used in the parameter of <code>dns.handler<\/code> is alive in the heap, which is in this object. The delegate syntax is more simple to declare than Javascript, and it is also type-safe. This DNS resolver will work on any platform thrown at it, thanks to D&#8217;s compile-time <a href=\"http:\/\/dlang.org\/spec\/version.html#VersionCondition\">version conditions<\/a>.<\/p><\/blockquote>\n<p>libasync makes use of the asynchronous I\/O facilities available on each supported platform and provides a number of event-handlers out of the box.<\/p>\n<blockquote><p>Cross-platform event handlers have been defined for DNS resolution, UDP Messages, (Buffered\/Unbuffered) TCP Connections, TCP Listeners, File Operations, Thread-local (Notifiers) and Cross-thread Signals, Timers and File Watchers. The intrinsics involve EPoll for Linux, KQueue for OS X and BSD, and overlapped I\/O for Windows. With all of these features thoroughly tested through a vibe.d driver, libasync has become a very fast and reliable library which I use in all of my projects.\u00a0My benchmarks show it as being a little slower than the libevent driver in vibe.d, though its self-explanatory code base makes it seamless to understand, maintain, and deploy.<\/p><\/blockquote>\n<p>A libasync driver has been added to vibe.d and work is going on to improve the library&#8217;s performance.<\/p>\n<blockquote><p>The stability of the underlying OS features makes for very little need for changes, although there is a big improvement involving <a href=\"https:\/\/en.wikipedia.org\/wiki\/Proactor_pattern\">the proactor pattern<\/a> in the works for <a href=\"https:\/\/github.com\/Calrama\/libasync\/tree\/develop\">libasync<\/a> and <a href=\"https:\/\/github.com\/vibe-d\/eventcore\/commit\/95ccc347d587610e1ba7545ce4c01eedf241febc\">a new architecture for vibe.d<\/a>. Together, those two developments are likely to increase the library&#8217;s performance significantly.<\/p><\/blockquote>\n<p>If you find yourself needing an event loop in D and want to give libasync a spin, you can visit <a href=\"https:\/\/code.dlang.org\/packages\/libasync\">the library&#8217;s page at the DUB repository<\/a> for information on how to add it as a dependency to your own DUB-managed projects. libasync, in turn, has only one dependency itself, another library maintained by Etienne that provides a set of allocators and allocator-friendly containers called <a href=\"https:\/\/github.com\/etcimon\/memutils\">memutils<\/a>.<\/p>\n<p>It wasn&#8217;t so long ago that anyone using D who wanted something like libasync or memutils would need to either roll their own or bind to a C library.\u00a0The ever-expanding list of libraries in <a href=\"https:\/\/code.dlang.org\/\">the DUB repository<\/a>, created and made available by members of the D community\u00a0like Etienne, make it much easier to jump into D today than ever before.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>libasync is a cross-platform event loop library written completely in D. \u00a0It was created, and continues to be maintained, by Etienne Cimon, who started it as a native driver for vibe.d, a modular asynchronous I\/O framework most often used for web app development in D. In 2014 or so, I was looking for a framework [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[13,22],"tags":[],"_links":{"self":[{"href":"https:\/\/dlang.org\/blog\/wp-json\/wp\/v2\/posts\/459"}],"collection":[{"href":"https:\/\/dlang.org\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/dlang.org\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/dlang.org\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/dlang.org\/blog\/wp-json\/wp\/v2\/comments?post=459"}],"version-history":[{"count":7,"href":"https:\/\/dlang.org\/blog\/wp-json\/wp\/v2\/posts\/459\/revisions"}],"predecessor-version":[{"id":466,"href":"https:\/\/dlang.org\/blog\/wp-json\/wp\/v2\/posts\/459\/revisions\/466"}],"wp:attachment":[{"href":"https:\/\/dlang.org\/blog\/wp-json\/wp\/v2\/media?parent=459"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/dlang.org\/blog\/wp-json\/wp\/v2\/categories?post=459"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/dlang.org\/blog\/wp-json\/wp\/v2\/tags?post=459"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}