<turbo-stream action="append" target="posts_list"><template>    <div class="postbit" id="374652" data-post-id="374652">
  <section>
    <div class="post-wrap">


					<div class="post-header">
		        <div class="user-avatar">
		          <img alt="josevalim" src="https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/josevalim/120/1787_2.png" width="120" height="120" />
		        </div>
					
						<div class="user-details">
		          <div class="user-name">
		            <h3>
                  josevalim
                  </h3>
		          </div>
						
			          <div class="user-title">
									<span>Creator of Elixir</span>
			          </div>
						</div>
					
					</div>

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<aside class="quote no-group" data-username="BartOtten" data-post="41" data-topic="57105">
<div class="title">
<div class="quote-controls"></div>
<img alt="" width="24" height="24" src="https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/bartotten/48/25440_2.png" class="avatar"> BartOtten:</div>
<blockquote>
<p>It might be me, but I fail to see the point why it is unfair (and like to be corrected)</p>
</blockquote>
</aside>
<p>In this case, they are different because in one you send 30k fewer AST nodes to the compiler, so there should be no surprises why one is much faster than the other and it should not be generalized as <code>case</code> being faster than <code>def</code>. For example, you could also have generated 10k <code>defp</code> without the guards and a single <code>def</code> that has a guard and calls it, and the result would be the same as <code>case</code>, but using heads. So the result fully boils down to how you write the code and not the constructs involved, it is not about <code>case</code> in particular being immune to guards.</p>
<p>Another way to put this is that the titles of your benchmarks should not be <code>case</code> vs <code>heads</code>, it is more like <code>1 guard</code> vs <code>10000 guards</code>. <img src="https://forum.elixirforum.com/images/emoji/apple/slight_smile.png?v=15" title=":slight_smile:" class="emoji" alt=":slight_smile:" loading="lazy" width="20" height="20"></p>
<p>EDIT: furthermore, the only case where you can do the “guards optimization” above (unifying all guards into a single one) is when the guards themselves are redundant. This is because you are generating this code:</p>
<pre data-code-wrap="elixir"><code class="lang-elixir">def foo(x = 1) when is_integer(x)
def foo(x = 2) when is_integer(x)
</code></pre>
<p>where you can extract the guards out, but arguably you should get rid of them altogether. They are only adding unnecessary compiler work.</p>
<p>Instead, when you are generating multiple clauses, it is often because the guards themselves are different, such as this:</p>
<pre data-code-wrap="elixir"><code class="lang-elixir">def foo(x) when x === 1
def foo(x) when x === 2
</code></pre>
<p>And then you can’t eliminate them, neither in <code>def</code> nor in <code>case</code>. What could happen is that you can partially extract some guards, such as this:</p>
<pre data-code-wrap="elixir"><code class="lang-elixir">def foo(x, y) when x === 1 and is_integer(y)
def foo(x, y) when x === 2 and is_integer(y)
</code></pre>
<p>Where you could move <code>is_integer(y)</code> out and have the remaining in a <code>case</code> (or a <code>defp</code> as established above). But I can’t see where you would be able to extract <strong>all guards</strong> out, as in your example.</p> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="374652" data-batch-url="/posts/batch_likers">
                        4
                      </span>
                      <!-- <span class="thread-count js-solved-indicator" title="Marked as solution"></span> -->
	                </div>
	                <div class="go-to-post">
	                  <a title="Go to post" alt="Go to post" href="https://forum.elixirforum.com/t/compiling-files-with-many-function-heads-is-very-slow-otp-26-issue/57105/42">Post #41</a>
	                </div>
	            </div>
              <div id="likers-container-374652" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="374652"
                     data-batch-url="/posts/batch_likers">
                  <div class="post-likers"></div>
                </div>
              </div>
	        </div>
			

    </div>

    <div class="triangle-top-right type-standard-post cat-standard-post" title="Post #41"></div>
  </section>
