<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Alex Woods</title>
    <description>The latest articles on DEV Community by Alex Woods (@alexhwoods).</description>
    <link>https://dev.to/alexhwoods</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F208184%2F649ffba2-c9c7-4ca8-a010-f20359d5fb44.jpeg</url>
      <title>DEV Community: Alex Woods</title>
      <link>https://dev.to/alexhwoods</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/alexhwoods"/>
    <language>en</language>
    <item>
      <title>Kotlin Algorithm Challenge No. 3</title>
      <dc:creator>Alex Woods</dc:creator>
      <pubDate>Wed, 22 Jan 2020 18:13:40 +0000</pubDate>
      <link>https://dev.to/alexhwoods/kotlin-algorithm-challenge-no-3-alj</link>
      <guid>https://dev.to/alexhwoods/kotlin-algorithm-challenge-no-3-alj</guid>
      <description>&lt;p&gt;Originally posted &lt;a href="http://bit.ly/37jB0v8"&gt;here&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The following problem is the &lt;a href="https://leetcode.com/problems/reverse-integer/"&gt;Reverse Integer&lt;/a&gt; problem on LeetCode.&lt;/p&gt;

&lt;h1&gt;
  
  
  Problem
&lt;/h1&gt;

&lt;p&gt;Given a 32-bit signed integer, reverse its digits.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight kotlin"&gt;&lt;code&gt;&lt;span class="k"&gt;fun&lt;/span&gt; &lt;span class="nf"&gt;reverse&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;x&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;Int&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt; &lt;span class="nc"&gt;Int&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="c1"&gt;// TODO&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;




&lt;h1&gt;
  
  
  Solution
&lt;/h1&gt;

&lt;p&gt;This problem is not too difficult (i.e. this is absolutely a fair interview question!). The approach we're going to take is &lt;strong&gt;breaking down the number by multiples of 10, and using the broken off piece to construct the reversed number&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;There might be a temptation to convert the value to a string, reverse it, then convert it back again. Don’t do that, it's completely against the spirit of the problem. I also trust math more than I trust type conversion, if code like this were to ever make it into production.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight kotlin"&gt;&lt;code&gt;&lt;span class="k"&gt;fun&lt;/span&gt; &lt;span class="nf"&gt;reverse&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;x&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;Int&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt; &lt;span class="nc"&gt;Int&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="py"&gt;int&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;x&lt;/span&gt;
    &lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="py"&gt;sum&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;

    &lt;span class="k"&gt;while&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;int&lt;/span&gt; &lt;span class="p"&gt;!=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="kd"&gt;val&lt;/span&gt; &lt;span class="py"&gt;digit&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;int&lt;/span&gt; &lt;span class="p"&gt;%&lt;/span&gt; &lt;span class="mi"&gt;10&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

        &lt;span class="kd"&gt;val&lt;/span&gt; &lt;span class="py"&gt;newSum&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;10&lt;/span&gt;&lt;span class="p"&gt;*&lt;/span&gt;&lt;span class="n"&gt;sum&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;toLong&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;+&lt;/span&gt; &lt;span class="n"&gt;digit&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;toLong&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
        &lt;span class="nf"&gt;check&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;newSum&lt;/span&gt; &lt;span class="p"&gt;&amp;gt;&lt;/span&gt; &lt;span class="nc"&gt;Integer&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;MIN_VALUE&lt;/span&gt; &lt;span class="p"&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class="n"&gt;newSum&lt;/span&gt; &lt;span class="p"&gt;&amp;lt;&lt;/span&gt; &lt;span class="nc"&gt;Integer&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;MAX_VALUE&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="s"&gt;"Integer overflowed, input too large"&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;

        &lt;span class="n"&gt;sum&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;newSum&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;toInt&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
        &lt;span class="n"&gt;int&lt;/span&gt; &lt;span class="p"&gt;/=&lt;/span&gt; &lt;span class="mi"&gt;10&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;

    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;sum&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;





