Commit graph

16 commits

Author SHA1 Message Date
Dave Collins 6c7f45fdb7 Add 2014 to copyright dates. 2014-01-08 23:44:08 -06:00
Marco Peereboom 6ad853019a use fastsha256 2013-11-19 09:26:22 -06:00
Dave Collins dd41f7e91a Minor cleanup.
This commit fixes a couple of comments and cleans up a couple of things
golint complained about.
2013-11-07 06:21:44 -06:00
Dave Collins 9ee6a8aeb6 Optimize writeElement.
This commit modifies the writeElement function to have a "fast path" which uses type
assertions for all of the types which btcwire write so the more expensive
reflection-based binary.Write can be avoided.

Also, this changes all cases that were writing raw ShaHash (32-byte) arrays (which
requires a stack copy) instead simply passing the pointer.

The following benchmark results show the results for serializing a block header
after these changes:

Before: BenchmarkWriteBlockHeader         500000              5566 ns/op
After:  BenchmarkWriteBlockHeader        1000000               991 ns/op

This is part of the ongoing effort to optimize serialization as noted in
conformal/btcd#27.
2013-11-07 00:56:20 -06:00
Dave Collins bc85a31016 Optimize readElement.
The following benchmark results show the results for deserializing a block header:

Before: BenchmarkReadBlockHeader          500000              5916 ns/op
After:  BenchmarkReadBlockHeader         1000000              2078 ns/op

This is part of the ongoing effort to optimize serialization as noted in
conformal/btcd#27.
2013-11-06 23:00:17 -06:00
Dave Collins 8a1828a2d6 Optimize writeVarString.
Before:

BenchmarkWriteVarStr4    1000000              1114 ns/op
BenchmarkWriteVarStr10   1000000              1352 ns/op

After:

BenchmarkWriteVarStr4    5000000               291 ns/op
BenchmarkWriteVarStr10  10000000               248 ns/op

This is part ef the ongoing effort to optimize serialization as noted in
conformal/btcd#27.
2013-11-05 23:59:40 -06:00
Dave Collins 4002051a22 Optimize readVarString.
Before:

BenchmarkReadVarStr4     1000000              1698 ns/op
BenchmarkReadVarStr10    1000000              1812 ns/op

After:

BenchmarkReadVarStr4     2000000               853 ns/op
BenchmarkReadVarStr10    5000000               712 ns/op

This is part ef the ongoing effort to optimize serialization as noted in
conformal/btcd#27.
2013-11-05 23:33:21 -06:00
Dave Collins 6263efcc71 Slightly optimize readVarInt.
This commit slightly optimizes the readVarInt function in the case of
multiple-byte variable length integers.  It also reduces the amount of
memory garbage it generates.

Before:

BenchmarkReadVarInt1     5000000               386 ns/op
BenchmarkReadVarInt3     5000000               693 ns/op
BenchmarkReadVarInt5     2000000               793 ns/op
BenchmarkReadVarInt9     5000000               709 ns/op

After:

BenchmarkReadVarInt1     5000000               387 ns/op
BenchmarkReadVarInt3     5000000               471 ns/op
BenchmarkReadVarInt5     5000000               575 ns/op
BenchmarkReadVarInt9     5000000               473 ns/op

This is part ef the ongoing effort to optimize serialization as noted in
conformal/btcd#27.
2013-11-05 22:09:03 -06:00
Dave Collins 50c10faf4e Optimize writeVarInt.
Before:

BenchmarkWriteVarInt1   10000000               173 ns/op
BenchmarkWriteVarInt3    2000000               965 ns/op
BenchmarkWriteVarInt5    2000000               966 ns/op
BenchmarkWriteVarInt9    2000000               968 ns/op

After:

BenchmarkWriteVarInt1   20000000               101 ns/op
BenchmarkWriteVarInt3   20000000               136 ns/op
BenchmarkWriteVarInt5   10000000               142 ns/op
BenchmarkWriteVarInt9   10000000               156 ns/op

This is part of the ongoing effort to optimize serialization as noted in
conformal/btcd#27.
2013-11-05 20:33:14 -06:00
Dave Collins 26cb71d805 Expose new SerializeSize API for transactions.
This commit adds a new function named SerializeSize to the public API for
MsgTx, TxOut, and TxIn which can be used to determine how many bytes the
serialized data would take without having to actually serialize it.

The following benchmark shows the difference between using the new
function to get the serialize size for a typical transaction and
serializing into a temporary buffer and taking the length of it:

Bufffer: BenchmarkTxSerializeSizeBuffer     200000           7050 ns/op
New:     BenchmarkTxSerializeSizeNew     100000000             18 ns/op

This is part of the ongoing effort to optimize serialization as noted in
conformal/btcd#27.
2013-10-31 00:20:14 -05:00
Dave Collins cbf648a02f Slightly optimize writeVarInt for the normal case.
Most variable length integers are smaller numbers, so this commit reverses
the order of the if checks in the writeVarInt to assume smaller numbers
are more common.

This is part of the ongoing effort to optimize serialization as noted in
conformal/btcd#27
2013-10-30 21:11:10 -05:00
Dave Collins 5cc32bbfc7 Add bounds checking to all variable length allocs.
Several of the bitcoin data structures contain variable length entries,
many of which have well-defined maximum limits.  However, there are still
a few cases, such as variable length strings and number of transactions
which don't have clearly defined maximum limits.  Instead they are only
limited by the maximum size of a message.

In order to efficiently decode messages, space is pre-allocated for the
slices which hold these variable length pieces as to avoid needing to
dynamically grow the backing arrays.  Due to this however, it was
previously possible to claim extremely high slice lengths which exceed
available memory (or maximum allowed slice lengths).

This commit imposes limits to all of these cases based on calculating
the maximum possible number of elements that could fit into a message
and using those as sane upper limits.

The variable length string case was found (and tests added to hit it) by
drahn@ which prompted an audit to find all cases.
2013-10-25 08:55:39 -05:00
Jonathan Gillham fa1d343430 Removed extra local variable assignment from DoubleSha256. 2013-10-14 23:55:18 +01:00
Dave Collins 95ecbadb8e Ensure readVarInt handles short buf on first byte.
It is technically possible for the Read method on a reader to return zero
bytes read with a nil error even though that behavior is "discouraged" by
the interface documenation.  This commit switches the read of the first
byte to use io.ReadFull which will always error in this case.
2013-10-06 22:06:34 -05:00
Dave Collins e7abb08b6b Fix a comment typo. 2013-05-09 21:24:47 -05:00
Dave Collins 69b27dd5d3 Initial implementation. 2013-05-08 18:58:29 -05:00