Demonstrate HN: C++ virtual_vec vector implementation

Demonstrate HN: C++ virtual_vec vector implementation


virtual_vec is a form compatible with std::vector. The implementation targets extensive vectors that (1) are pushed to continuously and (2) the size is now not identified, (3) web a mounted memory upper inch. This guarantees O(1) for appending to the dwell. Attain gift the reason for this implementation is experimentation, and it has now not been tested in a production system.


When the vector is instantiated, it reserves a extensive chunk of virtual memory addresses from the operating system. Reminiscence from this tackle space is dedicated to our job because the vector grows. We below no conditions want to reproduction the full vector to a unique tackle space.

This implementation is hardcoded to make exhaust of 4GB virtual tackle for every vector, so that you may web hundreds of these per job. The quantity used to be chosen arbitrarily and may possibly additionally be made bigger or smaller depending to your exhaust case. For evident causes here is fully acceptable to 64-bit systems.


NOTE: Appropriate now this fully builds on Linux (would gladly accept patches to augment assorted OSes). To construct this you correct want to consist of virtual_vec.h and virtual_vec.cpp and a c++17 (or newer) compiler.

Working tests

gtest will web to be save in.

To flee the tests:

To flee the tests below valgrind:

Working benchmarks

Google benchmark will web to be save in.


Below is a table of benchmarks evaluating the performance of virtual_vec to std::vector. The check calls push_back till # elems / sizeof(int64_t) exceeds Storage size.

All benchmarks are carried out on a Intel i7-8665U (8) @ 4.800GHz. Time is recorded in CPU time.

One ingredient to agree with in tips is these benchmarks were carried out with -O2. With -O3 std::vector will get 10% sooner.

Storage size std::vector (ns) virtual_vec (ns) diff (%)
512B 1221 755 38
1KB 2248 1627 27
8KB 16635 12635 24
32KB 85878 38748 54
131KB 375485 234833 37
524KB 1534855 798632 47
1MB 2940128 1276827 56
4MB 13290644 6129833 53
8MB 29162281 12560318 56
10MB 39694352 13500909 65

Read Extra

Leave a Reply

Your email address will not be published. Required fields are marked *