Skip to content

Commit

Permalink
Merge pull request libbitcoin#1363 from evoskuil/master
Browse files Browse the repository at this point in the history
Use no-stream optimization for tx/header hashing.
  • Loading branch information
evoskuil authored May 9, 2023
2 parents d2199bc + 6a7242f commit a1b777f
Show file tree
Hide file tree
Showing 16 changed files with 983 additions and 223 deletions.
7 changes: 6 additions & 1 deletion Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -738,7 +738,6 @@ include_bitcoin_system_streamdir = ${includedir}/bitcoin/system/stream
include_bitcoin_system_stream_HEADERS = \
include/bitcoin/system/stream/binary.hpp \
include/bitcoin/system/stream/device.hpp \
include/bitcoin/system/stream/iostream.hpp \
include/bitcoin/system/stream/make_stream.hpp \
include/bitcoin/system/stream/make_streamer.hpp \
include/bitcoin/system/stream/stream.hpp \
Expand All @@ -753,6 +752,12 @@ include_bitcoin_system_stream_devices_HEADERS = \
include/bitcoin/system/stream/devices/flip_sink.hpp \
include/bitcoin/system/stream/devices/push_sink.hpp

include_bitcoin_system_stream_iostreamdir = ${includedir}/bitcoin/system/stream/iostream
include_bitcoin_system_stream_iostream_HEADERS = \
include/bitcoin/system/stream/iostream/iostream.hpp \
include/bitcoin/system/stream/iostream/istream.hpp \
include/bitcoin/system/stream/iostream/ostream.hpp

