好好爱自己!

sync.Pool is much slower than using channel, so why should we use sync.Pool?

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
package client
 
import (
    "runtime"
    "sync"
    "testing"
)
 
type MPool chan interface{}
 
 
type A struct {
    s        string
    b        int
    overflow *[2]*[]*string
 
}
 
var p = sync.Pool{
    New: func() interface{} { return new(A) },
}
 
var mp MPool = make(chan interface{}, 100)
 
func get() interface{} {
    select {
    case r := <-mp:
        return r
    default:
        return new(A)
    }
}
 
func put(a interface{}) {
    select {
    case mp <- a:
    default:
    }
    return
}
 
func pool() {
    a := p.Get()
    p.Put(a)
}
 
 
func init() {
    runtime.GOMAXPROCS(8)
}
 
func BenchmarkName(b *testing.B) {
    //for i := 0; i < 20; i++ {
    //    p.Put(new(A))
    //}
    //b.ResetTimer()
    //for i := 0; i < b.N; i++ {
    //    for i := 0; i < 100; i++ {
    //        go func() {
    //            p.Put(p.Get())
    //        }()
     //    }
     //}
     for i := 0; i < 20; i++ {
         p.Put(new(A))
     }
     b.ResetTimer()
     for i := 0; i < b.N; i++ {
         for i := 0; i < 100; i++ {
             p.Put(p.Get())
         }
     }
 }
  
 func BenchmarkNotPool(b *testing.B) {
     for i := 0; i < 20; i++ {
         put(new(A))
     }
     b.ResetTimer()
     for i := 0; i < b.N; i++ {
         for i := 0; i < 100; i++ {
             a := get()
             put(a)
         }
     }
 }
                                                            
                                                           

  测试结果:

 

 

 

原文:https://stackoverflow.com/questions/50851421/sync-pool-is-much-slower-than-using-channel-so-why-should-we-use-sync-pool

-----------------------------------------------------------------------

I read sync.Pool design, but find it is two logic, why we need localPool to solve lock compete. We can just use chan to implement one.

Using channel is 4x times faster than sync.pool!

Besides pool can clear object, what advantage does it have?

This is the pool implementation and benchmarking code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
package client
 
import (
    "runtime"
    "sync"
    "testing"
)
 
type MPool chan interface{}
 
 
type A struct {
    s        string
    b        int
    overflow *[2]*[]*string
 
}
 
var p = sync.Pool{
    New: func() interface{} { return new(A) },
}
 
var mp MPool = make(chan interface{}, 100)
 
func get() interface{} {
    select {
    case r := <-mp:
        return r
    default:
        return new(A)
    }
}
 
func put(a interface{}) {
    select {
    case mp <- a:
    default:
    }
    return
}
 
func pool() {
    a := p.Get()
    p.Put(a)
}
 
 
func init() {
    runtime.GOMAXPROCS(8)
}
 
func BenchmarkName(b *testing.B) {
    for i := 0; i < 20; i++ {
        p.Put(new(A))
    }
    b.ResetTimer()
    for i := 0; i < b.N; i++ {
        for i := 0; i < 100; i++ {
            go func() {
                p.Put(p.Get())
            }()
        }
    }
}
 
func BenchmarkNotPool(b *testing.B) {
    for i := 0; i < 20; i++ {
        put(new(A))
    }
    b.ResetTimer()
    for i := 0; i < b.N; i++ {
        for i := 0; i < 100; i++ {
            a := get()
            put(a)
        }
    }
}

  Answer:

You are not benchmarking the same thing, so you can't compare the results.

BenchmarkName() launches goroutines which have significant overheard and you don't even wait for those goroutines to finish, while BenchmarkNotPool() just gets and puts an object in the pool in the same goroutine.

If you modify BenchmarkName() to do the same, the benchmark results actually show it's the other way: sync.Pool is more than 3 times faster, which is true, so that's its use / advantage.

1
2
3
4
5
6
7
8
9
10
11
func BenchmarkName(b *testing.B) {
    for i := 0; i < 20; i++ {
        p.Put(new(A))
    }
    b.ResetTimer()
    for i := 0; i < b.N; i++ {
        for i := 0; i < 100; i++ {
            p.Put(p.Get())
        }
    }
}

  

Results:

BenchmarkName-8           500000              2453 ns/op
BenchmarkNotPool-8        200000              7984 ns/op

Also see related question: How to implement Memory Pooling in Golang

 

Thanks for your great answer, my bad, forgot to do go in BechmarkNotPool, but in adverse we proof use sync.Pool is better  Jun 14 '18 at 7:49 
  •  
    @petelin You can't just launch goroutines in the middle of the benchmark function, and not wait for them to complete. That will render the benchmark result completely useless. 
    – icza
     Jun 14 '18 at 8:09 
posted @   立志做一个好的程序员  阅读(89)  评论(0编辑  收藏  举报
编辑推荐:
· AI与.NET技术实操系列:基于图像分类模型对图像进行分类
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
阅读排行:
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· 25岁的心里话
· ollama系列01:轻松3步本地部署deepseek,普通电脑可用
· 按钮权限的设计及实现
历史上的今天:
2018-09-30 es6中的import,export浏览器已经支持
2016-09-30 Jquery 源码学习
2016-09-30 php 中的魔术方法-----“事件方法”
2016-09-30 php 语法中有 let 吗?

不断学习创作,与自己快乐相处

点击右上角即可分享
微信分享提示