</div>
    <div class="postbit" id="374655" data-post-id="374655">
  <section>
    <div class="post-wrap">


					<div class="post-header">
		        <div class="user-avatar">
		          <img alt="BartOtten" src="https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/BartOtten/120/25440_2.png" width="120" height="120" />
		        </div>
					
						<div class="user-details">
		          <div class="user-name">
		            <h3>
                  BartOtten
                  </h3>
		          </div>
						
						</div>
					
					</div>

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<aside class="quote group-livebook_core_team" data-username="josevalim" data-post="42" data-topic="57105">
<div class="title">
<div class="quote-controls"></div>
<img alt="" width="24" height="24" src="https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/josevalim/48/1787_2.png" class="avatar"> josevalim:</div>
<blockquote>
<p>and it should not be generalized as <code>case</code> being faster than <code>def</code></p>
</blockquote>
</aside>
<p>Thanks for the clarification. Indeed the constructs themselves are not to blame, while my words indicate they do. I was trying to point to the names of the written scenarios rather than the constructs themselves; and failed to do so correctly.</p> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="374655" data-batch-url="/posts/batch_likers">
                        3
                      </span>
                      <!-- <span class="thread-count js-solved-indicator" title="Marked as solution"></span> -->
	                </div>
	                <div class="go-to-post">
	                  <a title="Go to post" alt="Go to post" href="https://forum.elixirforum.com/t/compiling-files-with-many-function-heads-is-very-slow-otp-26-issue/57105/43">Post #42</a>
	                </div>
	            </div>
              <div id="likers-container-374655" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="374655"
                     data-batch-url="/posts/batch_likers">
                  <div class="post-likers"></div>
                </div>
              </div>
	        </div>
			

    </div>

    <div class="triangle-top-right type-standard-post cat-standard-post" title="Post #42"></div>
  </section>
</div>
    <div class="postbit" id="374671" data-post-id="374671">
  <section>
    <div class="post-wrap">


					<div class="post-header">
		        <div class="user-avatar">
		          <img alt="Schultzer" src="https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/Schultzer/120/4339_2.png" width="120" height="120" />
		        </div>
					
						<div class="user-details">
		          <div class="user-name">
		            <h3>
                  Schultzer
                  </h3>
		          </div>
						
						</div>
					
					</div>

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<aside class="quote group-livebook_core_team" data-username="josevalim" data-post="40" data-topic="57105">
<div class="title">
<div class="quote-controls"></div>
<img alt="" width="24" height="24" src="https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/josevalim/48/1787_2.png" class="avatar"> josevalim:</div>
<blockquote>
<pre data-code-wrap="elixir"><code class="lang-elixir">  case_ast =
    for i &lt;- 1..10000 do
      hd(
        quote do
          {x = unquote(i), y = unquote(i)} when is_integer(x) and is_integer(y) -&gt; y
        end
      )
    end

  def foo(x, y) do
    case {x, y} do
      unquote(case_ast)
    end
  end
</code></pre>
</blockquote>
</aside>
<p>Thats a neat example, I did a little test with this vs using guards, especially for unicode parsing. With SQL not only does it have to be valid but only specific sets are allowed, the case function compiles fast and also have good runtime performance roughly 2.5x</p>
<pre data-code-wrap="elixir"><code class="lang-elixir">➜  sql git:(main) ✗ mix sql.bench
Operating System: macOS
CPU Information: Apple M1 Max
Number of Available Cores: 10
Available memory: 64 GB
Elixir 1.20.0-dev
Erlang 28.1
JIT enabled: true

Benchmark suite executing with the following configuration:
warmup: 2 s
time: 5 s
memory time: 2 s
reduction time: 2 s
parallel: 1
inputs: 1..100_000
Estimated total run time: 22 s

Measured function call overhead as: 0 ns
Benchmarking case with input 1..100_000 ...
Benchmarking guard with input 1..100_000 ...
Calculating statistics...
Formatting results...

