@next2d/cache 2.13.1 → 3.0.1

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/README.md CHANGED
@@ -1,11 +1,226 @@
1
1
  @next2d/cache
2
2
  =============
3
3
 
4
- ## Installation
4
+ **Important**: `@next2d/cache` prohibits importing other packages. This package is a foundational module that must remain independent to avoid circular dependencies.
5
+
6
+ **重要**: `@next2d/cache` は他の packages の import を禁止しています。このパッケージは基盤モジュールであり、循環依存を避けるために独立を維持する必要があります。
7
+
8
+ ---
9
+
10
+ A package for managing rendering cache in Next2D Player. It caches position information on atlas textures corresponding to DisplayObject rendering results, improving performance during re-rendering.
11
+
12
+ Next2Dプレイヤーのレンダリングキャッシュを管理するパッケージです。DisplayObjectの描画結果に対応するアトラステクスチャ上の位置情報をキャッシュし、再描画時のパフォーマンスを向上させます。
13
+
14
+ ## Overview / 概要
15
+
16
+ `@next2d/cache` optimizes rendering performance by caching DisplayObject rendering results as position information on atlas textures (`Node` from `@next2d/texture-packer`), enabling reuse of identical rendering content.
17
+
18
+ `@next2d/cache`は、DisplayObjectの描画結果をアトラステクスチャ上の位置情報(`@next2d/texture-packer`の`Node`)としてキャッシュし、同じ描画内容を再利用することでレンダリングパフォーマンスを最適化します。
19
+
20
+ Cache is managed on both the main thread and the rendering thread (Worker):
21
+
22
+ メインスレッドと描画スレッド(Worker)の両方でキャッシュを管理しています:
23
+
24
+ - **Main Thread / メインスレッド**: Manages cache existence with boolean values (sets `true` as value) using the same key
25
+ - **Worker Thread (Rendering Thread) / Workerスレッド(描画スレッド)**: Caches `Node` objects from `@next2d/texture-packer`. `Node` contains rectangle coordinate information (index, x, y, w, h) drawn on atlas textures, which is used during Instanced Array rendering to sample the correct region from the atlas texture.
26
+
27
+ Cache keys are generated from scale, alpha values, and filter parameters, detecting changes in transformation matrices and filters to update the cache appropriately.
28
+
29
+ キャッシュキーはスケール、アルファ値、フィルターパラメータから生成され、変換行列やフィルターの変更を検知して適切にキャッシュを更新します。
30
+
31
+ ### How Caching Works / キャッシュの仕組み
32
+
33
+ 1. **Atlas Registration of Rendering Results / 描画結果のアトラス登録**: DisplayObject rendering results are stored in atlas textures
34
+ 2. **Node Information Caching / Node情報のキャッシュ**: Rectangle position (x, y, width, height) and index on atlas texture are cached as `Node`
35
+ 3. **Usage in Instanced Rendering / Instanced描画での利用**: Cached `Node` coordinate information is used for efficient Instanced Array rendering
36
+
37
+ ## Directory Structure / ディレクトリ構造
38
+
39
+ ```
40
+ src/
41
+ ├── index.ts # Export definitions / エクスポート定義
42
+ ├── CacheStore.ts # Main cache management class / キャッシュ管理メインクラス
43
+ ├── CacheUtil.ts # Utility functions / ユーティリティ関数
44
+ └── CacheStore/
45
+ └── service/
46
+ ├── CacheStoreDestroyService.ts # Cache destruction / キャッシュ破棄
47
+ ├── CacheStoreGenerateFilterKeysService.ts # Filter key generation / フィルターキー生成
48
+ ├── CacheStoreGenerateKeysService.ts # Cache key generation / キャッシュキー生成
49
+ ├── CacheStoreGetService.ts # Cache retrieval / キャッシュ取得
50
+ ├── CacheStoreHasService.ts # Cache existence check / キャッシュ存在確認
51
+ ├── CacheStoreRemoveByIdService.ts # Delete by ID / ID指定削除
52
+ ├── CacheStoreRemoveService.ts # Cache deletion / キャッシュ削除
53
+ ├── CacheStoreRemoveTimerScheduledCacheService.ts # Timer deletion execution / タイマー削除実行
54
+ ├── CacheStoreRemoveTimerService.ts # Timer deletion registration / タイマー削除登録
55
+ ├── CacheStoreResetService.ts # Reset all caches / 全キャッシュリセット
56
+ └── CacheStoreSetService.ts # Cache storage / キャッシュ保存
57
+ ```
58
+
59
+ ## Key Components / 主要コンポーネント
60
+
61
+ ### CacheStore
62
+ The central class for caching, providing the following features:
63
+
64
+ キャッシュの中心となるクラスで、以下の機能を提供します:
65
+
66
+ - **Cache Store / キャッシュストア**: Map storing `Node` data with unique_key and cache key pairs
67
+ - **Cache Trash / キャッシュトラッシュ**: Temporary storage for caches scheduled for deletion
68
+ - **Timer Control / タイマー制御**: Cache lifecycle management through delayed deletion
69
+ - **Canvas Pool / Canvasプール**: Reuse pool for temporary rendering HTMLCanvasElements
70
+
71
+ ### Node (@next2d/texture-packer)
72
+ The actual cached data entity, containing the following information:
73
+
74
+ キャッシュされるデータの実体で、以下の情報を持ちます:
75
+
76
+ - `index`: Atlas texture identification number / アトラステクスチャの識別番号
77
+ - `x`, `y`: Rectangle x,y coordinates on atlas texture / アトラステクスチャ上の矩形のx,y座標
78
+ - `w`, `h`: Rectangle width and height / 矩形の幅と高さ
79
+
80
+ ## Data Flow / データフロー
81
+
82
+ ```mermaid
83
+ sequenceDiagram
84
+ participant DO as DisplayObject
85
+ participant CS as CacheStore
86
+ participant Atlas as AtlasManager
87
+ participant Store as Cache Store (Node)
88
+
89
+ DO->>CS: generateKeys(scale, alpha)
90
+ CS-->>DO: cacheKey
91
+ DO->>CS: has(uniqueKey, cacheKey)
92
+ alt Cache exists / キャッシュあり
93
+ CS->>Store: get(uniqueKey, cacheKey)
94
+ Store-->>DO: cached Node (x, y, w, h, index)
95
+ Note over DO: Render with Instanced Array<br/>Sample atlas using Node coordinates<br/>Instanced Arrayで描画<br/>Node座標でアトラスをサンプリング
96
+ else Cache not found / キャッシュなし
97
+ Note over DO: New rendering process<br/>新規描画処理
98
+ DO->>Atlas: Register rendering result to atlas<br/>アトラスに描画結果を登録
99
+ Atlas-->>DO: Node (coordinate info / 座標情報)
100
+ DO->>CS: set(uniqueKey, cacheKey, Node)
101
+ CS->>Store: store Node
102
+ end
103
+ ```
104
+
105
+ ## Cache and Instanced Rendering / キャッシュとインスタンス描画
106
+
107
+ ```mermaid
108
+ flowchart TD
109
+ subgraph "Cache Registration Flow / キャッシュ登録フロー"
110
+ A[DisplayObject Rendering<br/>DisplayObject描画] --> B[Draw to Atlas Texture<br/>アトラステクスチャに描画]
111
+ B --> C[Get Node<br/>index, x, y, w, h<br/>Node取得]
112
+ C --> D[Save Node to CacheStore<br/>CacheStoreにNodeを保存]
113
+ end
114
+
115
+ subgraph "Cache Usage Flow / キャッシュ利用フロー"
116
+ E[DisplayObject Re-render<br/>DisplayObject再描画] --> F{Cache exists?<br/>キャッシュ存在?}
117
+ F -->|Yes| G[Get Node<br/>Nodeを取得]
118
+ G --> H[Instanced Array Rendering<br/>Instanced Array描画]
119
+ H --> I[Sample Atlas with Node coordinates<br/>Node座標でアトラスをサンプリング]
120
+ F -->|No| A
121
+ end
122
+
123
+ subgraph "Instanced Array Data / Instanced Array データ"
124
+ J["Instance Data:<br/>- rect (Node.x, Node.y, Node.w, Node.h)<br/>- atlas index (Node.index)<br/>- transform matrix<br/>- color transform"]
125
+ end
126
+
127
+ I --> J
128
+ ```
129
+
130
+ ## Cache Lifecycle / キャッシュライフサイクル
131
+
132
+ ```mermaid
133
+ flowchart TD
134
+ A[DisplayObject Render Request<br/>DisplayObject描画リクエスト] --> B{Cache exists?<br/>キャッシュ存在?}
135
+ B -->|Yes| C[Get Node from Cache<br/>キャッシュからNode取得]
136
+ B -->|No| D[New Rendering & Atlas Registration<br/>新規描画・アトラス登録]
137
+ D --> E[Save Node info to Cache<br/>Node情報をキャッシュに保存]
138
+ C --> G[Render with Instanced Array<br/>Instanced Arrayで描画]
139
+ E --> G
140
+ ```
141
+
142
+ ### Deletion Flow (Delayed Deletion Mechanism) / 削除フロー(遅延削除メカニズム)
143
+
144
+ Timer-based delayed deletion reduces re-rendering costs for temporarily hidden objects.
145
+
146
+ タイマーによる遅延削除で、一時的に非表示になったオブジェクトの再描画コストを削減します。
147
+
148
+ ```mermaid
149
+ flowchart TD
150
+ H[DisplayObject Deletion Request<br/>DisplayObject削除要求] --> I["removeTimer called<br/>Set trash flag<br/>removeTimer呼び出し<br/>trashフラグ設定"]
151
+ I --> J["Register to trashStore<br/>Start 1-second timer<br/>trashStoreに登録<br/>1秒タイマー開始"]
152
+ J --> K{Re-access during timer?<br/>タイマー中に再アクセス?}
153
+
154
+ K -->|Yes| L["get() removes trash flag<br/>get()でtrashフラグ削除"]
155
+ L --> M[Deletion cancelled<br/>Continue using cache<br/>削除キャンセル<br/>キャッシュ継続利用]
156
+
157
+ K -->|No| N["Timer complete<br/>$removeCache = true<br/>タイマー完了"]
158
+ N --> O["removeTimerScheduledCache<br/>Check trash flag<br/>trashフラグ確認"]
159
+ O --> P{Trash flag remains?<br/>trashフラグ残存?}
160
+ P -->|Yes| Q[Execute removeById<br/>removeById実行]
161
+ Q --> R[Complete cache deletion<br/>キャッシュ完全削除]
162
+ R --> S[Release Node<br/>Free atlas region<br/>Nodeをrelease<br/>アトラス領域を解放]
163
+ P -->|No| T[Skip deletion<br/>削除スキップ]
164
+ ```
165
+
166
+ ### Deletion Flow Details / 削除フローの詳細
167
+
168
+ 1. **removeTimer**: Called when DisplayObject is deleted, sets `trash` flag, registers to trashStore, and starts 1-second timer
169
+ - DisplayObject削除時に呼び出し、`trash`フラグを設定してtrashStoreに登録、1秒タイマー開始
170
+ 2. **Flag removal via get()**: When cache is accessed via `get()` during timer, `data.delete("trash")` removes the flag
171
+ - タイマー中に`get()`でキャッシュにアクセスすると、`data.delete("trash")`でフラグが削除される
172
+ 3. **removeTimerScheduledCache**: After timer completes, only entries with remaining `trash` flag are actually deleted
173
+ - タイマー完了後、`trash`フラグが残っているエントリのみを実際に削除
174
+
175
+ ## Thread Architecture / スレッドアーキテクチャ
176
+
177
+ ```mermaid
178
+ flowchart LR
179
+ subgraph "Main Thread / メインスレッド"
180
+ MC[CacheStore]
181
+ MB["Boolean Cache<br/>(Existence check / 存在確認用)"]
182
+ MC --> MB
183
+ end
184
+
185
+ subgraph "Worker Thread (Renderer) / Workerスレッド"
186
+ WC[CacheStore]
187
+ WN["Node Cache<br/>(Coordinate info / 座標情報)"]
188
+ WC --> WN
189
+ end
190
+
191
+ subgraph "Atlas Texture / アトラステクスチャ"
192
+ AT[Atlas Texture<br/>アトラステクスチャ<br/>2048x2048]
193
+ R1[Region 1<br/>Node.x,y,w,h]
194
+ R2[Region 2<br/>Node.x,y,w,h]
195
+ RN[Region N...]
196
+ AT --> R1
197
+ AT --> R2
198
+ AT --> RN
199
+ end
200
+
201
+ MB -.->|Same key existence check<br/>同じキーで存在確認| WN
202
+ WN -.->|Coordinate reference<br/>座標情報参照| AT
203
+ ```
204
+
205
+ ### Inter-Thread Cache Coordination / スレッド間のキャッシュ連携
206
+
207
+ | Thread / スレッド | Cache Content / キャッシュ内容 | Purpose / 用途 |
208
+ |---------|--------------|------|
209
+ | Main Thread / メインスレッド | `boolean` (`true`) | Cache existence check, render command generation decision / キャッシュの存在確認、描画コマンド生成の判断 |
210
+ | Worker Thread / Workerスレッド | `Node` (index, x, y, w, h) | Atlas coordinate reference during Instanced Array rendering / Instanced Array描画時のアトラス座標参照 |
211
+
212
+ Both threads use the same key (unique_key + cacheKey) to maintain cache consistency.
213
+
214
+ 両スレッドで同じキー(unique_key + cacheKey)を使用することで、キャッシュの整合性を保っています。
215
+
216
+ ## Installation / インストール
5
217
 