&lt;p&gt;You can see the progression of our &lt;code&gt;sum&lt;/code&gt; variable in the following example.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight kotlin"&gt;&lt;code&gt;&lt;span class="mi"&gt;1234&lt;/span&gt;

&lt;span class="mi"&gt;10&lt;/span&gt;&lt;span class="p"&gt;*&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt; &lt;span class="p"&gt;+&lt;/span&gt; &lt;span class="mi"&gt;4&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;4&lt;/span&gt;
&lt;span class="mi"&gt;10&lt;/span&gt;&lt;span class="p"&gt;*&lt;/span&gt;&lt;span class="mi"&gt;4&lt;/span&gt; &lt;span class="p"&gt;+&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;43&lt;/span&gt;
&lt;span class="mi"&gt;10&lt;/span&gt;&lt;span class="p"&gt;*&lt;/span&gt;&lt;span class="mi"&gt;43&lt;/span&gt; &lt;span class="p"&gt;+&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;432&lt;/span&gt;
&lt;span class="mi"&gt;10&lt;/span&gt;&lt;span class="p"&gt;*&lt;/span&gt;&lt;span class="mi"&gt;432&lt;/span&gt; &lt;span class="p"&gt;+&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;4321&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;




&lt;h1&gt;
  
  
  Writing Good Kotlin Code
&lt;/h1&gt;

&lt;p&gt;But how does idiomatic Kotlin code fit into this problem?&lt;br&gt;
&lt;br&gt;&lt;/p&gt;
&lt;h2&gt;
  
  
  Pure Functions
&lt;/h2&gt;

&lt;p&gt;A pure function is a function that doesn't mutate it's input, and it has no side effects. Because of these two things, when a pure function is run multiple times with the same input, it gives the same result.&lt;/p&gt;

&lt;p&gt;It's not obvious in the above code, but Kotlin is making me go out of my way to write a pure function. Try the following:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight kotlin"&gt;&lt;code&gt;&lt;span class="k"&gt;fun&lt;/span&gt; &lt;span class="nf"&gt;foo&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;x&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;Int&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;x&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;4&lt;/span&gt;  &lt;span class="c1"&gt;// an error pops up: "Val cannot be reassigned"&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;





&lt;p&gt;In a similar vein, collections are immutable by default.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight kotlin"&gt;&lt;code&gt;&lt;span class="k"&gt;fun&lt;/span&gt; &lt;span class="nf"&gt;foo&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;list&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;List&lt;/span&gt;&lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;Int&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;list&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;add&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;4&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="c1"&gt;// nope! the compiler won't allow it!&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;





&lt;p&gt;The above isn't allowed, because the &lt;code&gt;List&lt;/code&gt; interface extends the &lt;code&gt;Collection&lt;/code&gt; interface, &lt;em&gt;which doesn't contain an &lt;code&gt;add&lt;/code&gt; method&lt;/em&gt;. If you want an add method, you have to declare &lt;code&gt;MutableList&lt;/code&gt;, which extends &lt;code&gt;MutableCollection&lt;/code&gt;. And putting &lt;code&gt;MutableList&lt;/code&gt; as a type of a function input parameter should set off a red flag in your head.&lt;br&gt;
&lt;br&gt;&lt;/p&gt;
&lt;h2&gt;
  
  
  Check
&lt;/h2&gt;

&lt;p&gt;The &lt;code&gt;check&lt;/code&gt; function checks some condition. If it is &lt;code&gt;false&lt;/code&gt;, it throws an &lt;code&gt;IllegalStateException&lt;/code&gt;. We can use it to make sure people are only calling our function at the right time.&lt;/p&gt;

