Skip to content

DRAFT: Implement IntRangeSet class to be used for IMAP ActiveSync#26

Draft
amulet1 wants to merge 3 commits into
horde:FRAMEWORK_6_0from
horde6:int_range_set
Draft

DRAFT: Implement IntRangeSet class to be used for IMAP ActiveSync#26
amulet1 wants to merge 3 commits into
horde:FRAMEWORK_6_0from
horde6:int_range_set

Conversation

@amulet1
Copy link
Copy Markdown
Member

@amulet1 amulet1 commented Apr 6, 2026

I would like to rework some internals related to how Horde handles IMAP sync.

As this point it is just to introduce a class that could be used to work with UIDs effectively by storing them as sorted array of non-overlaping pairs like these: [start1, end1, start2, end2, ... ].

Based on my preliminary tests it would significantly outperform most array-based operations that are currently used, both memory- and speed-wise. The class is based on my work for an unrelated project.

The class is obviously will be further enhanced/reworked, and moved to the right place. So for how this is just a draft.

The class could be used for servers without MODSEQ too. For that we would have to keep flags differently - instead of keeping flags by UID/type like $flags[$uid]['read'], we would keep separate flags as instance of the IntRangeSet class with UIDs for which the flag is set, e.g.

// just a rough idea
$uids = new IntSetRange();
$read = new IntSetRange();
$flagged = new IntSetRange();
...
$read->add($uid);        // set flag
$read->remove($uid); // remove flag

// check if 'read' flag is set for UID
if ($read->contains($uid)) {
...

}

The class implements several optimized methods that should be helpful for syncing IMAP folders via Activesync:

  • contains - uses binary search to locate specific UID
  • union - add elements of another set to the current set
  • intersect - keep elements that belong to current and another set
  • subtract - remove elements from set that exist in another set.
  • diff (static, optimized) - compares set A with B and returns 3 sets in a single pass: added, unchanged, removed
  • __toString - returns IMAP message sequence string
  • fromString - parses IMAP message sequence string

As the first step, the Horde_ActiveSync_Folder_Imap should be updated to use the new class.

Other potential uses: replace/improve Horde_Imap_Client_Ids class.

Added tests.php and benchmark.php (yes, I know they do not belong here, but this is a draft)
@ralflang
Copy link
Copy Markdown
Member

ralflang commented Apr 7, 2026

This looks promising. Let's sync with Torben when it's in "ready for review" state.

…ated lookups

Fast-append path in addRange for sorted input
O(m+n) two-pointer sweeps for union, subtract, intersect, diff
String initialization via constructor
@ralflang
Copy link
Copy Markdown
Member

@amulet1 This looks pretty good already - do you feel it is ready?

@amulet1
Copy link
Copy Markdown
Member Author

amulet1 commented Apr 16, 2026

It still cannot be incorporated directly into any existing code without some serious changes in IMAP client, Activesync and possible in the Core. I am slowly (not enough time) studying the existing code to see how to fit it better. Plugging it here and there without overall redesign, just to have it, makes little sense.

@TDannhauer
Copy link
Copy Markdown
Contributor

@amulet1 what is your proposal to continue with?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants