ryan961 / memo

https://github.com/ryan961/memo/issues
Creative Commons Zero v1.0 Universal
0 stars 0 forks source link

Go Slice: Robust generic functions on slices #11

Open ryan961 opened 6 months ago

ryan961 commented 6 months ago

🤖 AI Summary

This article delves into the robust generic functions on slices in the Go programming language. Type parameters enable the writing of functions like slices.Index for all types of slice elements, negating the need to implement Index for each different type of element. The article also discusses the underlying structure of slices, including the pointer, length, and capacity, as well as how this affects their representation in memory.

The slices package introduces, among others, a straightforward delete function (slices.Delete) that facilitates the removal of slice elements without requiring a complex syntax. However, the article warns about the necessity of considering the original slice invalid after calling these functions, as failure to do so or ignoring the return value can lead to errors.

Additionally, the article sheds light on the problem of "unwanted liveness", which could cause memory leaks because deleted elements remain attached to memory. Solutions to this issue include letting users manually set obsolete pointers to nil or modifying the delete function to automatically set outdated elements to zero—though the latter negatively affects efficiency, it helps reduce memory leaks and lower the cognitive load on users. Thus, in the interest of simplifying the user experience, the second option was eventually chosen.

🖇️ Details

đź“ť Note

In Go 1.22, the risk of memory leaks is avoided by always setting obsolete elements to nil.

Before Go 1.22:

image

In Go 1.22:

image

The code changed in the five functions uses the new built-in function clear (Go 1.21) to set the obsolete elements to the zero value of the element type of s:

image

The zero value of E is nil when E is a type of pointer, slice, map, chan, or interface.