<?xml version="1.0" encoding="utf-8"?><feed xmlns="http://www.w3.org/2005/Atom" ><generator uri="https://jekyllrb.com/" version="3.10.0">Jekyll</generator><link href="https://laszloashin.github.io/ray/feed.xml" rel="self" type="application/atom+xml" /><link href="https://laszloashin.github.io/ray/" rel="alternate" type="text/html" /><updated>2026-01-03T08:57:07+00:00</updated><id>https://laszloashin.github.io/ray/feed.xml</id><title type="html">Size-optimizing my old toy-raytracer</title><subtitle>The story of fighting software bloat by reducing the size of a toy raytracer under 4096 bytes on Linux, Windows and MacOS.</subtitle><entry><title type="html">Where it all began</title><link href="https://laszloashin.github.io/ray/jekyll/update/2022/03/19/there-it-all-began.html" rel="alternate" type="text/html" title="Where it all began" /><published>2022-03-19T15:19:22+00:00</published><updated>2022-03-19T15:19:22+00:00</updated><id>https://laszloashin.github.io/ray/jekyll/update/2022/03/19/there-it-all-began</id><content type="html" xml:base="https://laszloashin.github.io/ray/jekyll/update/2022/03/19/there-it-all-began.html"><![CDATA[<p>Writing a raytracer was a school homework excersise back in 2007.
For this project I didn’t use source control so I don’t have history
data. I think it was a one source file project when I submitted it.</p>

<p>In 2008 improvements were made and I split the code into more source files.
In 2013 I <a href="https://github.com/LaszloAshin/ray/commit/df76ccff197d94574bdcf75941a7571c0ab57db6">imported them to git</a>.
This version had some issues:</p>

<ul>
  <li>it contained an unfinished Kd-Tree implementation,</li>
  <li>there was a lot of dead code,</li>
  <li>it was very slow.</li>
</ul>

<p>Let’s see some metrics. To get this initial code <a href="https://github.com/LaszloAshin/ray/commit/df76ccff197d94574bdcf75941a7571c0ab57db6">df76ccf</a>
link I needed to modify the Makefile like this:</p>

<div class="language-diff highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="p">@@ -39,7 +39,7 @@</span> $(BUILDDIR)/%.o: %.cc
 -include $(ray_OBJS:%.o=$(DEPDIR)/%.d)

 ray: $(ray_OBJS:%.o=$(BUILDDIR)/%.o)
<span class="gd">-       @$(CC) $(LDFLAGS) -o $@ $^
</span><span class="gi">+       @$(CC) $^ $(LDFLAGS) -o $@
</span>        @echo "  LN    $@"

 clean:
</code></pre></div></div>

<p>Using <code class="language-plaintext highlighter-rouge">gcc-10.2.1</code> it compiles to a <code class="language-plaintext highlighter-rouge">56936</code> byte x86_64 executable on
linux. This is the unstripped size.</p>

<div class="language-plaintext highlighter-rouge"><div class="highlight"><pre class="highlight"><code>$ size ray
   text	   data	    bss	    dec	    hex	filename
  35323	   2056	    624	  38003	   9473	ray
</code></pre></div></div>

<p>The program needs about 14 seconds on my machine to render the 1280x800
output image:</p>

<div class="language-plaintext highlighter-rouge"><div class="highlight"><pre class="highlight"><code>$ time ./ray
62.60% Output has been written to tracement.tga
94.40% Output has been written to tracement.tga
freeNode(0)
./ray  52.22s user 0.06s system 372% cpu 14.026 total
</code></pre></div></div>

<p>Here is the result rendering:</p>

<p><img src="/ray/assets/2022-03-19-tracement.tga.png" alt="result rendering" /></p>

<p>So that’s where I started from.</p>

<p>The initial goal was to see if I can speed the thing up.</p>]]></content><author><name></name></author><category term="jekyll" /><category term="update" /><summary type="html"><![CDATA[Writing a raytracer was a school homework excersise back in 2007. For this project I didn’t use source control so I don’t have history data. I think it was a one source file project when I submitted it.]]></summary></entry></feed>