go的临时对象池--sync.Pool
Introduction - 1.71.0 https://www.boost.org/doc/libs/1_71_0/libs/pool/doc/html/boost_pool/pool/introduction.html
What is Pool?
Pool allocation is a memory allocation scheme that is very fast, but limited in its usage. For more information on pool allocation (also called simple segregated storage, see concepts concepts and Simple Segregated Storage).
Why should I use Pool?
Using Pools gives you more control over how memory is used in your program. For example, you could have a situation where you want to allocate a bunch of small objects at one point, and then reach a point in your program where none of them are needed any more. Using pool interfaces, you can choose to run their destructors or just drop them off into oblivion; the pool interface will guarantee that there are no system memory leaks.
When should I use Pool?
Pools are generally used when there is a lot of allocation and deallocation of small objects. Another common usage is the situation above, where many objects may be dropped out of memory.
In general, use Pools when you need a more efficient way to do unusual memory control.
Which pool allocator should I use?
pool_allocator
is a more general-purpose solution, geared towards efficiently servicing requests for any number of contiguous chunks.
fast_pool_allocator
is also a general-purpose solution but is geared towards efficiently servicing requests for one chunk at a time; it will work for contiguous chunks, but not as well as pool_allocator.
If you are seriously concerned about performance, use fast_pool_allocator
when dealing with containers such as std::list
, and use pool_allocator
when dealing with containers such as std::vector
.
深入浅出对象池(Object Pool) - 技术交流 - ITeye博客 https://www.iteye.com/blog/hulefei29-2018391
1.背景
对象池为了避免频繁创建耗时或耗资源的大对象,事先在对象池中创建好一定数量的大对象,然后尽量复用对象池中的对象,用户用完大对象之后放回对象池。
2.问题
目前纵观主流语言的实现方式无外乎3个步骤:
- 初始创建一定数量的对象池(也允许从外面添加对象)。
- 从对象池中取对象来使用。
- 用完之后返回对象池。
一般情况下这样是OK的,可能存在的问题是在第三步,有两个问题:
- 不方便,每次都需要显式回收对象。
- 忘记将对象放回对象池,造成资源浪费。
3.改进动机
解决显式回收的问题,实现自动回收,省心省力。
4.技术内幕
借助c++智能指针,因为智能指针可以自定义删除器,在智能指针释放的时候会调用删除器,在删除器中我们将用完的对象重新放回对象池。思路比较简单,但实现的时候需要考虑两个问题:
- 什么时候定义删除器?
- 用shared_ptr还是unique_ptr?
4.1什么时候定义删除器
自定义删除器只做一件事,就是将对象重新放入对象池。如果对象池初始化的时候就自定义删除器的话,删除器中的逻辑是将对象放回对象池,放回的时候无法再定义一个这样的删除器,所以这种做法行不通。
需要注意,回收的对象只能是默认删除器的。除了前述原因之外,另外一个原因是对象池释放的时候需要释放所有的智能指针,释放的时候如果存在自定义删除器将会导致对象无法删除。
只有在get的时候定义删除器才行,但是初始创建或加入的智能指针是默认删除器,所以我们需要把智能指针的默认删除器改为自定义删除器。
4.2用shared_ptr还是unique_ptr
因为我们需要把智能指针的默认删除器改为自定义删除器,用shared_ptr会很不方便,因为你无法直接将shared_ptr的删除器修改为自定义删除器,虽然你可以通过重新创建一个新对象,把原对象拷贝过来的做法来实现,
但是这样做效率比较低。而unique_ptr由于是独占语义,提供了一种简便的方法方法可以实现修改删除器,所以用unique_ptr是最适合的。