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


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

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<p>While Erlang VM is pretty good with io in general, stdout is not very performant.  The logic here is that when you need stdout you’re likely to be in anger,and even more likely, needing to capture stdout <em>on another node</em> across the distributed cluster.  Also, io events should be atomic.  We don’t want a situation where one process is writing some content to io and another process scoots in, blats something out, rudely interrupting the content you cared about.</p>
<p>Default Std io in Erlang goes through something called the group leader which is a single highly bottlenecked process that has lots of capabilities for interacting in  concurrent systems in a sane fashion. It’s effectively handling lots of coordination.  So expecting stdio to be performant in elixir is not a good idea.  Why not write to a file or socket instead?</p> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="295502" 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/question-regarding-improving-fizzbuzz-io-performance/56816/22">Post #21</a>
	                </div>
	            </div>
              <div id="likers-container-295502" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="295502"
                     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 #21"></div>
  </section>
</div>
    <div class="postbit" id="295503" data-post-id="295503">
  <section>
    <div class="post-wrap">


					<div class="post-header">
		        <div class="user-avatar">
		          <img alt="technusm1" src="https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/technusm1/120/31120_2.png" width="120" height="120" />
		        </div>
					
						<div class="user-details">
		          <div class="user-name">
		            <h3>
                  technusm1
                    <span class="op-star" title="Thread Starter">
                      <img alt="OP" class="op-star-icon" src="/assets/thread-icons/thread-icon-thread-starter-df91e872.png" />
                    </span>
                  </h3>
		          </div>
						
						</div>
					
					</div>

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<aside class="quote no-group" data-username="ityonemo" data-post="22" data-topic="56816">
<div class="title">
<div class="quote-controls"></div>
<img alt="" width="24" height="24" src="https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/ityonemo/48/11341_2.png" class="avatar"> ityonemo:</div>
<blockquote>
<p>The logic here is that when you need stdout you’re likely to be in anger,and even more likely, needing to capture stdout <em>on another node</em> across the distributed cluster.</p>
</blockquote>
</aside>
<p>I can’t help smile reading the above sentence, even though I truly don’t understand it. Could I get more explanation for this please? <img src="https://forum.elixirforum.com/images/emoji/apple/sweat_smile.png?v=15" title=":sweat_smile:" class="emoji" alt=":sweat_smile:" loading="lazy" width="20" height="20"></p>
<blockquote>
<p>Also, io events should be atomic</p>
</blockquote>
<p>Nice explanation of this and I completely agree that this should be the default behavior (frankly, many other languages have the same default requirements for stdio). But it isn’t required for my use case where I need fast IO. So,</p>
<blockquote>
<p>Why not write to a file or socket instead?</p>
</blockquote>
<p>I am now writing to a socket as per above suggestions.</p>
<p>Thanks</p> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="295503" 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/question-regarding-improving-fizzbuzz-io-performance/56816/23">Post #22</a>
	                </div>
	            </div>
              <div id="likers-container-295503" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="295503"
                     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 #22"></div>
  </section>
</div>
    <div class="postbit" id="295504" data-post-id="295504">
  <section>
    <div class="post-wrap">


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

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<p>Yep!  One feature of the BEAM is that you can create a node, let’s say on your laptop, and connect into another node, let’s say in prod, using Erlang distribution and use the connection to debug what’s going on the remote node.  Then let’s say you execute some code remotely that prints something to “stdio”.  It’s useless if that IO gets sent to stdout on the remote node, if you’re lucky it gets logged but more likely it goes straight to /dev/null.</p>
<p>So Erlang will know that the code is being run remotely, assign that code’s group leader to your group leader on your laptop, and so IO calls to “stdout” in functions on the remote node will be forwarded to stdout <em>on your laptop</em>.  To a first approximation, anyways.  If you trigger code in a persistent genservers, that won’t hold true, for example, because that process holds on to its original group leader.</p> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="295504" 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/question-regarding-improving-fizzbuzz-io-performance/56816/24">Post #23</a>
	                </div>
	            </div>
              <div id="likers-container-295504" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="295504"
                     data-batch-url="/posts/batch_likers">
                  <div class="post-likers"></div>
                </div>
              </div>
	        </div>
			

    </div>

    <div class="triangle-top-right type-most-liked cat-most-liked" title="One of the top 3 liked posts in this thread!"></div>
  </section>
