@next2d/cache 3.15.0 → 3.16.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@next2d/cache",
3
- "version": "3.15.0",
3
+ "version": "3.16.0",
4
4
  "description": "Next2D Cache Package",
5
5
  "author": "Toshiyuki Ienaga<ienaga@next2d.app> (https://github.com/ienaga/)",
6
6
  "license": "MIT",
@@ -14,10 +14,11 @@ export const execute = (pool, object) => {
14
14
  }
15
15
  if ("canvas" in object) {
16
16
  const canvas = object.canvas;
17
- const width = canvas.width;
18
- const height = canvas.height;
19
- object.clearRect(0, 0, width + 1, height + 1);
20
17
  // canvas reset
18
+ // 大きさを設定するとキャンバスの内容と描画状態は初期化されるので、先に clearRect で
19
+ // 全面を消す必要は無い (消すと、捨てる直前のキャンバス全体をもう 1 度書き換えることになる)。
20
+ // Setting the size resets the bitmap and the drawing state, so clearing the whole
21
+ // canvas first is not needed (it would rewrite the entire canvas right before discarding it).
21
22
  canvas.width = canvas.height = 1;
22
23
  // pool
23
24
  pool.push(canvas);
@@ -0,0 +1,17 @@
1
+ import type { CacheStore } from "../../CacheStore";
2
+ /**
3
+ * @description LRU上限($maxStoreSize)を超えた分のキャッシュを、参照が古い順に削除する。
4
+ * 削除したIDは$removeIdsに積み、worker側のGPUリソース(アトラス領域等)も
5
+ * 解放されるようにする。描画情報をworkerに送ったあとに呼ぶこと。
6
+ * Removes caches beyond the LRU limit ($maxStoreSize), least recently used
7
+ * first. Removed IDs are pushed to $removeIds so that the worker-side GPU
8
+ * resources (atlas regions etc.) are released as well. Call this after the
9
+ * render queue has been posted to the worker.
10
+ *
11
+ * @param {CacheStore} cache_store
12
+ * @param {Map} data_store
13
+ * @return {void}
14
+ * @method
15
+ * @protected
16
+ */
17
+ export declare const execute: (cache_store: CacheStore, data_store: Map<string, Map<string, any>>) => void;
@@ -0,0 +1,26 @@
1
+ /**
2
+ * @description LRU上限($maxStoreSize)を超えた分のキャッシュを、参照が古い順に削除する。
3
+ * 削除したIDは$removeIdsに積み、worker側のGPUリソース(アトラス領域等)も
4
+ * 解放されるようにする。描画情報をworkerに送ったあとに呼ぶこと。
5
+ * Removes caches beyond the LRU limit ($maxStoreSize), least recently used
6
+ * first. Removed IDs are pushed to $removeIds so that the worker-side GPU
7
+ * resources (atlas regions etc.) are released as well. Call this after the
8
+ * render queue has been posted to the worker.
9
+ *
10
+ * @param {CacheStore} cache_store
11
+ * @param {Map} data_store
12
+ * @return {void}
13
+ * @method
14
+ * @protected
15
+ */
16
+ export const execute = (cache_store, data_store) => {
17
+ cache_store.$overLimit = false;
18
+ if (cache_store.$maxStoreSize <= 0) {
19
+ return;
20
+ }
21
+ while (data_store.size > cache_store.$maxStoreSize) {
22
+ const oldestId = data_store.keys().next().value;
23
+ cache_store.removeById(oldestId);
24
+ cache_store.$removeIds.push(+oldestId);
25
+ }
26
+ };
@@ -31,21 +31,18 @@ export const execute = (cache_store, data_store, unique_key, key, value = null)
31
31
  }
32
32
  // set cache
33
33
  data.set(key, value);
