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

Code before:
    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};

    bench.run([&] {
        PartiallyDownloadedBlock pdb{&pool};
        auto res = pdb.InitData(cmpctblock, extratxn);

        // if there were duplicates the benchmark will be invalid
        // (eg, extra txns will be skipped) and we will receive
        // READ_STATUS_FAILED
        assert(res == READ_STATUS_OK);
    });
}

static void BlockEncodingNoExtra(benchmark::Bench& bench)
{
    BlockEncodingBench(bench, 50000, 0);
}

static void BlockEncodingStdExtra(benchmark::Bench& bench)
{
    static_assert(DEFAULT_BLOCK_RECONSTRUCTION_EXTRA_TXN == 100);
    BlockEncodingBench(bench, 50000, 100);
}

static void BlockEncodingLargeExtra(benchmark::Bench& bench)
{
    BlockEncodingBench(bench, 50000, 5000);
}

Review comment (suggestion):
nit:

```suggestion
    bench.unit("block").run([&] {
```

and maybe:

```suggestion
    bench.unit("block").timeUnit(1ms, "ms").run([&] {
```


Code after:
    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};

    bench.run([&] {
        PartiallyDownloadedBlock pdb{&pool};
        auto res = pdb.InitData(cmpctblock, extratxn);

        // if there were duplicates the benchmark will be invalid
        // (eg, extra txns will be skipped) and we will receive
        // READ_STATUS_FAILED
        assert(res == READ_STATUS_OK);
    });
}

static void BlockEncodingNoExtra(benchmark::Bench& bench)
{
    BlockEncodingBench(bench, 50000, 0);
}

static void BlockEncodingStdExtra(benchmark::Bench& bench)
{
    static_assert(DEFAULT_BLOCK_RECONSTRUCTION_EXTRA_TXN == 100);
    BlockEncodingBench(bench, 50000, 100);
}

static void BlockEncodingLargeExtra(benchmark::Bench& bench)
{
    BlockEncodingBench(bench, 50000, 5000);
}