自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

手擼一個對象池,你學會了嗎?

開發(fā) 前端
對象的池子,與線程池、內存池類似,減少頻繁創(chuàng)建和銷毀對象帶來的成本(特別是消耗資源較大的對象),可用于實現對象的緩存和復用。這也算是一種設計模式。

[[427113]]

 什么是對象池?

對象的池子,與線程池、內存池類似,減少頻繁創(chuàng)建和銷毀對象帶來的成本(特別是消耗資源較大的對象),可用于實現對象的緩存和復用。這也算是一種設計模式。

話不多說,直接上代碼:

  1. #include <algorithm> 
  2. #include <cassert> 
  3. #include <cmath> 
  4. #include <complex> 
  5. #include <iostream> 
  6. #include <memory> 
  7. #include <numeric
  8. #include <vector> 
  9.  
  10. struct A { 
  11.     A(std::string s) { str_ = std::move(s); } 
  12.  
  13.     void print() { std::cout << str_ << std::endl; } 
  14.  
  15.     std::string str_; 
  16. }; 
  17.  
  18. template <typename T, typename Allocator = std::allocator<T>> 
  19. class ObjectPool { 
  20.    public
  21.     ObjectPool() = default
  22.     ~ObjectPool() { 
  23.         assert(freeObjects_.size() == kInitChunkSize * (std::pow(2, pool_.size()) - 1)); 
  24.  
  25.         size_t chunkSize{kInitChunkSize}; 
  26.         for (auto* chunk : pool_) { 
  27.             allocator_.deallocate(chunk, chunkSize); 
  28.             chunkSize *= 2; 
  29.         } 
  30.         pool_.clear(); 
  31.     } 
  32.  
  33.     template <typename... Args> 
  34.     std::shared_ptr<T> acquireObject(Args... args) { 
  35.         if (freeObjects_.empty()) { 
  36.             addChunk(); 
  37.         } 
  38.  
  39.         T* object{freeObjects_.back()}; 
  40.  
  41.         new (object) T{std::forward<Args>(args)...}; 
  42.  
  43.         freeObjects_.pop_back(); 
  44.  
  45.         return std::shared_ptr<T>(object, [this](T* object) { 
  46.             std::_Destroy(object); 
  47.             freeObjects_.push_back(object); 
  48.         }); 
  49.     } 
  50.  
  51.    private: 
  52.     std::vector<T*> pool_; 
  53.  
  54.     std::vector<T*> freeObjects_; 
  55.  
  56.     static const size_t kInitChunkSize{5}; 
  57.  
  58.     size_t newChunkSize{kInitChunkSize}; 
  59.  
  60.     void addChunk() { 
  61.         std::cout << "add Chunk \n"
  62.  
  63.         auto* firstNewObject{allocator_.allocate(newChunkSize)}; 
  64.         pool_.push_back(firstNewObject); 
  65.  
  66.         auto oldFreeObjectSize{freeObjects_.size()}; 
  67.         freeObjects_.resize(oldFreeObjectSize + newChunkSize); 
  68.         std::iota(std::begin(freeObjects_) + oldFreeObjectSize, std::end(freeObjects_), firstNewObject); 
  69.  
  70.         newChunkSize *= 2; 
  71.     } 
  72.  
  73.     Allocator allocator_; 
  74. }; 
  75.  
  76. using APool = ObjectPool<A>; 
  77.  
  78. int main() { 
  79.     APool pool; 
  80.     for (int i = 0; i < 20; i++) { 
  81.         auto x = pool.acquireObject(std::string("hello")); 
  82.         x->print(); 
  83.     } 
  84.     return 0; 

上面的對象池實現在每次請求對象的時候都調用了構造函數和析構函數,這里大家可以根據實際情況自行選擇是否必要調用。如果構造和析構成本也比較高,可以再想辦法節(jié)省對應的開銷。

 

責任編輯:武曉燕 來源: 程序喵大人
相關推薦

2022-02-08 09:09:45

智能指針C++

2023-03-26 22:02:53

APMPR監(jiān)控

2024-06-21 08:15:25

2024-06-19 09:47:21

2023-09-19 08:03:50

rebase?merge

2023-04-27 08:42:50

效果

2024-04-01 08:13:59

排行榜MySQL持久化

2024-01-05 07:46:15

JS克隆對象JSON

2024-09-26 09:10:08

2023-01-10 08:43:15

定義DDD架構

2024-02-04 00:00:00

Effect數據組件

2023-07-26 13:11:21

ChatGPT平臺工具

2024-01-19 08:25:38

死鎖Java通信

2024-01-02 12:05:26

Java并發(fā)編程

2023-08-01 12:51:18

WebGPT機器學習模型

2023-11-27 07:33:55

2022-04-26 10:47:53

分配權限vuejs

2023-07-10 07:17:29

無效化空窗口

2021-10-27 06:49:34

線程池Core函數

2023-10-26 07:15:46

點贊
收藏

51CTO技術棧公眾號