</div>
    <div class="postbit" id="295506" data-post-id="295506">
  <section>
    <div class="post-wrap">


					<div class="post-header">
		        <div class="user-avatar">
		          <img alt="technusm1" src="https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/technusm1/120/31120_2.png" width="120" height="120" />
		        </div>
					
						<div class="user-details">
		          <div class="user-name">
		            <h3>
                  technusm1
                    <span class="op-star" title="Thread Starter">
                      <img alt="OP" class="op-star-icon" src="/assets/thread-icons/thread-icon-thread-starter-df91e872.png" />
                    </span>
                  </h3>
		          </div>
						
						</div>
					
					</div>

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<p>Oh I read about this! I think it was in Sasa Juric’s <strong>Elixir in Action</strong> when I was getting started with elixir.</p>
<p>Here’s a quote from that book:</p>
<blockquote>
<p>All standard I/O calls (such as IO.puts/1) are forwarded to the group leader — a process that’s in charge of performing the actual input or output. A spawned process inherits the group leader from the process that spawned it, even when you’re spawning a process on another node. Therefore, your process may run on node2, but its group leader is still on node1. As a consequence, the string to be printed is created on node2 (as the string contents prove), but the output is printed on node1.</p>
</blockquote>
<p>Your point about needing to capture stdout on another node across a distributed cluster makes better sense now. Thanks <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> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="295506" 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/question-regarding-improving-fizzbuzz-io-performance/56816/25">Post #24</a>
	                </div>
	            </div>
              <div id="likers-container-295506" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="295506"
                     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 #24"></div>
  </section>
</div>
    <div class="postbit" id="295514" data-post-id="295514">
  <section>
    <div class="post-wrap">


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

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<p>Yes and all of that infrastructure is costly.  In the end, printing hello world quickly is far less important than having a robust debugging experience when you want up be doing debugging the least</p> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="295514" 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/question-regarding-improving-fizzbuzz-io-performance/56816/26">Post #25</a>
	                </div>
	            </div>
              <div id="likers-container-295514" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="295514"
                     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 #25"></div>
  </section>
</div>
    <div class="postbit" id="295650" data-post-id="295650">
  <section>
    <div class="post-wrap">


					<div class="post-header">
		        <div class="user-avatar">
		          <img alt="mpope" src="/assets/icons/user-9f439610.png" width="120" height="120" />
		        </div>
					
						<div class="user-details">
		          <div class="user-name">
		            <h3>
                  mpope
                  </h3>
		          </div>
						
						</div>
					
					</div>

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<p>I meant that you can now probably avoid the flatten since there is no more message passing. That’d save a binary allocation.</p> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="295650" 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/question-regarding-improving-fizzbuzz-io-performance/56816/27">Post #26</a>
	                </div>
	            </div>
              <div id="likers-container-295650" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="295650"
                     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 #26"></div>
  </section>
</div>
    <div class="postbit" id="295700" data-post-id="295700">
  <section>
    <div class="post-wrap">


					<div class="post-header">
		        <div class="user-avatar">
		          <img alt="technusm1" src="https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/technusm1/120/31120_2.png" width="120" height="120" />
		        </div>
					
						<div class="user-details">
		          <div class="user-name">
		            <h3>
                  technusm1
                    <span class="op-star" title="Thread Starter">
                      <img alt="OP" class="op-star-icon" src="/assets/thread-icons/thread-icon-thread-starter-df91e872.png" />
                    </span>
                  </h3>
		          </div>
						
						</div>
					
					</div>

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<p><a class="mention" href="/u/mpope" rel="nofollow">@mpope</a> since i’m using ports now, I believe there’s still message passing going on, so keeping the binary around is still the way to go. I tried getting rid of it, it actually reduces throughput.</p>
<p>I’ve been reading on compiler optimizations in OTP 26 and types and ranges specific optimizations have caught my eye. Here: <a href="https://www.erlang.org/blog/more-optimizations/" rel="nofollow">More Optimizations in the Compiler and JIT - Erlang/OTP</a></p>
<p>Looking at Erlang’s efficiency guide, I think that there is a <code>small integer</code> data type somewhere, maybe internally, that can accomodate integers as less than 2^60 in 1 word (meaning the JIT could potentially use machine instructions for arithmetic operations rather than erlang’s internal BigNum arithmetic). Here: <a href="https://www.erlang.org/doc/efficiency_guide/advanced.html" rel="nofollow">Erlang – Advanced</a></p>
<p>I am currently trying to get some of the above optimizations from OTP 26 working by adding guard checks for <code>range = lower..upper</code> like:</p>
<p>replacing <code>defp process_range(range = lower.._upper)</code></p>
<p>with <code>defp process_range(range = lower..upper) when is_integer(lower) and lower &gt;= 0 and lower &lt; 288230376151711744 and is_integer(upper) and upper &gt;= 0 and upper &lt; 288230376151711744</code></p>
<p>and similar guard checks for various other functions throughout my project, in the hopes of getting some performance improvements (faster computations and less assembly lines), but to no avail.</p>
<p>Could anyone point me as to whether I’m proceeding in the right direction or not? If yes, any pointers or demos about achieving such speedups would be welcome. <img src="https://forum.elixirforum.com/images/emoji/apple/confused.png?v=15" title=":confused:" class="emoji" alt=":confused:" loading="lazy" width="20" height="20"></p>
<p>Thanks</p> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="295700" 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/question-regarding-improving-fizzbuzz-io-performance/56816/28">Post #27</a>
	                </div>
	            </div>
              <div id="likers-container-295700" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="295700"
                     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 #27"></div>
  </section>
