Skip to content

Commit 72efc26

Browse files
LarryRuanehebasto
andcommitted
util: improve streams.h:FindByte() performance
Avoid use of the expensive mod operator (%) when calculating the buffer offset. No functional difference. Co-authored-by: Hennadii Stepanov <32963518+hebasto@users.noreply.github.com>
1 parent 604df63 commit 72efc26

File tree

5 files changed

+19
-9
lines changed

5 files changed

+19
-9
lines changed

src/bench/streams_findbyte.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ static void FindByte(benchmark::Bench& bench)
2020

2121
bench.run([&] {
2222
bf.SetPos(0);
23-
bf.FindByte(1);
23+
bf.FindByte(std::byte(1));
2424
});
2525

2626
// Cleanup

src/streams.h

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -756,15 +756,25 @@ class CBufferedFile
756756
}
757757

758758
//! 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)
760760
{
761+
// For best performance, avoid mod operation within the loop.
762+
size_t buf_offset{size_t(m_read_pos % uint64_t(vchBuf.size()))};
761763
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.
763768
Fill();
764-
if (vchBuf[m_read_pos % vchBuf.size()] == std::byte{ch}) {
765-
break;
766769
}
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;
768778
}
769779
}
770780
};

src/test/fuzz/buffered_file.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ FUZZ_TARGET(buffered_file)
5353
return;
5454
}
5555
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>()));
5757
} catch (const std::ios_base::failure&) {
5858
}
5959
},

src/test/streams_tests.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -463,7 +463,7 @@ BOOST_AUTO_TEST_CASE(streams_buffered_file_rand)
463463
size_t find = currentPos + InsecureRandRange(8);
464464
if (find >= fileSize)
465465
find = fileSize - 1;
466-
bf.FindByte(uint8_t(find));
466+
bf.FindByte(std::byte(find));
467467
// The value at each offset is the offset.
468468
BOOST_CHECK_EQUAL(bf.GetPos(), find);
469469
currentPos = find;

src/validation.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4565,7 +4565,7 @@ void Chainstate::LoadExternalBlockFile(
45654565
try {
45664566
// locate a header
45674567
unsigned char buf[CMessageHeader::MESSAGE_START_SIZE];
4568-
blkdat.FindByte(params.MessageStart()[0]);
4568+
blkdat.FindByte(std::byte(params.MessageStart()[0]));
45694569
nRewind = blkdat.GetPos() + 1;
45704570
blkdat >> buf;
45714571
if (memcmp(buf, params.MessageStart(), CMessageHeader::MESSAGE_START_SIZE)) {

0 commit comments

Comments
 (0)