From a783270b09af3d873c08a01d13f802018b69fb02 Mon Sep 17 00:00:00 2001 From: a73x Date: Sun, 29 Dec 2024 19:13:27 +0000 Subject: 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 --- public/posts/2024-08-31-01.html | 67 +++++++++++++++++------------------------ 1 file changed, 28 insertions(+), 39 deletions(-) (limited to 'public/posts/2024-08-31-01.html') 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 @@ +
← Posts

Go's unique pkg

https://pkg.go.dev/unique

-
-

The unique package provides facilities for canonicalising (“interning”) comparable values.1

+

The unique package provides facilities for canonicalising ("interning") comparable values.1

-

oh yeah, thats obvious I fully understand what this package does now, great read, tune in for the next post.

- -

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 Interning.

- +

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 Interning.

So lets try again.

- -

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.

- +

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.

So the dumbed down version might look like

-
var internedStrings sync.Map
-
-func Intern(s string) string {
-	if val, ok := internedStrings.Load(s); ok { 
-		return val.(string) 
-	} 
-	internedStrings.Store(s, s) 
-	return s 
-}
-
-

With a small demo here https://go.dev/play/p/piSYjCHIcLr

- -

This implementation is fairly naive, it can only grow and it only works with strings, so naturally go’s implementation is a better.

- -

It’s also worth noting, that since strings are a pointer under the hood

- +
var internedStrings sync.Map
+
+func Intern(s string) string {
+	if val, ok := internedStrings.Load(s); ok { 
+		return val.(string) 
+	} 
+	internedStrings.Store(s, s) 
+	return s 
+}
+

With a small demo here https://go.dev/play/p/piSYjCHIcLr

+

This implementation is fairly naive, it can only grow and it only works with strings, so naturally go's implementation is a better.

+

It's also worth noting, that since strings are a pointer under the hood

-

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 is sufficient to just check their pointers.2

+

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 is sufficient to just check their pointers.2

-

So to recap, goes unique package provides a way to reuse objects instead of creating new ones, if we consider the objects of equal value.

- -
- -
- + -- cgit v1.2.3