&lt;p&gt;Similar to &lt;code&gt;check&lt;/code&gt; is the function &lt;code&gt;require&lt;/code&gt;. We pass it a condition, and if &lt;code&gt;false&lt;/code&gt;, it throws an &lt;code&gt;IllegalArgumentException&lt;/code&gt;. So it is used for validating input. e.g.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight kotlin"&gt;&lt;code&gt;&lt;span class="k"&gt;fun&lt;/span&gt; &lt;span class="nf"&gt;poundsToKilograms&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;weightInPounds&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;Double&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nf"&gt;require&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;weightInPounds&lt;/span&gt; &lt;span class="p"&gt;&amp;gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="s"&gt;"There's no such thing as a negative weight."&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;

    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;weightInPounds&lt;/span&gt; &lt;span class="p"&gt;/&lt;/span&gt; &lt;span class="mf"&gt;2.2&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;




&lt;h2&gt;
  
  
  Augmented Assignment Operators
&lt;/h2&gt;

&lt;p&gt;The slightly funny looking operator &lt;code&gt;/=&lt;/code&gt; means divide and then assign. That line is equivalent to the following.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight kotlin"&gt;&lt;code&gt;&lt;span class="n"&gt;int&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;int&lt;/span&gt; &lt;span class="p"&gt;/&lt;/span&gt; &lt;span class="mi"&gt;10&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;





&lt;h1&gt;
  
  
  Summary
&lt;/h1&gt;

&lt;p&gt;Thank you for reading the most recent Kotlin Algorithm Challenge! If you want to learn more about Kotlin, you can see all the articles I’ve written about it &lt;a href="https://www.alexhwoods.com/kotlin/"&gt;here&lt;/a&gt;.&lt;/p&gt;

</description>
      <category>kotlin</category>
      <category>algorithms</category>
    </item>
    <item>
      <title>How the Node.js Event Loop Polls</title>
      <dc:creator>Alex Woods</dc:creator>
      <pubDate>Thu, 22 Aug 2019 00:34:33 +0000</pubDate>
      <link>https://dev.to/alexhwoods/how-the-node-js-event-loop-polls-49lk</link>
      <guid>https://dev.to/alexhwoods/how-the-node-js-event-loop-polls-49lk</guid>
      <description>&lt;p&gt;Say we are a Node.js client, and we've made a request to some server. What happens as we're waiting for that response? How does the event loop know when to put the associated callback on the event queue?&lt;/p&gt;

&lt;p&gt;Say we are a Node.js client, and we've made a request to some server.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;What happens as we're waiting for that response?&lt;/li&gt;
&lt;li&gt;How does the event loop know when to put the associated callback on the event queue?&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Demultiplexing and the event loop
&lt;/h2&gt;

&lt;p&gt;Node.js's event loop is implemented in a library called &lt;a href="http://docs.libuv.org/en/v1.x/"&gt;libuv&lt;/a&gt;, which is also used by Julia and Python. We're going to dive into its internals.&lt;/p&gt;

&lt;p&gt;The following is 1 iteration of the event loop:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight c"&gt;&lt;code&gt;&lt;span class="n"&gt;uv__update_time&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;loop&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="n"&gt;uv__run_timers&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;loop&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="n"&gt;uv__run_pending&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;loop&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="n"&gt;uv__run_idle&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;loop&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="n"&gt;uv__run_prepare&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;loop&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="c1"&gt;// our method of interest&lt;/span&gt;
&lt;span class="o"&gt;+------------------------------+&lt;/span&gt;
&lt;span class="o"&gt;|&lt;/span&gt; &lt;span class="n"&gt;uv__io_poll&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;loop&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;timeout&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;  &lt;span class="o"&gt;|&lt;/span&gt;
&lt;span class="o"&gt;+------------------------------+&lt;/span&gt;

&lt;span class="n"&gt;uv__run_check&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;loop&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="n"&gt;uv__run_closing_handles&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;loop&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;





&lt;p&gt;The method we care about, &lt;code&gt;uv__io_poll&lt;/code&gt;, basically does the following:&lt;/p&gt;