##### With input 1..100_000 #####
Name           ips        average  deviation         median         99th %
case        15.07 M       66.35 ns ±42238.21%          42 ns          42 ns
guard        6.14 M      162.76 ns ±15740.33%         125 ns         167 ns

Comparison:
case        15.07 M
guard        6.14 M - 2.45x slower +96.41 ns

Memory usage statistics:

Name    Memory usage
case             40 B
guard            40 B - 1.00x memory usage +0 B

**All measurements for memory usage were the same**

Reduction count statistics:

Name Reduction count
case                2
guard               2 - 1.00x reduction count +0

</code></pre>
<p>I think it would be worth it to consider documenting these tricks akind to how we have anti patterns, as it feels like there is lots of ways to shoot yourself in the foot, and these simple neat examples could really shine.</p> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="374671" data-batch-url="/posts/batch_likers">
                        3
                      </span>
                      <!-- <span class="thread-count js-solved-indicator" title="Marked as solution"></span> -->
	                </div>
	                <div class="go-to-post">
	                  <a title="Go to post" alt="Go to post" href="https://forum.elixirforum.com/t/compiling-files-with-many-function-heads-is-very-slow-otp-26-issue/57105/44">Post #43</a>
	                </div>
	            </div>
              <div id="likers-container-374671" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="374671"
                     data-batch-url="/posts/batch_likers">
                  <div class="post-likers"></div>
                </div>
              </div>
	        </div>
			

    </div>

    <div class="triangle-top-right type-standard-post cat-standard-post" title="Post #43"></div>
  </section>
</div>
    <div class="postbit" id="374727" data-post-id="374727">
  <section>
    <div class="post-wrap">


					<div class="post-header">
		        <div class="user-avatar">
		          <img alt="Schultzer" src="https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/Schultzer/120/4339_2.png" width="120" height="120" />
		        </div>
					
						<div class="user-details">
		          <div class="user-name">
		            <h3>
                  Schultzer
                  </h3>
		          </div>
						
						</div>
					
					</div>

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<p>Lol, spoke to soon, if your doing more complex things this approach quickly becomes extremely slow and requires alot of var!.</p> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="374727" data-batch-url="/posts/batch_likers">
                        1
                      </span>
                      <!-- <span class="thread-count js-solved-indicator" title="Marked as solution"></span> -->
	                </div>
	                <div class="go-to-post">
	                  <a title="Go to post" alt="Go to post" href="https://forum.elixirforum.com/t/compiling-files-with-many-function-heads-is-very-slow-otp-26-issue/57105/45">Post #44</a>
	                </div>
	            </div>
              <div id="likers-container-374727" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="374727"
                     data-batch-url="/posts/batch_likers">
                  <div class="post-likers"></div>
                </div>
              </div>
	        </div>
			

    </div>

    <div class="triangle-top-right type-standard-post cat-standard-post" title="Post #44"></div>
  </section>
</div>
    <div class="postbit" id="374728" data-post-id="374728">
  <section>
    <div class="post-wrap">


					<div class="post-header">
		        <div class="user-avatar">
		          <img alt="BartOtten" src="https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/BartOtten/120/25440_2.png" width="120" height="120" />
		        </div>
					
						<div class="user-details">
		          <div class="user-name">
		            <h3>
                  BartOtten
                  </h3>
		          </div>
						
						</div>
					
					</div>

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<p>Do you have an example of the code? Also wondering why you would need a lot of <code>var!</code></p> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="374728" data-batch-url="/posts/batch_likers">
                        0
                      </span>
                      <!-- <span class="thread-count js-solved-indicator" title="Marked as solution"></span> -->
	                </div>
	                <div class="go-to-post">
	                  <a title="Go to post" alt="Go to post" href="https://forum.elixirforum.com/t/compiling-files-with-many-function-heads-is-very-slow-otp-26-issue/57105/46">Post #45</a>
	                </div>
	            </div>
              <div id="likers-container-374728" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="374728"
                     data-batch-url="/posts/batch_likers">
                  <div class="post-likers"></div>
                </div>
              </div>
	        </div>
			

    </div>

    <div class="triangle-top-right type-standard-post cat-standard-post" title="Post #45"></div>
  </section>
