1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
|
<!DOCTYPE html>
<html lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta name="description" content="Home for a73x" />
<meta name="author" content="a73x" />
<meta name="viewport"
content="user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0, width=device-width" />
<title>a73x</title>
<link rel="stylesheet" href="/static/styles.css">
<link rel="stylesheet" href="/static/syntax.css">
<link rel="icon" type="image/x-icon" href="/static/favicon.svg">
</head>
<body>
<main>
<div class="header">
<div class="header-title">
<h1>a73x</h1>
<sub>high effort, low reward</sub>
</div>
<nav class="nav">
<ul>
<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="/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 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>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>So the dumbed down version might look like</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 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" role="doc-endnotes">
<hr />
<ol>
<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>
<footer>
<br />
<hr />
<br />
<p>see something you disagree with? email: <a href="mailto:[email protected]">[email protected]</a></p>
</footer>
</main>
</body>
</html>
|