Skip to content

Commit 84b6db6

Browse files
authored
Merge pull request #1631 from evoskuil/master
Optimize siphash and chain to_data() stream readers.
2 parents 3a12b9d + 9ec51ad commit 84b6db6

File tree

11 files changed

+42
-40
lines changed

11 files changed

+42
-40
lines changed

include/bitcoin/system/impl/stream/iostream/istream.ipp

+8-5
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ namespace system {
2828

2929
// Allowed here for low level performance benefit.
3030
BC_PUSH_WARNING(NO_POINTER_ARITHMETIC)
31+
BC_PUSH_WARNING(NO_UNSAFE_COPY_N)
3132

3233
template <typename Character>
3334
template <typename Buffer>
@@ -150,18 +151,19 @@ template <typename Character>
150151
void
151152
istream<Character>::read(char_type* data, std::streamsize count) NOEXCEPT
152153
{
153-
const auto bytes = possible_narrow_sign_cast<size_t>(count);
154+
auto bytes = possible_narrow_sign_cast<size_t>(count);
154155

155156
if (is_overflow(bytes))
156157
{
158+
if (state_ != goodbit)
159+
return;
160+
161+
// Allow read to end if state was good (std::istream behavior).
162+
bytes = end_ - position_;
157163
setstate(badbit);
158-
return;
159164
}
160165

161-
BC_PUSH_WARNING(NO_UNSAFE_COPY_N)
162166
std::copy_n(position_, bytes, data);
163-
BC_POP_WARNING()
164-
165167
position_ += bytes;
166168
}
167169

@@ -181,6 +183,7 @@ istream<Character>::is_overflow(pos_type size) const NOEXCEPT
181183
return (state_ != goodbit) || (size > (end_ - position_));
182184
}
183185

186+
BC_POP_WARNING()
184187
BC_POP_WARNING()
185188

186189
} // namespace system

src/chain/block.cpp

+3-2
Original file line numberDiff line numberDiff line change
@@ -167,8 +167,9 @@ size_t block::get_allocation() const NOEXCEPT
167167
data_chunk block::to_data(bool witness) const NOEXCEPT
168168
{
169169
data_chunk data(serialized_size(witness));
170-
stream::out::copy ostream(data);
171-
to_data(ostream, witness);
170+
stream::out::fast ostream(data);
171+
write::bytes::fast out(ostream);
172+
to_data(out, witness);
172173
return data;
173174
}
174175

src/chain/header.cpp

+3-6
Original file line numberDiff line numberDiff line change
@@ -165,12 +165,9 @@ void header::assign_data(reader& source) NOEXCEPT
165165
data_chunk header::to_data() const NOEXCEPT
166166
{
167167
data_chunk data(serialized_size());
168-
169-
BC_PUSH_WARNING(NO_THROW_IN_NOEXCEPT)
170-
stream::out::copy ostream(data);
171-
BC_POP_WARNING()
172-
173-
to_data(ostream);
168+
stream::out::fast ostream(data);
169+
write::bytes::fast out(ostream);
170+
to_data(out);
174171
return data;
175172
}
176173

src/chain/input.cpp

+3-2
Original file line numberDiff line numberDiff line change
@@ -243,8 +243,9 @@ bool operator!=(const cref_point& left, const cref_point& right) NOEXCEPT
243243
data_chunk input::to_data() const NOEXCEPT
244244
{
245245
data_chunk data(serialized_size(false));
246-
stream::out::copy ostream(data);
247-
to_data(ostream);
246+
stream::out::fast ostream(data);
247+
write::bytes::fast out(ostream);
248+
to_data(out);
248249
return data;
249250
}
250251

src/chain/operation.cpp

+3-6
Original file line numberDiff line numberDiff line change
@@ -397,12 +397,9 @@ operation operation::from_string(const std::string& mnemonic) NOEXCEPT
397397
data_chunk operation::to_data() const NOEXCEPT
398398
{
399399
data_chunk data(serialized_size());
400-
401-
BC_PUSH_WARNING(NO_THROW_IN_NOEXCEPT)
402-
stream::out::copy ostream(data);
403-
BC_POP_WARNING()
404-
405-
to_data(ostream);
400+
stream::out::fast ostream(data);
401+
write::bytes::fast out(ostream);
402+
to_data(out);
406403
return data;
407404
}
408405

