aboutsummaryrefslogtreecommitdiff
path: root/src/hash
diff options
context:
space:
mode:
authorCholerae Hu <choleraehyq@gmail.com>2018-08-03 14:29:37 +0800
committerBrad Fitzpatrick <bradfitz@golang.org>2018-08-21 04:50:21 +0000
commit2a5df067654970557f5038d238954eb9893e7f31 (patch)
tree23f0e2a0db445c2d4e99aeeb4f9db708bc88dd8a /src/hash
parent80fe2e6e37120e665cd523e1edf7eb1401b9b73b (diff)
downloadgo-2a5df067654970557f5038d238954eb9893e7f31.tar.gz
go-2a5df067654970557f5038d238954eb9893e7f31.zip
hash/crc64: lazily initialize slice8Tables
Saves 36KB of memory in stdlib packages. Updates #26775 Change-Id: I0f9d7b17d9768f6fb980d5fbba7c45920215a5fc Reviewed-on: https://go-review.googlesource.com/127735 Run-TryBot: Brad Fitzpatrick <bradfitz@golang.org> TryBot-Result: Gobot Gobot <gobot@golang.org> Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org>
Diffstat (limited to 'src/hash')
-rw-r--r--src/hash/crc64/crc64.go17
1 files changed, 15 insertions, 2 deletions
diff --git a/src/hash/crc64/crc64.go b/src/hash/crc64/crc64.go
index a799a017c9..063c63c6a3 100644
--- a/src/hash/crc64/crc64.go
+++ b/src/hash/crc64/crc64.go
@@ -10,6 +10,7 @@ package crc64
import (
"errors"
"hash"
+ "sync"
)
// The size of a CRC-64 checksum in bytes.
@@ -28,13 +29,24 @@ const (
type Table [256]uint64
var (
- slicing8TableISO = makeSlicingBy8Table(makeTable(ISO))
- slicing8TableECMA = makeSlicingBy8Table(makeTable(ECMA))
+ slicing8TablesBuildOnce sync.Once
+ slicing8TableISO *[8]Table
+ slicing8TableECMA *[8]Table
)
+func buildSlicing8TablesOnce() {
+ slicing8TablesBuildOnce.Do(buildSlicing8Tables)
+}
+
+func buildSlicing8Tables() {
+ slicing8TableISO = makeSlicingBy8Table(makeTable(ISO))
+ slicing8TableECMA = makeSlicingBy8Table(makeTable(ECMA))
+}
+
// MakeTable returns a Table constructed from the specified polynomial.
// The contents of this Table must not be modified.
func MakeTable(poly uint64) *Table {
+ buildSlicing8TablesOnce()
switch poly {
case ISO:
return &slicing8TableISO[0]
@@ -141,6 +153,7 @@ func readUint64(b []byte) uint64 {
}
func update(crc uint64, tab *Table, p []byte) uint64 {
+ buildSlicing8TablesOnce()
crc = ^crc
// Table comparison is somewhat expensive, so avoid it for small sizes
for len(p) >= 64 {