Files
sfall/optimization/index.html
T

39 lines
26 KiB
HTML
Raw Normal View History

2026-01-03 12:58:54 +00:00
<!DOCTYPE html> <html lang="en-US"> <head> <meta charset="UTF-8"> <meta http-equiv="X-UA-Compatible" content="IE=Edge"> <link rel="stylesheet" href="/sfall/assets/css/just-the-docs-default.css"> <link rel="stylesheet" href="/sfall/assets/css/just-the-docs-head-nav.css" id="jtd-head-nav-stylesheet"> <style id="jtd-nav-activation"> .site-nav > ul.nav-list:first-child > li:not(:nth-child(8)) > a, .site-nav > ul.nav-list:first-child > li > ul > li a { background-image: none; } .site-nav > ul.nav-list:not(:first-child) a, .site-nav li.external a { background-image: none; } .site-nav > ul.nav-list:first-child > li:nth-child(8) > a { font-weight: 600; text-decoration: none; }.site-nav > ul.nav-list:first-child > li:nth-child(8) > button svg { transform: rotate(-90deg); }.site-nav > ul.nav-list:first-child > li.nav-list-item:nth-child(8) > ul.nav-list { display: block; } </style> <script src="/sfall/assets/js/vendor/lunr.min.js"></script> <script src="/sfall/assets/js/just-the-docs.js"></script> <meta name="viewport" content="width=device-width, initial-scale=1"> <link rel="icon" href="/sfall/favicon.ico" type="image/x-icon"> <!-- Begin Jekyll SEO tag v2.8.0 --> <title>Optimization | sfall</title> <meta name="generator" content="Jekyll v3.9.0" /> <meta property="og:title" content="Optimization" /> <meta property="og:locale" content="en_US" /> <meta name="description" content="Sfall documentation" /> <meta property="og:description" content="Sfall documentation" /> <link rel="canonical" href="/sfall/optimization/" /> <meta property="og:url" content="/sfall/optimization/" /> <meta property="og:site_name" content="sfall" /> <meta property="og:type" content="website" /> <meta name="twitter:card" content="summary" /> <meta property="twitter:title" content="Optimization" /> <script type="application/ld+json"> {"@context":"https://schema.org","@type":"WebPage","description":"Sfall documentation","headline":"Optimization","url":"/sfall/optimization/"}</script> <!-- End Jekyll SEO tag --> <link rel="apple-touch-icon" sizes="180x180" href="/assets/favicon/apple-touch-icon.png"> <link rel="icon" type="image/png" sizes="32x32" href="/assets/favicon/favicon-32x32.png"> <link rel="icon" type="image/png" sizes="16x16" href="/assets/favicon/favicon-16x16.png"> <link rel="manifest" href="/assets/favicon/site.webmanifest"> <link rel="mask-icon" href="/assets/favicon/safari-pinned-tab.svg" color="#5bbad5"> <meta name="msapplication-TileColor" content="#da532c"> <meta name="msapplication-config" content="/assets/favicon/browserconfig.xml"> <meta name="theme-color" content="#ffffff"> </head> <body> <a class="skip-to-main" href="#main-content">Skip to main content</a> <svg xmlns="http://www.w3.org/2000/svg" class="d-none"> <symbol id="svg-link" viewBox="0 0 24 24"> <title>Link</title> <svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="feather feather-link"> <path d="M10 13a5 5 0 0 0 7.54.54l3-3a5 5 0 0 0-7.07-7.07l-1.72 1.71"></path><path d="M14 11a5 5 0 0 0-7.54-.54l-3 3a5 5 0 0 0 7.07 7.07l1.71-1.71"></path> </svg> </symbol> <symbol id="svg-menu" viewBox="0 0 24 24"> <title>Menu</title> <svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="feather feather-menu"> <line x1="3" y1="12" x2="21" y2="12"></line><line x1="3" y1="6" x2="21" y2="6"></line><line x1="3" y1="18" x2="21" y2="18"></line> </svg> </symbol> <symbol id="svg-arrow-right" viewBox="0 0 24 24"> <title>Expand</title> <svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="feather feather-chevron-right"> <polyline points="9 18 15 12 9 6"></polyline> </svg> </symbol> <!-- Feather. MIT License: https://github.com/feathericons/feather/blob/master/LICENSE --> <symbol id="svg-external-link" width="
2022-05-30 13:00:21 +00:00
</code></pre></div> </div> </li> <li>constant variable initialization: All variables are initialised to some value, (0, if you dont specify anything else,) so sslc attempts to make use of that fact to remove the first assignment to a variable if the first assignment is a constant expression. <div class="language-plaintext highlighter-rouge"><div class="highlight"><pre class="highlight"><code>variable a; -&gt; variable a := 4;
a := 4; -&gt;
2023-03-31 14:16:17 +00:00
</code></pre></div> </div> </li> <li>constant propagation: checks for values assigned to variables which can be computed at compile time, and replaces relevant references to the symbol by the constant. The original store is not removed by this optimization. Global variables are considered for this optimization only if they are not marked import or export, and are not assigned to anywhere in the script. <div class="language-plaintext highlighter-rouge"><div class="highlight"><pre class="highlight"><code>a := 4; -&gt; a := 4;
2021-06-01 04:05:32 +00:00
foo(a); -&gt; foo(4);
2022-05-30 13:00:21 +00:00
</code></pre></div> </div> </li> <li>dead code removal: Checks for and removes code which cannot be reached, either because it is hidden behind a return or because the argument to an if statement can be computed at compile time. <div class="language-plaintext highlighter-rouge"><div class="highlight"><pre class="highlight"><code>if (True) then begin -&gt; display_msg("foo");
2021-06-01 04:05:32 +00:00
display_msg("foo"); -&gt;
end else begin -&gt;
display_msg("bar"); -&gt;
end -&gt;
2022-05-30 13:00:21 +00:00
</code></pre></div> </div> </li> <li>unreferenced variable elimination: Checks for variables which are never referenced, and removes them. Also applies to global variables, as long as they are not marked for export. <div class="language-plaintext highlighter-rouge"><div class="highlight"><pre class="highlight"><code>variable i, j, k; -&gt; variable i;
i := 1; -&gt; i := 1;
return; -&gt; return;
2022-04-07 13:19:56 +00:00
</code></pre></div> </div> </li> <li>unreferenced procedure elimination: Checks for any procedures which are never called, and removes them. <div class="language-plaintext highlighter-rouge"><div class="highlight"><pre class="highlight"><code>procedure foo begin return "foo"; end -&gt; procedure foo begin return "foo"; end
2021-06-01 04:05:32 +00:00
procedure bar begin return "bar"; end -&gt; procedure start begin
procedure start begin -&gt; display_msg(foo);
display_msg(foo); -&gt; end
end -&gt;
2022-05-30 13:00:21 +00:00
</code></pre></div> </div> </li> <li>dead store removal: Removes variable assignments if the result of the variable is unused, and if the expression used to compute the value of the variable is provably free of side effects. (See <code class="language-plaintext highlighter-rouge">pure</code> keyword) <div class="language-plaintext highlighter-rouge"><div class="highlight"><pre class="highlight"><code>a := "moo"; -&gt; a := "foo";
a := "foo"; -&gt; display_msg(a);
2021-06-01 04:05:32 +00:00
display_msg(a); -&gt;
2022-05-30 13:00:21 +00:00
a := "bar"; -&gt;
2022-04-07 13:19:56 +00:00
</code></pre></div> </div> </li> <li>store combination: Where there are two stores in a row to the same variable, the two expressions are combined. <div class="language-plaintext highlighter-rouge"><div class="highlight"><pre class="highlight"><code>var1 := var2; -&gt; var1 := var2 + var3;
2022-05-30 13:00:21 +00:00
var1 += var3; -&gt;
</code></pre></div> </div> </li> <li>variable combination: Where usage regions of variables do not overlap, combine the variables to provide additional candidates for unreferenced variable elimination. Very useful for scripts containing multiple <code class="language-plaintext highlighter-rouge">foreach</code> loops, which generate 2 or 3 hidden variables each. <div class="language-plaintext highlighter-rouge"><div class="highlight"><pre class="highlight"><code>a := "foo"; -&gt; a := "foo";
2021-06-01 04:05:32 +00:00
display_msg(a); -&gt; display_msg(a);
2022-05-30 13:00:21 +00:00
b := "bar"; -&gt; a := "bar";
2021-06-01 04:05:32 +00:00
display_msg(b); -&gt; display_msg(a);
2022-05-30 13:00:21 +00:00
</code></pre></div> </div> </li> <li>namelist compression: Fallout stores the names of all file scope variables and procedures in a namelist which is saved into the .int. Any of these that are unreferenced can be removed, and the names of global variables can be modified to make them shorter.</li> </ul> <h2 id="writing-your-own-code"> <a href="#writing-your-own-code" class="anchor-heading" aria-labelledby="writing-your-own-code"><svg viewBox="0 0 16 16" aria-hidden="true"><use xlink:href="#svg-link"></use></svg></a> Writing your own code </h2> <ul> <li> <p>Dont have global scripts running any more often that you need them to. Not everything needs to be run every single frame.</p> </li> <li>Never concat constant strings with the <code class="language-plaintext highlighter-rouge">+</code> operator, as it forces the operation to be done at runtime. The compiler can cope with constant strings being placed next to each other without the need for a <code class="language-plaintext highlighter-rouge">+</code>, which results in far more efficient code as the combination is done at lex time. <div class="language-plaintext highlighter-rouge"><div class="highlight"><pre class="highlight"><code>#define GLOB_PREFIX "ts__" -&gt; #define GLOB_PREFIX "ts__"
2021-06-01 04:05:32 +00:00
procedure start begin -&gt; procedure start begin
set_sfall_global(GLOB_PREFIX + "foo1", 0); -&gt; set_sfall_global(GLOB_PREFIX "foo1", 0);
end -&gt; end
2022-05-30 13:00:21 +00:00
</code></pre></div> </div> </li> <li>Avoid function calls in <code class="language-plaintext highlighter-rouge">while</code> loops. Function calls are expensive in comparison to variable lookups, so its more efficient to move the function call out of the loop and store the result in a variable. <div class="language-plaintext highlighter-rouge"><div class="highlight"><pre class="highlight"><code>while i &lt; len_array(array) do begin -&gt; tmp := len_array(array);
2021-06-01 04:05:32 +00:00
... -&gt; while i &lt; tmp do begin
end -&gt; ...
-&gt; end
2026-01-03 12:58:54 +00:00
</code></pre></div> </div> </li> <li> <p>Mark functions with <code class="language-plaintext highlighter-rouge">pure</code> or <code class="language-plaintext highlighter-rouge">inline</code> where relevant.</p> <ul> <li> <p><code class="language-plaintext highlighter-rouge">pure</code> is a hint to the optimizer that a procedure has no side effects. (i.e. theres no way to tell that its been called aside from its return value.) Pure procedures cannot modify global variables, or call any other procedure that isnt itself pure. Functions marked with pure can only be used in expressions (i.e. you cannot use the <code class="language-plaintext highlighter-rouge">call &lt;procedure&gt;</code> syntax to call them.) If there are non-pure terms in an expression, it prevents that expression being considered for dead store removal. Where no such optimizations can be performed, or if optimization is disabled, marking a procedure with pure will have no effect on the compiled code.</p> </li> <li> <p><code class="language-plaintext highlighter-rouge">inline</code> is an instruction to the compiler to replace calls to the marked procedure with a copy of the procedures code instead of having a separate call. Inlined procedures cannot use the <code class="language-plaintext highlighter-rouge">return</code> command, cannot be predefined, and cannot be used as part of an expression. Inlining if a procedure is only going to be called once is always a win, but if there are multiple calls to a procedure you will end up bloating the size of the generated code.</p> </li> </ul> </li> </ul> </main> <hr> <footer> <div class="d-xs-block d-md-none"> <div class="mt-4 fs-2"> This site uses <a href="https://github.com/just-the-docs/just-the-docs">Just the Docs</a>, a documentation theme for Jekyll. </div> </div> </footer> </div> </div> <div class="search-overlay"></div> </div> </body> </html>