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

Code before:
}

/**
 * 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);
				}
			}
			iteratedEntries.add(entryIndex);
		}
		for (const entryIndex of shouldBeRemovedEntries) {

Review comment (refactor):
Just a minor thing, but maybe if you pass `allEntries[entryIndex]` as the argument, then all accesses to the module property will happen in the helper and it might slightly improve readability.

Code after:
}

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];
		}
		// If the knownLoadedAtoms changed, all dependent dynamic entries need to be
		// updated again
		if (updatedLoadedAtoms !== knownLoadedAtoms) {
			alreadyLoadedAtomsByEntry[dynamicEntryIndex] = updatedLoadedAtoms;