Skip to content

Sort message identifiers once when chunking - #206

Merged
stevebauman merged 1 commit into
masterfrom
fix/sort-chunked-messages-once
Nov 16, 2026
Merged

stevebauman merged 1 commit into
masterfrom
fix/sort-chunked-messages-once

Conversation

@stevebauman

Copy link
Copy Markdown
Member

Chunking messages currently sorts the entire search result before fetching each page. With 35,798 messages and a chunk size of 10, that sorts the same UID list 3,580 times.

This moves client-side ordering into sortMessages(), which runs once before the chunk loop and before pagination for normal retrieval. It preserves ascending and descending fetch order and the existing handling of server-side sorting. search() continues to return matching UIDs directly, so counting and bulk operations avoid unnecessary sorting.

The following benchmarks compare this change against v1.25.10 on PHP 8.4.27 with CLI OPcache and JIT disabled. They exercise chunk() using synthetic SEARCH responses containing ascending UIDs with gaps, then retrieve messages in the default newest-first order.

UIDs Chunk size Before CPU time After CPU time CPU time reduction
10,000 10 3.018 seconds 0.016 seconds 99.5%
35,798 10 43.886 seconds 0.063 seconds 99.9%
35,798 100 4.354 seconds 0.054 seconds 98.8%
35,798 1,000 0.480 seconds 0.054 seconds 88.9%

Results are averages across six alternating runs per version, except the 35,798-UID workload with chunks of 10, which used two runs per version. Measurements include parsing the in-memory SEARCH response, pagination, and message object construction. Network latency and fetching message bodies or metadata are excluded. Every run verified the returned UIDs, their order, and the chunk count.

All 1,038 unit tests pass.

Refs #200.

@stevebauman
stevebauman merged commit 6f8e1f4 into master Nov 16, 2026
13 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant