optimization - Minimize memory allocations for go map -


i digging optimising highly used code in go. question boils down following code snippet (copied memory allocations pprof list command). can see allocations done in line map being filled (line 959).

routine ======================== cart.benchmarkmapwithoutcapacityfixval in /.../cart_test.go    3328966    3328966 (flat, cum) 15.50% of total          .          .    954:          .          .    955:func benchmarkmapwithoutcapacityfixval(b *testing.b) {          .          .    956:   := 0; < b.n; i++ {          .          .    957:       m := make(map[int]float32)          .          .    958:       k := 0; k < 10; k++ {    3328966    3328966    959:           m[k] = 0.434295723423          .          .    960:       }          .          .    961:   }          .          .    962:} 

here trying do: trying allocate memory before (inner) loop there no unnecessary allocations happening:

routine ======================== cart.benchmarkmapwithcapacityfixval in /.../cart_test.go    3214263    3214263 (flat, cum) 14.97% of total          .          .    963:          .          .    964:func benchmarkmapwithcapacityfixval(b *testing.b) {          .          .    965:   := 0; < b.n; i++ {    3048075    3048075    966:       m := make(map[int]float32, 10)          .          .    967:       k := 0; k < 10; k++ {     166188     166188    968:           m[k] = 0.434295723423          .          .    969:       }          .          .    970:   }          .          .    971:} 

why there still allocations happening in line 968 (second sample) , how can correctly allocate map before inner loop?

a map not array. can't preallocate space in because can't know in map elements inserted. in make(map..., x) x capacity hint, doesn't bound map , doesn't guarantee keys hash it. such best effort of minimizing number of future allocations, there's no way eliminate of them.

in particular example should use array rather map if want perfect control on allocations. proper array you'd have 1 allocation.


Comments

Popular posts from this blog

Failed to execute goal org.apache.maven.plugins:maven-surefire-plugin:2.12:test (default-test) on project.Error occurred in starting fork -

windows - Debug iNetMgr.exe unhandle exception System.Management.Automation.CmdletInvocationException -

configurationsection - activeMq-5.13.3 setup configurations for wildfly 10.0.0 -