Browse files

Merge pull request #2 from trofi/master

Achieve 25% speedup in '--all-in-ram' mode
  • Loading branch information...
2 parents c739523 + da6e18a commit f1993547d3351181979cbed7786002cd03bfc7d0 @jjwhitney committed Oct 31, 2012
Showing with 40 additions and 26 deletions.
  1. +4 −0 src/Makefile
  2. +18 −9 src/bdelta.cpp
  3. +1 −1 src/bdelta.h
  4. +1 −1 src/checksum.h
  5. +16 −15 src/libbdelta.cpp
View
4 src/Makefile
@@ -8,6 +8,10 @@ ALL_TARGETS = bpatch libbdelta.so bdelta
all: $(ALL_TARGETS)
+libbdelta.a: libbdelta.cpp compatibility.h checksum.h file.h
+ $(CXX) -c $(CXXFLAGS) $(LDFLAGS) $< -o $<.o
+ $(AR) rfs $@ $<.o
+
libbdelta.so: libbdelta.cpp compatibility.h checksum.h file.h
$(CXX) -shared -fPIC $(CXXFLAGS) $(LDFLAGS) $< -o $@
View
27 src/bdelta.cpp
@@ -21,15 +21,24 @@
#include "file.h"
#include "compatibility.h"
-void *f_read(void *f, void *buf, unsigned place, unsigned num) {
+const void *f_read(void *f, void *buf, unsigned place, unsigned num) {
fseek((FILE *)f, place, SEEK_SET);
fread_fixed((FILE *)f, buf, num);
return buf;
}
-void *m_read(void *f, void *buf, unsigned place, unsigned num) {
- memcpy (buf, (char*)f + place, num);
- return buf;
+const void *m_read(void *f, void * buf, unsigned place, unsigned num) {
+ if (0) {
+ /*
+ * BDelta uses only returned pointer
+ * and does not modify it's contents.
+ *
+ * But bugs happen.
+ */
+ memcpy (buf, (char*)f + place, num);
+ return buf;
+ }
+ return (const char*)f + place;
}
void my_pass(BDelta_Instance *b, unsigned blocksize, unsigned minMatchSize, unsigned flags) {
@@ -92,7 +101,6 @@ int main(int argc, char **argv) {
// 141-160 811 821 823 827 829 839 853 857 859 863 877 881 883 887 907 911 919 929 937 941
// 161-180 947 953 967 971 977 983 991 997
- int seq[] = {503, 127, 31, 7, 5, 3, -31, 31, 7, 5, 3, -7, 2};
my_pass(b, 997, 1994, 0);
my_pass(b, 503, 1006, 0);
my_pass(b, 127, 254, 0);
@@ -158,13 +166,14 @@ int main(int argc, char **argv) {
for (int i = 0; i < nummatches; ++i) {
unsigned num = copyloc2[i];
while (num > 0) {
- unsigned towrite = (num > 4096) ? 4096 : num;
+ unsigned towrite = (num > 4096) ? 4096 : num;
unsigned char buf[4096];
+ const void * b;
if (all_ram_mode)
- m_read(m2, buf, fp, towrite);
+ b = m_read(m2, buf, fp, towrite);
else
- f_read(f2, buf, fp, towrite);
- fwrite_fixed(fout, buf, towrite);
+ b = f_read(f2, buf, fp, towrite);
+ fwrite_fixed(fout, b, towrite);
num -= towrite;
fp += towrite;
}
View
2 src/bdelta.h
@@ -22,7 +22,7 @@ typedef struct _BDelta_Instance BDelta_Instance;
// Callback function must return a pointer to the data requested.
// A "fill and forget" buffer is provided, but can be ignored, so
// long as the data persists throughout the life of bdelta_pass().
-typedef void *(*bdelta_readCallback)(void *handle, void *buf, unsigned place, unsigned num);
+typedef const void *(*bdelta_readCallback)(void *handle, void *buf, unsigned place, unsigned num);
BDelta_Instance *bdelta_init_alg(unsigned data1_size, unsigned data2_size,
bdelta_readCallback cb, void *handle1, void *handle2,
View
2 src/checksum.h
@@ -18,7 +18,7 @@ struct Hash {
public:
typedef uint64_t Value;
Hash() {}
- Hash(Token *buf, unsigned blocksize) {
+ Hash(const Token *buf, unsigned blocksize) {
value = 0;
for (unsigned num = 0; num < blocksize; ++num)
advance_add(buf[num]);
View
31 src/libbdelta.cpp
@@ -58,10 +58,10 @@ struct _BDelta_Instance {
int access_int;
int errorcode;
- Token *read1(void *buf, unsigned place, unsigned num)
- {return (Token*)cb(handle1, buf, place, num);}
- Token *read2(void *buf, unsigned place, unsigned num)
- {return (Token*)cb(handle2, buf, place, num);}
+ const Token *read1(void *buf, unsigned place, unsigned num)
+ {return (const Token*)cb(handle1, buf, place, num);}
+ const Token *read2(void *buf, unsigned place, unsigned num)
+ {return (const Token*)cb(handle2, buf, place, num);}
};
struct Checksums_Instance {
@@ -82,15 +82,15 @@ struct Checksums_Instance {
};
-unsigned match_buf_forward(void *buf1, void *buf2, unsigned num) {
+unsigned match_buf_forward(const void *buf1, const void *buf2, unsigned num) {
unsigned i = 0;
- while (i < num && ((Token*)buf1)[i]==((Token*)buf2)[i]) ++i;
+ while (i < num && ((const Token*)buf1)[i]==((const Token*)buf2)[i]) ++i;
return i;
}
-unsigned match_buf_backward(void *buf1, void *buf2, unsigned num) {
+unsigned match_buf_backward(const void *buf1, const void *buf2, unsigned num) {
int i = num;
do --i;
- while (i >= 0 && ((Token*)buf1)[i] == ((Token*)buf2)[i]);
+ while (i >= 0 && ((const Token*)buf1)[i] == ((const Token*)buf2)[i]);
return num - i - 1;
}
unsigned match_forward(BDelta_Instance *b, unsigned p1, unsigned p2) {
@@ -99,8 +99,8 @@ unsigned match_forward(BDelta_Instance *b, unsigned p1, unsigned p2) {
numtoread = std::min(b->data1_size - p1, b->data2_size - p2);
if (numtoread > 4096) numtoread = 4096;
Token buf1[4096], buf2[4096];
- Token *read1 = b->read1(buf1, p1, numtoread),
- *read2 = b->read2(buf2, p2, numtoread);
+ const Token *read1 = b->read1(buf1, p1, numtoread),
+ *read2 = b->read2(buf2, p2, numtoread);
p1 += numtoread; p2 += numtoread;
match = match_buf_forward(read1, read2, numtoread);
num += match;
@@ -113,10 +113,11 @@ unsigned match_backward(BDelta_Instance *b, unsigned p1, unsigned p2, unsigned b
do {
numtoread = std::min(p1, p2);
if (numtoread > blocksize) numtoread = blocksize;
+ if (numtoread > 4096) numtoread = 4096;
p1 -= numtoread; p2 -= numtoread;
Token buf1[4096], buf2[4096];
- Token *read1 = b->read1(buf1, p1, numtoread),
- *read2 = b->read2(buf2, p2, numtoread);
+ const Token *read1 = b->read1(buf1, p1, numtoread),
+ *read2 = b->read2(buf2, p2, numtoread);
match = match_buf_backward(read1, read2, numtoread);
num += match;
} while (match && match == numtoread);
@@ -149,8 +150,8 @@ void findMatches(BDelta_Instance *b, Checksums_Instance *h, unsigned minMatchSiz
unsigned best1, best2, bestnum = 0;
unsigned processMatchesPos;
- Token *inbuf = b->read2(buf1, start, blocksize),
- *outbuf;
+ const Token *inbuf = b->read2(buf1, start, blocksize),
+ *outbuf;
Hash hash = Hash(inbuf, blocksize);
unsigned buf_loc = blocksize;
for (unsigned j = start + blocksize; j <= end; ++j) {
@@ -307,7 +308,7 @@ void bdelta_pass_2(BDelta_Instance *b, unsigned blocksize, unsigned minMatchSize
for (unsigned i = 0; i < numunused; ++i) {
unsigned first = unused[i].p, last = unused[i].p + unused[i].num;
for (unsigned loc = first; loc + blocksize <= last; loc += blocksize) {
- Token *read = b->read1(buf, loc, blocksize);
+ const Token *read = b->read1(buf, loc, blocksize);
Hash::Value blocksum = Hash(read, blocksize).getValue();
// Adjacent checksums are never repeated.
//if (! h.numchecksums || blocksum != h.checksums[h.numchecksums - 1].cksum)

0 comments on commit f199354

Please sign in to comment.