Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
MB-52131: Optimize CheckpointCursor::operator< from O(num_items) to O(1)
CheckpointCursor's operator< is currently O(n) in the case where two cursors are pointing at the same item. This is due to the fact that if they point to the same Checkpoint and seqno, we must check their distance along the Checkpoint (as one may be pointing to a meta-item which shares a seqno but it ahread of the cursor) - however to do so requires calling std::distance(begin(), cursorIterator), and as the CheckpointCursor itorator is not random access (it's essentially a std::list::iterator) we must perform an O(n) iteration from the beginning of the container to the cursor. This results in the following performance characteristics as the Checkpoint grows in items: Running ./ep_engine_benchmarks Run on (10 X 24.0102 MHz CPU s) CPU Caches: L1 Data 64 KiB (x10) L1 Instruction 128 KiB (x10) L2 Unified 4096 KiB (x5) Load Average: 6.28, 11.37, 18.63 --------------------------------------------------------------------------------- Benchmark Time CPU Iterations --------------------------------------------------------------------------------- CheckpointBench/GetLowestCursor/1 13.8 ns 13.7 ns 51002565 CheckpointBench/GetLowestCursor/10 23.5 ns 23.4 ns 30190632 CheckpointBench/GetLowestCursor/100 200 ns 200 ns 3489427 CheckpointBench/GetLowestCursor/1000 2115 ns 2112 ns 332576 CheckpointBench/GetLowestCursor/10000 37857 ns 37799 ns 18850 CheckpointBench/GetLowestCursor/100000 270262 ns 269972 ns 2537 However, as part of patch "MB-48506: Introduce CheckpointCursor::distance" (74b9cf4) we added a 'distance' member to each CheckpointCursor which caches its distance from the start of the container, and is updated whenever a cursor moves. As such, we can use the cached distanece value in heckpointCursor::operator<, reducing the cost to O(1) in the case where two cursors point to the same item: Running ./ep_engine_benchmarks Run on (10 X 24.1207 MHz CPU s) CPU Caches: L1 Data 64 KiB (x10) L1 Instruction 128 KiB (x10) L2 Unified 4096 KiB (x5) Load Average: 7.73, 12.32, 19.30 --------------------------------------------------------------------------------- Benchmark Time CPU Iterations --------------------------------------------------------------------------------- CheckpointBench/GetLowestCursor/1 13.8 ns 13.8 ns 50626686 CheckpointBench/GetLowestCursor/10 13.9 ns 13.8 ns 50958011 CheckpointBench/GetLowestCursor/100 13.8 ns 13.8 ns 50932798 CheckpointBench/GetLowestCursor/1000 13.9 ns 13.9 ns 50392340 CheckpointBench/GetLowestCursor/10000 14.0 ns 14.0 ns 50426827 CheckpointBench/GetLowestCursor/100000 13.9 ns 13.8 ns 50859526 (cherry picked from commit 5b28136) Change-Id: I1814155d7eff9773ab29c30f6b76836963a75ac8 Reviewed-on: https://review.couchbase.org/c/kv_engine/+/174849 Well-Formed: Restriction Checker Tested-by: Build Bot <build@couchbase.com> Reviewed-by: Paolo Cocchi <paolo.cocchi@couchbase.com>
- Loading branch information