Skip to content

Commit fa38d86

Browse files
author
MarcoFalke
committed
Use only Span{} constructor for byte-like types where possible
This removes bloat that is not needed.
1 parent fa257bc commit fa38d86

10 files changed

+23
-23
lines changed

src/bench/load_external.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ static void LoadExternalBlockFile(benchmark::Bench& bench)
3434
ss << static_cast<uint32_t>(benchmark::data::block413567.size());
3535
// We can't use the streaming serialization (ss << benchmark::data::block413567)
3636
// because that first writes a compact size.
37-
ss.write(MakeByteSpan(benchmark::data::block413567));
37+
ss << Span{benchmark::data::block413567};
3838

3939
// Create the test file.
4040
{

src/net.cpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -2939,13 +2939,13 @@ void CaptureMessageToFile(const CAddress& addr,
29392939
AutoFile f{fsbridge::fopen(path, "ab")};
29402940

29412941
ser_writedata64(f, now.count());
2942-
f.write(MakeByteSpan(msg_type));
2942+
f << Span{msg_type};
29432943
for (auto i = msg_type.length(); i < CMessageHeader::COMMAND_SIZE; ++i) {
29442944
f << uint8_t{'\0'};
29452945
}
29462946
uint32_t size = data.size();
29472947
ser_writedata32(f, size);
2948-
f.write(AsBytes(data));
2948+
f << data;
29492949
}
29502950

29512951
std::function<void(const CAddress& addr,

src/pubkey.h

+2-2
Original file line numberDiff line numberDiff line change
@@ -142,14 +142,14 @@ class CPubKey
142142
{
143143
unsigned int len = size();
144144
::WriteCompactSize(s, len);
145-
s.write(AsBytes(Span{vch, len}));
145+
s << Span{vch, len};
146146
}
147147
template <typename Stream>
148148
void Unserialize(Stream& s)
149149
{
150150
const unsigned int len(::ReadCompactSize(s));
151151
if (len <= SIZE) {
152-
s.read(AsWritableBytes(Span{vch, len}));
152+
s >> Span{vch, len};
153153
if (len != size()) {
154154
Invalidate();
155155
}

src/test/fuzz/p2p_transport_serialization.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ FUZZ_TARGET_INIT(p2p_transport_serialization, initialize_p2p_transport_serializa
7777
assert(msg.m_time == m_time);
7878

7979
std::vector<unsigned char> header;
80-
auto msg2 = CNetMsgMaker{msg.m_recv.GetVersion()}.Make(msg.m_type, MakeUCharSpan(msg.m_recv));
80+
auto msg2 = CNetMsgMaker{msg.m_recv.GetVersion()}.Make(msg.m_type, Span{msg.m_recv});
8181
serializer.prepareForTransport(msg2, header);
8282
}
8383
}

src/test/serialize_tests.cpp

+7-7
Original file line numberDiff line numberDiff line change
@@ -186,32 +186,32 @@ BOOST_AUTO_TEST_CASE(noncanonical)
186186
std::vector<char>::size_type n;
187187

188188
// zero encoded with three bytes:
189-
ss.write(MakeByteSpan("\xfd\x00\x00").first(3));
189+
ss << Span{"\xfd\x00\x00"}.first(3);
190190
BOOST_CHECK_EXCEPTION(ReadCompactSize(ss), std::ios_base::failure, isCanonicalException);
191191

192192
// 0xfc encoded with three bytes:
193-
ss.write(MakeByteSpan("\xfd\xfc\x00").first(3));
193+
ss << Span{"\xfd\xfc\x00"}.first(3);
194194
BOOST_CHECK_EXCEPTION(ReadCompactSize(ss), std::ios_base::failure, isCanonicalException);
195195

196196
// 0xfd encoded with three bytes is OK:
197-
ss.write(MakeByteSpan("\xfd\xfd\x00").first(3));
197+
ss << Span{"\xfd\xfd\x00"}.first(3);
198198
n = ReadCompactSize(ss);
199199
BOOST_CHECK(n == 0xfd);
200200

201201
// zero encoded with five bytes:
202-
ss.write(MakeByteSpan("\xfe\x00\x00\x00\x00").first(5));
202+
ss << Span{"\xfe\x00\x00\x00\x00"}.first(5);
203203
BOOST_CHECK_EXCEPTION(ReadCompactSize(ss), std::ios_base::failure, isCanonicalException);
204204

205205
// 0xffff encoded with five bytes:
206-
ss.write(MakeByteSpan("\xfe\xff\xff\x00\x00").first(5));
206+
ss << Span{"\xfe\xff\xff\x00\x00"}.first(5);
207207
BOOST_CHECK_EXCEPTION(ReadCompactSize(ss), std::ios_base::failure, isCanonicalException);
208208

209209
// zero encoded with nine bytes:
210-
ss.write(MakeByteSpan("\xff\x00\x00\x00\x00\x00\x00\x00\x00").first(9));
210+
ss << Span{"\xff\x00\x00\x00\x00\x00\x00\x00\x00"}.first(9);
211211
BOOST_CHECK_EXCEPTION(ReadCompactSize(ss), std::ios_base::failure, isCanonicalException);
212212

213213
// 0x01ffffff encoded with nine bytes:
214-
ss.write(MakeByteSpan("\xff\xff\xff\xff\x01\x00\x00\x00\x00").first(9));
214+
ss << Span{"\xff\xff\xff\xff\x01\x00\x00\x00\x00"}.first(9);
215215
BOOST_CHECK_EXCEPTION(ReadCompactSize(ss), std::ios_base::failure, isCanonicalException);
216216
}
217217

src/uint256.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ class base_blob
7777
template<typename Stream>
7878
void Serialize(Stream& s) const
7979
{
80-
s.write(MakeByteSpan(m_data));
80+
s << Span(m_data);
8181
}
8282

8383
template<typename Stream>

src/wallet/dump.cpp

+6-6
Original file line numberDiff line numberDiff line change
@@ -57,12 +57,12 @@ bool DumpWallet(const ArgsManager& args, CWallet& wallet, bilingual_str& error)
5757
// Write out a magic string with version
5858
std::string line = strprintf("%s,%u\n", DUMP_MAGIC, DUMP_VERSION);
5959
dump_file.write(line.data(), line.size());
60-
hasher.write(MakeByteSpan(line));
60+
hasher << Span{line};
6161

6262
// Write out the file format
6363
line = strprintf("%s,%s\n", "format", db.Format());
6464
dump_file.write(line.data(), line.size());
65-
hasher.write(MakeByteSpan(line));
65+
hasher << Span{line};
6666

6767
if (ret) {
6868

@@ -83,7 +83,7 @@ bool DumpWallet(const ArgsManager& args, CWallet& wallet, bilingual_str& error)
8383
std::string value_str = HexStr(ss_value);
8484
line = strprintf("%s,%s\n", key_str, value_str);
8585
dump_file.write(line.data(), line.size());
86-
hasher.write(MakeByteSpan(line));
86+
hasher << Span{line};
8787
}
8888
}
8989

@@ -160,7 +160,7 @@ bool CreateFromDump(const ArgsManager& args, const std::string& name, const fs::
160160
return false;
161161
}
162162
std::string magic_hasher_line = strprintf("%s,%s\n", magic_key, version_value);
163-
hasher.write(MakeByteSpan(magic_hasher_line));
163+
hasher << Span{magic_hasher_line};
164164

165165
// Get the stored file format
166166
std::string format_key;
@@ -191,7 +191,7 @@ bool CreateFromDump(const ArgsManager& args, const std::string& name, const fs::
191191
warnings.push_back(strprintf(_("Warning: Dumpfile wallet format \"%s\" does not match command line specified format \"%s\"."), format_value, file_format));
192192
}
193193
std::string format_hasher_line = strprintf("%s,%s\n", format_key, format_value);
194-
hasher.write(MakeByteSpan(format_hasher_line));
194+
hasher << Span{format_hasher_line};
195195

196196
DatabaseOptions options;
197197
DatabaseStatus status;
@@ -236,7 +236,7 @@ bool CreateFromDump(const ArgsManager& args, const std::string& name, const fs::
236236
}
237237

238238
std::string line = strprintf("%s,%s\n", key, value);
239-
hasher.write(MakeByteSpan(line));
239+
hasher << Span{line};
240240

241241
if (key.empty() || value.empty()) {
242242
continue;

src/wallet/test/db_tests.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -196,7 +196,7 @@ BOOST_AUTO_TEST_CASE(db_cursor_prefix_byte_test)
196196
for (const auto& database : TestDatabases(m_path_root)) {
197197
std::unique_ptr<DatabaseBatch> batch = database->MakeBatch();
198198
for (const auto& [k, v] : {e, p, ps, f, fs, ff, ffs}) {
199-
batch->Write(MakeUCharSpan(k), MakeUCharSpan(v));
199+
batch->Write(Span{k}, Span{v});
200200
}
201201
CheckPrefix(*batch, StringBytes(""), {e, p, ps, f, fs, ff, ffs});
202202
CheckPrefix(*batch, StringBytes("prefix"), {p, ps});

src/wallet/wallet.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -3878,7 +3878,7 @@ bool CWallet::MigrateToSQLite(bilingual_str& error)
38783878
bool began = batch->TxnBegin();
38793879
assert(began); // This is a critical error, the new db could not be written to. The original db exists as a backup, but we should not continue execution.
38803880
for (const auto& [key, value] : records) {
3881-
if (!batch->Write(MakeUCharSpan(key), MakeUCharSpan(value))) {
3881+
if (!batch->Write(Span{key}, Span{value})) {
38823882
batch->TxnAbort();
38833883
m_database->Close();
38843884
fs::remove(m_database->Filename());

src/wallet/walletdb.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -1161,7 +1161,7 @@ bool WalletBatch::EraseRecords(const std::unordered_set<std::string>& types)
11611161
}
11621162

11631163
// Make a copy of key to avoid data being deleted by the following read of the type
1164-
Span<const unsigned char> key_data = MakeUCharSpan(key);
1164+
Span key_data{key};
11651165

11661166
std::string type;
11671167
key >> type;

0 commit comments

Comments
 (0)