Repository: bitcoin/bitcoin
File: src/bench/blockencodings.cpp (C++)

Code before:
    BenchCBHAST(InsecureRandomContext& rng, int txs) : CBlockHeaderAndShortTxIDs(DummyBlock(), rng.rand64())
    {
        shorttxids.reserve(txs);
        while (txs-- > 0) {
            shorttxids.push_back(rng.randbits<SHORTTXIDS_LENGTH*8>());
        }
    }
};
} // anon namespace

static void BlockEncodingBench(benchmark::Bench& bench, size_t n_pool, size_t n_extra)
{
    const auto testing_setup = MakeNoLogFileContext<const ChainTestingSetup>(ChainType::MAIN);
    CTxMemPool& pool = *Assert(testing_setup->m_node.mempool);
    InsecureRandomContext rng(11);

    LOCK2(cs_main, pool.cs);

    std::vector<CTransactionRef> extratxn;
    extratxn.reserve(n_extra);

    // bump up the size of txs
    std::array<std::byte,200> sigspam;
    sigspam.fill(std::byte(42));

    // a reasonably large mempool of 50k txs, ~10MB total
    std::vector<CTransactionRef> refs;
    refs.reserve(n_pool + n_extra);
    for (size_t i = 0; i < n_pool + n_extra; ++i) {
        CMutableTransaction tx = CMutableTransaction();
        tx.vin.resize(1);
        tx.vin[0].scriptSig = CScript() << sigspam;
        tx.vin[0].scriptWitness.stack.push_back({1});
        tx.vout.resize(1);
        tx.vout[0].scriptPubKey = CScript() << OP_1 << OP_EQUAL;
        tx.vout[0].nValue = i;
        refs.push_back(MakeTransactionRef(tx));
    }

    // ensure mempool ordering is different to memory ordering of transactions,
    // to simulate a mempool that has changed over time
    std::shuffle(refs.begin(), refs.end(), rng);

    for (size_t i = 0; i < n_pool; ++i) {
        AddTx(refs[i], /*fee=*/refs[i]->vout[0].nValue, pool);
    }
    for (size_t i = n_pool; i < n_pool + n_extra; ++i) {
        extratxn.push_back(refs[i]);
    }

    BenchCBHAST cmpctblock{rng, 3000};

Review comment (suggestion):
```Suggestion
    // a reasonably large mempool of 50k txs, ~10MB total plus variable extra txns
```

Code after:
    BenchCBHAST(InsecureRandomContext& rng, int txs) : CBlockHeaderAndShortTxIDs(DummyBlock(), rng.rand64())
    {
        shorttxids.reserve(txs);
        while (txs-- > 0) {
            shorttxids.push_back(rng.randbits<SHORTTXIDS_LENGTH*8>());
        }
    }
};
} // anon namespace

static void BlockEncodingBench(benchmark::Bench& bench, size_t n_pool, size_t n_extra)
{
    const auto testing_setup = MakeNoLogFileContext<const ChainTestingSetup>(ChainType::MAIN);
    CTxMemPool& pool = *Assert(testing_setup->m_node.mempool);
    InsecureRandomContext rng(11);

    LOCK2(cs_main, pool.cs);

    std::vector<std::pair<Wtxid, CTransactionRef>> extratxn;
    extratxn.reserve(n_extra);

    // bump up the size of txs
    std::array<std::byte,200> sigspam;
    sigspam.fill(std::byte(42));

    // a reasonably large mempool of 50k txs, ~10MB total
    std::vector<CTransactionRef> refs;
    refs.reserve(n_pool + n_extra);
    for (size_t i = 0; i < n_pool + n_extra; ++i) {
        CMutableTransaction tx = CMutableTransaction();
        tx.vin.resize(1);
        tx.vin[0].scriptSig = CScript() << sigspam;
        tx.vin[0].scriptWitness.stack.push_back({1});
        tx.vout.resize(1);
        tx.vout[0].scriptPubKey = CScript() << OP_1 << OP_EQUAL;
        tx.vout[0].nValue = i;
        refs.push_back(MakeTransactionRef(tx));
    }

    // ensure mempool ordering is different to memory ordering of transactions,
    // to simulate a mempool that has changed over time
    std::shuffle(refs.begin(), refs.end(), rng);

    for (size_t i = 0; i < n_pool; ++i) {
        AddTx(refs[i], /*fee=*/refs[i]->vout[0].nValue, pool);
    }
    for (size_t i = n_pool; i < n_pool + n_extra; ++i) {
        extratxn.emplace_back(refs[i]->GetWitnessHash(), refs[i]);
    }

    BenchCBHAST cmpctblock{rng, 3000};