Skip to content

Commit 9778586

Browse files
committed
Merge #12886: Introduce Span type and use it instead of FLATDATA
9272d70 Support serializing Span<unsigned char> and use that instead of FLATDATA (Pieter Wuille) 833bc08 Add Slice: a (pointer, size) array view that acts like a container (Pieter Wuille) Pull request description: Introduce a new data type `Span`, which is an encapsulated pointer + size (like C++20's `std::span` or LevelDB's `Slice`), and represents a view to a sequence of objects laid out continuously in memory. The immediate use case is replacing the remaining `FLATDATA` invocations. Instead of those, we support serializing/deserializing unsigned char `Span`s (treating them as arrays). A longer term goal for `Span`s is making the script execution operate on them rather than on `CScript` itself. This will allow separate storage mechanisms for scripts. Tree-SHA512: 7b0da3c802e5df367f223275004d16b04262804c007b7c73fda927176f0a9c3b2ef3225fa842cb73500b0df73175ec1419f1f5239de2402e21dd9ae8e5d05233
2 parents 15c3bb4 + 9272d70 commit 9778586

File tree

5 files changed

+57
-48
lines changed

5 files changed

+57
-48
lines changed

src/Makefile.am

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -316,6 +316,7 @@ libbitcoin_consensus_a_SOURCES = \
316316
script/script_error.cpp \
317317
script/script_error.h \
318318
serialize.h \
319+
span.h \
319320
tinyformat.h \
320321
uint256.cpp \
321322
uint256.h \

src/compressor.h

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
#include <primitives/transaction.h>
1010
#include <script/script.h>
1111
#include <serialize.h>
12+
#include <span.h>
1213

1314
class CKeyID;
1415
class CPubKey;
@@ -51,12 +52,12 @@ class CScriptCompressor
5152
void Serialize(Stream &s) const {
5253
std::vector<unsigned char> compr;
5354
if (CompressScript(script, compr)) {
54-
s << CFlatData(compr);
55+
s << MakeSpan(compr);
5556
return;
5657
}
5758
unsigned int nSize = script.size() + nSpecialScripts;
5859
s << VARINT(nSize);
59-
s << CFlatData(script);
60+
s << MakeSpan(script);
6061
}
6162

6263
template<typename Stream>
@@ -65,7 +66,7 @@ class CScriptCompressor
6566
s >> VARINT(nSize);
6667
if (nSize < nSpecialScripts) {
6768
std::vector<unsigned char> vch(GetSpecialScriptSize(nSize), 0x00);
68-
s >> CFlatData(vch);
69+
s >> MakeSpan(vch);
6970
DecompressScript(script, nSize, vch);
7071
return;
7172
}
@@ -76,7 +77,7 @@ class CScriptCompressor
7677
s.ignore(nSize);
7778
} else {
7879
script.resize(nSize);
79-
s >> CFlatData(script);
80+
s >> MakeSpan(script);
8081
}
8182
}
8283
};

src/netaddress.h

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
#include <compat.h>
1313
#include <serialize.h>
14+
#include <span.h>
1415

1516
#include <stdint.h>
1617
#include <string>
@@ -167,10 +168,13 @@ class CService : public CNetAddr
167168
template <typename Stream, typename Operation>
168169
inline void SerializationOp(Stream& s, Operation ser_action) {
169170
READWRITE(ip);
171+
172+
// TODO: introduce native support for BE serialization in serialize.h
170173
unsigned short portN = htons(port);
171-
READWRITE(FLATDATA(portN));
172-
if (ser_action.ForRead())
174+
READWRITE(Span<unsigned char>((unsigned char*)&portN, 2));
175+
if (ser_action.ForRead()) {
173176
port = ntohs(portN);
177+
}
174178
}
175179
};
176180

src/serialize.h

Lines changed: 5 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
#include <vector>
2323

2424
#include <prevector.h>
25+
#include <span.h>
2526

2627
static const unsigned int MAX_SIZE = 0x02000000;
2728

@@ -41,7 +42,7 @@ constexpr deserialize_type deserialize {};
4142

