Skip to content

Commit

Permalink
Major optimization for updating docs with large numbers of revisions
Browse files Browse the repository at this point in the history
Use an O(1) NSDictionary instead of an O(n) CBL_RevisionList to look up
locally-known revisions by revID when inserting an existing revision.

In the database I'm testing with, this sped up the entire replication
by a factor of 8x! (From ~55sec down to ~7sec.)
  • Loading branch information
snej committed Apr 24, 2015
1 parent 5c6eed4 commit 5b98b28
Showing 1 changed file with 10 additions and 7 deletions.
17 changes: 10 additions & 7 deletions Source/CBL_SQLiteStorage.m
Original file line number Diff line number Diff line change
Expand Up @@ -1625,19 +1625,22 @@ - (CBLStatus) forceInsert: (CBL_Revision*)inRev
__block BOOL inConflict = NO;
CBLStatus status = [self inTransaction: ^CBLStatus {
// First look up the document's row-id and all locally-known revisions of it:
CBL_RevisionList* localRevs = nil;
NSMutableDictionary* localRevs = nil;
NSString* oldWinningRevID = nil;
BOOL oldWinnerWasDeletion = NO;
BOOL isNewDoc = (history.count == 1);
SInt64 docNumericID = [self createOrGetDocNumericID: docID isNew: &isNewDoc];
if (docNumericID <= 0)
return self.lastDbError;
if (!isNewDoc) {
localRevs = [self getAllRevisionsOfDocumentID: docID
numericID: docNumericID
onlyCurrent: NO];
if (!localRevs)
CBL_RevisionList* localRevsList = [self getAllRevisionsOfDocumentID: docID
numericID: docNumericID
onlyCurrent: NO];
if (!localRevsList)
return self.lastDbError;
localRevs = [[NSMutableDictionary alloc] initWithCapacity: localRevsList.count];
for (CBL_Revision* rev in localRevsList)
localRevs[rev.revID] = rev;

// Look up which rev is the winner, before this insertion
CBLStatus tempStatus;
Expand All @@ -1653,7 +1656,7 @@ - (CBLStatus) forceInsert: (CBL_Revision*)inRev
if (validationBlock) {
CBL_Revision* oldRev = nil;
for (NSUInteger i = 1; i<history.count; ++i) {
oldRev = [localRevs revWithDocID: docID revID: history[i]];
oldRev = localRevs[history[i]];
if (oldRev)
break;
}
Expand All @@ -1670,7 +1673,7 @@ - (CBLStatus) forceInsert: (CBL_Revision*)inRev
SequenceNumber localParentSequence = 0;
for (NSInteger i = history.count - 1; i>=0; --i) {
NSString* revID = history[i];
CBL_Revision* localRev = [localRevs revWithDocID: docID revID: revID];
CBL_Revision* localRev = localRevs[revID];
if (localRev) {
// This revision is known locally. Remember its sequence as the parent of the next one:
sequence = localRev.sequence;
Expand Down

0 comments on commit 5b98b28

Please sign in to comment.