Maybe I'm still asleep, but can anyone who read this explain this part -
map[string]*int instead of map[string]int so we only have to allocate once per unique word.
map[string]*int instead of map[string]int so we only have to allocate once per unique word.
1. Hash word, retrieve the value from the counts map
2. Increment the value
3. Hash word, store the value into the counts map
By change counts to a map of strings to pointers of ints, you only need to perform #3 once per unique word. Once you've read the int pointer, you need only write to the pointer and not re-hash the word and write that to the map, which can be expensive.https://github.com/benhoyt/countwords/blob/master/optimized....