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

Code before:
				).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[],
	allEntries: readonly Module[]
) {
	// Remove entries from dependent entries if a chunk is already loaded without
	// that entry.
	let chunkMask = 1n;
	const topLevelAwaitDynamicImporterSetByEntry = new Map<number, Set<Module>>();
	for (const { dependentEntries } of chunkAtoms) {
		const shouldBeRemovedEntries = new Set<number>();
		const iteratedEntries = new Set<number>();
		for (const entryIndex of dependentEntries) {
			if ((alreadyLoadedAtomsByEntry[entryIndex] & chunkMask) === chunkMask) {
				const topLevelAwaitDynamicImporterSet = getOrCreate(
					topLevelAwaitDynamicImporterSetByEntry,
					entryIndex,
					() =>
						getTopLevelAwaitDynamicImporterSet(
							allEntries[entryIndex].includedTopLevelAwaitDynamicImporters
						)
				);
				let isExistedCircular = false;
				for (const iteratedEntryIndex of iteratedEntries) {
					const includedModules = [
						allEntries[iteratedEntryIndex],
						...allEntries[iteratedEntryIndex].getDependenciesToBeIncluded()
					];
					isExistedCircular = includedModules.some(
						includedModule =>
							includedModule instanceof Module &&
							topLevelAwaitDynamicImporterSet.has(includedModule)
					);
					if (isExistedCircular) break;
				}
				if (isExistedCircular) {
					shouldBeRemovedEntries.clear();
					break;
				} else {
					shouldBeRemovedEntries.add(entryIndex);

Review comment (suggestion):
I find it confusing that this has the same name as the set of direct TLA importers. Maybe call it

```suggestion
				const transitiveTopLevelAwaitingDynamicImporters = getOrCreate(
```

and similarly change related names in other places? Alternatively, the property on Module could be called `directTopLevelAwaitingDynamicImports`.

Code after:
	for (const [module, dependentEntries] of dependentEntriesByModule) {
		if (!modulesInManualChunks.has(module)) {
			yield { dependentEntries, modules: [module] };
		}
	}
}

function getStaticDependencyAtomsByEntry(
	allEntries: readonly Module[],
	chunkAtoms: ModulesWithDependentEntries[]
) {
	// The indices correspond to the indices in allEntries. The atoms correspond
	// to bits in the bigint values where chunk 0 is the lowest bit.
	const staticDependencyAtomsByEntry: bigint[] = allEntries.map(() => 0n);

	// This toggles the bits for each atom that is a dependency of an entry
	let atomMask = 1n;
	for (const { dependentEntries } of chunkAtoms) {
		for (const entryIndex of dependentEntries) {
			staticDependencyAtomsByEntry[entryIndex] |= atomMask;
		}
		atomMask <<= 1n;
	}
	return staticDependencyAtomsByEntry;
}

// Warning: This will consume dynamicallyDependentEntriesByDynamicEntry.
function getAlreadyLoadedAtomsByEntry(
	staticDependencyAtomsByEntry: bigint[],
	dynamicallyDependentEntriesByDynamicEntry: Map<number, Set<number>>,
	dynamicImportsByEntry: readonly ReadonlySet<number>[],
	allEntries: readonly Module[]
) {
	// Dynamic entries have all atoms as already loaded initially because we then
	// intersect with the static dependency atoms of all dynamic importers.
	// Static entries cannot have already loaded atoms.
	const alreadyLoadedAtomsByEntry: bigint[] = allEntries.map((_entry, entryIndex) =>
		dynamicallyDependentEntriesByDynamicEntry.has(entryIndex) ? -1n : 0n
	);
	for (const [
		dynamicEntryIndex,
		dynamicallyDependentEntries
	] of dynamicallyDependentEntriesByDynamicEntry) {
		// We delete here so that they can be added again if necessary to be handled
		// again by the loop
		dynamicallyDependentEntriesByDynamicEntry.delete(dynamicEntryIndex);
		const knownLoadedAtoms = alreadyLoadedAtomsByEntry[dynamicEntryIndex];
		let updatedLoadedAtoms = knownLoadedAtoms;
		for (const entryIndex of dynamicallyDependentEntries) {
			updatedLoadedAtoms &=
				staticDependencyAtomsByEntry[entryIndex] | alreadyLoadedAtomsByEntry[entryIndex];