Commit 86b6e6f
perf(ilp): replace O(N²) SF segment sort at open
SegmentLog.scanDirectory used insertion sort over the segments list. At
the documented sf_max_total_bytes / sf_max_bytes ceiling (1 TiB / 64
MiB ≈ 16K segments) that is ~268M comparisons + array shifts → multi-
second wall time before the I/O thread can start.
Replaced with an in-place quicksort with median-of-three pivot. O(N log
N) average, no allocation (matching the surrounding code's discipline),
recursion depth bounded by ~2 log₂(N) by always recursing into the
smaller partition and looping on the larger.
Median-of-three is required because the insertion sort's only saving
grace was O(N) on already-sorted input, which is the common case from
readdir on filesystems that return entries in lexicographic (and
therefore baseSeq-hex) order. A naive first-element-pivot quicksort
would degrade back to O(N²) in exactly that scenario.
Tests:
- testLargeSegmentCountReopensInOrder (SegmentLogTortureTest):
generates 1024 sealed segments, reopens, asserts the replay returns
every appended seq exactly once in order, and that reopen+replay
completes within a generous wall-clock bound that catches a
regression back to O(N²) at the production ceiling.
- Full SF + adjacent suite: 117/117 pass.
Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>1 parent cf3152d commit 86b6e6f
2 files changed
Lines changed: 125 additions & 12 deletions
File tree
- core/src
- main/java/io/questdb/client/cutlass/qwp/client/sf
- test/java/io/questdb/client/test/cutlass/qwp/client/sf
Lines changed: 60 additions & 12 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
404 | 404 | | |
405 | 405 | | |
406 | 406 | | |
407 | | - | |
408 | | - | |
409 | | - | |
410 | | - | |
411 | | - | |
412 | | - | |
413 | | - | |
414 | | - | |
415 | | - | |
416 | | - | |
417 | | - | |
418 | | - | |
| 407 | + | |
| 408 | + | |
| 409 | + | |
| 410 | + | |
| 411 | + | |
| 412 | + | |
| 413 | + | |
419 | 414 | | |
420 | 415 | | |
421 | 416 | | |
| |||
698 | 693 | | |
699 | 694 | | |
700 | 695 | | |
| 696 | + | |
| 697 | + | |
| 698 | + | |
| 699 | + | |
| 700 | + | |
| 701 | + | |
| 702 | + | |
| 703 | + | |
| 704 | + | |
| 705 | + | |
| 706 | + | |
| 707 | + | |
| 708 | + | |
| 709 | + | |
| 710 | + | |
| 711 | + | |
| 712 | + | |
| 713 | + | |
| 714 | + | |
| 715 | + | |
| 716 | + | |
| 717 | + | |
| 718 | + | |
| 719 | + | |
| 720 | + | |
| 721 | + | |
| 722 | + | |
| 723 | + | |
| 724 | + | |
| 725 | + | |
| 726 | + | |
| 727 | + | |
| 728 | + | |
| 729 | + | |
| 730 | + | |
| 731 | + | |
| 732 | + | |
| 733 | + | |
| 734 | + | |
| 735 | + | |
| 736 | + | |
| 737 | + | |
| 738 | + | |
| 739 | + | |
| 740 | + | |
| 741 | + | |
| 742 | + | |
| 743 | + | |
| 744 | + | |
| 745 | + | |
| 746 | + | |
| 747 | + | |
| 748 | + | |
701 | 749 | | |
702 | 750 | | |
703 | 751 | | |
| |||
Lines changed: 65 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
479 | 479 | | |
480 | 480 | | |
481 | 481 | | |
| 482 | + | |
| 483 | + | |
| 484 | + | |
| 485 | + | |
| 486 | + | |
| 487 | + | |
| 488 | + | |
| 489 | + | |
| 490 | + | |
| 491 | + | |
| 492 | + | |
| 493 | + | |
| 494 | + | |
| 495 | + | |
| 496 | + | |
| 497 | + | |
| 498 | + | |
| 499 | + | |
| 500 | + | |
| 501 | + | |
| 502 | + | |
| 503 | + | |
| 504 | + | |
| 505 | + | |
| 506 | + | |
| 507 | + | |
| 508 | + | |
| 509 | + | |
| 510 | + | |
| 511 | + | |
| 512 | + | |
| 513 | + | |
| 514 | + | |
| 515 | + | |
| 516 | + | |
| 517 | + | |
| 518 | + | |
| 519 | + | |
| 520 | + | |
| 521 | + | |
| 522 | + | |
| 523 | + | |
| 524 | + | |
| 525 | + | |
| 526 | + | |
| 527 | + | |
| 528 | + | |
| 529 | + | |
| 530 | + | |
| 531 | + | |
| 532 | + | |
| 533 | + | |
| 534 | + | |
| 535 | + | |
| 536 | + | |
| 537 | + | |
| 538 | + | |
| 539 | + | |
| 540 | + | |
| 541 | + | |
| 542 | + | |
| 543 | + | |
| 544 | + | |
| 545 | + | |
| 546 | + | |
482 | 547 | | |
483 | 548 | | |
484 | 549 | | |
| |||
0 commit comments