</div>
    <div class="postbit" id="374729" data-post-id="374729">
  <section>
    <div class="post-wrap">


					<div class="post-header">
		        <div class="user-avatar">
		          <img alt="Schultzer" src="https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/Schultzer/120/4339_2.png" width="120" height="120" />
		        </div>
					
						<div class="user-details">
		          <div class="user-name">
		            <h3>
                  Schultzer
                  </h3>
		          </div>
						
						</div>
					
					</div>

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<p>I was trying to refactor this, to aviod the guard: <a href="https://github.com/elixir-dbvisor/sql/blob/911a0795d87666cbf77068d3a92b9fe3bdcf0a3e/lib/lexer.ex#L247-L249" class="inline-onebox" rel="noopener nofollow ugc">sql/lib/lexer.ex at 911a0795d87666cbf77068d3a92b9fe3bdcf0a3e · elixir-dbvisor/sql · GitHub</a> so far it’s causing infinite compilations.</p>
<p>The only way I found to make it work was creating a specific function but then all the optimizations was lost <img src="https://forum.elixirforum.com/images/emoji/apple/frowning.png?v=15" title=":frowning:" class="emoji" alt=":frowning:" loading="lazy" width="20" height="20">, it was too good to be true.</p> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="374729" data-batch-url="/posts/batch_likers">
                        0
                      </span>
                      <!-- <span class="thread-count js-solved-indicator" title="Marked as solution"></span> -->
	                </div>
	                <div class="go-to-post">
	                  <a title="Go to post" alt="Go to post" href="https://forum.elixirforum.com/t/compiling-files-with-many-function-heads-is-very-slow-otp-26-issue/57105/47">Post #46</a>
	                </div>
	            </div>
              <div id="likers-container-374729" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="374729"
                     data-batch-url="/posts/batch_likers">
                  <div class="post-likers"></div>
                </div>
              </div>
	        </div>
			

    </div>

    <div class="triangle-top-right type-standard-post cat-standard-post" title="Post #46"></div>
  </section>
</div>
    <div class="postbit" id="374732" data-post-id="374732">
  <section>
    <div class="post-wrap">


					<div class="post-header">
		        <div class="user-avatar">
		          <img alt="BartOtten" src="https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/BartOtten/120/25440_2.png" width="120" height="120" />
		        </div>
					
						<div class="user-details">
		          <div class="user-name">
		            <h3>
                  BartOtten
                  </h3>
		          </div>
						
						</div>
					
					</div>

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<p>The example you quoted from José does include guards. Just as many as the Heads-variant.</p>
<p>The ‘trick’ demonstrated by my Case-variant is the absence of 9.999 x 2 guards, as only a single function head has the 2 guards and all 9.999 case clauses can go without. As José mention, the same effect can be reached by having 1 pub function with guards delegating to 10.000 unguarded defp’s.</p>
<p>That being said: so far I could not reproduce any speed improvement with José’s Anon-variant in combination with Macro’s. Maybe the example is too simple for it to have enough impact to overcome an initial penalty..?</p>
<p><em>To be double clear</em>: Yes, there is pattern matching on a specific integer AND a guards checking if the args are integers; the guards are useless and only added to test performance <em>with guards</em>.</p>
<pre data-code-wrap="elixir"><code class="lang-elixir">defmodule AnonMacro do

  defmacro fast do
    quote bind_quoted: [] do
      for i &lt;- 1..10000 do
        def foo(x = unquote(i), y = unquote(i)) when is_integer(x) and is_integer(y), do: unquote(i)
      end
    end
  end
end

defmodule Anon.A do
  import AnonMacro

  fast()
end


defmodule Anon.B do
  import AnonMacro

  fast()
