Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improves bsdiff performance by preventing excessive iterations when processing similar data blocks #1

Open
wants to merge 2 commits into
base: 2.x
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 21 additions & 2 deletions Vendor/bsdiff/bsdiff.c
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ static off_t search(off_t *I, u_char *old, off_t oldsize,
}

x = st + (en - st)/2;
if (memcmp(old + I[x], new, (size_t)(MIN(oldsize - I[x], newsize))) < 0) {
if (memcmp(old + I[x], new, (size_t)(MIN(oldsize - I[x], newsize))) <= 0) {
return search(I, old, oldsize, new, newsize, x, en, pos);
} else {
return search(I, old, oldsize, new, newsize, st, x, pos);
Expand Down Expand Up @@ -212,8 +212,16 @@ int bsdiff(int argc, char *argv[])
lastoffset = 0;
while (scan < newsize) {
oldscore = 0;

/* If we come across a large block of data that only differs
* by less than 8 bytes, this loop will take a long time to
* go past that block of data. We need to track the number of
* times we're stuck in the block and break out of it. */
int num_less_than_eight = 0;
off_t prev_len, prev_oldscore, prev_pos;
for (scsc = scan += len; scan < newsize; scan++) {
prev_len = len;
prev_oldscore = oldscore;
prev_pos = pos;
/* 'oldscore' is the number of characters that match between the
* substrings 'old[lastoffset + scan:lastoffset + scsc]' and
* 'new[scan:scsc]'. */
Expand All @@ -240,6 +248,17 @@ int bsdiff(int argc, char *argv[])
if ((scan + lastoffset < oldsize) &&
(old[scan + lastoffset] == new[scan]))
oldscore--;

const off_t fuzz = 8;
if (prev_len - fuzz <= len && len <= prev_len &&
prev_oldscore - fuzz <= oldscore &&
oldscore <= prev_oldscore &&
prev_pos <= pos && pos <= prev_pos + fuzz &&
oldscore <= len && len <= oldscore + fuzz)
++num_less_than_eight;
else
num_less_than_eight=0;
if (num_less_than_eight > 100) break;
}

/* Skip this section if we found an exact match that would be
Expand Down
Loading