<div dir="ltr">Hello,<div><br></div><div>While profiling my app I came across a significant difference in the performance between base64:decode/1 and base64:mime_decode/1. The implementations are very similar, but based on my experimentation, I think the impl of mime_decode is preventing the Erlang compiler from enabling some tail recursion optimizations.</div><div><br></div><div>Unfortunately, I don't have deep enough knowledge of the compiler to confirm this directly, but I did make some modifications and was able to achieve some better results.</div><div><br></div><div>Here are my findings:</div><div><br></div><div><div><div><font face="monospace, monospace">27> base64_timing:run().</font></div><div><font face="monospace, monospace">Function                  |  Time     |  Rel</font></div><div><font face="monospace, monospace">--------------------------+-----------+-----</font></div><div><font face="monospace, monospace">base64:decode/1             287838 us   1.0</font></div><div><font face="monospace, monospace">base64:mime_decode/1        493222 us   1.71</font></div><div><font face="monospace, monospace">base64_edits:mime_decode/1  270877 us   0.94</font></div><div><font face="monospace, monospace">    TESTS ok</font></div><div><font face="monospace, monospace">ok</font></div></div></div><div><br></div><div>Note: The timing test provided in base64_timing.erl creates a 7 MB base64-encoded binary and decodes it using the various functions. It also includes a copy of the unit tests for base64:mime_decode/1 that appear in the Erlang 19.1 release.</div><div><br></div><div>The basic idea of the modifications in base64_edits is a focus on maintaining clean tail recursion throughout the implementation. I found that any expression that would "collapse" the tail (T) by reading it in full (specifically: pattern matching against <<>>) would nearly double the execution time. The decode map is simply copied from base64.erl for convenience.</div><div><br></div><div>With that, my questions --</div><div>1. Is there a flaw in my measurements and/or mime_decode modifications?</div><div>2. Is there interest in me submitting a patch with these findings?</div><div><br></div><div>Thanks!</div><div>Jesse Stimpson</div><div><br></div><div><div><br></div></div><div><font face="monospace, monospace"><br></font></div></div>