Commit 5602cc7
committed
Merge bitcoin/bitcoin#16981: Improve runtime performance of --reindex
db92989 Faster -reindex by initially deserializing only headers (Larry Ruane)
c72de99 util: add CBufferedFile::SkipTo() to move ahead in the stream (Larry Ruane)
48a6890 Add LoadExternalBlockFile() benchmark (Larry Ruane)
Pull request description:
### Background
During the first part of reindexing, `LoadExternalBlockFile()` sequentially reads raw blocks from the `blocks/blk00nnn.dat` files (rather than receiving them from peers, as with initial block download) and eventually adds all of them to the block index. When an individual block is initially read, it can't be immediately added unless all its ancestors have been added, which is rare (only about 8% of the time), because the blocks are not sorted by height. When the block can't be immediately added to the block index, its disk location is saved in a map so it can be added later. When its parent is later added to the block index, `LoadExternalBlockFile()` reads and deserializes the block from disk a second time and adds it to the block index. Most blocks (92%) get deserialized twice.
### This PR
During the initial read, it's rarely useful to deserialize the entire block; only the header is needed to determine if the block can be added to the block index immediately. This change to `LoadExternalBlockFile()` initially deserializes only a block's header, then deserializes the entire block only if it can be added immediately. This reduces reindex time on mainnet by 7 hours on a Raspberry Pi, which translates to around a 25% reduction in the first part of reindexing (adding blocks to the index), and about a 6% reduction in overall reindex time.
Summary: The performance gain is the result of deserializing each block only once, except its header which is deserialized twice, but the header is only 80 bytes.
ACKs for top commit:
andrewtoth:
ACK db92989
achow101:
ACK db92989
aureleoules:
ACK db92989 - minor changes and new benchmark since last review
theStack:
re-ACK db92989
stickies-v:
re-ACK db92989
Tree-SHA512: 5a5377192c11edb5b662e18f511c9beb8f250bc88aeadf2f404c92c3232a7617bade50477ebf16c0602b9bd3b68306d3ee7615de58acfd8cae664d28bb7b0136File tree
6 files changed
+238
-37
lines changed- src
- bench
- test
- test/functional
6 files changed
+238
-37
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
32 | 32 | | |
33 | 33 | | |
34 | 34 | | |
| 35 | + | |
35 | 36 | | |
36 | 37 | | |
37 | 38 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
| 29 | + | |
| 30 | + | |
| 31 | + | |
| 32 | + | |
| 33 | + | |
| 34 | + | |
| 35 | + | |
| 36 | + | |
| 37 | + | |
| 38 | + | |
| 39 | + | |
| 40 | + | |
| 41 | + | |
| 42 | + | |
| 43 | + | |
| 44 | + | |
| 45 | + | |
| 46 | + | |
| 47 | + | |
| 48 | + | |
| 49 | + | |
| 50 | + | |
| 51 | + | |
| 52 | + | |
| 53 | + | |
| 54 | + | |
| 55 | + | |
| 56 | + | |
| 57 | + | |
| 58 | + | |
| 59 | + | |
| 60 | + | |
| 61 | + | |
| 62 | + | |
| 63 | + | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
612 | 612 | | |
613 | 613 | | |
614 | 614 | | |
615 | | - | |
616 | 615 | | |
617 | 616 | | |
618 | 617 | | |
| |||
630 | 629 | | |
631 | 630 | | |
632 | 631 | | |
| 632 | + | |
| 633 | + | |
| 634 | + | |
| 635 | + | |
| 636 | + | |
| 637 | + | |
| 638 | + | |
| 639 | + | |
| 640 | + | |
| 641 | + | |
| 642 | + | |
| 643 | + | |
| 644 | + | |
| 645 | + | |
| 646 | + | |
| 647 | + | |
| 648 | + | |
| 649 | + | |
| 650 | + | |
| 651 | + | |
| 652 | + | |
| 653 | + | |
633 | 654 | | |
634 | 655 | | |
635 | 656 | | |
| |||
667 | 688 | | |
668 | 689 | | |
669 | 690 | | |
670 | | - | |
671 | | - | |
672 | | - | |
673 | 691 | | |
674 | | - | |
675 | | - | |
676 | | - | |
677 | | - | |
678 | | - | |
679 | | - | |
680 | | - | |
681 | | - | |
682 | | - | |
683 | | - | |
684 | | - | |
| 692 | + | |
| 693 | + | |
| 694 | + | |
685 | 695 | | |
686 | 696 | | |
687 | 697 | | |
| 698 | + | |
| 699 | + | |
| 700 | + | |
| 701 | + | |
| 702 | + | |
| 703 | + | |
| 704 | + | |
| 705 | + | |
688 | 706 | | |
689 | 707 | | |
690 | 708 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
253 | 253 | | |
254 | 254 | | |
255 | 255 | | |
256 | | - | |
| 256 | + | |
257 | 257 | | |
258 | 258 | | |
259 | 259 | | |
| |||
322 | 322 | | |
323 | 323 | | |
324 | 324 | | |
325 | | - | |
| 325 | + | |
326 | 326 | | |
327 | 327 | | |
328 | 328 | | |
329 | 329 | | |
330 | 330 | | |
331 | 331 | | |
332 | 332 | | |
| 333 | + | |
| 334 | + | |
| 335 | + | |
| 336 | + | |
| 337 | + | |
| 338 | + | |
| 339 | + | |
| 340 | + | |
| 341 | + | |
| 342 | + | |
| 343 | + | |
| 344 | + | |
| 345 | + | |
| 346 | + | |
| 347 | + | |
| 348 | + | |
| 349 | + | |
| 350 | + | |
| 351 | + | |
| 352 | + | |
| 353 | + | |
| 354 | + | |
| 355 | + | |
| 356 | + | |
| 357 | + | |
| 358 | + | |
| 359 | + | |
| 360 | + | |
| 361 | + | |
| 362 | + | |
| 363 | + | |
| 364 | + | |
| 365 | + | |
| 366 | + | |
| 367 | + | |
| 368 | + | |
| 369 | + | |
| 370 | + | |
| 371 | + | |
| 372 | + | |
| 373 | + | |
| 374 | + | |
| 375 | + | |
| 376 | + | |
| 377 | + | |
| 378 | + | |
| 379 | + | |
| 380 | + | |
| 381 | + | |
333 | 382 | | |
334 | 383 | | |
335 | 384 | | |
| |||
361 | 410 | | |
362 | 411 | | |
363 | 412 | | |
364 | | - | |
| 413 | + | |
365 | 414 | | |
366 | 415 | | |
367 | 416 | | |
| |||
399 | 448 | | |
400 | 449 | | |
401 | 450 | | |
| 451 | + | |
| 452 | + | |
| 453 | + | |
| 454 | + | |
| 455 | + | |
| 456 | + | |
| 457 | + | |
| 458 | + | |
| 459 | + | |
| 460 | + | |
402 | 461 | | |
403 | 462 | | |
404 | 463 | | |
| |||
415 | 474 | | |
416 | 475 | | |
417 | 476 | | |
418 | | - | |
| 477 | + | |
419 | 478 | | |
420 | 479 | | |
421 | 480 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
4389 | 4389 | | |
4390 | 4390 | | |
4391 | 4391 | | |
| 4392 | + | |
| 4393 | + | |
4392 | 4394 | | |
4393 | 4395 | | |
4394 | 4396 | | |
| |||
4412 | 4414 | | |
4413 | 4415 | | |
4414 | 4416 | | |
| 4417 | + | |
4415 | 4418 | | |
4416 | 4419 | | |
4417 | 4420 | | |
4418 | | - | |
4419 | | - | |
| 4421 | + | |
| 4422 | + | |
4420 | 4423 | | |
4421 | 4424 | | |
4422 | 4425 | | |
4423 | | - | |
4424 | | - | |
4425 | | - | |
4426 | | - | |
4427 | | - | |
4428 | | - | |
| 4426 | + | |
| 4427 | + | |
| 4428 | + | |
| 4429 | + | |
| 4430 | + | |
| 4431 | + | |
| 4432 | + | |
4429 | 4433 | | |
4430 | 4434 | | |
4431 | 4435 | | |
4432 | | - | |
| 4436 | + | |
4433 | 4437 | | |
4434 | | - | |
| 4438 | + | |
4435 | 4439 | | |
4436 | | - | |
| 4440 | + | |
4437 | 4441 | | |
4438 | 4442 | | |
4439 | 4443 | | |
4440 | 4444 | | |
4441 | 4445 | | |
4442 | 4446 | | |
4443 | 4447 | | |
4444 | | - | |
4445 | | - | |
4446 | | - | |
4447 | | - | |
4448 | | - | |
4449 | | - | |
4450 | | - | |
| 4448 | + | |
| 4449 | + | |
| 4450 | + | |
| 4451 | + | |
| 4452 | + | |
| 4453 | + | |
| 4454 | + | |
| 4455 | + | |
| 4456 | + | |
| 4457 | + | |
| 4458 | + | |
| 4459 | + | |
| 4460 | + | |
4451 | 4461 | | |
4452 | 4462 | | |
4453 | 4463 | | |
| |||
0 commit comments