&lt;p&gt;Say the event loop is watching &lt;em&gt;n&lt;/em&gt; open sockets 👀, because we have &lt;em&gt;n&lt;/em&gt; unresolved requests. It does this by maintaining a &lt;strong&gt;watcher queue&lt;/strong&gt;, which is just a list of &lt;em&gt;n&lt;/em&gt; watchers—basically a socket with some metadata.&lt;/p&gt;

&lt;p&gt;Then, the polling mechanism recieves an event. At notification time, it doesn't yet know which open socket this corresponds to.&lt;/p&gt;

&lt;p&gt;All of our watchers (in the watcher queue) are identified by a &lt;strong&gt;file descriptor&lt;/strong&gt;. This is just an integer that acts as an ID for an open I/O resource. This is a common thing in an operating system.&lt;/p&gt;

&lt;p&gt;The event we received contains an id field (named &lt;code&gt;ident&lt;/code&gt;), which is a file descriptor. Once we have the file descriptor, we can get the watcher. This is the step that gives this process the name &lt;em&gt;demultiplexing&lt;/em&gt;.&lt;/p&gt;

&lt;p&gt;Finally, once we have the watcher, we can get the callback to put on the event queue.&lt;/p&gt;

&lt;h2&gt;
  
  
  The polling mechanism?
&lt;/h2&gt;

&lt;p&gt;In the above description, we glossed over something that seems kind of magical—what is the polling mechanism and how does event loop receive an event?&lt;/p&gt;

&lt;p&gt;The short answer is that it uses a system call to be notified of such events. Which one depends on the operating system.&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;OS&lt;/th&gt;
&lt;th&gt;Function&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;FreeBSD (Mac)&lt;/td&gt;
&lt;td&gt;&lt;a href="https://en.wikipedia.org/wiki/Kqueue"&gt;kqueue&lt;/a&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Linux&lt;/td&gt;
&lt;td&gt;&lt;a href="https://en.wikipedia.org/wiki/Epoll"&gt;epoll&lt;/a&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Windows&lt;/td&gt;
&lt;td&gt;&lt;a href="https://docs.microsoft.com/en-us/windows/win32/fileio/i-o-completion-ports"&gt;IOCP&lt;/a&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;Let's take a look at &lt;code&gt;kqueue&lt;/code&gt;, but first let's review what happens when we our computer receives a packet.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;When the kernel gets a packet from the network interface, it decodes the packet and figures out what TCP connection the packet is associated with based on the source IP, source port, destination IP, and destination port. This information is used to look up the struct sock in memory associated with that connection. Assuming the packet is in sequence, the data payload is then copied into the socket’s receive buffer. [3]&lt;br&gt;
&lt;/p&gt;
&lt;/blockquote&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;How kqueue recieves a notification:

                           +--------------------------+
                           |                          |          +-------------+         +-------------+   
                           |                          |          |             |         |             |   
receives packet +---------&amp;gt;+    Network Interface     +---------&amp;gt;+   Socket    +--------&amp;gt;+   kqueue    |   
                           |                          |          |             |         |             |   
                           |                          |          +-------------+         +-------------+   
                           +--------------------------+
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;





&lt;p&gt;After this occurs, the socket (our event-generating entity of interest) traverses the kqueue's list of registered events (called &lt;code&gt;knotes&lt;/code&gt;), and finds the one it belongs to. A filter function decides whether it merits reporting. [2] &lt;code&gt;kqueue&lt;/code&gt; would then report it to the user program.&lt;/p&gt;

&lt;p&gt;Here are some of the events an application might register with &lt;code&gt;kqueue&lt;/code&gt;.&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Event name&lt;/th&gt;
&lt;th&gt;Operation tracked&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;EVFILT_READ&lt;/td&gt;
&lt;td&gt;Descriptor has data to read&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;EVFILT_AIO&lt;/td&gt;
&lt;td&gt;Asynchronous I/O associated with descriptor has completed&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;EVFILT_TIMER&lt;/td&gt;
&lt;td&gt;An event-based timer has expired&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;&lt;code&gt;kqueue&lt;/code&gt; is actually pretty simple. It's just a FreeBSD system call which provides notification to a user program of a kernel event.&lt;/p&gt;

