jeromefroe
jeromefroe

Reputation: 1395

Pooling Maps in Golang

I was curious if anyone has tried to pool maps in Go before? I've read about pooling buffers previously, and I was wondering if by similar reasoning it could make sense to pool maps if one has to create and destroy them frequently or if there was any reason why, a priori, it might not be efficient. When a map is returned to the pool, one would have to iterate through it and delete all elements, but it seems a popular recommendation is to create a new map instead of deleting the entries in a map which has already been allocated and reusing it which makes me think that pooling maps may not be as beneficial.

Upvotes: 4

Views: 3372

Answers (2)

Grzegorz Żur
Grzegorz Żur

Reputation: 49221

If your maps change (a lot) in size by deleting or adding entries this will cause new allocations and there will be no benefit of pooling them.

If your maps will not change in size but only the values of the keys will change then pooling will be a successful optimization.

This will work well when you read table-like structures, for instance CSV files or database tables. Each row will contain exactly the same columns, so you don't need to clear any entry.

The benchmark below shows no allocation when run with go test -benchmem -bench . to

package mappool

import "testing"

const SIZE = 1000000

func BenchmarkMap(b *testing.B) {
    m := make(map[int]int)

    for i := 0; i < SIZE; i++ {
        m[i] = i
    }

    b.ResetTimer()

    for i := 0; i < b.N; i++ {
        for i := 0; i < SIZE; i++ {
            m[i] = m[i] + 1
        }
    }
}

Upvotes: 5

Alex Nichol
Alex Nichol

Reputation: 7510

Like @Grzegorz Żur says, if your maps don't change in size very much, then pooling is helpful. To test this, I made a benchmark where pooling wins out. The output on my machine is:

Pool time: 115.977µs
No-pool time: 160.828µs

Benchmark code:

package main

import (
    "fmt"
    "math/rand"
    "time"
)

const BenchIters = 1000

func main() {
    pool := map[int]int{}
    poolTime := benchmark(func() {
        useMapForSomething(pool)

        // Return to pool by clearing the map.
        for key := range pool {
            delete(pool, key)
        }
    })

    nopoolTime := benchmark(func() {
        useMapForSomething(map[int]int{})
    })

    fmt.Println("Pool time:", poolTime)
    fmt.Println("No-pool time:", nopoolTime)
}

func useMapForSomething(m map[int]int) {
    for i := 0; i < 1000; i++ {
        m[rand.Intn(300)] += 5
    }
}

// benchmark measures how long f takes, on average.
func benchmark(f func()) time.Duration {
    start := time.Now().UnixNano()
    for i := 0; i < BenchIters; i++ {
        f()
    }
    return time.Nanosecond * time.Duration((time.Now().UnixNano()-start)/BenchIters)
}

Upvotes: 2

Related Questions