mirror of
https://github.com/bitcoin/bitcoin.git
synced 2025-11-10 05:57:59 +01:00
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>
This commit is contained in:
@@ -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;
|
||||
|
||||
Reference in New Issue
Block a user