In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article shows you how to deeply analyze the implementation of C++ object pool automatic collection technology, the content is concise and easy to understand, absolutely can make your eyes bright, through the detailed introduction of this article, I hope you can get something.
Object pooling can significantly improve performance, and it can be very inefficient if an object is created frequently and is time-consuming or expensive. Object pooling avoids repeated object creation by means of object reuse. It will first create a certain number of objects and put them in the pool. When users need to create objects, they can get them directly from the object pool, and then put them back into the object pool for reuse. This approach avoids the repeated creation of time-consuming or resource-consuming large objects and greatly improves the performance of the program. The editor will discuss the technical characteristics and source code implementation of object pooling.
Object pool class diagram
ObjectPool: manages the pool of the object instance.
Client: user.
Applicability:
Instances of the class can be reused.
The process of instantiating a class is expensive.
Classes are instantiated more frequently.
Effect:
Saves the overhead of creating class instances.
Saves time in creating class instances.
The storage space increases with the increase of objects.
problem
At present, there are no more than three steps in the implementation of mainstream languages:
Initially create a certain number of object pools (objects are also allowed to be added from the outside).
Fetch objects from the object pool for use.
Return to the object pool after use.
In general, this is OK. The possible problem is that in the third step, there are two problems:
Inconvenient, you need to explicitly recycle objects every time.
Forget to put the object back into the object pool, resulting in a waste of resources.
Improvement motivation
Solve the problem of explicit recycling, realize automatic recycling, save worry and effort. The improved object pool does not need to provide the release method, and the object will be automatically recycled. The improved class diagram is as follows.
Technical insider
Because the smart pointer can customize the deleter, the deleter will be called when the smart pointer is released, and we will put the used objects back into the object pool in the deleter. The idea is relatively simple, but there are two issues to consider when implementing:
When will the deleter be defined?
Shared_ptr or unique_ptr?
1. When to define the deleter
The custom deleter only does one thing, which is to put the object back into the object pool. If the deleter is customized when the object pool is initialized, the logic in the deleter is to put the object back into the object pool, and there is no way to define such an deleter when it is put back, so this will not work. It is important to note that recycled objects can only be deleted by default. In addition to the above reasons, another reason is that all smart pointers need to be released when the object pool is released, and if there is a custom deleter at the time of release, the object cannot be deleted. The deleter can only be defined when get, but the smart pointer created or added initially is the default deleter, so we need to change the default deleter of the smart pointer to a custom deleter.
1.2 using shared_ptr or unique_ptr
Because we need to change the default deleter of the smart pointer to a custom deleter, it will be very inconvenient to use shared_ptr, because you cannot directly modify the shared_ptr deleter into a custom deleter, although you can recreate a new object and copy the original object to achieve, but this is relatively inefficient. Because unique_ptr is exclusive semantics, it provides a simple way to implement the modification deleter, so unique_ptr is the most suitable.
1.3 implementation source code
# pragma once # include # include # include template class SimpleObjectPool {public: using DeleterType = std::function; void add (std::unique_ptr t) {pool_.push_back (std::move (t));} std::unique_ptr get () {if (pool_.empty ()) {throw std::logic_error ("no more object") } / / every time add custom deleter for default unique_ptr std::unique_ptr ptr (pool_.back () .release (), [this] (T* t) {pool_.push_back (std::unique_ptr (t));}); pool_.pop_back (); return std::move (ptr) } bool empty () const {return pool_.empty ();} size_t size () const {return pool_.size ();} private: std::vector pool_;}; / / test code void test_object_pool () {SimpleObjectPool p; p.add (std::unique_ptr (new A () P.add (std::unique_ptr (new A (); {auto t = p.get (); p.get ();} {p.get (); p.get ();} std::cout
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.