You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardexpand all lines: aoc2023-first-days.html
+9-9
Original file line number
Diff line number
Diff line change
@@ -16,12 +16,12 @@ <h2><a href="/">naming is hard</a></h2>
16
16
</div>
17
17
<divclass="article">
18
18
<sectionid="Zero-allocation-hello-world-in-Rust">
19
-
<h1date="2023/12/02" hide="true">Zero allocation hello world in Rust</h1>
20
-
<p>This year of <ahref="https://adventofcode.com">Advent of Code</a> I decided to try <ahref="https://www.rust-lang.org/"><strong>Rust</strong></a>. I’m complete newbie and still learning basic concepts of language (btw, <ahref="https://rust-book.cs.brown.edu">Brown Book</a> is amazing), but there were one idea that I wanted to try for the AoC challenges - I need to implement <strong>zero allocation</strong> solutions (at least for the first ones)!</p>
19
+
<aclass="heading-anchor" href="#Zero-allocation-hello-world-in-Rust"><h1date="2023/12/02" hide="true">Zero allocation hello world in Rust</h1>
20
+
</a><p>This year of <ahref="https://adventofcode.com">Advent of Code</a> I decided to try <ahref="https://www.rust-lang.org/"><strong>Rust</strong></a>. I’m complete newbie and still learning basic concepts of language (btw, <ahref="https://rust-book.cs.brown.edu">Brown Book</a> is amazing), but there were one idea that I wanted to try for the AoC challenges - I need to implement <strong>zero allocation</strong> solutions (at least for the first ones)!</p>
21
21
<p>What does it mean, <strong>zero allocation</strong>? Rust Book has nice <ahref="https://rust-book.cs.brown.edu/ch04-01-what-is-ownership.html">chapter about ownership</a> which describes such concepts like memory, <em>stack</em> and <em>heap</em>. In short, your program usually operating with memory from two regions:</p>
22
22
<p>…something about zero allocations…</p>
23
23
<p>How can we analyze allocations of our program? I found nice tool <ahref="https://github.com/matt-kimball/allocscope">allocscope</a> which record all allocations made with <code>malloc</code> in your program and allow you to analyze source of that allocations. Also, you can compile simple C code in shared library in order to override default <code>malloc</code> and add some debug information to it:</p>
@@ -48,7 +48,7 @@ <h1 date="2023/12/02" hide="true">Zero allocation hello world in Rust</h1>
48
48
<p>Couple of them looks pretty suspicious: 1024 bytes allocations is almost surely used for some intermediate buffers. We are printing string to the console - so most likely that Rust implementation of writes to <code>stdout</code> uses buffering for performance.</p>
49
49
<p>If we will unwind all macros we should get some code equivalent to the <code>write_all</code> call on <code>stdout()</code> stream: <code>io::stdout().write_all(b"Hello, World!")</code>.</p>
50
50
<p>We can look up for the code of <code>io</code> module and indeed see, that <ahref="https://doc.rust-lang.org/src/std/io/stdio.rs.html#614"><code>stdout()</code></a> creates synchronized instance wrapped with <ahref="https://doc.rust-lang.org/src/std/io/buffered/linewriter.rs.html#87"><code>LineWriter</code></a> which has default buffer size of 1KiB.</p>
<p>Ok, let’s get rid of the <code>stdout</code> then and use <code>stderr</code> which also creates synchronized instance but without any additional buffering. We can write to <code>stderr</code> explicitly or use <code>eprintln!</code> macro for the same purpose. Let’s see how much memory we allocate in this case in our new program:</p>
0 commit comments