&lt;p&gt;In our case, libuv is the user program.&lt;/p&gt;

&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;This has certainly helped me understand the core of what libuv is. It provides Node with its event loop; it uses callback style API's, and most importantly, it abstracts away the complexity of interfacing with system calls.&lt;/p&gt;

&lt;p&gt;It's "polling" mechanism is not inherently that complex, because the system calls it uses are event-driven. It just has to keep a data structure of the callbacks registered for each event.&lt;/p&gt;

&lt;h2&gt;
  
  
  Sources
&lt;/h2&gt;

&lt;ol&gt;
&lt;li&gt;&lt;a href="https://github.com/libuv/libuv"&gt;Libuv source code&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://www.amazon.com/Design-Implementation-FreeBSD-Operating-System/dp/0201702452"&gt;The Design and Implementation of the FreeBSD Operating System&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://eklitzke.org/how-tcp-sockets-work"&gt;How TCP Sockets Work&lt;/a&gt;&lt;/li&gt;
&lt;/ol&gt;

</description>
      <category>node</category>
      <category>eventloop</category>
    </item>
    <item>
      <title>How similar is the execution of Java and JavaScript?</title>
      <dc:creator>Alex Woods</dc:creator>
      <pubDate>Sun, 14 Apr 2019 00:00:00 +0000</pubDate>
      <link>https://dev.to/alexhwoods/how-similar-is-the-execution-of-java-and-javascript-3548</link>
      <guid>https://dev.to/alexhwoods/how-similar-is-the-execution-of-java-and-javascript-3548</guid>
      <description>&lt;p&gt;In this article, I will compare the execution of Java and JavaScript. I am intentionally avoiding a lot of details about memory, as that is a whole other topic.&lt;/p&gt;

&lt;h1&gt;
  
  
  Execution at the processor level
&lt;/h1&gt;

&lt;p&gt;With each passing clock cycle, the processor executes a single microinstruction. These are strings whose bits drive the control lines of the CPU. We get a series of microinstructions by the translation of an instruction (frequently called machine code). The set of all available instructions for a processor is called the Instruction Set Architecture (ISA), which in a sense is the API of your computer’s processor.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;source code -&amp;gt; instructions (now within processor) -&amp;gt; microinstructions (executed each clock cycle)&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Statements in a program can be taken by a compiler and turned into machine code (instructions). The &lt;a href="https://jvns.ca/blog/2014/09/06/how-to-read-an-executable/"&gt;best example&lt;/a&gt; of this is C. Different processors, however, provide different Instruction Set Architectures. Does this mean we have to write a different compiler for each processor? (Yes, it does.) This is a problem as compiler’s do more and more work for the programmer.&lt;/p&gt;

&lt;p&gt;We can solve this by keeping all the added features in our program executor the same, except for the processor. If we virtualize the processor, and provide an ISA specially designed for the execution of the language in question, we now have a component we can take in and out, as we move from one OS to another (presumably with different processors).&lt;/p&gt;

&lt;h1&gt;
  
  
  Virtualizing the processor
&lt;/h1&gt;

