Repository: rollup/rollup
File: src/utils/chunkAssignment.ts (TypeScript)

Code before:
					shouldBeRemovedEntries.clear();
					break;
				} else {
					shouldBeRemovedEntries.add(entryIndex);
				}
			}
			iteratedEntries.add(entryIndex);
		}
		for (const entryIndex of shouldBeRemovedEntries) {
			dependentEntries.delete(entryIndex);
		}
		chunkMask <<= 1n;
	}
}

function getTopLevelAwaitDynamicImporterSet(includedTopLevelAwaitDynamicImporters: Module[]) {
	const stack = [...includedTopLevelAwaitDynamicImporters];
	const topLevelAwaitDynamicImporterSet = new Set(stack);
	while (stack.length > 0) {
		const includedTopLevelAwaitDynamicImporter = stack.pop()!;
		topLevelAwaitDynamicImporterSet.add(includedTopLevelAwaitDynamicImporter);
		stack.push(...includedTopLevelAwaitDynamicImporter.includedTopLevelAwaitDynamicImporters);
	}
	return topLevelAwaitDynamicImporterSet;
}

function getChunksWithSameDependentEntriesAndCorrelatedAtoms(
	chunkAtoms: ModulesWithDependentEntries[],
	staticDependencyAtomsByEntry: bigint[],
	alreadyLoadedAtomsByEntry: bigint[],
	minChunkSize: number
) {
	const chunksBySignature: Record<string, ChunkDescription> = Object.create(null);
	const chunkByModule = new Map<Module, ChunkDescription>();
	const sizeByAtom: number[] = new Array(chunkAtoms.length);
	let sideEffectAtoms = 0n;
	let atomMask = 1n;
	let index = 0;
	for (const { dependentEntries, modules } of chunkAtoms) {
		let chunkSignature = 0n;
		let correlatedAtoms = -1n;
		for (const entryIndex of dependentEntries) {
			chunkSignature |= 1n << BigInt(entryIndex);
			// Correlated atoms are the atoms that are guaranteed to be loaded as
			// well when a given atom is loaded. It is the intersection of the already
			// loaded modules of each chunk merged with its static dependencies.
			correlatedAtoms &=
				staticDependencyAtomsByEntry[entryIndex] | alreadyLoadedAtomsByEntry[entryIndex];
		}
		const chunk = (chunksBySignature[String(chunkSignature)] ||= {
			containedAtoms: 0n,

Review comment (suggestion):
I do not think you need the stack here at all. A Set iterator is guaranteed to not iterate over any entry of the Set more than once even if you add new entries to the Set. This is a hack we actually use in many other places as well. Then this can become (including the rename and the changed parameter)

```suggestion
function getTransitiveTopLevelAwaitingDynamicImporters(entry: Module) {
	const transitiveTopLevelAwaitingDynamicImporters = new Set([
		...entry.includedTopLevelAwaitingDynamicImporters
	]);
	for (const topLevelAwaitDynamicImporter of transitiveTopLevelAwaitingDynamicImporters) {
		for (const includedTopLevelAwaitDynamicImporter of topLevelAwaitDynamicImporter.includedTopLevelAwaitingDynamicImporters) {
			transitiveTopLevelAwaitingDynamicImporters.add(includedTopLevelAwaitDynamicImporter);
		}
	}
	return transitiveTopLevelAwaitingDynamicImporters;
}

```

Code after:
		let updatedLoadedAtoms = knownLoadedAtoms;
		for (const entryIndex of dynamicallyDependentEntries) {
			updatedLoadedAtoms &=
				staticDependencyAtomsByEntry[entryIndex] | alreadyLoadedAtomsByEntry[entryIndex];
		}
		// If the knownLoadedAtoms changed, all dependent dynamic entries need to be
		// updated again
		if (updatedLoadedAtoms !== knownLoadedAtoms) {
			alreadyLoadedAtomsByEntry[dynamicEntryIndex] = updatedLoadedAtoms;
			for (const dynamicImport of dynamicImportsByEntry[dynamicEntryIndex]) {
				// If this adds an entry that was deleted before, it will be handled
				// again. This is the reason why we delete every entry from this map
				// that we processed.
				getOrCreate(
					dynamicallyDependentEntriesByDynamicEntry,
					dynamicImport,
					getNewSet<number>
				).add(dynamicEntryIndex);
			}
		}
	}
	return alreadyLoadedAtomsByEntry;
}

/**
 * This removes all unnecessary dynamic entries from the dependentEntries in its
 * first argument if a chunk is already loaded without that entry.
 */
function removeUnnecessaryDependentEntries(
	chunkAtoms: ModulesWithDependentEntries[],
	alreadyLoadedAtomsByEntry: bigint[],
	awaitedAlreadyLoadedAtomsByEntry: bigint[]
) {
	// Remove entries from dependent entries if a chunk is already loaded without
	// that entry. Do not remove already loaded atoms where all dynamic imports
	// are awaited to avoid cycles in the output.
	let chunkMask = 1n;
	for (const { dependentEntries } of chunkAtoms) {
		for (const entryIndex of dependentEntries) {
			if (
				(alreadyLoadedAtomsByEntry[entryIndex] & chunkMask) === chunkMask &&
				(awaitedAlreadyLoadedAtomsByEntry[entryIndex] & chunkMask) === 0n
			) {
				dependentEntries.delete(entryIndex);
			}
		}
		chunkMask <<= 1n;
	}
}

function getChunksWithSameDependentEntriesAndCorrelatedAtoms(