Skip to content

Commit bf352e7

Browse files
evo: use Span<const unsigned char> in specialtx filter extraction callback to avoid temporary allocations; adjust helpers and call site
1 parent c734b64 commit bf352e7

File tree

3 files changed

+12
-11
lines changed

3 files changed

+12
-11
lines changed

src/blockfilter.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -198,7 +198,7 @@ static GCSFilter::ElementSet BasicFilterElements(const CBlock& block,
198198
}
199199

200200
// Extract special transaction elements using delegation pattern
201-
ExtractSpecialTxFilterElements(*tx, [&elements](const std::vector<unsigned char>& data) {
201+
ExtractSpecialTxFilterElements(*tx, [&elements](Span<const unsigned char> data) {
202202
elements.emplace(data.begin(), data.end());
203203
});
204204
}

src/evo/specialtx_filter.cpp

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -34,19 +34,19 @@
3434
*/
3535
// Helper function to add a script to the filter if it's not empty
3636
static void AddScriptElement(const CScript& script,
37-
const std::function<void(const std::vector<unsigned char>&)>& addElement)
37+
const std::function<void(Span<const unsigned char>)>& addElement)
3838
{
3939
if (!script.empty()) {
40-
addElement(std::vector<unsigned char>(script.begin(), script.end()));
40+
addElement(MakeUCharSpan(script));
4141
}
4242
}
4343

4444
// Helper function to add a hash/key to the filter
4545
template<typename T>
4646
static void AddHashElement(const T& hash,
47-
const std::function<void(const std::vector<unsigned char>&)>& addElement)
47+
const std::function<void(Span<const unsigned char>)>& addElement)
4848
{
49-
addElement(std::vector<unsigned char>(hash.begin(), hash.end()));
49+
addElement(MakeUCharSpan(hash));
5050
}
5151

5252
// NOTE(maintenance): Keep this in sync with
@@ -55,7 +55,7 @@ static void AddHashElement(const T& hash,
5555
// transaction type here, update the bloom filter routine accordingly
5656
// (and vice versa) to avoid compact-filter vs bloom-filter divergence.
5757
void ExtractSpecialTxFilterElements(const CTransaction& tx,
58-
const std::function<void(const std::vector<unsigned char>&)>& addElement)
58+
const std::function<void(Span<const unsigned char>)>& addElement)
5959
{
6060
if (!tx.HasExtraPayloadField()) {
6161
return; // not a special transaction
@@ -67,8 +67,7 @@ void ExtractSpecialTxFilterElements(const CTransaction& tx,
6767
// Add collateral outpoint
6868
CDataStream stream(SER_NETWORK, PROTOCOL_VERSION);
6969
stream << opt_proTx->collateralOutpoint;
70-
auto span = MakeUCharSpan(stream);
71-
addElement(std::vector<unsigned char>(span.begin(), span.end()));
70+
addElement(MakeUCharSpan(stream));
7271

7372
// Add owner key ID
7473
AddHashElement(opt_proTx->keyIDOwner, addElement);

src/evo/specialtx_filter.h

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
#define BITCOIN_EVO_SPECIALTX_FILTER_H
77

88
#include <functional>
9-
#include <vector>
9+
#include <span.h>
1010

1111
class CTransaction;
1212

@@ -16,9 +16,11 @@ class CTransaction;
1616
* SPV clients can detect special transactions using either filtering mechanism.
1717
*
1818
* @param tx The transaction to extract elements from
19-
* @param addElement Callback function to add extracted elements to the filter
19+
* @param addElement Callback to add extracted elements to the filter. Uses
20+
* Span<const unsigned char> to avoid intermediate
21+
* allocations.
2022
*/
2123
void ExtractSpecialTxFilterElements(const CTransaction& tx,
22-
const std::function<void(const std::vector<unsigned char>&)>& addElement);
24+
const std::function<void(Span<const unsigned char>)>& addElement);
2325

2426
#endif // BITCOIN_EVO_SPECIALTX_FILTER_H

0 commit comments

Comments
 (0)