end

defmodule Anon.C do
  import AnonMacro

  fast()
end
</code></pre>
<p><strong>The result</strong></p>
<pre data-code-wrap="elixir"><code class="lang-elixir">test3.ex compiled in 9551.563 ms (guard heavy, missing optimization technique)
  -&gt; Modules: [Heads.A, Heads.B, Heads.C]

test1.ex compiled in 10683.215 ms (guard heavy, wrapped in anon fun)
  -&gt; Modules: [AnonMacro, Anon.A, Anon.B, Anon.C]
</code></pre>
<p><strong>The Schultzer-case</strong><br>
I still wonder why you would need more <code>var!</code> <img src="https://forum.elixirforum.com/images/emoji/apple/slight_smile.png?v=15" title=":slight_smile:" class="emoji" alt=":slight_smile:" loading="lazy" width="20" height="20"> Can you give us an example of a rewritten block?</p> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="374732" data-batch-url="/posts/batch_likers">
                        1
                      </span>
                      <!-- <span class="thread-count js-solved-indicator" title="Marked as solution"></span> -->
	                </div>
	                <div class="go-to-post">
	                  <a title="Go to post" alt="Go to post" href="https://forum.elixirforum.com/t/compiling-files-with-many-function-heads-is-very-slow-otp-26-issue/57105/48">Post #47</a>
	                </div>
	            </div>
              <div id="likers-container-374732" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="374732"
                     data-batch-url="/posts/batch_likers">
                  <div class="post-likers"></div>
                </div>
              </div>
	        </div>
			

    </div>

    <div class="triangle-top-right type-standard-post cat-standard-post" title="Post #47"></div>
  </section>
</div>
    <div class="postbit" id="374735" data-post-id="374735">
  <section>
    <div class="post-wrap">


					<div class="post-header">
		        <div class="user-avatar">
		          <img alt="Eiji" src="https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/Eiji/120/36743_2.png" width="120" height="120" />
		        </div>
					
						<div class="user-details">
		          <div class="user-name">
		            <h3>
                  Eiji
                  </h3>
		          </div>
						
						</div>
					
					</div>

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<p>I’ve done a lot of work in <code>metaprogramming</code> and also decided to rewrite <code>def</code>, so it uses <code>case</code> inside. Maybe you can take a look at my code for inspiration:</p><aside class="onebox allowlistedgeneric" data-onebox-src="https://gitlab.com/ramurix-software/enumex/-/tree/master/lib/enumex/generator/static/components?ref_type=heads">
  <header class="source">
      <img src="https://gitlab.com/assets/favicon-72a2cad5025aa931d6ea56c3201d1f18e68a8cd39788c7c80d5b2b82aa5143ef.png" class="site-icon" alt="" width="32" height="32">

      <a href="https://gitlab.com/ramurix-software/enumex/-/tree/master/lib/enumex/generator/static/components?ref_type=heads" target="_blank" rel="noopener nofollow ugc">GitLab</a>
  </header>

  <article class="onebox-body">
    <img width="64" height="64" src="https://gitlab.com/assets/twitter_card-570ddb06edf56a2312253c5872489847a0f385112ddbcd71ccfa1570febab5d2.jpg" class="thumbnail onebox-avatar" alt="">

<h3><a href="https://gitlab.com/ramurix-software/enumex/-/tree/master/lib/enumex/generator/static/components?ref_type=heads" target="_blank" rel="noopener nofollow ugc">lib/enumex/generator/static/components · master · Ramurix Software / enumex ·...</a></h3>

  <p>A comprehensive library for working with both static and dynamic enum structures in Elixir.</p>


  </article>

  <div class="onebox-metadata">
    
    
  </div>

  <div style="clear: both"></div>
