vanilladocs
vanillapunch.com
API / core/sharedbakecache

core/sharedbakecache

구운 그림 나눠 쓰기. (vanilla.js core/graphic/sharedbakecache.js, ShadowText, ShadowImage, SoftShade 가 쓴다)

  • 같은 모양(열쇠)이면 한 번만 굽고 여러 컴포넌트가 같은 그림(Rc)을 그린다. GPU 텍스처도 그림마다 하나라 함께 준다.
  • 원본은 열쇠마다 쓰는 컴포넌트 수를 세고 놓기(release)를 부른다. 러스트 판은 컴포넌트가 Rc 를 쥐는 것이 곧 참조 수다. 열쇠가 바뀌어 다른 것을 쥐거나 컴포넌트가 사라지면 Rc 가 놓인다. 캐시만 쥔 것(Rc::strong_count 가 1)이 아무도 쓰지 않는 것이다.
  • 아무도 쓰지 않는 것은 바로 버리지 않고 남겨 둔다. (같은 모양이 곧 다시 오면 굽지 않는다) 새로 담을 때 MAXIMUM_IDLE_BAKE_COUNT 를 넘었으면 오래 안 쓴 것부터 버린다.
  • 굽지 못한 것(None)은 담지 않는다. 엔진은 한 스레드에서 돌아 캐시는 스레드 하나의 전역이다.

Sharing baked images. (vanilla.js core/graphic/sharedbakecache.js, used by ShadowText, ShadowImage and SoftShade)

  • The same shape (key) is baked only once and several components draw the same image (Rc). The GPU texture is also one per image, so it is shared too.
  • The original counts the components using each key and calls release. In the Rust version, a component holding the Rc is itself the reference count. When the key changes and it holds something else, or the component goes away, the Rc is released. One held only by the cache (Rc::strong_count is 1) is one nobody uses.
  • Ones nobody uses are not discarded right away but kept. (if the same shape comes back soon, it is not baked again) When adding a new one, if MAXIMUM_IDLE_BAKE_COUNT is exceeded, the least recently used are discarded first.
  • Failed bakes (None) are not stored. The engine runs on one thread, so the cache is a global of that one thread.
예제Example같은 열쇠는 한 번만 굽고 같은 그림을 나눠 쓴다The same key is baked only once and shares the same image
use std::rc::Rc;
use vanilla::core::sharedbakecache::{acquire_shared_bake, read_shared_bake_count};

let mut bake_count = 0;
let first_bake = acquire_shared_bake("label|16px|Hello", || {
	bake_count += 1;
	Some(Rc::new(String::from("Hello")))
}).unwrap();
let second_bake = acquire_shared_bake("label|16px|Hello", || {
	bake_count += 1;
	Some(Rc::new(String::from("Hello")))
}).unwrap();
assert_eq!(bake_count, 1);
assert!(Rc::ptr_eq(&first_bake, &second_bake));
assert_eq!(read_shared_bake_count(), (1, 0));
drop(first_bake);
drop(second_bake);
assert_eq!(read_shared_bake_count(), (1, 1));

함수Functions

함수functionpub fn acquire_shared_bake<T: 'static>(bake_key: &str, create_bake: impl FnOnce() -> Option<Rc<T>>) -> Option<Rc<T>>#0.4.0 부터since 0.4.0소스source

구운 그림 쥐기. (열쇠에 구운 것이 있으면 그것을, 없으면 create_bake 로 구워 담고 준다, 원본 acquireSharedBake)

  • 돌려준 Rc 를 쥐고 있는 동안은 버리지 않는다. 열쇠가 같으면 종류(T)도 같아야 한다. (종류가 다르면 새로 굽고 바꿔 담는다)

Acquires a baked image. (returns the one baked for the key if there is one, otherwise bakes it with create_bake, stores and returns it, the original acquireSharedBake)

  • It is not discarded while the returned Rc is held. The same key must also have the same type (T). (if the type differs, it bakes a new one and replaces it)
bake_key&str
열쇠. (모양을 가르는 글, 같으면 같은 그림)Key. (text that distinguishes shapes, the same text means the same image)
create_bakeimpl FnOnce() -> Option<Rc<T>>
열쇠에 담긴 것이 없을 때만 부르는 굽기 함수. (None 을 돌려주면 담지 않는다)Bake function called only when nothing is stored for the key. (not stored if it returns None)

반환ReturnsOption<Rc<T>> 담긴 그림이나 새로 구운 그림. 굽기 함수가 None 을 돌려주면 None.The stored image or a newly baked image. None if the bake function returns None.

함수functionpub fn read_shared_bake_count() -> (usize, usize)#0.4.0 부터since 0.4.0소스source

지금 들고 있는 구운 그림 수와 그 가운데 아무도 쓰지 않는 수. (시험과 통계용, 원본 readSharedBakeCount)

Number of baked images held now and how many of them nobody uses. (for tests and statistics, the original readSharedBakeCount)

반환Returns(usize, usize) (담긴 수, 그 가운데 캐시만 쥔 수). (캐시만 쥔 것은 Rc::strong_count 가 1 인 것)(number stored, number of those held only by the cache). (held only by the cache means Rc::strong_count is 1)