Go Optimizations 101 Tapir Liu 2024 scribd download
Go Optimizations 101 Tapir Liu 2024 scribd download
com
https://textbookfull.com/product/go-optimizations-101-tapir-
liu/
OR CLICK BUTTON
DOWNLOAD NOW
https://textbookfull.com/product/buddhism-101-arnold-kozak/
textboxfull.com
https://textbookfull.com/product/black-hat-go-go-programming-for-
hackers-and-pentesters-tom-steele/
textboxfull.com
https://textbookfull.com/product/power-play-go-big-or-go-home-1st-
edition-rosemary-willhide/
textboxfull.com
https://textbookfull.com/product/world-history-101-tom-head/
textboxfull.com
Go. No Go. Is Your Business Idea Ready to Launch? Lauri
Harrison
https://textbookfull.com/product/go-no-go-is-your-business-idea-ready-
to-launch-lauri-harrison/
textboxfull.com
https://textbookfull.com/product/black-hat-go-go-programming-for-
hackers-and-pentesters-1st-edition-tom-steele/
textboxfull.com
https://textbookfull.com/product/let-s-go-learn-to-build-professional-
web-applications-with-go-alex-edwards/
textboxfull.com
https://textbookfull.com/product/let-s-go-learn-to-build-professional-
web-applications-with-go-alex-edwards-2/
textboxfull.com
https://textbookfull.com/product/real-estate-investing-101-michele-
cagan/
textboxfull.com
Acknowledgments
Firstly, thanks to the entire Go community. An active and responsive
community ensured this book was finished on time.
I also would like to thank all gophers who ever made influences on
this book, be it directly or indirectly, intentionally or unintentionally.
golang, https://go.dev/
gomarkdown, https://github.com/gomarkdown/markdown
goini, https://github.com/zieckey/goini
go-epub, https://github.com/bmaupin/go-epub
pandoc, https://pandoc.org
calibre, https://calibre-ebook.com/
GIMP, https://www.gimp.org
Tapir was ever (maybe will be again) an indie game developer. You
can find his games here: tapirgames.com.
About GoTV
During writing this book, the tool GoTV is used to manage
installations of multiple Go toolchain versions and check the behavior
differences between Go toolchain versions.
Feedback
Welcome to improve this book by submitting corrections to Go 101
issue list (https://github.com/go101/go101) for all kinds of mistakes,
such as typos, grammar errors, wording inaccuracies, wrong
explanations, description flaws, code bugs, etc.
If a value always contains only one part, then the part may be also
called as the direct part of the value, and we say the value has no
indirect parts.
boolean types
numeric types (int8, uint8, int16, uint16, int32, uint32, int64,
uint64, int, uint, uintptr, float32, float64, complex64,
complex128)
pointer types
unsafe pointer types
struct types
array types
And a value of the following kinds of types always may contain one
or more indirect parts:
slice types
map types
channel types
function types
interface types
string types
Value/type sizes
The size of a value part means how many bytes are needed to be
allocated in memory to store the value part at run time.
The size of a value exactly equals to the size of the direct part of the
value. In other words, the indirect parts of a value don't contribute
to the size of the value. The reason? It has been mentioned above:
when assigning/copying a value, only the direct part of the value is
copied and the indirect parts might be shared by multiple values, so
it is not a good idea to let the same indirect parts contribute to value
sizes.
A struct values holds all its fields. In other words, a struct value
is composed of all its fields. At runtime, the fields of a struct are
allocated on the same memory block as the struct itself. Copying
a struct value means copying all the fields of the struct value.
So all the fields of a struct value contribute to the size of the
struct value.
Like struct values, an array value holds all its elements. In other
words, an array is composed of all its elements. At runtime, the
elements of an array are allocated on the same memory block
as the array itself. Copying an array value means copying all the
elements of the array value. So all elements of an array
contribute to the size of the array value.
Internally, a slice uses a pointer (on the direct part) to reference all
its elements (on the indirect part). The length and capacity
information (two int values) of a slice is stored on the direct part of
the slice. From memory allocation point of view, it doesn't hold its
elements. Its elements are allocated on another (indirect) value part
other than its direct part. When assigning a slice value to another
slice value, none elements of the slice get copied. After assigning,
the source slice and the destination slice both reference (but not
hold) the same elements. So the elements of a slice don't contribute
to the size of a specified slice. This is why the sizes of all slice types
are the same.
Like slice values, a map value just references all its entries and a
buffered channel value just references its elements being buffered.
a string just references all its containing bytes (on the indirect
part), though in logic, we can also think a string holds all its
containing bytes. The length information of a string is stored on
the direct part of the string as an int value.
an interface value just references its dynamic value, though in
logic, we can also think an interface value holds its dynamic
value.
Memory alignments
To fully utilize CPU instructions and get the best performance, the
(start) addresses of the memory blocks allocated for (the direct parts
of) values of a specified type must be aligned as multiples of an
integer N. Here N is called the alignment guarantee of that type.
Struct padding
To satisfy type alignment guarantee rules mentioned previously, Go
compilers may pad some bytes after certain fields of struct values.
The padded bytes are counted for struct sizes. So the size of a struct
type may be not a simple sum of the sizes of all its fields.
For example, the size of the struct type shown in the following code
is 24 on 64-bit architectures.
type T1 struct {
a int8
// 7 bytes are padded here
b int64
c int16
// 6 bytes are padded here.
}
type T2 struct {
a int8
// 1 byte is padded here
c int16
// 4 bytes are padded here.
b int64
}
package main
import "unsafe"
type T1 struct {
a int8
b int64
c int16
}
type T2 struct {
a int8
c int16
b int64
}
func main(){
// The printed values are got on
// 64-bit architectures.
println(unsafe.Sizeof(T1{})) // 24
println(unsafe.Sizeof(T2{})) // 16
}
What are small-size struct and array values? There is also not a
formal definition. The official standard Go compiler tweaks some
implementation details from version to version. However, in practice,
we can view struct types with no more than 4 native-word-size fields
and array types with no more than 4 native-word-size elements as
small-size values, such as struct{a, b, c, d int} , struct{element
For the official standard Go compiler 1.22 versions, a copy cost leap
happens between copying 9-element arrays and copying 10-element
arrays (the element size is one native word). The similar is for
copying 9-field structs and copying 10-field structs (each filed size is
one native word).
The proof:
package copycost
import "testing"
const N = 1024
type Element = uint64
This results indicate copying arrays with less than 10 elements and
structs with less than 10 fields might be specially optimized.
package copycost
import "testing"
//go:noinline
func Add5(x, y T5) (z T5) {
z.a = x.a + y.a
z.b = x.b + y.b
z.c = x.c + y.c
z.d = x.d + y.d
z.e = x.e + y.e
return
}
The following are several examples which show the costs of copying
some large-size values.
Example 1:
package copycost
import "testing"
const N = 1024
//go:noinline
func Sum_RangeArray(a [N]int) (r int) {
for _, v := range a {
r += v
}
return
}
//go:noinline
func Sum_RangeArrayPtr1(a *[N]int) (r int) {
for _, v := range *a {
r += v
}
return
}
//go:noinline
func Sum_RangeArrayPtr2(a *[N]int) (r int) {
for _, v := range a {
r += v
}
return
}
//go:noinline
func Sum_RangeSlice(a []int) (r int) {
for _, v := range a {
r += v
}
return
}
//===========
var r [128]int
because the array value is only copied once in calling this function.
The copy happens when range over the array.
Example 2:
package copycost
import "testing"
//go:noinline
func Sum_PlainForLoop(s []Element) (r int64) {
for i := 0; i < len(s); i++ {
r += s[i][0]
}
return
}
//go:noinline
func Sum_OneIterationVar(s []Element) (r int64) {
for i := range s {
r += s[i][0]
}
return
}
//go:noinline
func Sum_UseSecondIterationVar(s []Element) (r int64) {
for _, v := range s {
r += v[0]
}
return
}
//===================
var r [128]int64
Fig. 83
Fig. 84
Fig. 85
Fig. 88
Fig. 97
Fig. 98
Welcome to our website – the ideal destination for book lovers and
knowledge seekers. With a mission to inspire endlessly, we offer a
vast collection of books, ranging from classic literary works to
specialized publications, self-development books, and children's
literature. Each book is a new journey of discovery, expanding
knowledge and enriching the soul of the reade
Our website is not just a platform for buying books, but a bridge
connecting readers to the timeless values of culture and wisdom. With
an elegant, user-friendly interface and an intelligent search system,
we are committed to providing a quick and convenient shopping
experience. Additionally, our special promotions and home delivery
services ensure that you save time and fully enjoy the joy of reading.
textbookfull.com