From 8459bebceb9533948193774371cbd9fd571b78ea Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lo=C3=AFc=20Hoguin?= Date: Wed, 16 Oct 2019 09:48:31 +0200 Subject: Cowboy 2.7.0 --- articles/erlang-validate-utf8/index.html | 18 +++++++++++------- 1 file changed, 11 insertions(+), 7 deletions(-) (limited to 'articles/erlang-validate-utf8') 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 (required for text and close frames as per the spec).

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 Flexible and Economical UTF-8 Decoder. This is the C99 implementation:

-
@@ -113,7 +113,7 @@ uint32_t inline }

And this is the Erlang implementation I came up with:

-
@@ -147,7 +147,7 @@ http://www.gnu.org/software/src-highlite -->

Does it look similar to you? So how did we get there?

I started with a naive implementation of the original. First, we don'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.

-
@@ -161,7 +161,7 @@ http://www.gnu.org/software/src-highlite -->

It was time to step into crazy land.

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 C and State. And by write I mean generate.

So I opened my Erlang shell, defined the variable D to be the tuple ?UTF8D with its 400 elements, and then ran the following expression (after a bit of trial and error):

-
@@ -175,7 +175,7 @@ http://www.gnu.org/software/src-highlite -->

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.

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.

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:

-
@@ -189,7 +189,7 @@ http://www.gnu.org/software/src-highlite --> validate_utf8(<< 7, Rest/bits >>, 0) -> validate_utf8(Rest, 0);

But also:

-
@@ -207,7 +207,7 @@ http://www.gnu.org/software/src-highlite -->

Patterns, my favorites!

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.

The patterns above can be found here in the resulting function:

-
@@ -233,6 +233,10 @@ http://www.gnu.org/software/src-highlite --> +
  • Cowboy 2.7
  • + + +
  • Gun 2.0 pre-release 1
  • -- cgit v1.2.3