6
218
  ```
7
219
  npm install @next2d/cache
8
220
  ```
9
221
 
10
- ## License
222
+ ## License / ライセンス
223
+
11
224
  This project is licensed under the [MIT License](https://opensource.org/licenses/MIT) - see the [LICENSE](LICENSE) file for details.
225
+
226
+ このプロジェクトは[MITライセンス](https://opensource.org/licenses/MIT)の下でライセンスされています。詳細は[LICENSE](LICENSE)ファイルを参照してください。
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@next2d/cache",
3
- "version": "2.13.1",
3
+ "version": "3.0.1",
4
4
  "description": "Next2D Cache Package",
5
5
  "author": "Toshiyuki Ienaga<ienaga@next2d.app> (https://github.com/ienaga/)",
6
6
  "license": "MIT",
@@ -1,13 +1,13 @@
1
1
  /**
2
- * @description キャッシュストアのキーを生成
3
- * Generate cache store keys
2
+ * @description キャッシュストアのフィルターキーを生成
3
+ * Generate cache store filter keys
4
4
  *
5
5
  * @param {number} a
6
6
  * @param {number} b
7
7
  * @param {number} c
8
8
  * @param {number} d
9
- * @return {string}
9
+ * @return {number}
10
10
  * @method
11
11
  * @public
12
12
  */
13
- export declare const execute: (a: number, b: number, c: number, d: number) => string;
13
+ export declare const execute: (a: number, b: number, c: number, d: number) => number;
@@ -1,15 +1,56 @@
1
1
  /**
2
- * @description キャッシュストアのキーを生成
3
- * Generate cache store keys
2
+ * @description キャッシュストアのフィルターキーを生成
3
+ * Generate cache store filter keys
4
4
  *
5
5
  * @param {number} a
6
6
  * @param {number} b
7
7
  * @param {number} c
8
8
  * @param {number} d
9
- * @return {string}
9
+ * @return {number}
10
10
  * @method
11
11
  * @public
12
12
  */
13
13
  export const execute = (a, b, c, d) => {
14
- return `${a}${b}${c}${d}`;
14
+ let hash = 2166136261; // FNV-1aオフセット basis
15
+ // a処理
16
+ let num = a * 100 | 0;
17
+ hash ^= num & 0xff;
18
+ hash = Math.imul(hash, 16777619);
19
+ hash ^= num >>> 8 & 0xff;
20
+ hash = Math.imul(hash, 16777619);
21
+ hash ^= num >>> 16 & 0xff;
22
+ hash = Math.imul(hash, 16777619);
23
+ hash ^= num >>> 24;
24
+ hash = Math.imul(hash, 16777619);
25
+ // b処理
26
+ num = b * 100 | 0;
27
+ hash ^= num & 0xff;
28
+ hash = Math.imul(hash, 16777619);
29
+ hash ^= num >>> 8 & 0xff;
30
+ hash = Math.imul(hash, 16777619);
31
+ hash ^= num >>> 16 & 0xff;
32
+ hash = Math.imul(hash, 16777619);
33
+ hash ^= num >>> 24;
34
+ hash = Math.imul(hash, 16777619);
35
+ // c処理
36
+ num = c * 100 | 0;
37
+ hash ^= num & 0xff;
38
+ hash = Math.imul(hash, 16777619);
39
+ hash ^= num >>> 8 & 0xff;
40
+ hash = Math.imul(hash, 16777619);
41
+ hash ^= num >>> 16 & 0xff;
42
+ hash = Math.imul(hash, 16777619);
43
+ hash ^= num >>> 24;
44
+ hash = Math.imul(hash, 16777619);
45
+ // d処理
46
+ num = d * 100 | 0;
47
+ hash ^= num & 0xff;
48
+ hash = Math.imul(hash, 16777619);
49
+ hash ^= num >>> 8 & 0xff;
50
+ hash = Math.imul(hash, 16777619);
51
+ hash ^= num >>> 16 & 0xff;
52
+ hash = Math.imul(hash, 16777619);
53
+ hash ^= num >>> 24;
54
+ hash = Math.imul(hash, 16777619);
55
+ return (hash >>> 0) % 16777216;
15
56
  };
@@ -10,20 +10,38 @@
10
10
  * @public
11
11
  */
12
12
  export const execute = (x_scale, y_scale, alpha) => {
13
- const values = [x_scale * 100, y_scale * 100];
14
- if (alpha) {
15
- values.push(alpha * 100);
16
- }
17
13
  let hash = 2166136261; // FNV-1aオフセット basis
18
- for (let idx = 0; idx < values.length; ++idx) {
19
- let num = values[idx] | 0; // 整数として扱う
20
- // 32bit整数の各バイトを処理
21
- for (let i = 0; i < 4; i++) {
22
- const byte = num & 0xff;
23
- hash ^= byte;
24
- hash = Math.imul(hash, 16777619); // FNV-1a の FNV prime
25
- num >>>= 8;
26
- }
14
+ // x_scale処理
15
+ let num = x_scale * 100 | 0;
16
+ hash ^= num & 0xff;
17
+ hash = Math.imul(hash, 16777619);
18
+ hash ^= num >>> 8 & 0xff;
19
+ hash = Math.imul(hash, 16777619);
20
+ hash ^= num >>> 16 & 0xff;
21
+ hash = Math.imul(hash, 16777619);
22
+ hash ^= num >>> 24;
23
+ hash = Math.imul(hash, 16777619);
24
+ // y_scale処理
25
+ num = y_scale * 100 | 0;
26
+ hash ^= num & 0xff;
27
+ hash = Math.imul(hash, 16777619);
28
+ hash ^= num >>> 8 & 0xff;
29
+ hash = Math.imul(hash, 16777619);
30
+ hash ^= num >>> 16 & 0xff;
31
+ hash = Math.imul(hash, 16777619);
32
+ hash ^= num >>> 24;
33
+ hash = Math.imul(hash, 16777619);
34
+ // alpha処理(alphaが0以外の場合のみ)
35
+ if (alpha) {
36
+ num = alpha * 100 | 0;
37
+ hash ^= num & 0xff;
38
+ hash = Math.imul(hash, 16777619);
39
+ hash ^= num >>> 8 & 0xff;
40
+ hash = Math.imul(hash, 16777619);
41
+ hash ^= num >>> 16 & 0xff;
42
+ hash = Math.imul(hash, 16777619);
43
+ hash ^= num >>> 24;
44
+ hash = Math.imul(hash, 16777619);
27
45
  }
28
46
  return (hash >>> 0) % 16777216;
29
47
  };
@@ -191,10 +191,10 @@ export declare class CacheStore {
191
191
  * @param {number} b
192
192
  * @param {number} c
193
193
  * @param {number} d
194
- * @return {string}
194
+ * @return {number}
195
195
  * @method
196
196
  * @public
197
197
  */
198
- generateFilterKeys(a: number, b: number, c: number, d: number): string;
198
+ generateFilterKeys(a: number, b: number, c: number, d: number): number;
199
199
  }
200
200
  export declare const $cacheStore: CacheStore;
package/src/CacheStore.js CHANGED
@@ -263,7 +263,7 @@ export class CacheStore {
263
263
  * @param {number} b
264
264
  * @param {number} c
265
265
  * @param {number} d
266
- * @return {string}
266
+ * @return {number}
267
267
  * @method
268
268
  * @public
269
269
  */