File tree Expand file tree Collapse file tree 5 files changed +19
-9
lines changed Expand file tree Collapse file tree 5 files changed +19
-9
lines changed Original file line number Diff line number Diff line change @@ -20,7 +20,7 @@ static void FindByte(benchmark::Bench& bench)
20
20
21
21
bench.run ([&] {
22
22
bf.SetPos (0 );
23
- bf.FindByte (1 );
23
+ bf.FindByte (std::byte ( 1 ) );
24
24
});
25
25
26
26
// Cleanup
Original file line number Diff line number Diff line change @@ -756,15 +756,25 @@ class CBufferedFile
756
756
}
757
757
758
758
// ! search for a given byte in the stream, and remain positioned on it
759
- void FindByte (uint8_t ch )
759
+ void FindByte (std::byte byte )
760
760
{
761
+ // For best performance, avoid mod operation within the loop.
762
+ size_t buf_offset{size_t (m_read_pos % uint64_t (vchBuf.size ()))};
761
763
while (true ) {
762
- if (m_read_pos == nSrcPos)
764
+ if (m_read_pos == nSrcPos) {
765
+ // No more bytes available; read from the file into the buffer,
766
+ // setting nSrcPos to one beyond the end of the new data.
767
+ // Throws exception if end-of-file reached.
763
768
Fill ();
764
- if (vchBuf[m_read_pos % vchBuf.size ()] == std::byte{ch}) {
765
- break ;
766
769
}
767
- m_read_pos++;
770
+ const size_t len{std::min<size_t >(vchBuf.size () - buf_offset, nSrcPos - m_read_pos)};
771
+ const auto it_start{vchBuf.begin () + buf_offset};
772
+ const auto it_find{std::find (it_start, it_start + len, byte)};
773
+ const size_t inc{size_t (std::distance (it_start, it_find))};
774
+ m_read_pos += inc;
775
+ if (inc < len) break ;
776
+ buf_offset += inc;
777
+ if (buf_offset >= vchBuf.size ()) buf_offset = 0 ;
768
778
}
769
779
}
770
780
};
Original file line number Diff line number Diff line change @@ -53,7 +53,7 @@ FUZZ_TARGET(buffered_file)
53
53
return ;
54
54
}
55
55
try {
56
- opt_buffered_file->FindByte (fuzzed_data_provider.ConsumeIntegral <uint8_t >());
56
+ opt_buffered_file->FindByte (std::byte ( fuzzed_data_provider.ConsumeIntegral <uint8_t >() ));
57
57
} catch (const std::ios_base::failure&) {
58
58
}
59
59
},
Original file line number Diff line number Diff line change @@ -463,7 +463,7 @@ BOOST_AUTO_TEST_CASE(streams_buffered_file_rand)
463
463
size_t find = currentPos + InsecureRandRange (8 );
464
464
if (find >= fileSize)
465
465
find = fileSize - 1 ;
466
- bf.FindByte (uint8_t (find));
466
+ bf.FindByte (std::byte (find));
467
467
// The value at each offset is the offset.
468
468
BOOST_CHECK_EQUAL (bf.GetPos (), find);
469
469
currentPos = find;
Original file line number Diff line number Diff line change @@ -4565,7 +4565,7 @@ void Chainstate::LoadExternalBlockFile(
4565
4565
try {
4566
4566
// locate a header
4567
4567
unsigned char buf[CMessageHeader::MESSAGE_START_SIZE];
4568
- blkdat.FindByte (params.MessageStart ()[0 ]);
4568
+ blkdat.FindByte (std::byte ( params.MessageStart ()[0 ]) );
4569
4569
nRewind = blkdat.GetPos () + 1 ;
4570
4570
blkdat >> buf;
4571
4571
if (memcmp (buf, params.MessageStart (), CMessageHeader::MESSAGE_START_SIZE)) {
You can’t perform that action at this time.
0 commit comments