4243
/**
4344
* Used to bypass the rule against non-const reference to temporary
44-
* where it makes sense with wrappers such as CFlatData or CTxDB
45+
* where it makes sense with wrappers.
4546
*/
4647
template<typename T>
4748
inline T& REF(const T& val)
@@ -185,6 +186,8 @@ template<typename Stream> inline void Serialize(Stream& s, float a ) { ser_wri
185186
template<typename Stream> inline void Serialize(Stream& s, double a ) { ser_writedata64(s, ser_double_to_uint64(a)); }
186187
template<typename Stream, int N> inline void Serialize(Stream& s, const char (&a)[N]) { s.write(a, N); }
187188
template<typename Stream, int N> inline void Serialize(Stream& s, const unsigned char (&a)[N]) { s.write(CharCast(a), N); }
189+
template<typename Stream> inline void Serialize(Stream& s, const Span<const unsigned char>& span) { s.write(CharCast(span.data()), span.size()); }
190+
template<typename Stream> inline void Serialize(Stream& s, const Span<unsigned char>& span) { s.write(CharCast(span.data()), span.size()); }
188191

189192
template<typename Stream> inline void Unserialize(Stream& s, char& a ) { a = ser_readdata8(s); } // TODO Get rid of bare char
190193
template<typename Stream> inline void Unserialize(Stream& s, int8_t& a ) { a = ser_readdata8(s); }
@@ -199,6 +202,7 @@ template<typename Stream> inline void Unserialize(Stream& s, float& a ) { a =
199202
template<typename Stream> inline void Unserialize(Stream& s, double& a ) { a = ser_uint64_to_double(ser_readdata64(s)); }
200203
template<typename Stream, int N> inline void Unserialize(Stream& s, char (&a)[N]) { s.read(a, N); }
201204
template<typename Stream, int N> inline void Unserialize(Stream& s, unsigned char (&a)[N]) { s.read(CharCast(a), N); }
205+
template<typename Stream> inline void Unserialize(Stream& s, Span<unsigned char>& span) { s.read(CharCast(span.data()), span.size()); }
202206

203207
template<typename Stream> inline void Serialize(Stream& s, bool a) { char f=a; ser_writedata8(s, f); }
204208
template<typename Stream> inline void Unserialize(Stream& s, bool& a) { char f=ser_readdata8(s); a=f; }
@@ -384,51 +388,10 @@ I ReadVarInt(Stream& is)
384388
}
385389
}
386390

387-
#define FLATDATA(obj) CFlatData((char*)&(obj), (char*)&(obj) + sizeof(obj))
388391
#define VARINT(obj, ...) WrapVarInt<__VA_ARGS__>(REF(obj))
389392
#define COMPACTSIZE(obj) CCompactSize(REF(obj))
390393
#define LIMITED_STRING(obj,n) LimitedString< n >(REF(obj))
391394

392-
/**
393-
* Wrapper for serializing arrays and POD.
394-
*/
395-
class CFlatData
396-
{
397-
protected:
398-
char* pbegin;
399-
char* pend;
400-
public:
401-
CFlatData(void* pbeginIn, void* pendIn) : pbegin((char*)pbeginIn), pend((char*)pendIn) { }
402-
template <class T, class TAl>
403-
explicit CFlatData(std::vector<T,TAl> &v)
404-
{
405-
pbegin = (char*)v.data();
406-
pend = (char*)(v.data() + v.size());
407-
}
408-
template <unsigned int N, typename T, typename S, typename D>
409-
explicit CFlatData(prevector<N, T, S, D> &v)
410-
{
411-
pbegin = (char*)v.data();
412-
pend = (char*)(v.data() + v.size());
413-
}
414-
char* begin() { return pbegin; }
415-
const char* begin() const { return pbegin; }
416-
char* end() { return pend; }
417-
const char* end() const { return pend; }
418-
419-
template<typename Stream>
420-
void Serialize(Stream& s) const
421-
{
422-
s.write(pbegin, pend - pbegin);
423-
}
424-
425-
template<typename Stream>
426-
void Unserialize(Stream& s)
427-
{
428-
s.read(pbegin, pend - pbegin);
429-
}
430-
};
431-
432395
template<VarIntMode Mode, typename I>
433396
class CVarInt
434397
{

src/span.h

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
// Copyright (c) 2018 The Bitcoin Core developers
2+
// Distributed under the MIT software license, see the accompanying
3+
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
4+
5+
#ifndef BITCOIN_SPAN_H
6+
#define BITCOIN_SPAN_H
7+
8+
#include <type_traits>
9+
#include <cstddef>
10+
11+
/** A Span is an object that can refer to a contiguous sequence of objects.
12+
*
13+
* It implements a subset of C++20's std::span.
14+
*/
15+
template<typename C>
16+
class Span
17+
{
18+
C* m_data;
19+
std::ptrdiff_t m_size;
20+
21+
public:
22+
constexpr Span() noexcept : m_data(nullptr), m_size(0) {}
23+
constexpr Span(C* data, std::ptrdiff_t size) noexcept : m_data(data), m_size(size) {}
24+
25+
constexpr C* data() const noexcept { return m_data; }
26+
constexpr std::ptrdiff_t size() const noexcept { return m_size; }
27+
};
28+
29+
/** Create a span to a container exposing data() and size().
30+
*
31+
* This correctly deals with constness: the returned Span's element type will be
32+
* whatever data() returns a pointer to. If either the passed container is const,
33+
* or its element type is const, the resulting span will have a const element type.
34+
*
35+
* std::span will have a constructor that implements this functionality directly.
36+
*/
37+
template<typename V>
38+
constexpr Span<typename std::remove_pointer<decltype(std::declval<V>().data())>::type> MakeSpan(V& v) { return Span<typename std::remove_pointer<decltype(std::declval<V>().data())>::type>(v.data(), v.size()); }
39+
40+
#endif

0 commit comments

Comments
 (0)