Skip to content

Commit

Permalink
bdelta: add '--all-in-ram' commandline option
Browse files Browse the repository at this point in the history
$ time ./bdelta /tmp/foo.{old,new} foo-old-to-new.bdt;  time ./bdelta --all-in-ram /tmp/foo.{old,new} foo-old-to-new.bdt

real    3m19.176s
user    2m1.324s
sys     1m17.076s

real    1m46.074s
user    1m41.454s
sys     0m3.669s

File sizes are ~80 megabytes each.
The option greatly reduces I/O overhead (sys time) and speeds up delta creation.

Signed-off-by: Sergei Trofimovich <slyfox@gentoo.org>
  • Loading branch information
Sergei Trofimovich committed Sep 26, 2012
1 parent fb0a916 commit 47d4b35
Showing 1 changed file with 40 additions and 3 deletions.
43 changes: 40 additions & 3 deletions src/bdelta.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include "bdelta.h"
#include "file.h"
#include "compatibility.h"
Expand All @@ -25,15 +27,30 @@ void *f_read(void *f, void *buf, unsigned place, unsigned num) {
return buf;
}

void *m_read(void *f, void *buf, unsigned place, unsigned num) {
memcpy (buf, (char*)f + place, num);
return buf;
}

void my_pass(BDelta_Instance *b, unsigned blocksize, unsigned minMatchSize, unsigned flags) {
bdelta_pass(b, blocksize, minMatchSize, 0, flags);
bdelta_clean_matches(b, BDELTA_REMOVE_OVERLAP);
}

int main(int argc, char **argv) {
try {
bool all_ram_mode = false;
char * m1 = NULL;
char * m2 = NULL;

if (argc > 1 && strcmp(argv[1], "--all-in-ram") == 0)
{
all_ram_mode = true;
--argc;
++argv;
}
if (argc != 4) {
printf("usage: bdelta <oldfile> <newfile> <patchfile>\n");
printf("usage: bdelta [--all-in-ram] <oldfile> <newfile> <patchfile>\n");
printf("needs two files to compare + output file:\n");
exit(1);
}
Expand All @@ -45,8 +62,22 @@ int main(int argc, char **argv) {
unsigned size2 = getLenOfFile(argv[2]);
FILE *f1 = fopen(argv[1], "rb"),
*f2 = fopen(argv[2], "rb");

BDelta_Instance *b;

BDelta_Instance *b = bdelta_init_alg(size, size2, f_read, f1, f2, 1);
if (all_ram_mode)
{
m1 = new char[size];
m2 = new char[size2];
fread_fixed(f1, m1, size);
fread_fixed(f2, m2, size2);

b = bdelta_init_alg(size, size2, m_read, m1, m2, 1);
}
else
{
b = bdelta_init_alg(size, size2, f_read, f1, f2, 1);
}
int nummatches;

// List of primes for reference. Taken from Wikipedia.
Expand Down Expand Up @@ -129,7 +160,10 @@ int main(int argc, char **argv) {
while (num > 0) {
unsigned towrite = (num > 4096) ? 4096 : num;
unsigned char buf[4096];
f_read(f2, buf, fp, towrite);
if (all_ram_mode)
m_read(m2, buf, fp, towrite);
else
f_read(f2, buf, fp, towrite);
fwrite_fixed(fout, buf, towrite);
num -= towrite;
fp += towrite;
Expand All @@ -142,6 +176,9 @@ int main(int argc, char **argv) {

bdelta_done_alg(b);

delete [] m1;
delete [] m2;

fclose(f1);
fclose(f2);

Expand Down

0 comments on commit 47d4b35

Please sign in to comment.