Go Optimizations 101 (20220829) by Tapir Liu

Go Optimizations 101 (20220829) by Tapir Liu

Author:Tapir Liu
Language: eng
Format: epub


Structs

Avoid accessing fields of a struct in a loop though pointers to the struct

It is much faster to let CPU instructions process registers than process memory. So, to let the compiler generate less memory-processing assembly instructions for a loop, we should avoid accessing fields of a struct though pointers to the struct in the loop.

For example, in the following code, the function g is much performant than the function f.

package structs import "testing" const N = 1000 type T struct { x int } //go:noinline func f(t *T) { t.x = 0 for i := 0; i < N; i++ { t.x += i } } //go:noinline func g(t *T) { var x = 0 for i := 0; i < N; i++ { x += i } t.x = x } var t = &T{} func Benchmark_f(b *testing.B) { for i := 0; i < b.N; i++ { f(t) } } func Benchmark_g(b *testing.B) { for i := 0; i < b.N; i++ { g(t) } }

The benchmark results:

Benchmark_f-4 2402 ns/op Benchmark_g-4 461.3 ns/op

The function g uses a local variable x to store the sum value and assigns the sum value to the struct field in the end. The official standard Go compiler is smart enough to only generate register-processing assembly instructions for the loop of the function g.

The function f is actually equivalent to the function h declared below.

//go:noinline func h(t *T) { x := &t.x for i := 0; i < N; i++ { *x += i } }

So the suggestion introduced here is actually equivalent to the one introduced in the last chapter (avoid unnecessary pointer dereferences in a loop).



Download



Copyright Disclaimer:
This site does not store any files on its server. We only index and link to content provided by other sites. Please contact the content providers to delete copyright contents if any and email us, we'll remove relevant links or contents immediately.
Popular ebooks
Deep learning with TensorFlow and Keras by Derrick mwiti(818)
Chicken Soup for the Soul Presents Teens Talkin' Faith by Jack Canfield(649)
Understanding PDA Autism in Kids: A Guide for Parents and Teachers to Support Neurodiverse Learners by Jehu Len(555)
The Victorian Era: A Captivating Guide to the Life of Queen Victoria and an Era in the History of the United Kingdom Known for Its Hierarchy-Based Social Order by Captivating History(424)
Brain Teasers to Build Critical Thinking Skills by Safarova Kris(411)
Brain Teasers to Build Critical Thinking Skills: Brain Exercises for Tech, Banking, Case Interview Prep, and to Keep Your Mind Sharp by Kris Safarova(411)
100 Ideas for Secondary Teachers: Engaging Parents by Janet Goodall & Kathryn Weston(388)
Python 101 - Fundamentals by Sam(373)
Critical Curriculum Leadership : A Framework for Progressive Education by Rose M. Ylimaki(363)
Writing Solid Code: Development Philosophies for Writing Bug-Free Programs by Steve Maguire(357)
The Art of Emotional Validation: Improve Your Communication Skills and Transform Your Relationships by Validating Emotions and Feelings by Emily Wright(339)
Intersectionality in Educational Research by Dannielle Joy Davis; James L. Olive; Rachelle J. Brunn-Bevel; Susan R. Jones(331)
The Knights Templar: An Enthralling History of the Rise and Fall of the Most Influential Catholic Military Order by Wellman Billy(330)
A Beginner's Guide to SSD Firmware by Unknown(327)
The Future Knowledge Compendium by Ellyard Peter;(319)
How to be assertive in any situation by Hadfield Sue & Hasson Gill(310)
Making Connections in and Through Arts-Based Educational Research by Hala Mreiwed Mindy R. Carter Sara Hashem Candace H. Blake-Amarante(306)
Foundations of Educational Research by Victoria Elliott(305)
What Every Teacher Should Know about Learning, Memory, and the Brain by Tileston Donna E. Walker;(305)
Message from the Pleiades; The Contact Notes of Eduard Billy Meier v1 only by unknow(301)