src/chain/output.cpp

+3-2
Original file line numberDiff line numberDiff line change
@@ -131,8 +131,9 @@ bool output::operator!=(const output& other) const NOEXCEPT
131131
data_chunk output::to_data() const NOEXCEPT
132132
{
133133
data_chunk data(serialized_size());
134-
stream::out::copy ostream(data);
135-
to_data(ostream);
134+
stream::out::fast ostream(data);
135+
write::bytes::fast out(ostream);
136+
to_data(out);
136137
return data;
137138
}
138139

src/chain/point.cpp

+3-6
Original file line numberDiff line numberDiff line change
@@ -156,12 +156,9 @@ void point::assign_data(reader& source) NOEXCEPT
156156
data_chunk point::to_data() const NOEXCEPT
157157
{
158158
data_chunk data(serialized_size());
159-
160-
BC_PUSH_WARNING(NO_THROW_IN_NOEXCEPT)
161-
stream::out::copy ostream(data);
162-
BC_POP_WARNING()
163-
164-
to_data(ostream);
159+
stream::out::fast ostream(data);
160+
write::bytes::fast out(ostream);
161+
to_data(out);
165162
return data;
166163
}
167164

src/chain/script.cpp

+3-2
Original file line numberDiff line numberDiff line change
@@ -307,8 +307,9 @@ script script::from_string(const std::string& mnemonic) NOEXCEPT
307307
data_chunk script::to_data(bool prefix) const NOEXCEPT
308308
{
309309
data_chunk data(serialized_size(prefix));
310-
stream::out::copy ostream(data);
311-
to_data(ostream, prefix);
310+
stream::out::fast ostream(data);
311+
write::bytes::fast out(ostream);
312+
to_data(out, prefix);
312313
return data;
313314
}
314315

src/chain/transaction.cpp

+3-2
Original file line numberDiff line numberDiff line change
@@ -243,8 +243,9 @@ data_chunk transaction::to_data(bool witness) const NOEXCEPT
243243
witness &= segregated_;
244244

245245
data_chunk data(serialized_size(witness));
246-
stream::out::copy ostream(data);
247-
to_data(ostream, witness);
246+
stream::out::fast ostream(data);
247+
write::bytes::fast out(ostream);
248+
to_data(out, witness);
248249
return data;
249250
}
250251

src/chain/witness.cpp

+3-2
Original file line numberDiff line numberDiff line change
@@ -246,8 +246,9 @@ witness witness::from_string(const std::string& mnemonic) NOEXCEPT
246246
data_chunk witness::to_data(bool prefix) const NOEXCEPT
247247
{
248248
data_chunk data(serialized_size(prefix));
249-
stream::out::copy ostream(data);
250-
to_data(ostream, prefix);
249+
stream::out::fast ostream(data);
250+
write::bytes::fast out(ostream);
251+
to_data(out, prefix);
251252
return data;
252253
}
253254

src/hash/siphash.cpp

+7-5
Original file line numberDiff line numberDiff line change
@@ -83,14 +83,16 @@ uint64_t siphash(const siphash_key& key,
8383

8484
constexpr auto eight = sizeof(uint64_t);
8585
const auto bytes = message.size();
86-
read::bytes::copy source(message);
86+
stream::in::fast stream(message);
87+
read::bytes::fast source(stream);
8788

8889
for (size_t index = eight; index <= bytes; index += eight)
89-
compression_round(v0, v1, v2, v3,
90-
source.read_8_bytes_little_endian());
90+
compression_round(v0, v1, v2, v3, source.read_8_bytes_little_endian());
9191

92-
auto last = is_zero(bytes % eight) ? 0_u64 :
93-
source.read_8_bytes_little_endian();
92+
// Read zero to seven remainder bytes (zero padded, fails stream).
93+
BC_ASSERT(source);
94+
auto last = source.read_8_bytes_little_endian();
95+
BC_ASSERT(!source);
9496

9597
last ^= ((bytes % max_encoded_byte_count) << to_bits(sub1(eight)));
9698
compression_round(v0, v1, v2, v3, last);

0 commit comments

Comments
 (0)