34
- // LRU: 上限が設定されている場合(メインスレッドのopt-in)、
35
- // 触れたIDを最新に更新し、超過分を古い順にevictする。
36
- // evictは既存のタイマー削除と同じ経路(removeById + $removeIds)で
37
- // worker側のGPUリソースも解放される。上限0(デフォルト)では従来挙動のまま。
34
+ // LRU: 上限が設定されている場合(メインスレッドのopt-in)、触れたIDを最新に更新する。
35
+ // 上限を超えていても、ここでは削除しない。描画情報の生成中に削除すると、削除したIDを
36
+ // 同じ生成の中で作り直すことがあり、その場合 worker には「作り直しの描画」のあとに
37
+ // 「削除」が届いて、作り直したキャッシュが消える(メイン側はキャッシュ済みと認識した
38
+ // ままなので、以後その DisplayObject が描画されなくなる)。
39
+ // 超過分の削除は、描画情報を worker に送ったあとで evictOverLimit() が行う。
40
+ // 上限0(デフォルト)では従来挙動のまま。
38
41
  if (cache_store.$maxStoreSize > 0) {
39
42
  data_store.delete(unique_key);
40
43
  data_store.set(unique_key, data);
41
- while (data_store.size > cache_store.$maxStoreSize) {
42
- const oldestId = data_store.keys().next().value;
43
- // 触れたばかりのIDは削除しない(上限1未満の設定に対する安全弁)
44
- if (oldestId === unique_key) {
45
- break;
46
- }
47
- cache_store.removeById(oldestId);
48
- cache_store.$removeIds.push(+oldestId);
44
+ if (data_store.size > cache_store.$maxStoreSize) {
45
+ cache_store.$overLimit = true;
49
46
  }
50
47
  }
51
48
  };
