]> Cypherpunks repositories - gostls13.git/commitdiff
hash/crc32,hash/crc64: use sync.OnceFunc
authorapocelipes <seve3r@outlook.com>
Thu, 26 Sep 2024 08:08:07 +0000 (08:08 +0000)
committerGopher Robot <gobot@golang.org>
Thu, 26 Sep 2024 19:48:18 +0000 (19:48 +0000)
Use sync.OnceFunc to simplify the code and to reduce global variables.

Change-Id: I7676339177e082c5be93dcf8121e379a6a7de912
GitHub-Last-Rev: f796c49260ad19c33b616c9543b99f7f425dc2fe
GitHub-Pull-Request: golang/go#69633
Reviewed-on: https://go-review.googlesource.com/c/go/+/615920
Reviewed-by: David Chase <drchase@google.com>
LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
Auto-Submit: Ian Lance Taylor <iant@google.com>
Reviewed-by: Ian Lance Taylor <iant@google.com>
src/hash/crc32/crc32.go
src/hash/crc64/crc64.go

index b659959959c6fa0b9e88644a410c103f4e01d782..bc1ae310dd02d376755128b4a73bedb546792c79 100644 (file)
@@ -78,10 +78,9 @@ type Table [256]uint32
 var castagnoliTable *Table
 var castagnoliTable8 *slicing8Table
 var updateCastagnoli func(crc uint32, p []byte) uint32
-var castagnoliOnce sync.Once
 var haveCastagnoli atomic.Bool
 
-func castagnoliInit() {
+var castagnoliInitOnce = sync.OnceFunc(func() {
        castagnoliTable = simpleMakeTable(Castagnoli)
 
        if archAvailableCastagnoli() {
@@ -96,7 +95,7 @@ func castagnoliInit() {
        }
 
        haveCastagnoli.Store(true)
-}
+})
 
 // IEEETable is the table for the [IEEE] polynomial.
 var IEEETable = simpleMakeTable(IEEE)
@@ -104,9 +103,8 @@ var IEEETable = simpleMakeTable(IEEE)
 // ieeeTable8 is the slicing8Table for IEEE
 var ieeeTable8 *slicing8Table
 var updateIEEE func(crc uint32, p []byte) uint32
-var ieeeOnce sync.Once
 
-func ieeeInit() {
+var ieeeInitOnce = sync.OnceFunc(func() {
        if archAvailableIEEE() {
                archInitIEEE()
                updateIEEE = archUpdateIEEE
@@ -117,17 +115,17 @@ func ieeeInit() {
                        return slicingUpdate(crc, ieeeTable8, p)
                }
        }
-}
+})
 
 // MakeTable returns a [Table] constructed from the specified polynomial.
 // The contents of this [Table] must not be modified.
 func MakeTable(poly uint32) *Table {
        switch poly {
        case IEEE:
-               ieeeOnce.Do(ieeeInit)
+               ieeeInitOnce()
                return IEEETable
        case Castagnoli:
-               castagnoliOnce.Do(castagnoliInit)
+               castagnoliInitOnce()
                return castagnoliTable
        default:
                return simpleMakeTable(poly)
@@ -147,7 +145,7 @@ type digest struct {
 // marshal and unmarshal the internal state of the hash.
 func New(tab *Table) hash.Hash32 {
        if tab == IEEETable {
-               ieeeOnce.Do(ieeeInit)
+               ieeeInitOnce()
        }
        return &digest{0, tab}
 }
@@ -202,7 +200,7 @@ func update(crc uint32, tab *Table, p []byte, checkInitIEEE bool) uint32 {
                return updateCastagnoli(crc, p)
        case tab == IEEETable:
                if checkInitIEEE {
-                       ieeeOnce.Do(ieeeInit)
+                       ieeeInitOnce()
                }
                return updateIEEE(crc, p)
        default:
@@ -238,7 +236,7 @@ func Checksum(data []byte, tab *Table) uint32 { return Update(0, tab, data) }
 // ChecksumIEEE returns the CRC-32 checksum of data
 // using the [IEEE] polynomial.
 func ChecksumIEEE(data []byte) uint32 {
-       ieeeOnce.Do(ieeeInit)
+       ieeeInitOnce()
        return updateIEEE(0, data)
 }
 
index bdfd82ed314dda14c947799c037d2d93a335ee84..66e53620b0a852859c0e128dd8ba00662a8bff57 100644 (file)
@@ -30,14 +30,11 @@ const (
 type Table [256]uint64
 
 var (
-       slicing8TablesBuildOnce sync.Once
-       slicing8TableISO        *[8]Table
-       slicing8TableECMA       *[8]Table
+       slicing8TableISO  *[8]Table
+       slicing8TableECMA *[8]Table
 )
 
-func buildSlicing8TablesOnce() {
-       slicing8TablesBuildOnce.Do(buildSlicing8Tables)
-}
+var buildSlicing8TablesOnce = sync.OnceFunc(buildSlicing8Tables)
 
 func buildSlicing8Tables() {
        slicing8TableISO = makeSlicingBy8Table(makeTable(ISO))