&lt;p&gt;As alluded to above, both the languages in question (Java and JavaScript), provide an abstraction on top of machine code in order manage this complexity. This “abstraction of machine code” is called bytecode. In Java, &lt;a href="https://docs.oracle.com/javase/specs/jvms/se7/html/jvms-6.html"&gt;it is the .class files&lt;/a&gt;, and with JavaScript it varies depending on the engine in question. To see the bytecode used by the V8 engine (the one used in Node.js and Chrome), execute it with the &lt;code&gt;--print-bytecode&lt;/code&gt; flag. &lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;To run a JavaScript program purely with V8 &lt;br&gt;
(excluding Node’s runtime), install V8 and run&lt;br&gt;
&lt;code&gt;v8 hello.js&lt;/code&gt; (where &lt;code&gt;hello.js&lt;/code&gt; is your program) &lt;br&gt; e.g. &lt;code&gt;v8 --print-bytecode hello.js&lt;/code&gt;&lt;br&gt;
(Because seeing the bytecode of Node’s runtime isn’t the best for education purposes).&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;When I say “virtualizing the processor”, what I mean is that we are executing the program in a virtual machine. We have some entity (either the JVM interpreter or the Ignition bytecode executor), that knows how to execute their version of bytecode. What we are using here is called a &lt;a href="https://en.wikipedia.org/wiki/Virtual_machine#Process_virtual_machines"&gt;process virtual machine&lt;/a&gt;. This is in stark contrast to a &lt;a href="https://en.wikipedia.org/wiki/Virtual_machine#System_virtual_machines"&gt;system virtual machine&lt;/a&gt;, which has its own guest operating system (think VMWare). A process virtual machine is much lighter, and its sole purpose is executing a program. To be clear, it does further virtualize other things, such as memory, which means there exists a mapping. For simplicity, the &lt;a href="http://www.ittc.ku.edu/~kulkarni/teaching/EECS768/slides/chapter3.pdf"&gt;mapping is just a direct mapping&lt;/a&gt;. Note that memory is already virtualized on the host OS; the memory blocks given to a process appear continuous, but are not guaranteed to be so. The most complicated thing we are abstracting away is the processor and it’s machine code.&lt;/p&gt;

&lt;p&gt;In the JVM, for example, the interpreter takes a line of bytecode, and knows how to execute it without providing further translation into machine code.&lt;/p&gt;

&lt;p&gt;In V8, the Ignition interpreter acts in a similar way. it takes a statement from the parsed &lt;a href="https://en.wikipedia.org/wiki/Abstract_syntax_tree"&gt;AST&lt;/a&gt;, turns it into bytecode, and executes it by itself.&lt;/p&gt;

&lt;p&gt;Let’s dive deeper into the details of the two.&lt;/p&gt;

&lt;h1&gt;
  
  
  How the JVM Executes Programs
&lt;/h1&gt;

&lt;p&gt;Let’s say we have some Java source code. We use the Java compiler (javac) to turn it into bytecode (.class files). Then we feed it into the JVM.&lt;/p&gt;

&lt;p&gt;The JVM takes it, and after some initialization steps (class loading, bytecode verifying, etc.), a statement of bytecode arrives at the interpreter, which executes it.&lt;/p&gt;

&lt;p&gt;As execution continues, if some piece of code (e.g. a function) has been executed a certain number of times (i.e. it is “hot”), the JVM execution engine contains a JIT optimizing compiler which will turn it into machine code, allowing it to execute faster.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--7_3BsSW---/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://thepracticaldev.s3.amazonaws.com/i/vqpfb5u2n08uw12r4mim.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--7_3BsSW---/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://thepracticaldev.s3.amazonaws.com/i/vqpfb5u2n08uw12r4mim.png" alt="jvm"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Note that the “JIT compiler” in this case goes from bytecode to machine code. &lt;br&gt;
JavaScript’s JIT compiling, in contrast, goes from source code to bytecode.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h1&gt;
  
  
  How the V8 Engine Executes Programs
&lt;/h1&gt;

&lt;p&gt;In contrast, JavaScript is JIT compiled. That means there is no pre-compilation step before it is fed into the execution engine; we feed the engine raw source code.&lt;/p&gt;

&lt;p&gt;Say, we have some JavaScript source code. It is parsed and turned into an AST. That is then fed into Ignition, V8’s interpreter. &lt;a href="https://v8.dev/docs/ignition"&gt;Ignition&lt;/a&gt; turns that into bytecode, and executes it.&lt;/p&gt;

