-end</code></pre><p>Julia's <a href="https://en.wikipedia.org/wiki/Constant_folding">constant propagation</a> will ensure that most accesses of those fields will be determined at compile-time, so this simple change robustly fixes many inference problems.</p><h2 id="Fixing-Core.Box"><a class="docs-heading-anchor" href="#Fixing-Core.Box">Fixing <code>Core.Box</code></a><a id="Fixing-Core.Box-1"></a><a class="docs-heading-anchor-permalink" href="#Fixing-Core.Box" title="Permalink"></a></h2><p><a href="https://github.com/JuliaLang/julia/issues/15276">Julia issue 15276</a> is one of the more surprising forms of inference failure; it is the most common cause of a <code>Core.Box</code> annotation. If other variables depend on the <code>Box</code>ed variable, then a single <code>Core.Box</code> can lead to widespread inference problems. For this reason, these are also among the first inference problems you should tackle.</p><p>Read <a href="https://docs.julialang.org/en/v1/manual/performance-tips/#man-performance-captured">this explanation of why this happens and what you can do to fix it</a>. If you are directed to find <code>Core.Box</code> inference triggers via <a href="../../reference/#SnoopCompile.suggest"><code>suggest</code></a>, you may need to explore around the call site a bit– the inference trigger may be in the closure itself, but the fix needs to go in the method that creates the closure.</p><p>Use of <code>ascend</code> is highly recommended for fixing <code>Core.Box</code> inference failures.</p><h2 id="Handling-edge-cases"><a class="docs-heading-anchor" href="#Handling-edge-cases">Handling edge cases</a><a id="Handling-edge-cases-1"></a><a class="docs-heading-anchor-permalink" href="#Handling-edge-cases" title="Permalink"></a></h2><p>You can sometimes get invalidations from failing to handle "formal" possibilities. For example, operations with regular expressions might return a <code>Union{Nothing, RegexMatch}</code>. You can sometimes get poor type inference by writing code that fails to take account of the possibility that <code>nothing</code> might be returned. For example, a comprehension</p><pre><code class="language-julia hljs">ms = [m.match for m in match.((rex,), my_strings)]</code></pre><p>might be replaced with</p><pre><code class="language-julia hljs">ms = [m.match for m in match.((rex,), my_strings) if m !== nothing]</code></pre><p>and return a better-typed result.</p></article><nav class="docs-footer"><a class="docs-footer-prevpage" href="../gotchas/">« Precompilation "gotcha"s</a><a class="docs-footer-nextpage" href="../../reference/">Reference »</a><div class="flexbox-break"></div><p class="footer-message">Powered by <a href="https://github.com/JuliaDocs/Documenter.jl">Documenter.jl</a> and the <a href="https://julialang.org/">Julia Programming Language</a>.</p></nav></div><div class="modal" id="documenter-settings"><div class="modal-background"></div><div class="modal-card"><header class="modal-card-head"><p class="modal-card-title">Settings</p><button class="delete"></button></header><section class="modal-card-body"><p><label class="label">Theme</label><div class="select"><select id="documenter-themepicker"><option value="auto">Automatic (OS)</option><option value="documenter-light">documenter-light</option><option value="documenter-dark">documenter-dark</option><option value="catppuccin-latte">catppuccin-latte</option><option value="catppuccin-frappe">catppuccin-frappe</option><option value="catppuccin-macchiato">catppuccin-macchiato</option><option value="catppuccin-mocha">catppuccin-mocha</option></select></div></p><hr/><p>This document was generated with <a href="https://github.com/JuliaDocs/Documenter.jl">Documenter.jl</a> version 1.8.0 on <span class="colophon-date" title="Sunday 24 November 2024 11:43">Sunday 24 November 2024</span>. Using Julia version 1.11.1.</p></section><footer class="modal-card-foot"></footer></div></div></div></body></html>
0 commit comments