aboutsummaryrefslogtreecommitdiff
path: root/src/compressor.h
diff options
context:
space:
mode:
authorWilliam Casarin <jb55@jb55.com>2020-05-01 17:31:38 -0700
committerWilliam Casarin <jb55@jb55.com>2020-05-15 15:26:54 -0700
commit83a425d25af033086744c1c8c892015014ed46bd (patch)
tree1fd1094b3c5c37aa900b49b496e0d1c40468b7b6 /src/compressor.h
parent844d2070a2c0106bb7a54be5cad7d4da4d9cd55e (diff)
downloadbitcoin-83a425d25af033086744c1c8c892015014ed46bd.tar.xz
compressor: use a prevector in compressed script serialization
Use a prevector for stack allocation instead of heap allocation during script compression and decompression. These functions were doing millions of unnecessary heap allocations during IBD. We introduce a CompressedScript type alias for this prevector. It is size 33 as that is the maximum size of a compressed script. Fix the DecompressScript header to match the variable name from compressor.cpp Signed-off-by: William Casarin <jb55@jb55.com>
Diffstat (limited to 'src/compressor.h')
-rw-r--r--src/compressor.h20
1 files changed, 16 insertions, 4 deletions
diff --git a/src/compressor.h b/src/compressor.h
index 478bfff0b6..40b2496f06 100644
--- a/src/compressor.h
+++ b/src/compressor.h
@@ -6,14 +6,26 @@
#ifndef BITCOIN_COMPRESSOR_H
#define BITCOIN_COMPRESSOR_H
+#include <prevector.h>
#include <primitives/transaction.h>
#include <script/script.h>
#include <serialize.h>
#include <span.h>
-bool CompressScript(const CScript& script, std::vector<unsigned char> &out);
+/**
+ * This saves us from making many heap allocations when serializing
+ * and deserializing compressed scripts.
+ *
+ * This prevector size is determined by the largest .resize() in the
+ * CompressScript function. The largest compressed script format is a
+ * compressed public key, which is 33 bytes.
+ */
+using CompressedScript = prevector<33, unsigned char>;
+
+
+bool CompressScript(const CScript& script, CompressedScript& out);
unsigned int GetSpecialScriptSize(unsigned int nSize);
-bool DecompressScript(CScript& script, unsigned int nSize, const std::vector<unsigned char> &out);
+bool DecompressScript(CScript& script, unsigned int nSize, const CompressedScript& in);
/**
* Compress amount.
@@ -51,7 +63,7 @@ struct ScriptCompression
template<typename Stream>
void Ser(Stream &s, const CScript& script) {
- std::vector<unsigned char> compr;
+ CompressedScript compr;
if (CompressScript(script, compr)) {
s << MakeSpan(compr);
return;
@@ -66,7 +78,7 @@ struct ScriptCompression
unsigned int nSize = 0;
s >> VARINT(nSize);
if (nSize < nSpecialScripts) {
- std::vector<unsigned char> vch(GetSpecialScriptSize(nSize), 0x00);
+ CompressedScript vch(GetSpecialScriptSize(nSize), 0x00);
s >> MakeSpan(vch);
DecompressScript(script, nSize, vch);
return;