Hello nice people!
While benchmarking one of the functions I noticed that
```
result := make(map[string]float, SIZE) // SIZE >= N
result["key1"] = SOME_COMPUTED_ABOVE_VALUE
result["key2"] = SOME_COMPUTED_ABOVE_VALUE
// more keys here
result["keyN"] = SOME_COMPUTED_ABOVE_VALUE
return result
```
works twice as fast then
```
return map[string]float {
"key1": SOME_COMPUTED_ABOVE_VALUE,
"key2": SOME_COMPUTED_ABOVE_VALUE,
// more keys here
"keyN": SOME_COMPUTED_ABOVE_VALUE,
}
```
cause the latter results in calls to `hashGrows` if N is relatively big (I see this happening when N>=9)
I created small benchmarks to reproduce it.
See here
Do I understand correctly that creating a map using a literal is equivalent to creating an empty map and then filling it?
While one can compute minimal needed capacity to store the map literal I understand that actually allocating the map of this size may result in unexpected behavior (and definitely different from current one). Calculating at compile time what the hash table capacity is not possible cause it depends on env variables controlling that speed of hash tables growth.
At the same time I can imagine that compiler can emit a simple function which would compute the capacity at the runtime and allocate map of proper size.
So my question
1. Are my benchmarks correct?
2. If so is there a reason for this behavior?
Here are benchmarks
Small posts with my bench data