include_bitcoin_system_stream_streamersdir = ${includedir}/bitcoin/system/stream/streamers
include_bitcoin_system_stream_streamers_HEADERS = \
include/bitcoin/system/stream/streamers/bit_flipper.hpp \
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -407,7 +407,9 @@
<ClInclude Include="..\..\..\..\include\bitcoin\system\stream\devices\copy_source.hpp" />
<ClInclude Include="..\..\..\..\include\bitcoin\system\stream\devices\flip_sink.hpp" />
<ClInclude Include="..\..\..\..\include\bitcoin\system\stream\devices\push_sink.hpp" />
<ClInclude Include="..\..\..\..\include\bitcoin\system\stream\iostream.hpp" />
<ClInclude Include="..\..\..\..\include\bitcoin\system\stream\iostream\iostream.hpp" />
<ClInclude Include="..\..\..\..\include\bitcoin\system\stream\iostream\istream.hpp" />
<ClInclude Include="..\..\..\..\include\bitcoin\system\stream\iostream\ostream.hpp" />
<ClInclude Include="..\..\..\..\include\bitcoin\system\stream\make_stream.hpp" />
<ClInclude Include="..\..\..\..\include\bitcoin\system\stream\make_streamer.hpp" />
<ClInclude Include="..\..\..\..\include\bitcoin\system\stream\stream.hpp" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -124,38 +124,41 @@
<Filter Include="include\bitcoin\system\stream\devices">
<UniqueIdentifier>{39F60708-FF48-4C22-0000-000000000005}</UniqueIdentifier>
</Filter>
<Filter Include="include\bitcoin\system\stream\streamers">
<Filter Include="include\bitcoin\system\stream\iostream">
<UniqueIdentifier>{39F60708-FF48-4C22-0000-000000000006}</UniqueIdentifier>
</Filter>
<Filter Include="include\bitcoin\system\stream\streamers\interfaces">
<Filter Include="include\bitcoin\system\stream\streamers">
<UniqueIdentifier>{39F60708-FF48-4C22-0000-000000000007}</UniqueIdentifier>
</Filter>
<Filter Include="include\bitcoin\system\stream\streamers\interfaces">
<UniqueIdentifier>{39F60708-FF48-4C22-0000-000000000008}</UniqueIdentifier>
</Filter>
<Filter Include="include\bitcoin\system\unicode">
<UniqueIdentifier>{39F60708-FF48-4C22-0000-000000000011}</UniqueIdentifier>
</Filter>
<Filter Include="include\bitcoin\system\unicode\utf8_everywhere">
<UniqueIdentifier>{39F60708-FF48-4C22-0000-000000000008}</UniqueIdentifier>
<UniqueIdentifier>{39F60708-FF48-4C22-0000-000000000009}</UniqueIdentifier>
</Filter>
<Filter Include="include\bitcoin\system\wallet">
<UniqueIdentifier>{39F60708-FF48-4C22-0000-0000000000A2}</UniqueIdentifier>
</Filter>
<Filter Include="include\bitcoin\system\wallet\addresses">
<UniqueIdentifier>{39F60708-FF48-4C22-0000-000000000009}</UniqueIdentifier>
<UniqueIdentifier>{39F60708-FF48-4C22-0000-000000000010}</UniqueIdentifier>
</Filter>
<Filter Include="include\bitcoin\system\wallet\keys">
<UniqueIdentifier>{39F60708-FF48-4C22-0000-000000000010}</UniqueIdentifier>
<UniqueIdentifier>{39F60708-FF48-4C22-0000-000000000011}</UniqueIdentifier>
</Filter>
<Filter Include="include\bitcoin\system\wallet\mnemonics">
<UniqueIdentifier>{39F60708-FF48-4C22-0000-000000000011}</UniqueIdentifier>
<UniqueIdentifier>{39F60708-FF48-4C22-0000-000000000012}</UniqueIdentifier>
</Filter>
<Filter Include="include\bitcoin\system\words">
<UniqueIdentifier>{39F60708-FF48-4C22-0000-0000000000B2}</UniqueIdentifier>
</Filter>
<Filter Include="include\bitcoin\system\words\catalogs">
<UniqueIdentifier>{39F60708-FF48-4C22-0000-000000000012}</UniqueIdentifier>
<UniqueIdentifier>{39F60708-FF48-4C22-0000-000000000013}</UniqueIdentifier>
</Filter>
<Filter Include="resource">
<UniqueIdentifier>{39F60708-FF48-4C22-0000-000000000013}</UniqueIdentifier>
<UniqueIdentifier>{39F60708-FF48-4C22-0000-0000000000A4}</UniqueIdentifier>
</Filter>
<Filter Include="src">
<UniqueIdentifier>{39F60708-FF48-4C22-0000-000000000000}</UniqueIdentifier>
Expand Down Expand Up @@ -1097,8 +1100,14 @@
<ClInclude Include="..\..\..\..\include\bitcoin\system\stream\devices\push_sink.hpp">
<Filter>include\bitcoin\system\stream\devices</Filter>
</ClInclude>
<ClInclude Include="..\..\..\..\include\bitcoin\system\stream\iostream.hpp">
<Filter>include\bitcoin\system\stream</Filter>
<ClInclude Include="..\..\..\..\include\bitcoin\system\stream\iostream\iostream.hpp">
<Filter>include\bitcoin\system\stream\iostream</Filter>
</ClInclude>
<ClInclude Include="..\..\..\..\include\bitcoin\system\stream\iostream\istream.hpp">
<Filter>include\bitcoin\system\stream\iostream</Filter>
</ClInclude>
<ClInclude Include="..\..\..\..\include\bitcoin\system\stream\iostream\ostream.hpp">
<Filter>include\bitcoin\system\stream\iostream</Filter>
</ClInclude>
<ClInclude Include="..\..\..\..\include\bitcoin\system\stream\make_stream.hpp">
<Filter>include\bitcoin\system\stream</Filter>
Expand Down
4 changes: 3 additions & 1 deletion include/bitcoin/system.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,6 @@
#include <bitcoin/system/serial/serialize.hpp>
#include <bitcoin/system/stream/binary.hpp>
#include <bitcoin/system/stream/device.hpp>
#include <bitcoin/system/stream/iostream.hpp>
#include <bitcoin/system/stream/make_stream.hpp>
#include <bitcoin/system/stream/make_streamer.hpp>
#include <bitcoin/system/stream/stream.hpp>
Expand All @@ -188,6 +187,9 @@
#include <bitcoin/system/stream/devices/copy_source.hpp>
#include <bitcoin/system/stream/devices/flip_sink.hpp>
#include <bitcoin/system/stream/devices/push_sink.hpp>
#include <bitcoin/system/stream/iostream/iostream.hpp>
#include <bitcoin/system/stream/iostream/istream.hpp>
#include <bitcoin/system/stream/iostream/ostream.hpp>
#include <bitcoin/system/stream/streamers/bit_flipper.hpp>
#include <bitcoin/system/stream/streamers/bit_reader.hpp>
#include <bitcoin/system/stream/streamers/bit_writer.hpp>
Expand Down
161 changes: 148 additions & 13 deletions include/bitcoin/system/impl/stream/iostream/iostream.ipp
Original file line number Diff line number Diff line change
Expand Up @@ -26,17 +26,18 @@ namespace system {

BC_PUSH_WARNING(NO_POINTER_ARITHMETIC)

template <typename Buffer, typename Character>
INLINE iostream<Buffer, Character>::iostream(Buffer& buffer) NOEXCEPT
template <typename Character>
template <typename Buffer>
INLINE iostream<Character>::iostream(Buffer& buffer) NOEXCEPT
: position_(buffer.data()),
begin_(position_),
end_(begin_ + buffer.size()),
state_(goodbit)
{
}

template <typename Buffer, typename Character>
INLINE iostream<Buffer, Character>::iostream(uint8_t* begin,
template <typename Character>
INLINE iostream<Character>::iostream(uint8_t* begin,
ptrdiff_t size) NOEXCEPT
: position_(begin),
begin_(position_),
Expand All @@ -45,31 +46,165 @@ INLINE iostream<Buffer, Character>::iostream(uint8_t* begin,
{
}

template <typename Buffer, typename Character>
INLINE typename iostream<Buffer, Character>::iostate
iostream<Buffer, Character>::rdstate() const NOEXCEPT
template <typename Character>
INLINE typename iostream<Character>::iostate
iostream<Character>::rdstate() const NOEXCEPT
{
return state_;
}

template <typename Buffer, typename Character>
template <typename Character>
INLINE void
iostream<Buffer, Character>::setstate(iostate state) NOEXCEPT
iostream<Character>::setstate(iostate state) NOEXCEPT
{
state_ |= state;
}

template <typename Buffer, typename Character>
template <typename Character>
INLINE void
iostream<Buffer, Character>::clear(iostate state) NOEXCEPT
iostream<Character>::clear(iostate state) NOEXCEPT
{
state_ = state;
}


template <typename Character>
INLINE typename iostream<Character>::pos_type
iostream<Character>::tellg() const NOEXCEPT
{
return static_cast<pos_type>(position_ - begin_);
}

template <typename Character>
INLINE typename iostream<Character>::pos_type
iostream<Character>::tellp() const NOEXCEPT
{
return static_cast<pos_type>(position_ - begin_);
}

template <typename Character>
INLINE iostream<Character>&
iostream<Character>::seekg(off_type offset, seekdir direction) NOEXCEPT
{
if (state_ != goodbit)
return *this;

using namespace system;
switch (direction)
{
case beg:
{
if (is_negative(offset) || (offset > (end_ - begin_)))
{
setstate(badbit);
break;
}

position_ = begin_ + offset;
break;
}
case cur:
{
if ((is_negative(offset) && (offset < (begin_ - position_))) ||
(is_positive(offset) && (offset > (end_ - position_))))
{
setstate(badbit);
break;
}

position_ = position_ + offset;
break;
}
case end:
{
if (is_positive(offset) || (offset < (begin_ - end_)))
{
setstate(badbit);
break;
}

position_ = end_ + offset;
break;
}
default:
{
setstate(failbit);
break;
}
}

return *this;
}

template <typename Character>
INLINE typename iostream<Character>::int_type
iostream<Character>::peek() NOEXCEPT
{
constexpr auto eof = std::char_traits<Character>::eof();

if (is_overflow(1))
{
setstate(badbit);
return eof;
}

const uint8_t value = *position_;
return system::sign_cast<int_type>(value);
}

template <typename Character>
INLINE void
iostream<Character>::read(char_type* data, pos_type size) NOEXCEPT
{
if (is_overflow(size))
{
setstate(badbit);
return;
}

BC_PUSH_WARNING(NO_UNSAFE_COPY_N)
std::copy_n(position_, size, data);
BC_POP_WARNING()

position_ += size;
}

template <typename Character>
INLINE void
iostream<Character>::write(const char_type* data,
pos_type size) NOEXCEPT
{
if (is_overflow(size))
{
setstate(badbit);
return;
}

BC_PUSH_WARNING(NO_UNSAFE_COPY_N)
std::copy_n(data, size, position_);
BC_POP_WARNING()

position_ += size;
}

template <typename Character>
INLINE void
iostream<Character>::flush() NOEXCEPT
{
}

// private
template <typename Character>
constexpr bool
iostream<Character>::is_positive(off_type value) NOEXCEPT
{
return !is_zero(value) && !system::is_negative(value);
}

// private
template <typename Buffer, typename Character>
template <typename Character>
INLINE bool
iostream<Buffer, Character>::is_overflow(pos_type size) const NOEXCEPT
iostream<Character>::is_overflow(pos_type size) const NOEXCEPT
{
return (state_ != goodbit) || (size > (end_ - position_));
}
Expand Down
Loading

0 comments on commit a1b777f

Please sign in to comment.