Skip to content
This repository was archived by the owner on Apr 18, 2026. It is now read-only.

Commit dd961ae

Browse files
committed
Large files test
1 parent bf89e98 commit dd961ae

2 files changed

Lines changed: 60 additions & 59 deletions

File tree

src/blockhash.cc

Lines changed: 34 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414

1515
#include <config.h>
1616
#include "blockhash.h"
17+
#include <cstddef>
1718
#include <stdint.h> // uint32_t
1819
#include <string.h> // memcpy, memcmp
1920
#include <algorithm> // std::min
@@ -27,7 +28,7 @@ typedef unsigned long uword_t; // a machine word NOLINT
2728

2829
BlockHash::BlockHash(const char* source_data,
2930
size_t source_size,
30-
int starting_offset)
31+
size_t starting_offset)
3132
: source_data_(source_data),
3233
source_size_(source_size),
3334
hash_table_mask_(0),
@@ -95,7 +96,7 @@ BlockHash* BlockHash::CreateTargetHash(const char* target_data,
9596
size_t dictionary_size) {
9697
BlockHash* new_target_hash = new BlockHash(target_data,
9798
target_size,
98-
static_cast<int>(dictionary_size));
99+
static_cast<ptrdiff_t>(dictionary_size));
99100
if (!new_target_hash->Init(/* populate_hash_table = */ false)) {
100101
delete new_target_hash;
101102
return NULL;
@@ -156,9 +157,9 @@ void BlockHash::AddBlock(uint32_t hash_value) {
156157
return;
157158
}
158159
// The initial value of last_block_added_ is -1.
159-
int block_number = last_block_added_ + 1;
160-
const int total_blocks =
161-
static_cast<int>(source_size_ / kBlockSize); // round down
160+
size_t block_number = last_block_added_ + 1;
161+
const size_t total_blocks =
162+
static_cast<size_t>(source_size_ / kBlockSize); // round down
162163
if (block_number >= total_blocks) {
163164
VCD_DFATAL << "BlockHash::AddBlock() called"
164165
" with block number " << block_number
@@ -174,7 +175,7 @@ void BlockHash::AddBlock(uint32_t hash_value) {
174175
return;
175176
}
176177
const uint32_t hash_table_index = GetHashTableIndex(hash_value);
177-
const int first_matching_block = hash_table_[hash_table_index];
178+
const size_t first_matching_block = hash_table_[hash_table_index];
178179
if (first_matching_block < 0) {
179180
// This is the first entry with this hash value
180181
hash_table_[hash_table_index] = block_number;
@@ -200,14 +201,14 @@ void BlockHash::AddAllBlocks() {
200201
AddAllBlocksThroughIndex(static_cast<int>(source_size_));
201202
}
202203

203-
void BlockHash::AddAllBlocksThroughIndex(int end_index) {
204-
if (end_index > static_cast<int>(source_size_)) {
204+
void BlockHash::AddAllBlocksThroughIndex(size_t end_index) {
205+
if (end_index > static_cast<size_t>(source_size_)) {
205206
VCD_DFATAL << "BlockHash::AddAllBlocksThroughIndex() called"
206207
" with index " << end_index
207208
<< " higher than end index " << source_size_ << VCD_ENDL;
208209
return;
209210
}
210-
const int last_index_added = last_block_added_ * kBlockSize;
211+
const size_t last_index_added = last_block_added_ * kBlockSize;
211212
if (end_index <= last_index_added) {
212213
VCD_DFATAL << "BlockHash::AddAllBlocksThroughIndex() called"
213214
" with index " << end_index
@@ -221,11 +222,11 @@ void BlockHash::AddAllBlocksThroughIndex(int end_index) {
221222
// See: https://github.com/google/open-vcdiff/issues/40
222223
return;
223224
}
224-
int end_limit = end_index;
225+
size_t end_limit = end_index;
225226
// Don't allow reading any indices at or past source_size_.
226227
// The Hash function extends (kBlockSize - 1) bytes past the index,
227228
// so leave a margin of that size.
228-
int last_legal_hash_index = static_cast<int>(source_size() - kBlockSize);
229+
size_t last_legal_hash_index = static_cast<size_t>(source_size() - kBlockSize);
229230
if (end_limit > last_legal_hash_index) {
230231
end_limit = last_legal_hash_index + 1;
231232
}
@@ -300,9 +301,9 @@ bool BlockHash::BlockContentsMatch(const char* block1, const char* block2) {
300301
return BlockContentsMatchInline(block1, block2);
301302
}
302303

303-
inline int BlockHash::SkipNonMatchingBlocks(int block_number,
304+
inline size_t BlockHash::SkipNonMatchingBlocks(size_t block_number,
304305
const char* block_ptr) const {
305-
int probes = 0;
306+
size_t probes = 0;
306307
while ((block_number >= 0) &&
307308
!BlockContentsMatchInline(block_ptr,
308309
&source_data_[block_number * kBlockSize])) {
@@ -317,18 +318,18 @@ inline int BlockHash::SkipNonMatchingBlocks(int block_number,
317318
// Init() must have been called and returned true before using
318319
// FirstMatchingBlock or NextMatchingBlock. No check is performed
319320
// for this condition; the code will crash if this condition is violated.
320-
inline int BlockHash::FirstMatchingBlockInline(uint32_t hash_value,
321+
inline size_t BlockHash::FirstMatchingBlockInline(uint32_t hash_value,
321322
const char* block_ptr) const {
322323
return SkipNonMatchingBlocks(hash_table_[GetHashTableIndex(hash_value)],
323324
block_ptr);
324325
}
325326

326-
int BlockHash::FirstMatchingBlock(uint32_t hash_value,
327+
size_t BlockHash::FirstMatchingBlock(uint32_t hash_value,
327328
const char* block_ptr) const {
328329
return FirstMatchingBlockInline(hash_value, block_ptr);
329330
}
330331

331-
int BlockHash::NextMatchingBlock(int block_number,
332+
size_t BlockHash::NextMatchingBlock(size_t block_number,
332333
const char* block_ptr) const {
333334
if (static_cast<size_t>(block_number) >= GetNumberOfBlocks()) {
334335
VCD_DFATAL << "NextMatchingBlock called for invalid block number "
@@ -343,7 +344,7 @@ int BlockHash::NextMatchingBlock(int block_number,
343344
// dictionary is made up of spaces (' ') and the search string is also
344345
// made up of spaces, there will be one match for each block in the
345346
// dictionary.
346-
inline bool BlockHash::TooManyMatches(int* match_counter) {
347+
inline bool BlockHash::TooManyMatches(size_t* match_counter) {
347348
++(*match_counter);
348349
return (*match_counter) > kMaxMatchesToCheck;
349350
}
@@ -352,12 +353,12 @@ inline bool BlockHash::TooManyMatches(int* match_counter) {
352353
// that match the corresponding bytes to the left of target_match_start.
353354
// Will not examine more than max_bytes bytes, which is to say that
354355
// the return value will be in the range [0, max_bytes] inclusive.
355-
int BlockHash::MatchingBytesToLeft(const char* source_match_start,
356+
size_t BlockHash::MatchingBytesToLeft(const char* source_match_start,
356357
const char* target_match_start,
357-
int max_bytes) {
358+
size_t max_bytes) {
358359
const char* source_ptr = source_match_start;
359360
const char* target_ptr = target_match_start;
360-
int bytes_found = 0;
361+
size_t bytes_found = 0;
361362
while (bytes_found < max_bytes) {
362363
--source_ptr;
363364
--target_ptr;
@@ -373,12 +374,12 @@ int BlockHash::MatchingBytesToLeft(const char* source_match_start,
373374
// that match the corresponding bytes starting at target_match_end.
374375
// Will not examine more than max_bytes bytes, which is to say that
375376
// the return value will be in the range [0, max_bytes] inclusive.
376-
int BlockHash::MatchingBytesToRight(const char* source_match_end,
377+
size_t BlockHash::MatchingBytesToRight(const char* source_match_end,
377378
const char* target_match_end,
378-
int max_bytes) {
379+
size_t max_bytes) {
379380
const char* source_ptr = source_match_end;
380381
const char* target_ptr = target_match_end;
381-
int bytes_found = 0;
382+
size_t bytes_found = 0;
382383
while ((bytes_found < max_bytes) && (*source_ptr == *target_ptr)) {
383384
++bytes_found;
384385
++source_ptr;
@@ -399,24 +400,24 @@ void BlockHash::FindBestMatch(uint32_t hash_value,
399400
const char* target_start,
400401
size_t target_size,
401402
Match* best_match) const {
402-
int match_counter = 0;
403-
for (int block_number = FirstMatchingBlockInline(hash_value,
403+
size_t match_counter = 0;
404+
for (ptrdiff_t block_number = FirstMatchingBlockInline(hash_value,
404405
target_candidate_start);
405406
(block_number >= 0) && !TooManyMatches(&match_counter);
406407
block_number = NextMatchingBlock(block_number, target_candidate_start)) {
407-
int source_match_offset = block_number * kBlockSize;
408-
const int source_match_end = source_match_offset + kBlockSize;
408+
ptrdiff_t source_match_offset = block_number * kBlockSize;
409+
const ptrdiff_t source_match_end = source_match_offset + kBlockSize;
409410

410-
int target_match_offset =
411-
static_cast<int>(target_candidate_start - target_start);
412-
const int target_match_end = target_match_offset + kBlockSize;
411+
ptrdiff_t target_match_offset =
412+
static_cast<ptrdiff_t>(target_candidate_start - target_start);
413+
const ptrdiff_t target_match_end = target_match_offset + kBlockSize;
413414

414415
size_t match_size = kBlockSize;
415416
{
416417
// Extend match start towards beginning of unencoded data
417-
const int limit_bytes_to_left = std::min(source_match_offset,
418+
const ptrdiff_t limit_bytes_to_left = std::min(source_match_offset,
418419
target_match_offset);
419-
const int matching_bytes_to_left =
420+
const ptrdiff_t matching_bytes_to_left =
420421
MatchingBytesToLeft(source_data_ + source_match_offset,
421422
target_start + target_match_offset,
422423
limit_bytes_to_left);
@@ -433,7 +434,7 @@ void BlockHash::FindBestMatch(uint32_t hash_value,
433434
match_size +=
434435
MatchingBytesToRight(source_data_ + source_match_end,
435436
target_start + target_match_end,
436-
static_cast<int>(limit_bytes_to_right));
437+
static_cast<ptrdiff_t>(limit_bytes_to_right));
437438
}
438439
// Update in/out parameter if the best match found was better
439440
// than any match already stored in *best_match.

src/blockhash.h

Lines changed: 26 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -79,8 +79,8 @@ class BlockHash {
7979
Match() : size_(0), source_offset_(-1), target_offset_(-1) { }
8080

8181
void ReplaceIfBetterMatch(size_t candidate_size,
82-
int candidate_source_offset,
83-
int candidate_target_offset) {
82+
size_t candidate_source_offset,
83+
size_t candidate_target_offset) {
8484
if (candidate_size > size_) {
8585
size_ = candidate_size;
8686
source_offset_ = candidate_source_offset;
@@ -89,20 +89,20 @@ class BlockHash {
8989
}
9090

9191
size_t size() const { return size_; }
92-
int source_offset() const { return source_offset_; }
93-
int target_offset() const { return target_offset_; }
92+
size_t source_offset() const { return source_offset_; }
93+
size_t target_offset() const { return target_offset_; }
9494

9595
private:
9696
// The size of the best (longest) match passed to ReplaceIfBetterMatch().
9797
size_t size_;
9898

9999
// The source offset of the match, including the starting_offset_
100100
// of the BlockHash for which the match was found.
101-
int source_offset_;
101+
size_t source_offset_;
102102

103103
// The target offset of the match. An offset of 0 corresponds to the
104104
// data at target_start, which is an argument of FindBestMatch().
105-
int target_offset_;
105+
size_t target_offset_;
106106

107107
// Making these private avoids implicit copy constructor
108108
// & assignment operator
@@ -119,7 +119,7 @@ class BlockHash {
119119
// starting_offset_ will be zero; for a hash of previously encoded
120120
// target data, starting_offset_ will be equal to the dictionary size.
121121
//
122-
BlockHash(const char* source_data, size_t source_size, int starting_offset);
122+
BlockHash(const char* source_data, size_t source_size, size_t starting_offset);
123123

124124
~BlockHash();
125125

@@ -176,7 +176,7 @@ class BlockHash {
176176
// of whether the block is aligned evenly on a block boundary. The
177177
// BlockHash will only store hash entries for the evenly-aligned blocks.
178178
//
179-
void AddOneIndexHash(int index, uint32_t hash_value) {
179+
void AddOneIndexHash(size_t index, uint32_t hash_value) {
180180
if (index == NextIndexToAdd()) {
181181
AddBlock(hash_value);
182182
}
@@ -204,7 +204,7 @@ class BlockHash {
204204
// VCDiffEngine::Encode (in vcdiffengine.cc) uses this function to
205205
// add a whole range of data to a target hash when a COPY instruction
206206
// is generated.
207-
void AddAllBlocksThroughIndex(int end_index);
207+
void AddAllBlocksThroughIndex(size_t end_index);
208208

209209
// FindBestMatch takes a position within the unencoded target data
210210
// (target_candidate_start) and the hash value of the kBlockSize bytes
@@ -321,12 +321,12 @@ class BlockHash {
321321
// The expected number of true matches scales super-linearly
322322
// with the inverse of kBlockSize, but here a linear scale is used
323323
// for block sizes smaller than 32.
324-
static const int kMaxMatchesToCheck = (kBlockSize >= 32) ? 32 :
324+
static const size_t kMaxMatchesToCheck = (kBlockSize >= 32) ? 32 :
325325
(32 * (32 / kBlockSize));
326326

327327
// Do not skip more than this number of non-matching hash collisions
328328
// to find the next matching entry in the hash chain.
329-
static const int kMaxProbes = 16;
329+
static const size_t kMaxProbes = 16;
330330

331331
// Internal routine which calculates a hash table size based on kBlockSize and
332332
// the dictionary_size. Will return a power of two if successful, or 0 if an
@@ -346,11 +346,11 @@ class BlockHash {
346346

347347
// The index within source_data_ of the next block
348348
// for which AddBlock() should be called.
349-
int NextIndexToAdd() const {
349+
size_t NextIndexToAdd() const {
350350
return (last_block_added_ + 1) * kBlockSize;
351351
}
352352

353-
static inline bool TooManyMatches(int* match_counter);
353+
static inline bool TooManyMatches(size_t* match_counter);
354354

355355
const char* source_data() { return source_data_; }
356356
size_t source_size() { return source_size_; }
@@ -400,18 +400,18 @@ class BlockHash {
400400
// so if this function is called before the hash table has been populated
401401
// using AddAllBlocks() or AddBlock(), it will simply return -1
402402
// for any value of hash_value.
403-
int FirstMatchingBlock(uint32_t hash_value, const char* block_ptr) const;
403+
size_t FirstMatchingBlock(uint32_t hash_value, const char* block_ptr) const;
404404

405405
// Given a block number returned by FirstMatchingBlock()
406406
// or by a previous call to NextMatchingBlock(), returns
407407
// the next block number that matches the same hash value.
408408
// Returns -1 if no match was found.
409-
int NextMatchingBlock(int block_number, const char* block_ptr) const;
409+
size_t NextMatchingBlock(size_t block_number, const char* block_ptr) const;
410410

411411
// Inline version of FirstMatchingBlock. This saves the cost of a function
412412
// call when this routine is called from within the module. The external
413413
// (non-inlined) version is called only by unit tests.
414-
inline int FirstMatchingBlockInline(uint32_t hash_value,
414+
inline size_t FirstMatchingBlockInline(uint32_t hash_value,
415415
const char* block_ptr) const;
416416

417417
// Walk through the hash entry chain, skipping over any false matches
@@ -420,23 +420,23 @@ class BlockHash {
420420
// the first true match found, or -1 if no true match was found.
421421
// If block_number is a matching block, the function will return block_number
422422
// without skipping to the next block.
423-
int SkipNonMatchingBlocks(int block_number, const char* block_ptr) const;
423+
size_t SkipNonMatchingBlocks(size_t block_number, const char* block_ptr) const;
424424

425425
// Returns the number of bytes to the left of source_match_start
426426
// that match the corresponding bytes to the left of target_match_start.
427427
// Will not examine more than max_bytes bytes, which is to say that
428428
// the return value will be in the range [0, max_bytes] inclusive.
429-
static int MatchingBytesToLeft(const char* source_match_start,
429+
static size_t MatchingBytesToLeft(const char* source_match_start,
430430
const char* target_match_start,
431-
int max_bytes);
431+
size_t max_bytes);
432432

433433
// Returns the number of bytes starting at source_match_end
434434
// that match the corresponding bytes starting at target_match_end.
435435
// Will not examine more than max_bytes bytes, which is to say that
436436
// the return value will be in the range [0, max_bytes] inclusive.
437-
static int MatchingBytesToRight(const char* source_match_end,
437+
static size_t MatchingBytesToRight(const char* source_match_end,
438438
const char* target_match_end,
439-
int max_bytes);
439+
size_t max_bytes);
440440

441441
// The protected functions BlockContentsMatch, FirstMatchingBlock,
442442
// NextMatchingBlock, MatchingBytesToLeft, and MatchingBytesToRight
@@ -455,12 +455,12 @@ class BlockHash {
455455
// GetHashTableIndex(), or -1 if there is no matching block. This value can
456456
// then be used as an index into next_block_table_ to retrieve the entire set
457457
// of matching block numbers.
458-
std::vector<int> hash_table_;
458+
std::vector<ptrdiff_t> hash_table_;
459459

460460
// An array containing one element for each source block. Each element is
461461
// either -1 (== not found) or the index of the next block whose hash value
462462
// would produce a matching result from GetHashTableIndex().
463-
std::vector<int> next_block_table_;
463+
std::vector<ptrdiff_t> next_block_table_;
464464

465465
// This vector has the same size as next_block_table_. For every block number
466466
// B that is referenced in hash_table_, last_block_table_[B] will contain
@@ -473,7 +473,7 @@ class BlockHash {
473473
// lists, so that the match with the lowest index is returned first. This
474474
// should result in a more compact encoding because the VCDIFF format favors
475475
// smaller index values and repeated index values.
476-
std::vector<int> last_block_table_;
476+
std::vector<ptrdiff_t> last_block_table_;
477477

478478
// Performing a bitwise AND with hash_table_mask_ will produce a value ranging
479479
// from 0 to the number of elements in hash_table_.
@@ -489,12 +489,12 @@ class BlockHash {
489489
// For a hash of source (dictionary) data, starting_offset_ will be zero;
490490
// for a hash of previously encoded target data, starting_offset_ will be
491491
// equal to the dictionary size.
492-
const int starting_offset_;
492+
const size_t starting_offset_;
493493

494494
// The last index added by AddBlock(). This determines the block number
495495
// for successive calls to AddBlock(), and is also
496496
// used to determine the starting block for AddAllBlocksThroughIndex().
497-
int last_block_added_;
497+
size_t last_block_added_;
498498

499499
// Making these private avoids implicit copy constructor & assignment operator
500500
BlockHash(const BlockHash&); // NOLINT

0 commit comments

Comments
 (0)