* Split large content files with used and unused items * into two parts to separate used from unused items
()
| 480 | * into two parts to separate used from unused items |
| 481 | */ |
| 482 | _optimizeUnusedContent() { |
| 483 | // 1. Find a large content file with used and unused items |
| 484 | for (let i = 0; i < this.content.length; i++) { |
| 485 | const content = this.content[i]; |
| 486 | if (content === undefined) continue; |
| 487 | const size = content.getSize(); |
| 488 | if (size < MIN_CONTENT_SIZE) continue; |
| 489 | const used = content.used.size; |
| 490 | const total = content.items.size; |
| 491 | if (used > 0 && used < total) { |
| 492 | // 2. Remove this content |
| 493 | this.content[i] = undefined; |
| 494 | |
| 495 | // 3. Determine items for the used content file |
| 496 | const usedItems = new Set(content.used); |
| 497 | const newLoc = this._findLocation(); |
| 498 | this._gcAndUpdateLocation(usedItems, usedItems, newLoc); |
| 499 | |
| 500 | // 4. Create content file for used items |
| 501 | if (usedItems.size > 0) { |
| 502 | this.content[newLoc] = new PackContent( |
| 503 | usedItems, |
| 504 | new Set(usedItems), |
| 505 | async () => { |
| 506 | await content.unpack( |
| 507 | "it should be splitted into used and unused items" |
| 508 | ); |
| 509 | /** @type {Content} */ |
| 510 | const map = new Map(); |
| 511 | for (const identifier of usedItems) { |
| 512 | map.set( |
| 513 | identifier, |
| 514 | /** @type {Content} */ |
| 515 | (content.content).get(identifier) |
| 516 | ); |
| 517 | } |
| 518 | return new PackContentItems(map); |
| 519 | } |
| 520 | ); |
| 521 | } |
| 522 | |
| 523 | // 5. Determine items for the unused content file |
| 524 | const unusedItems = new Set(content.items); |
| 525 | /** @type {Items} */ |
| 526 | const usedOfUnusedItems = new Set(); |
| 527 | for (const identifier of usedItems) { |
| 528 | unusedItems.delete(identifier); |
| 529 | } |
| 530 | const newUnusedLoc = this._findLocation(); |
| 531 | this._gcAndUpdateLocation(unusedItems, usedOfUnusedItems, newUnusedLoc); |
| 532 | |
| 533 | // 6. Create content file for unused items |
| 534 | if (unusedItems.size > 0) { |
| 535 | this.content[newUnusedLoc] = new PackContent( |
| 536 | unusedItems, |
| 537 | usedOfUnusedItems, |
| 538 | async () => { |
| 539 | await content.unpack( |
no test coverage detected