summaryrefslogtreecommitdiffstats
path: root/articles/erlang-validate-utf8/index.html
diff options
context:
space:
mode:
Diffstat (limited to 'articles/erlang-validate-utf8/index.html')
-rw-r--r--articles/erlang-validate-utf8/index.html18
1 files changed, 11 insertions, 7 deletions
diff --git a/articles/erlang-validate-utf8/index.html b/articles/erlang-validate-utf8/index.html
index 9fa1529d..4569972f 100644
--- a/articles/erlang-validate-utf8/index.html
+++ b/articles/erlang-validate-utf8/index.html
@@ -73,7 +73,7 @@
change in the way the code validates UTF-8 data</a> (required for text and close frames as per the spec).</p>
<p>When looking into why the permessage-deflate tests in autobahntestsuite were taking such a long time, I found that autobahn is using an adaptation of the algorithm named <a href="http://bjoern.hoehrmann.de/utf-8/decoder/dfa/">Flexible
and Economical UTF-8 Decoder</a>. This is the C99 implementation:</p>
-<div class="listingblock"><div class="content"><!-- Generator: GNU source-highlight 3.1.8
+<div class="listingblock"><div class="content"><!-- Generator: GNU source-highlight 3.1.9
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
@@ -113,7 +113,7 @@ uint32_t inline
<font color="#FF0000">}</font></tt></pre>
</div></div>
<p>And this is the Erlang implementation I came up with:</p>
-<div class="listingblock"><div class="content"><!-- Generator: GNU source-highlight 3.1.8
+<div class="listingblock"><div class="content"><!-- Generator: GNU source-highlight 3.1.9
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
@@ -147,7 +147,7 @@ http://www.gnu.org/software/src-highlite -->
</div></div>
<p>Does it look similar to you? So how did we get there?</p>
<p>I started with a naive implementation of the original. First, we don&apos;t need the codepoint calculated and extracted for our validation function. We just want to know the data is valid, so we only need to calculate the next state. Then, the only thing we needed to be careful about was that tuples are 1-based, and that we need to stop processing the binary when we get the state 1 or when the binary is empty.</p>
-<div class="listingblock"><div class="content"><!-- Generator: GNU source-highlight 3.1.8
+<div class="listingblock"><div class="content"><!-- Generator: GNU source-highlight 3.1.9
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
@@ -161,7 +161,7 @@ http://www.gnu.org/software/src-highlite -->
<p>It was time to step into crazy land.</p>
<p>Erlang is very good at pattern matching, even more so than doing some arithmetic coupled by fetching elements from a tuple. So I decided I was going to write all possible clauses for all combinations of <code>C</code> and <code>State</code>. And by write I mean generate.</p>
<p>So I opened my Erlang shell, defined the variable <code>D</code> to be the tuple <code>?UTF8D</code> with its 400 elements, and then ran the following expression (after a bit of trial and error):</p>
-<div class="listingblock"><div class="content"><!-- Generator: GNU source-highlight 3.1.8
+<div class="listingblock"><div class="content"><!-- Generator: GNU source-highlight 3.1.9
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
@@ -175,7 +175,7 @@ http://www.gnu.org/software/src-highlite -->
<p>There was a little more work to be done on this generated code that I did using regular expressions. We need to recurse when the resulting state is not 1. We also need to stop when the binary is empty, making it the 2305th clause.</p>
<p>Still, 2305 is a lot. But hey, the code did work, and faster than the previous implementation too! But hey, perhaps I could find a way to reduce its size.</p>
<p>Removing all the clauses that return 1 and putting a catch-all clause at the end instead reduced the number to about 500, and showed that many clauses were similar:</p>
-<div class="listingblock"><div class="content"><!-- Generator: GNU source-highlight 3.1.8
+<div class="listingblock"><div class="content"><!-- Generator: GNU source-highlight 3.1.9
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
@@ -189,7 +189,7 @@ http://www.gnu.org/software/src-highlite -->
<b><font color="#000000">validate_utf8</font></b>(<font color="#990000">&lt;&lt;</font> <font color="#993399">7</font>, <font color="#009900">Rest</font><font color="#990000">/</font><font color="#FF6600">bits</font> <font color="#990000">&gt;&gt;</font>, <font color="#993399">0</font>) <font color="#990000">-&gt;</font> <b><font color="#000000">validate_utf8</font></b>(<font color="#009900">Rest</font>, <font color="#993399">0</font>);</tt></pre>
</div></div>
<p>But also:</p>
-<div class="listingblock"><div class="content"><!-- Generator: GNU source-highlight 3.1.8
+<div class="listingblock"><div class="content"><!-- Generator: GNU source-highlight 3.1.9
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
@@ -207,7 +207,7 @@ http://www.gnu.org/software/src-highlite -->
<p>Patterns, my favorites!</p>
<p>A little more time was spent to edit the 500 or so clauses into smaller equivalents, testing that performance was not impacted, and comitting the result.</p>
<p>The patterns above can be found here in the resulting function:</p>
-<div class="listingblock"><div class="content"><!-- Generator: GNU source-highlight 3.1.8
+<div class="listingblock"><div class="content"><!-- Generator: GNU source-highlight 3.1.9
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
@@ -233,6 +233,10 @@ http://www.gnu.org/software/src-highlite -->
+ <li><a href="https://ninenines.eu/articles/cowboy-2.7.0/">Cowboy 2.7</a></li>
+
+
+
<li><a href="https://ninenines.eu/articles/gun-2.0.0-pre.1/">Gun 2.0 pre-release 1</a></li>