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)
2020
2121 bench.run ([&] {
2222 bf.SetPos (0 );
23- bf.FindByte (1 );
23+ bf.FindByte (std::byte ( 1 ) );
2424 });
2525
2626 // Cleanup
Original file line number Diff line number Diff 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};
Original file line number Diff line number Diff 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 },
Original file line number Diff line number Diff 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;
Original file line number Diff line number Diff 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)) {
You can’t perform that action at this time.
0 commit comments