def _load_contents_with_earliest_strategy(self, contents: cirq.OP_TREE):
        """Optimized algorithm to load contents quickly.

        The default algorithm appends operations one-at-a-time, letting them
        fall back until they encounter a moment they cannot commute with. This
        is slow because it requires re-checking for conflicts at each moment.

        Here, we instead keep track of the greatest moment that contains each
        qubit, measurement key, and control key, and append the operation to
        the moment after the maximum of these. This avoids having to check each
        moment.

        Args:
            contents: The initial list of moments and operations defining the
                circuit. You can also pass in operations, lists of operations,
                or generally anything meeting the `cirq.OP_TREE` contract.
                Non-moment entries will be inserted according to the EARLIEST
                insertion strategy.
        """
        # PlacementCache holds dicts from the qubit/key to the greatest moment index that has it.
        placement_cache = cast(_PlacementCache, self._placement_cache)

        # We also maintain the dict from moment index to moments/ops that go into it, for use when
        # building the actual moments at the end.
        op_lists_by_index: dict[int, list[cirq.Operation]] = defaultdict(list)
        moments_by_index: dict[int, cirq.Moment] = {}

        # "mop" means current moment-or-operation
        for mop in ops.flatten_to_ops_or_moments(contents):
            # Identify the index of the moment to place this `mop` into.
            placement_index = placement_cache.append(mop)
            if isinstance(mop, Moment):
                moments_by_index[placement_index] = mop
            else:
                op_lists_by_index[placement_index].append(mop)

        # Finally, once everything is placed, we can construct and append the actual moments for
        # each index.
        for i in range(placement_cache._length):
            if i in moments_by_index:
                self._moments.append(moments_by_index[i].with_operations(op_lists_by_index[i]))
            else:
                self._moments.append(Moment(op_lists_by_index[i]))
Optimized algorithm to load contents quickly.

        The default algorithm appends operations one-at-a-time, letting them
        fall back until they encounter a moment they cannot commute with. This
        is slow because it requires re-checking for conflicts at each moment.

        Here, we instead keep track of the greatest moment that contains each
        qubit, measurement key, and control key, and append the operation to
        the moment after the maximum of these. This avoids having to check each
        moment.

        Args:
            contents: The initial list of moments and operations defining the
                circuit. You can also pass in operations, lists of operations,
                or generally anything meeting the `cirq.OP_TREE` contract.
                Non-moment entries will be inserted according to the EARLIEST
                insertion strategy.
        
cirq-core/cirq/circuits/circuit.py