Skip to content

Commit 439fbd4

Browse files
minh-bqholiman
andauthored
uint256: optimize WriteToArray (#190)
This change optimizes WriteToArray, and also implements PutUint256 for use in writing to "sufficiently large slices". --------- Co-authored-by: Martin Holst Swende <martin@swende.se>
1 parent 555918b commit 439fbd4

3 files changed

Lines changed: 72 additions & 13 deletions

File tree

benchmarks_test.go

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1020,3 +1020,38 @@ func BenchmarkExtendSign(b *testing.B) {
10201020
result.ExtendSign(a, n)
10211021
}
10221022
}
1023+
1024+
func BenchmarkWriteTo(b *testing.B) {
1025+
fa, err := FromHex("0x1100030405060708090a0b0c0d0ed1e870eec79504c60144cc7f5fc2bad1e611")
1026+
if err != nil {
1027+
b.Fatal(err)
1028+
}
1029+
b.Run("fixed-20", func(b *testing.B) {
1030+
dest := [20]byte{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff}
1031+
for i := 0; i < b.N; i++ {
1032+
fa.WriteToArray20(&dest)
1033+
}
1034+
_ = (string(dest[:])) // Prevent the compiler from optimizing away the op
1035+
})
1036+
b.Run("fixed-32", func(b *testing.B) {
1037+
dest := [32]byte{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff}
1038+
for i := 0; i < b.N; i++ {
1039+
fa.WriteToArray32(&dest)
1040+
}
1041+
_ = (string(dest[:])) // Prevent the compiler from optimizing away the op
1042+
})
1043+
b.Run("slice", func(b *testing.B) {
1044+
dest := make([]byte, 64)
1045+
for i := 0; i < b.N; i++ {
1046+
fa.WriteToSlice(dest)
1047+
}
1048+
_ = (string(dest[:])) // Prevent the compiler from optimizing away the op
1049+
})
1050+
b.Run("put256", func(b *testing.B) {
1051+
dest := make([]byte, 64)
1052+
for i := 0; i < b.N; i++ {
1053+
fa.PutUint256(dest)
1054+
}
1055+
_ = (string(dest[:])) // Prevent the compiler from optimizing away the op
1056+
})
1057+
}

uint256.go

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -144,18 +144,35 @@ func (z *Int) WriteToSlice(dest []byte) {
144144
}
145145
}
146146

147+
// PutUint256 writes all 32 bytes of z to the destination slice, including zero-bytes.
148+
// If dest is larger than 32 bytes, z will fill the first parts, and leave
149+
// the end untouched.
150+
// Note: The dest slice must be at least 32 bytes large, otherwise this
151+
// method will panic. The method WriteToSlice, which is slower, should be used
152+
// if the destination slice is smaller or of unknown size.
153+
func (z *Int) PutUint256(dest []byte) {
154+
_ = dest[31]
155+
binary.BigEndian.PutUint64(dest[0:8], z[3])
156+
binary.BigEndian.PutUint64(dest[8:16], z[2])
157+
binary.BigEndian.PutUint64(dest[16:24], z[1])
158+
binary.BigEndian.PutUint64(dest[24:32], z[0])
159+
}
160+
147161
// WriteToArray32 writes all 32 bytes of z to the destination array, including zero-bytes
148162
func (z *Int) WriteToArray32(dest *[32]byte) {
149-
for i := 0; i < 32; i++ {
150-
dest[31-i] = byte(z[i/8] >> uint64(8*(i%8)))
151-
}
163+
// The PutUint64()s are inlined and we get 4x (load, bswap, store) instructions.
164+
binary.BigEndian.PutUint64(dest[0:8], z[3])
165+
binary.BigEndian.PutUint64(dest[8:16], z[2])
166+
binary.BigEndian.PutUint64(dest[16:24], z[1])
167+
binary.BigEndian.PutUint64(dest[24:32], z[0])
152168
}
153169

154170
// WriteToArray20 writes the last 20 bytes of z to the destination array, including zero-bytes
155171
func (z *Int) WriteToArray20(dest *[20]byte) {
156-
for i := 0; i < 20; i++ {
157-
dest[19-i] = byte(z[i/8] >> uint64(8*(i%8)))
158-
}
172+
// The PutUint*()s are inlined and we get 3x (load, bswap, store) instructions.
173+
binary.BigEndian.PutUint32(dest[0:4], uint32(z[2]))
174+
binary.BigEndian.PutUint64(dest[4:12], z[1])
175+
binary.BigEndian.PutUint64(dest[12:20], z[0])
159176
}
160177

161178
// Uint64 returns the lower 64-bits of z

uint256_test.go

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -479,7 +479,7 @@ func TestUdivremQuick(t *testing.T) {
479479
var (
480480
u = []uint64{1, 0, 0, 0, 0}
481481
expected = new(Int)
482-
rem Int
482+
rem Int
483483
)
484484
udivrem([]uint64{}, u, &Int{0, 1, 0, 0}, &rem)
485485
copy(expected[:], u)
@@ -760,30 +760,37 @@ func TestWriteToSlice(t *testing.T) {
760760
}
761761

762762
}
763+
763764
func TestInt_WriteToArray(t *testing.T) {
764-
x1 := hex2Bytes("0000000000000000000000000000d1e870eec79504c60144cc7f5fc2bad1e611")
765+
x1 := hex2Bytes("0102030405060708090a0b0c0d0ed1e870eec79504c60144cc7f5fc2bad1e611")
765766
a := big.NewInt(0).SetBytes(x1)
766767
fa, _ := FromBig(a)
767768

768769
{
769770
dest := [20]byte{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff}
770771
fa.WriteToArray20(&dest)
771-
exp := hex2Bytes("0000d1e870eec79504c60144cc7f5fc2bad1e611")
772+
exp := hex2Bytes("0d0ed1e870eec79504c60144cc7f5fc2bad1e611")
772773
if !bytes.Equal(dest[:], exp) {
773774
t.Errorf("got %x, expected %x", dest, exp)
774775
}
775-
776776
}
777-
778777
{
779778
dest := [32]byte{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
780779
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff}
781780
fa.WriteToArray32(&dest)
782-
exp := hex2Bytes("0000000000000000000000000000d1e870eec79504c60144cc7f5fc2bad1e611")
781+
exp := hex2Bytes("0102030405060708090a0b0c0d0ed1e870eec79504c60144cc7f5fc2bad1e611")
782+
if !bytes.Equal(dest[:], exp) {
783+
t.Errorf("got %x, expected %x", dest, exp)
784+
}
785+
}
786+
{ // A 36-byte slice: the first 32 bytes are overwritten
787+
dest := []byte{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
788+
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff}
789+
fa.PutUint256(dest)
790+
exp := hex2Bytes("0102030405060708090a0b0c0d0ed1e870eec79504c60144cc7f5fc2bad1e611ffffffff")
783791
if !bytes.Equal(dest[:], exp) {
784792
t.Errorf("got %x, expected %x", dest, exp)
785793
}
786-
787794
}
788795
}
789796

0 commit comments

Comments
 (0)