</aside>
<p>
usage:</p><aside class="onebox allowlistedgeneric" data-onebox-src="https://gitlab.com/ramurix-software/enumex/-/tree/master/lib/enumex/static/components?ref_type=heads">
  <header class="source">
      <img src="https://gitlab.com/assets/favicon-72a2cad5025aa931d6ea56c3201d1f18e68a8cd39788c7c80d5b2b82aa5143ef.png" class="site-icon" alt="" width="32" height="32">

      <a href="https://gitlab.com/ramurix-software/enumex/-/tree/master/lib/enumex/static/components?ref_type=heads" target="_blank" rel="noopener nofollow ugc">GitLab</a>
  </header>

  <article class="onebox-body">
    <img width="64" height="64" src="https://gitlab.com/assets/twitter_card-570ddb06edf56a2312253c5872489847a0f385112ddbcd71ccfa1570febab5d2.jpg" class="thumbnail onebox-avatar" alt="">

<h3><a href="https://gitlab.com/ramurix-software/enumex/-/tree/master/lib/enumex/static/components?ref_type=heads" target="_blank" rel="noopener nofollow ugc">lib/enumex/static/components · master · Ramurix Software / enumex · GitLab</a></h3>

  <p>A comprehensive library for working with both static and dynamic enum structures in Elixir.</p>


  </article>

  <div class="onebox-metadata">
    
    
  </div>

  <div style="clear: both"></div>
</aside>

<p>Let me know if you need some context on it. In general I have some <code>DSL</code> and to not add a hidden public functions I decided to move everything that matches this pattern to a <code>Enumex.Generator.*</code> helper modules.</p>
<p>The number of clauses depends on the number of enums defined in each module, the number of its possible values and the type of function. For example if you want to generate all variants of sort (for id, index and struct) then you have much more clauses. Think that someone wants to write enum with all <code>HTTP</code> status codes or so. This is why I quickly got into thousands of clauses and need to rewrite it into <code>case</code>.</p>
<p>Hope it would help as a good examples for further discussion. <img src="https://forum.elixirforum.com/images/emoji/apple/heart.png?v=15" title=":heart:" class="emoji" alt=":heart:" loading="lazy" width="20" height="20"></p> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="374735" data-batch-url="/posts/batch_likers">
                        2
                      </span>
                      <!-- <span class="thread-count js-solved-indicator" title="Marked as solution"></span> -->
	                </div>
	                <div class="go-to-post">
	                  <a title="Go to post" alt="Go to post" href="https://forum.elixirforum.com/t/compiling-files-with-many-function-heads-is-very-slow-otp-26-issue/57105/49">Post #48</a>
	                </div>
	            </div>
              <div id="likers-container-374735" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="374735"
                     data-batch-url="/posts/batch_likers">
                  <div class="post-likers"></div>
                </div>
              </div>
	        </div>
			

    </div>

    <div class="triangle-top-right type-standard-post cat-standard-post" title="Post #48"></div>
  </section>
</div>
    <div class="postbit" id="374736" data-post-id="374736">
  <section>
    <div class="post-wrap">


					<div class="post-header">
		        <div class="user-avatar">
		          <img alt="Schultzer" src="https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/Schultzer/120/4339_2.png" width="120" height="120" />
		        </div>
					
						<div class="user-details">
		          <div class="user-name">
		            <h3>
                  Schultzer
                  </h3>
		          </div>
						
						</div>
					
					</div>

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<p>This is the cleanest I could figure out, but no gains only slower compilation and runtime.</p>
<pre data-code-wrap="elixir"><code class="lang-elixir">  case_ast =
    for c &lt;- Unicode.Set.to_pattern!("[[:Lu:], [:Ll:], [:Lt:], [:Lm:], [:Lo:], [:Nl:], [:Mn:], [:Mc:], [:Nd:], [:Pc:], [:Cf:]]") do
      hd(
        quote do
          &lt;&lt;unquote(c), rest::binary&gt;&gt; -&gt; ident_mid(rest, [unquote(c)|var!(data)], var!(line), var!(column), var!(context), var!(length)+1, var!(acc))
        end
      )
    end ++ quote do
      rest -&gt; ident_end(rest, var!(data), var!(line), var!(column), var!(context), var!(length), var!(acc))
    end
  defp ident_mid(rest, data, line, column, context, length, acc) do
    case rest do
      unquote(case_ast)
    end
  end
