Skip to content

Commit

Permalink
Merge pull request #179 from animetosho/avoid_checksum_copy
Browse files Browse the repository at this point in the history
Avoid copying back memory in FileChecksummer when data is valid
  • Loading branch information
BlackIkeEagle authored May 31, 2023
2 parents 277cda9 + 566dd42 commit ff990c0
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 @@ -133,14 +133,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 @@ -154,7 +160,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 @@ -102,7 +102,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 @@ -155,6 +156,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 @@ -169,13 +175,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 ff990c0

Please sign in to comment.