summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJoel Sing <jsing@google.com>2014-01-06 01:34:56 +1100
committerJoel Sing <jsing@google.com>2014-01-06 01:34:56 +1100
commit724fab9eab8460e5a48d6c998fa6e1eee71f96e6 (patch)
tree70d6178ea277569b059b7656724e80e9e0af5502
parentcc8432d2d4c290cf145eeee4f5cff8260755bee2 (diff)
downloadgo-724fab9eab8460e5a48d6c998fa6e1eee71f96e6.tar.gz
crypto/sha1, crypto/sha256, crypto/sha512: use copy for partial block
Use copy rather than a hand rolled loop when moving a partial input block to the scratch area. This results in a reasonable performance gain when partial blocks are written. Benchmarks on Intel(R) Xeon(R) CPU X5650 @ 2.67GHz with Go amd64: benchmark old MB/s new MB/s speedup SHA1 BenchmarkHash8Bytes 18.37 22.80 1.24x SHA256 BenchmarkHash8Bytes 11.86 13.78 1.16x SHA512 BenchmarkHash8Bytes 4.51 5.24 1.16x benchmark old ns/op new ns/op delta SHA1 BenchmarkHash8Bytes 435 350 -19.54% SHA256 BenchmarkHash8Bytes 674 580 -13.95% SHA512 BenchmarkHash8Bytes 1772 1526 -13.88% R=agl, dave, bradfitz CC=golang-codereviews https://codereview.appspot.com/35840044
-rw-r--r--src/pkg/crypto/sha1/sha1.go10
-rw-r--r--src/pkg/crypto/sha256/sha256.go10
-rw-r--r--src/pkg/crypto/sha512/sha512.go10
3 files changed, 6 insertions, 24 deletions
diff --git a/src/pkg/crypto/sha1/sha1.go b/src/pkg/crypto/sha1/sha1.go
index 8eb3f7a79..9f1a96e36 100644
--- a/src/pkg/crypto/sha1/sha1.go
+++ b/src/pkg/crypto/sha1/sha1.go
@@ -62,16 +62,10 @@ func (d *digest) Write(p []byte) (nn int, err error) {
nn = len(p)
d.len += uint64(nn)
if d.nx > 0 {
- n := len(p)
- if n > chunk-d.nx {
- n = chunk - d.nx
- }
- for i := 0; i < n; i++ {
- d.x[d.nx+i] = p[i]
- }
+ n := copy(d.x[d.nx:], p)
d.nx += n
if d.nx == chunk {
- block(d, d.x[0:])
+ block(d, d.x[:])
d.nx = 0
}
p = p[n:]
diff --git a/src/pkg/crypto/sha256/sha256.go b/src/pkg/crypto/sha256/sha256.go
index 89628a1b7..d84cebf2f 100644
--- a/src/pkg/crypto/sha256/sha256.go
+++ b/src/pkg/crypto/sha256/sha256.go
@@ -106,16 +106,10 @@ func (d *digest) Write(p []byte) (nn int, err error) {
nn = len(p)
d.len += uint64(nn)
if d.nx > 0 {
- n := len(p)
- if n > chunk-d.nx {
- n = chunk - d.nx
- }
- for i := 0; i < n; i++ {
- d.x[d.nx+i] = p[i]
- }
+ n := copy(d.x[d.nx:], p)
d.nx += n
if d.nx == chunk {
- block(d, d.x[0:])
+ block(d, d.x[:])
d.nx = 0
}
p = p[n:]
diff --git a/src/pkg/crypto/sha512/sha512.go b/src/pkg/crypto/sha512/sha512.go
index d2ada5137..bca7a91e2 100644
--- a/src/pkg/crypto/sha512/sha512.go
+++ b/src/pkg/crypto/sha512/sha512.go
@@ -106,16 +106,10 @@ func (d *digest) Write(p []byte) (nn int, err error) {
nn = len(p)
d.len += uint64(nn)
if d.nx > 0 {
- n := len(p)
- if n > chunk-d.nx {
- n = chunk - d.nx
- }
- for i := 0; i < n; i++ {
- d.x[d.nx+i] = p[i]
- }
+ n := copy(d.x[d.nx:], p)
d.nx += n
if d.nx == chunk {
- block(d, d.x[0:])
+ block(d, d.x[:])
d.nx = 0
}
p = p[n:]