</div>
    <div class="postbit" id="297544" data-post-id="297544">
  <section>
    <div class="post-wrap">


					<div class="post-header">
		        <div class="user-avatar">
		          <img alt="technusm1" src="https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/technusm1/120/31120_2.png" width="120" height="120" />
		        </div>
					
						<div class="user-details">
		          <div class="user-name">
		            <h3>
                  technusm1
                    <span class="op-star" title="Thread Starter">
                      <img alt="OP" class="op-star-icon" src="/assets/thread-icons/thread-icon-thread-starter-df91e872.png" />
                    </span>
                  </h3>
		          </div>
						
						</div>
					
					</div>

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<p>I decided to try porting the <code>process_range</code> function to a rust nif using Rustler to see how much of a performance gain I could get and if my algorithm was any good. The performance jumped from around 650 MiB/s to 2.7-2.8 GiB/s. Curiously, this is faster than the fastest rust answers on the CodeGolf thread perform on my machine (2.5 GiB/s).</p>
<p>Here’s the rustler nif code:</p>
<pre data-code-wrap="rust"><code class="lang-rust">
#[rustler::nif(schedule = "DirtyCpu")]
fn process(i: usize, range_size: usize) -&gt; String {
    let mut result = String::with_capacity(range_size * 20);
    let p = 4800;
    for j in 0..(range_size/p) {
        for x in (0..=(p-1)).step_by(15) {
            itoap::write_to_string(&amp;mut result, p * j + 1 + x + i);
            result.push('\n');
            itoap::write_to_string(&amp;mut result, p * j + 2 + x + i);
            result.push_str("\nFizz\n");
            itoap::write_to_string(&amp;mut result, p * j + 4 + x + i);
            result.push_str("\nBuzz\nFizz\n");
            itoap::write_to_string(&amp;mut result, p * j + 7 + x + i);
            result.push('\n');
            itoap::write_to_string(&amp;mut result, p * j + 8 + x + i);
            result.push_str("\nFizz\nBuzz\n");
            itoap::write_to_string(&amp;mut result, p * j + 11 + x + i);
            result.push_str("\nFizz\n");
            itoap::write_to_string(&amp;mut result, p * j + 13 + x + i);
            result.push('\n');
            itoap::write_to_string(&amp;mut result, p * j + 14 + x + i);
            result.push_str("\nFizzBuzz\n");
        }
    }
    return result;
}

rustler::init!("Elixir.Fizzbuzz", [process]);
</code></pre>
<p>So, as it turns out, pure Elixir’s just not a good fit for this case. But its good to know that it is possible to get a better performance by integrating code from other languages in it.</p>
<p>That’s all folks. Thank you for all your answers. I learned a lot. <img src="https://forum.elixirforum.com/images/emoji/apple/smile.png?v=15" title=":smile:" class="emoji" alt=":smile:" 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="297544" 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/question-regarding-improving-fizzbuzz-io-performance/56816/29">Post #28</a>
	                </div>
	            </div>
              <div id="likers-container-297544" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="297544"
                     data-batch-url="/posts/batch_likers">
                  <div class="post-likers"></div>
                </div>
              </div>
	        </div>
			

    </div>

    <div class="triangle-top-right type-last-post cat-last-post" title="Last post!"></div>
  </section>
</div>
</template></turbo-stream><turbo-stream action="replace" target="load-more-container"><template><div id="load-more-container" class="load-more-container">
    <span class="all-loaded">— All posts loaded —</span>
</div></template></turbo-stream>