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));