@@ -69,6 +69,19 @@ export declare class CacheStore {
69
69
  * @public
70
70
  */
71
71
  $maxStoreSize: number;
72
+ /**
73
+ * @description エントリ数がLRU上限($maxStoreSize)を超えているかどうか。
74
+ * 毎フレーム参照されるため、関数を介さず直接参照する。
75
+ * trueのとき、描画情報をworkerに送ったあとで evictOverLimit() を呼ぶ。
76
+ * Whether the number of entries exceeds the LRU limit ($maxStoreSize).
77
+ * Read directly (not through a function) because it is checked every frame.
78
+ * When true, call evictOverLimit() after posting the render queue.
79
+ *
80
+ * @type {boolean}
81
+ * @default false
82
+ * @public
83
+ */
84
+ $overLimit: boolean;
72
85
  /**
73
86
  * @constructor
74
87
  */
@@ -141,6 +154,17 @@ export declare class CacheStore {
141
154
  * @public
142
155
  */
143
156
  removeTimerScheduledCache(): void;
157
+ /**
158
+ * @description LRU上限を超えた分のキャッシュを、参照が古い順に削除する。
159
+ * 描画情報をworkerに送ったあとに呼ぶこと。
160
+ * Removes caches beyond the LRU limit, least recently used first.
161
+ * Call this after the render queue has been posted to the worker.
162
+ *
163
+ * @returns {void}
164
+ * @method
165
+ * @public
166
+ */
167
+ evictOverLimit(): void;
144
168
  /**
145
169
  * @description 指定IDのキャッシュを削除する
146
170
  * Delete the cache for the specified ID
package/src/CacheStore.js CHANGED
@@ -10,6 +10,7 @@ import { execute as cacheStoreGenerateFilterKeysService } from "./CacheStore/ser
10
10
  import { execute as cacheStoreRemoveTimerService } from "./CacheStore/service/CacheStoreRemoveTimerService";
11
11
  import { execute as cacheStoreRemoveTimerScheduledCacheService } from "./CacheStore/service/CacheStoreRemoveTimerScheduledCacheService";
12
12
  import { execute as cacheStoreCancelRemoveTimerService } from "./CacheStore/service/CacheStoreCancelRemoveTimerService";
13
+ import { execute as cacheStoreEvictOverLimitService } from "./CacheStore/service/CacheStoreEvictOverLimitService";
13
14
  /**
14
15
  * @description キャッシュ管理クラス
15
16
  * Cache management class
@@ -22,104 +23,6 @@ export class CacheStore {
22
23
  * @constructor
23
24
  */
24
25
  constructor() {
25
- /**
26
- * @description キャッシュプール
27
- * Cache pool
28
- *
29
- * @type {HTMLCanvasElement[]}
30
- * @private
31
- */
32
- Object.defineProperty(this, "_$pool", {
33
- enumerable: true,
34
- configurable: true,
35
- writable: true,
36
- value: void 0
37
- });
38
- /**
39
- * @description キャッシュストア
40
- * Cache store
41
- *
42
- * @type {Map<string, any>}
43
- * @private
44
- */
45
- Object.defineProperty(this, "_$store", {
46
- enumerable: true,
47
- configurable: true,
48
- writable: true,
49
- value: void 0
50
- });
51
- /**
52
- * @description キャッシュトラッシュ
53
- * Cache trash
54
- *
55
- * @type {Map<string, any>}
56
- * @private
57
- */
58
- Object.defineProperty(this, "_$trash", {
59
- enumerable: true,
60
- configurable: true,
61
- writable: true,
62
- value: void 0
63
- });
64
- /**
65
- * @description キャッシュ削除用のタイマーID
66
- * Timer ID for cache deletion
67
- *
68
- * @type {number | null}
69
- * @public
70
- */
71
- Object.defineProperty(this, "$timerId", {
72
- enumerable: true,
73
- configurable: true,
74
- writable: true,
75
- value: void 0
76
- });
77
- /**
78
- * @description キャッシュタイマーの削除フラグ
79
- * Deletion flag of cache timer
80
- *
81
- * @type {boolean}
82
- * @default false
83
- * @public
84
- */
85
- Object.defineProperty(this, "$removeCache", {
86
- enumerable: true,
87
- configurable: true,
88
- writable: true,
89
- value: void 0
90
- });
91
- /**
92
- * @description キャッシュ削除用のIDリスト
93
- * ID list for cache deletion
94
- *
95
- * @type {number[]}
96
- * @public
97
- */
98
- Object.defineProperty(this, "$removeIds", {
99
- enumerable: true,
100
- configurable: true,
101
- writable: true,
102
- value: void 0
103
- });
104
- /**
105
- * @description キャッシュストアの最大エントリ数(LRU)。0は無制限(デフォルト・従来挙動)。
106
- * 上限はメインスレッド側でのみ設定し、evictしたIDは$removeIds経由で
107
- * worker側のGPUリソース(アトラスノード等)も解放される。
108
- * Maximum number of cache entries (LRU). 0 means unlimited
109
- * (default, legacy behavior). Set only on the main thread;
110
- * evicted IDs are propagated to the worker via $removeIds so
111
- * GPU resources (atlas nodes etc.) are released as well.
112
- *
113
- * @type {number}
114
- * @default 0
115
- * @public
116
- */
117
- Object.defineProperty(this, "$maxStoreSize", {
118
- enumerable: true,
119
- configurable: true,
120
- writable: true,
121
- value: void 0
122
- });
123
26
  this._$pool = [];
124
27
  this._$store = new Map();
125
28
  this._$trash = new Map();
@@ -127,6 +30,7 @@ export class CacheStore {
127
30
  this.$removeIds = [];
128
31
  this.$removeCache = false;
129
32
  this.$maxStoreSize = 0;
33
+ this.$overLimit = false;
130
34
  }
131
35
  /**
132
36
  * @description 登録された全てのキャッシュをリセット・破棄する
@@ -210,6 +114,19 @@ export class CacheStore {
210
114
  removeTimerScheduledCache() {
211
115
  cacheStoreRemoveTimerScheduledCacheService(this, this._$trash);
212
116
  }
117
+ /**
118
+ * @description LRU上限を超えた分のキャッシュを、参照が古い順に削除する。
119
+ * 描画情報をworkerに送ったあとに呼ぶこと。
120
+ * Removes caches beyond the LRU limit, least recently used first.
121
+ * Call this after the render queue has been posted to the worker.
122
+ *
123
+ * @returns {void}
124
+ * @method
125
+ * @public
126
+ */
127
+ evictOverLimit() {
128
+ cacheStoreEvictOverLimitService(this, this._$store);
129
+ }
213
130
  /**
214
131
  * @description 指定IDのキャッシュを削除する
215
132
  * Delete the cache for the specified ID