* Merges small content files to a single content file
()
| 370 | * Merges small content files to a single content file |
| 371 | */ |
| 372 | _optimizeSmallContent() { |
| 373 | // 1. Find all small content files |
| 374 | // Treat unused content files separately to avoid |
| 375 | // a merge-split cycle |
| 376 | /** @type {number[]} */ |
| 377 | const smallUsedContents = []; |
| 378 | /** @type {number} */ |
| 379 | let smallUsedContentSize = 0; |
| 380 | /** @type {number[]} */ |
| 381 | const smallUnusedContents = []; |
| 382 | /** @type {number} */ |
| 383 | let smallUnusedContentSize = 0; |
| 384 | for (let i = 0; i < this.content.length; i++) { |
| 385 | const content = this.content[i]; |
| 386 | if (content === undefined) continue; |
| 387 | if (content.outdated) continue; |
| 388 | const size = content.getSize(); |
| 389 | if (size < 0 || size > MIN_CONTENT_SIZE) continue; |
| 390 | if (content.used.size > 0) { |
| 391 | smallUsedContents.push(i); |
| 392 | smallUsedContentSize += size; |
| 393 | } else { |
| 394 | smallUnusedContents.push(i); |
| 395 | smallUnusedContentSize += size; |
| 396 | } |
| 397 | } |
| 398 | |
| 399 | // 2. Check if minimum number is reached |
| 400 | /** @type {number[]} */ |
| 401 | let mergedIndices; |
| 402 | if ( |
| 403 | smallUsedContents.length >= CONTENT_COUNT_TO_MERGE || |
| 404 | smallUsedContentSize > MIN_CONTENT_SIZE |
| 405 | ) { |
| 406 | mergedIndices = smallUsedContents; |
| 407 | } else if ( |
| 408 | smallUnusedContents.length >= CONTENT_COUNT_TO_MERGE || |
| 409 | smallUnusedContentSize > MIN_CONTENT_SIZE |
| 410 | ) { |
| 411 | mergedIndices = smallUnusedContents; |
| 412 | } else { |
| 413 | return; |
| 414 | } |
| 415 | |
| 416 | /** @type {PackContent[]} */ |
| 417 | const mergedContent = []; |
| 418 | |
| 419 | // 3. Remove old content entries |
| 420 | for (const i of mergedIndices) { |
| 421 | mergedContent.push(/** @type {PackContent} */ (this.content[i])); |
| 422 | this.content[i] = undefined; |
| 423 | } |
| 424 | |
| 425 | // 4. Determine merged items |
| 426 | /** @type {Items} */ |
| 427 | const mergedItems = new Set(); |
| 428 | /** @type {Items} */ |
| 429 | const mergedUsedItems = new Set(); |