This is likely a controversial change, and will need very careful review and testing.
It adds a new basic data type (prevector<N, T>
) which is a fully API compatible drop-in replacement for std::vector<T>
(except it does not support custom allocators, does not have at()
, and misses a few comparison operators). It will allocate up to N elements inside the parent container directly, only switching over to heap-allocated storage if more is needed. The data structures for the N elements and the pointer/size metadata for the heap-allocated storage are shared, making it very efficient for small N.
CScript is switched to use this new type, reducing the memory consumption of mempool and chainstate.
A benchmark (reindex with script validation disabled):
02015-10-30 01:10:49 - Load block from disk: 0.00ms [0.07s]
12015-10-30 01:10:49 - Connect 595 transactions: 2.76ms (0.005ms/tx, 0.003ms/txin) [82.40s]
22015-10-30 01:10:49 - Verify 1096 txins: 2.80ms (0.003ms/txin) [88.73s]
32015-10-30 01:10:49 - Index writing: 1.29ms [61.31s]
42015-10-30 01:10:49 - Callbacks: 0.04ms [6.78s]
52015-10-30 01:10:49 - Connect total: 4.78ms [182.16s]
62015-10-30 01:10:49 - Flush: 0.77ms [38.90s]
72015-10-30 01:10:49 - Writing chainstate: 0.03ms [5.57s]
82015-10-30 01:10:49 UpdateTip: new best=000000000000046b1fdf00440a86e34886b2b0bd56472b22edb2805c92bbe4ca height=223133 log2_work=69.460921 tx=13407260 date=2013-02-25 23:16:10 progress=0
9.064773 cache=666.5MiB(1658277tx)
102015-10-30 01:10:49 - Connect postprocess: 1.46ms [46.22s]
112015-10-30 01:10:49 - Connect block: 7.03ms [272.92s]
12
132015-10-30 01:22:03 - Connect 595 transactions: 2.86ms (0.005ms/tx, 0.003ms/txin) [69.53s]
142015-10-30 01:22:03 - Verify 1096 txins: 2.90ms (0.003ms/txin) [75.80s]
152015-10-30 01:22:03 - Index writing: 1.26ms [56.37s]
162015-10-30 01:22:03 - Callbacks: 0.04ms [6.77s]
172015-10-30 01:22:03 - Connect total: 4.87ms [164.33s]
182015-10-30 01:22:03 - Flush: 1.14ms [24.95s]
192015-10-30 01:22:03 - Writing chainstate: 0.03ms [5.12s]
202015-10-30 01:22:03 UpdateTip: new best=000000000000046b1fdf00440a86e34886b2b0bd56472b22edb2805c92bbe4ca height=223133 log2_work=69.460921 tx=13407260 date=2013-02-25 23:16:10 progress=0
21.064773 cache=508.6MiB(1658277tx)
222015-10-30 01:22:03 - Connect postprocess: 1.36ms [43.18s]
232015-10-30 01:22:03 - Connect block: 7.41ms [237.66s]
So for this reindex up to 223133, the chainstate needs 23% less memory, and is 13% faster.
There are likely several other places where this datatype can be used.