-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathobject_pool.py
More file actions
43 lines (34 loc) · 1.48 KB
/
object_pool.py
File metadata and controls
43 lines (34 loc) · 1.48 KB
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
from collections import deque
class ObjectPool:
# プールサイズのデフォルト値は本PJにおける一般的な利用シーンを想定.
# bullet など必要数が多くなりそうな場面で明示的に設定する.
def __init__(self, instance_factory, init_size=16, max_size=64):
# list だと append は O(1) だが remove は O(n) なので、追加と削除が両方O(1)の queue を使う
self.not_used = deque()
self.instance_factory = instance_factory
self.current_size = 0
self._max_size = max_size
for _ in range(min(max_size, init_size)):
instance = self.instance_factory()
self.not_used.append(instance)
self.current_size += 1
def rent(self):
if len(self.not_used) == 0:
self.extends()
if len(self.not_used) == 0:
return None
instance = self.not_used.pop()
return instance
def back(self, instance):
self.not_used.append(instance)
def extends(self):
current_size = self.current_size
rest_of_increase = self._max_size - current_size
increase_length = min(current_size, rest_of_increase) # 長さを二倍にする
if increase_length == 0:
return
for _ in range(increase_length):
instance = self.instance_factory()
self.not_used.append(instance)
self.current_size += 1
# print(f"pool extended: {self.current_size}")