Go - Performance - What's the difference between pointer and value in struct? -
given following struct:
type exp struct { foo int, bar *int }
what difference in term of performance when using pointer or value in struct. there overhead or 2 schools of go programming?
i use pointers implement chained struct case have use pointers in struct in order gain performance?
ps: in above struct talk simple int other type (even custom one)
use form functionally useful program. basically, means if it's useful value nil
, use pointer.
from performance perspective, primitive numeric types more efficient copy dereference pointer. more complex data structures still faster copy if smaller cache line or 2 (under 128 bytes rule of thumb x86 cpus).
when things little larger, need benchmark if performance concerns you. cpus very efficient @ copying data, , there many variables involved determine locality , cache friendliness of data, depends on program's behavior, , hardware you're using.
this excellent series of articles if want better understand how memory , software interact: "what every programmer should know memory".
in short, tell people choose pointer or not based on logic of program, , worry performance later.
- use pointer if need pass modified.
- use pointer if need determine if unset/nil.
- use pointer if using type has methods pointer receivers.
Comments
Post a Comment