</code></pre> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="374736" data-batch-url="/posts/batch_likers">
                        0
                      </span>
                      <!-- <span class="thread-count js-solved-indicator" title="Marked as solution"></span> -->
	                </div>
	                <div class="go-to-post">
	                  <a title="Go to post" alt="Go to post" href="https://forum.elixirforum.com/t/compiling-files-with-many-function-heads-is-very-slow-otp-26-issue/57105/50">Post #49</a>
	                </div>
	            </div>
              <div id="likers-container-374736" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="374736"
                     data-batch-url="/posts/batch_likers">
                  <div class="post-likers"></div>
                </div>
              </div>
	        </div>
			

    </div>

    <div class="triangle-top-right type-standard-post cat-standard-post" title="Post #49"></div>
  </section>
</div>
    <div class="postbit" id="374737" data-post-id="374737">
  <section>
    <div class="post-wrap">


					<div class="post-header">
		        <div class="user-avatar">
		          <img alt="Schultzer" src="https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/Schultzer/120/4339_2.png" width="120" height="120" />
		        </div>
					
						<div class="user-details">
		          <div class="user-name">
		            <h3>
                  Schultzer
                  </h3>
		          </div>
						
						</div>
					
					</div>

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<p>Thanks, took a quick look, does not look as complex as what I’m doing, getting good speed while working on binaries is hard especially when you want to parse unicode, but it just really weird how the compiler could figure out to create a jump table from:</p>
<pre data-code-wrap="elixir"><code class="lang-elixir">defmodule Test do
  require Unicode.Set
  case_ast =
    for c &lt;- Unicode.Set.to_pattern!("[[:Lu:], [:Ll:], [:Lt:], [:Lm:], [:Lo:], [:Nl:], [:Mn:], [:Mc:], [:Nd:], [:Pc:], [:Cf:]]") do
      hd(
        quote do
          &lt;&lt;unquote(c), _::binary&gt;&gt; -&gt; true
        end
      )
    end ++ quote do
      _ -&gt; false
    end

  def case_fn(binary) do
    case binary do
      unquote(case_ast)
    end
  end

  def guard_fn(&lt;&lt;b::utf8, _::binary&gt;&gt;) when Unicode.Set.match?(b, "[[:Lu:], [:Ll:], [:Lt:], [:Lm:], [:Lo:], [:Nl:], [:Mn:], [:Mc:], [:Nd:], [:Pc:], [:Cf:]]"), do: true
  def guard_fn(_binary), do: false
end
</code></pre>
<p>But as soon as you throw in a function then it just capitulate.</p> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="374737" data-batch-url="/posts/batch_likers">
                        1
                      </span>
                      <!-- <span class="thread-count js-solved-indicator" title="Marked as solution"></span> -->
	                </div>
	                <div class="go-to-post">
	                  <a title="Go to post" alt="Go to post" href="https://forum.elixirforum.com/t/compiling-files-with-many-function-heads-is-very-slow-otp-26-issue/57105/51">Post #50</a>
	                </div>
	            </div>
              <div id="likers-container-374737" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="374737"
                     data-batch-url="/posts/batch_likers">
                  <div class="post-likers"></div>
                </div>
              </div>
	        </div>
			

    </div>

    <div class="triangle-top-right type-standard-post cat-standard-post" title="Post #50"></div>
  </section>
</div>
</template></turbo-stream><turbo-stream action="replace" target="load-more-container"><template><div id="load-more-container" class="load-more-container">
    <a class="load-more-button" data-turbo-stream="true" href="/topics/57105/load_more?page=6">Load more posts (19 remaining)</a>
</div></template></turbo-stream>