Skip to content

Commit

Permalink
Avoid copying back memory in FileChecksummer when data is valid
Browse files Browse the repository at this point in the history
This may be less efficient than a circular buffer, but is much easier to implement
  • Loading branch information
animetosho committed Mar 16, 2023
1 parent 0b266b5 commit 566dd42
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 7 deletions.
12 changes: 9 additions & 3 deletions src/filechecksummer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -135,14 +135,20 @@ bool FileCheckSummer::Jump(u64 distance)

// Fill the buffer from disk

bool FileCheckSummer::Fill(void)
bool FileCheckSummer::Fill(bool longfill)
{
// Have we already reached the end of the file
if (readoffset >= filesize)
return true;

// Don't bother filling if we have enough data in the buffer
if (tailpointer >= &buffer[blocksize] && !longfill)
return true;

// Try reading at least one block of data
const char *target = tailpointer == buffer ? &buffer[blocksize] : &buffer[2*blocksize];
// How much data can we read into the buffer
size_t want = (size_t)min(filesize-readoffset, (u64)(&buffer[2*blocksize]-tailpointer));
size_t want = (size_t)min(filesize-readoffset, (u64)(target-tailpointer));

if (want > 0)
{
Expand All @@ -156,7 +162,7 @@ bool FileCheckSummer::Fill(void)
}

// Did we fill the buffer
want = &buffer[2*blocksize] - tailpointer;
want = target - tailpointer;
if (want > 0)
{
// Blank the rest of the buffer
Expand Down
13 changes: 9 additions & 4 deletions src/filechecksummer.h
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,8 @@ class FileCheckSummer
void UpdateHashes(u64 offset, const void *buffer, size_t length);

//// Fill the buffers with more data from disk
bool Fill(void);
// Set longfill = true to force fill the whole buffer
bool Fill(bool longfill = false);

private:
// private copy constructor to prevent any misuse.
Expand Down Expand Up @@ -157,6 +158,11 @@ inline bool FileCheckSummer::Step(void)
return true;
}

// Ensure we have enough data in the buffer
if (tailpointer <= inpointer)
if(!Fill(true))
return false;

// Get the incoming and outgoing characters
char inch = *inpointer++;
char outch = *outpointer++;
Expand All @@ -171,13 +177,12 @@ inline bool FileCheckSummer::Step(void)
assert(outpointer == &buffer[blocksize]);

// Copy the data back to the beginning of the buffer
memmove(buffer, outpointer, (size_t)blocksize);
memcpy(buffer, outpointer, (size_t)blocksize);
inpointer = outpointer;
outpointer = buffer;
tailpointer -= blocksize;

// Fill the rest of the buffer
return Fill();
return true;
}


Expand Down

0 comments on commit 566dd42

Please sign in to comment.