diff options
| author | a73x <[email protected]> | 2024-12-29 19:13:27 +0000 |
|---|---|---|
| committer | a73x <[email protected]> | 2024-12-30 11:00:49 +0000 |
| commit | a783270b09af3d873c08a01d13f802018b69fb02 (patch) | |
| tree | bdac4e38357afa535515dd8dda790d7193f371d0 /public/posts/2024-08-31-01.html | |
| parent | 71513b80ebc21240b5b68d8bbbf8b7ee2f54893e (diff) | |
new markdown renderer
since TOC has a title now and it can compact toc headers, we can use
header 2 for everything
use the built in goldmark extension for syntax highlighting
Diffstat (limited to 'public/posts/2024-08-31-01.html')
| -rw-r--r-- | public/posts/2024-08-31-01.html | 67 |
1 files changed, 28 insertions, 39 deletions
diff --git a/public/posts/2024-08-31-01.html b/public/posts/2024-08-31-01.html index 6c3924f..be908a5 100644 --- a/public/posts/2024-08-31-01.html +++ b/public/posts/2024-08-31-01.html @@ -25,70 +25,59 @@ <ul> - <li><a class="no-decorations" href="/">home</a></li> + <li><a class="no-decorations" href="/">Home</a></li> - <li><a class="no-decorations" href="/posts">posts</a></li> + <li><a class="no-decorations" href="/posts">Posts</a></li> - <li><a class="no-decorations" href="/ethos">ethos</a></li> + <li><a class="no-decorations" href="/ethos">Ethos</a></li> </ul> </nav> </div> + <hr /> <a href="/posts">← Posts</a> <h1>Go's unique pkg</h1> <p><a href="https://pkg.go.dev/unique">https://pkg.go.dev/unique</a></p> - <blockquote> -<p>The unique package provides facilities for canonicalising (“interning”) comparable values.<sup class="footnote-ref" id="fnref:1"><a href="#fn:1">1</a></sup></p> +<p>The unique package provides facilities for canonicalising ("interning") comparable values.<sup id="fnref:1"><a href="#fn:1" class="footnote-ref" role="doc-noteref">1</a></sup></p> </blockquote> - <p>oh yeah, thats obvious I fully understand what this package does now, great read, tune in for the next post.</p> - -<p>Interning, is the re-use of an object of equal value instead of creating a new one. I’m pretending I knew this but really I’ve just reworded <a href="https://en.wikipedia.org/wiki/Interning_(computer_science)">Interning</a>.</p> - +<p>Interning, is the re-use of an object of equal value instead of creating a new one. I'm pretending I knew this but really I've just reworded <a href="https://en.wikipedia.org/wiki/Interning_(computer_science)">Interning</a>.</p> <p>So lets try again.</p> - -<p>If you’re parsing out a csv of bank transactions, its very likely a lot of names will be repeated. Instead of allocating memory for each string representing a merchant, you can simply reuse the the same string.</p> - +<p>If you're parsing out a csv of bank transactions, its very likely a lot of names will be repeated. Instead of allocating memory for each string representing a merchant, you can simply reuse the the same string.</p> <p>So the dumbed down version might look like</p> -<pre tabindex="0" class="chroma"><code><span class="line"><span class="cl"><span class="kd">var</span> <span class="nx">internedStrings</span> <span class="nx">sync</span><span class="p">.</span><span class="nx">Map</span> -</span></span><span class="line"><span class="cl"> -</span></span><span class="line"><span class="cl"><span class="kd">func</span> <span class="nf">Intern</span><span class="p">(</span><span class="nx">s</span> <span class="kt">string</span><span class="p">)</span> <span class="kt">string</span> <span class="p">{</span> -</span></span><span class="line"><span class="cl"> <span class="k">if</span> <span class="nx">val</span><span class="p">,</span> <span class="nx">ok</span> <span class="o">:=</span> <span class="nx">internedStrings</span><span class="p">.</span><span class="nf">Load</span><span class="p">(</span><span class="nx">s</span><span class="p">);</span> <span class="nx">ok</span> <span class="p">{</span> -</span></span><span class="line"><span class="cl"> <span class="k">return</span> <span class="nx">val</span><span class="p">.(</span><span class="kt">string</span><span class="p">)</span> -</span></span><span class="line"><span class="cl"> <span class="p">}</span> -</span></span><span class="line"><span class="cl"> <span class="nx">internedStrings</span><span class="p">.</span><span class="nf">Store</span><span class="p">(</span><span class="nx">s</span><span class="p">,</span> <span class="nx">s</span><span class="p">)</span> -</span></span><span class="line"><span class="cl"> <span class="k">return</span> <span class="nx">s</span> -</span></span><span class="line"><span class="cl"><span class="p">}</span> -</span></span></code></pre> -<p>With a small demo here <a href="https://go.dev/play/p/piSYjCHIcLr">https://go.dev/play/p/piSYjCHIcLr</a></p> - -<p>This implementation is fairly naive, it can only grow and it only works with strings, so naturally go’s implementation is a better.</p> - -<p>It’s also worth noting, that since strings are a pointer under the hood</p> - +<pre tabindex="0" style="background-color:#f0f0f0;-moz-tab-size:4;-o-tab-size:4;tab-size:4;white-space:pre-wrap;word-break:break-word;"><code><span style="color:#00a">var</span> internedStrings sync.Map + +<span style="color:#00a">func</span> <span style="color:#0a0">Intern</span>(s <span style="color:#0aa">string</span>) <span style="color:#0aa">string</span> { + <span style="color:#00a">if</span> val, ok := internedStrings.<span style="color:#0a0">Load</span>(s); ok { + <span style="color:#00a">return</span> val.(<span style="color:#0aa">string</span>) + } + internedStrings.<span style="color:#0a0">Store</span>(s, s) + <span style="color:#00a">return</span> s +} +</code></pre><p>With a small demo here <a href="https://go.dev/play/p/piSYjCHIcLr">https://go.dev/play/p/piSYjCHIcLr</a></p> +<p>This implementation is fairly naive, it can only grow and it only works with strings, so naturally go's implementation is a better.</p> +<p>It's also worth noting, that since strings are a pointer under the hood</p> <blockquote> -<p>When comparing two strings, if the pointers are not equal, then we must compare their contents to determine equality. But if we know that two strings are canonicalized, then it <em>is</em> sufficient to just check their pointers.<sup class="footnote-ref" id="fnref:2"><a href="#fn:2">2</a></sup></p> +<p>When comparing two strings, if the pointers are not equal, then we must compare their contents to determine equality. But if we know that two strings are canonicalized, then it <em>is</em> sufficient to just check their pointers.<sup id="fnref:2"><a href="#fn:2" class="footnote-ref" role="doc-noteref">2</a></sup></p> </blockquote> - <p>So to recap, goes <code>unique</code> package provides a way to reuse objects instead of creating new ones, if we consider the objects of equal value.</p> - -<div class="footnotes"> - -<hr> - +<div class="footnotes" role="doc-endnotes"> +<hr /> <ol> -<li id="fn:1"><a href="https://pkg.go.dev/unique">https://pkg.go.dev/unique</a> <a class="footnote-return" href="#fnref:1"><sup>[return]</sup></a></li> - -<li id="fn:2"><a href="https://go.dev/blog/unique">https://go.dev/blog/unique</a> <a class="footnote-return" href="#fnref:2"><sup>[return]</sup></a></li> +<li id="fn:1"> +<p><a href="https://pkg.go.dev/unique">https://pkg.go.dev/unique</a> <a href="#fnref:1" class="footnote-backref" role="doc-backlink">↩︎</a></p> +</li> +<li id="fn:2"> +<p><a href="https://go.dev/blog/unique">https://go.dev/blog/unique</a> <a href="#fnref:2" class="footnote-backref" role="doc-backlink">↩︎</a></p> +</li> </ol> - </div> |
