base/objectpool
오브젝트 풀. (원본 base/objectpool.js)
- 매 프레임 생성 / 해제되는 객체(탄환, 이펙트 등)를 재사용해 할당을 줄인다. (원본은 GC 압박을 줄이려고 만들었다)
- acquire() 로 빌리고 release() 로 되돌린다. 되돌릴 때 reset_handler 가 상태를 비운다.
- 러스트 판은 담는 타입을 T 로 고정한 제네릭이다. acquire 는 객체를 꺼내 넘겨주고(소유권 이동), release 는 다시 받는다. 원본 release(null) 무시는 러스트에 null 이 없어서 뺐다.
- clear() 는 원본처럼 놀고 있는 목록만 비우고, 지금까지 만든 수(total_count)는 그대로 둔다.
Object pool. (the original base/objectpool.js)
- Reuses objects created / freed every frame (bullets, effects and such) to reduce allocations. (the original was made to reduce GC pressure)
- Borrow with acquire() and return with release(). On return, reset_handler clears the state.
- The Rust version is a generic with the held type fixed as T. acquire takes an object out and hands it over (ownership moves), and release takes it back. The original's ignoring of release(null) was dropped because Rust has no null.
- clear() empties only the idle list as in the original, and leaves the number created so far (total_count) as is.
ObjectCreateHandlertype
새 객체를 만드는 함수.
Function that creates a new object.
pub type ObjectCreateHandler<T> = Box<dyn FnMut() -> T>ObjectResetHandlertype
되돌릴 때 상태를 비우는 함수. (객체를 인자로 받는다)
Function that clears the state on release. (receives the object as an argument)
pub type ObjectResetHandler<T> = Box<dyn FnMut(&mut T)>ObjectPoolstruct
오브젝트 풀. (T 객체를 빌려주고 되돌려 받아 다시 쓴다)
Object pool. (lends out T objects, takes them back and reuses them)
use vanilla::ObjectPool;
struct Bullet {
travel_distance: f64,
}
let mut object_pool = ObjectPool::new(
Box::new(|| Bullet { travel_distance: 0.0 }),
Some(Box::new(|bullet: &mut Bullet| {
bullet.travel_distance = 0.0;
})),
2,
);
assert_eq!(object_pool.get_free_count(), 2);
let mut bullet = object_pool.acquire();
bullet.travel_distance = 120.0;
object_pool.release(bullet);
let reused_bullet = object_pool.acquire();
assert_eq!(reused_bullet.travel_distance, 0.0);
assert_eq!(object_pool.get_total_count(), 2);pub fn new(create_handler: ObjectCreateHandler<T>, reset_handler: Option<ObjectResetHandler<T>>, preload_count: usize) -> ObjectPool<T>#생성. (create_handler 는 새 객체를 만들고, reset_handler 는 되돌릴 때 상태를 비우고, preload_count 만큼 미리 만든다)
Constructor. (create_handler creates a new object, reset_handler clears the state on return, and preload_count objects are created in advance)
- create_handlerObjectCreateHandler<T>
- 새 객체를 만드는 함수. (미리 만들 때와 빌릴 때 놀고 있는 것이 없으면 부른다)Function that creates a new object. (called when preloading, and when borrowing with nothing idle)
- reset_handlerOption<ObjectResetHandler<T>>
- 되돌릴 때 상태를 비우는 함수. None 이면 비우지 않고 그대로 넣는다.Function that clears the state on return. If None, the object is put back as is without clearing.
- preload_countusize
- 만들 때 미리 만들어 둘 객체 수.Number of objects to create in advance at creation.
반환ReturnsObjectPool<T> 놀고 있는 객체 preload_count 개를 든 풀. (전체 수도 preload_count)A pool holding preload_count idle objects. (the total count is also preload_count)
짝Counterpart: vanilla.js ObjectPool.constructor
빌린다. (놀고 있는 것이 없으면 새로 만든다, 가장 나중에 되돌린 것부터 나간다)
Borrows. (creates a new one if nothing is idle; the most recently returned goes out first)
반환ReturnsT 빌린 객체. (소유권이 넘어온다, 다 쓰면 release 로 되돌린다)The borrowed object. (ownership is handed over; return it with release when done)
짝Counterpart: vanilla.js ObjectPool.acquire
되돌린다. (reset_handler 로 상태를 비운 뒤 놀고 있는 목록에 넣는다)
Returns. (clears the state with reset_handler, then puts it in the idle list)
- instanceT
- 되돌릴 객체. (이 풀에서 빌린 것인지 보지 않는다, 전체 수는 바꾸지 않는다)Object to return. (does not check whether it was borrowed from this pool, does not change the total count)
짝Counterpart: vanilla.js ObjectPool.release
놀고 있는 객체 수 반환.
Returns the number of idle objects.
반환Returnsusize 놀고 있는 목록의 객체 수.Number of objects in the idle list.
짝Counterpart: vanilla.js ObjectPool.getFreeCount
지금까지 만든 전체 객체 수 반환.
Returns the total number of objects created so far.
반환Returnsusize create_handler 로 만든 객체 수. (clear 해도 줄지 않는다)Number of objects created with create_handler. (does not decrease on clear)
짝Counterpart: vanilla.js ObjectPool.getTotalCount
풀을 비운다. (놀고 있는 목록만 비운다)
Empties the pool. (empties only the idle list)
짝Counterpart: vanilla.js ObjectPool.clear