&lt;p&gt;Just like before, if we discover a hot function, we have an optimizing compiler to turn that into machine code. In V8, this entity is called &lt;a href="https://v8.dev/docs/turbofan"&gt;TurboFan&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Going to steal Franziska Hinkelmann’s picture, from her great &lt;a href="https://medium.com/dailyjs/understanding-v8s-bytecode-317d46c94775"&gt;blog post&lt;/a&gt; on the topic.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--qWVjXvMo--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://thepracticaldev.s3.amazonaws.com/i/mal1egmbkxhqs8t4mpbz.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--qWVjXvMo--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://thepracticaldev.s3.amazonaws.com/i/mal1egmbkxhqs8t4mpbz.png" alt="v8"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Note – Above I talked about process virtual machines. V8 is pretty sparse on those kind of internal details, but I did read enough to convince myself that they use a process virtual machine. (See &lt;a href="https://codeburst.io/node-js-v8-internals-an-illustrative-primer-83766e983bf6"&gt;here&lt;/a&gt;, &lt;a href="https://v8.dev/blog/turbofan-jit"&gt;here&lt;/a&gt;, and &lt;a href="https://docs.google.com/presentation/d/1chhN90uB8yPaIhx_h2M3lPyxPgdPmkADqSNAoXYQiVE/edit#slide=id.g1ba7f92079_2_0"&gt;here&lt;/a&gt;).&lt;/p&gt;

&lt;h1&gt;
  
  
  Similarities Between Java and JavaScript Execution
&lt;/h1&gt;

&lt;ul&gt;
&lt;li&gt;Both are run inside process virtual machines. They both have their own “bytecode”, which is just an abstraction of machine code. Those process virtual machines execute their corresponding bytecode.&lt;/li&gt;
&lt;li&gt;They both take the same approach for code that is “hot”. That is, an optimizing compiler turns the bytecode into machine code, using profiling information accumulated throughout execution.&lt;/li&gt;
&lt;/ul&gt;

&lt;h1&gt;
  
  
  Differences Between Java and JavaScript Execution
&lt;/h1&gt;

&lt;p&gt;There is one major difference here—JavaScript is JIT compiled, Java is not. Due to this, there’s no abstraction in between the JavaScript language and it’s execution engine (V8, in this case). That means it’s impossible to use V8 to execute another language.&lt;/p&gt;

&lt;p&gt;The JVM, on the other hand, only knows about &lt;code&gt;.class&lt;/code&gt; file bytecode. This gives space for other language that can compile to .class files. Enter Scala, Groovy, Clojure, and Kotlin. &lt;/p&gt;

&lt;p&gt;It’s ironic because this layer of abstraction only exists because there was a necessity to send bytecode over the network, around the time Java came out (early World Wide Web days, when Java was supported in the browser). Nowadays, this doesn’t matter &lt;em&gt;at all&lt;/em&gt;. These days we send JavaScript source code over the network, and the browser uses an execution engine (like V8) to execute it.&lt;/p&gt;

&lt;p&gt;I find this point fascinating, and I believe this indirection might lead to the JVM sticking around for a very long time, because we can choose to use it even if we don’t want to use Java. It’s appealing for language designers, because over half the work is done for them—all they have to do is design a language spec and a compiler to &lt;code&gt;.class&lt;/code&gt; bytecode.&lt;/p&gt;

&lt;h1&gt;
  
  
  Conclusion
&lt;/h1&gt;

&lt;p&gt;After researching and writing this, my opinion is now that the way that JVM languages and JavaScript are executed is much closer than I originally imagined. Hope ya learned something.&lt;/p&gt;

&lt;p&gt;Originally posted &lt;a href="https://www.alexhwoods.com/how-similar-is-the-execution-of-java-and-javascript"&gt;here&lt;/a&gt;.&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>java</category>
      <category>compilers</category>
    </item>
  </channel>
</rss>
