Repository: bitcoin/bitcoin
File: src/policy/ephemeral_policy.cpp (C++)

Code before:
            if (auto it = map_txid_ref.find(parent_txid); it != map_txid_ref.end()) {
                parent_ref = it->second;
            } else {
                parent_ref = tx_pool.get(parent_txid);
            }

            // Check for dust on parents
            if (parent_ref) {
                for (uint32_t out_index = 0; out_index < parent_ref->vout.size(); out_index++) {
                    const auto& tx_output = parent_ref->vout[out_index];
                    if (IsDust(tx_output, dust_relay_rate)) {
                        unspent_parent_dust.insert(COutPoint(parent_txid, out_index));
                    }
                }
            }

            processed_parent_set.insert(parent_txid);
        }

        // Now that we have gathered parents' dust, make sure it's spent
        // by the child
        for (const auto& tx_input : tx->vin) {
            unspent_parent_dust.erase(tx_input.prevout);
        }

        if (!unspent_parent_dust.empty()) {
            return txid;
        }
    }

    return std::nullopt;
}


Review comment (refactor):
perf nit: Could remove this line if using `.insert` and checking the return value instead of `.contains` above as suggested here https://github.com/bitcoin/bitcoin/pull/30239#discussion_r1831063128.

The current version is slightly easier to read but does an *extra lookup*. I understand if you want to leave as-is for readability as the perf-hit is probably negligible.

Code after:
        std::unordered_set<COutPoint, SaltedOutpointHasher> unspent_parent_dust;

        for (const auto& tx_input : tx->vin) {
            const Txid& parent_txid{tx_input.prevout.hash};
            // Skip parents we've already checked dust for
            if (processed_parent_set.contains(parent_txid)) continue;

            // We look for an in-package or in-mempool dependency
            CTransactionRef parent_ref = nullptr;
            if (auto it = map_txid_ref.find(parent_txid); it != map_txid_ref.end()) {
                parent_ref = it->second;
            } else {
                parent_ref = tx_pool.get(parent_txid);
            }

            // Check for dust on parents
            if (parent_ref) {
                for (uint32_t out_index = 0; out_index < parent_ref->vout.size(); out_index++) {
                    const auto& tx_output = parent_ref->vout[out_index];
                    if (IsDust(tx_output, dust_relay_rate)) {
                        unspent_parent_dust.insert(COutPoint(parent_txid, out_index));
                    }
                }
            }

            processed_parent_set.insert(parent_txid);
        }

        if (unspent_parent_dust.empty()) {
            continue;
        }

        // Now that we have gathered parents' dust, make sure it's spent
        // by the child
        for (const auto& tx_input : tx->vin) {
            unspent_parent_dust.erase(tx_input.prevout);
        }

        if (!unspent_parent_dust.empty()) {
            out_child_txid = tx->GetHash();
            out_child_state.Invalid(TxValidationResult::TX_MEMPOOL_POLICY, "missing-ephemeral-spends",
                                strprintf("tx %s did not spend parent's ephemeral dust", out_child_txid.ToString()));
            return false;
        }
    }

    return true;
}