1
0
mirror of https://github.com/golang/go synced 2024-11-22 10:34:46 -07:00

hash/crc32,hash/crc64: use sync.OnceFunc

Use sync.OnceFunc to simplify the code and to reduce global variables.

Change-Id: I7676339177e082c5be93dcf8121e379a6a7de912
GitHub-Last-Rev: f796c49260
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>
This commit is contained in:
apocelipes 2024-09-26 08:08:07 +00:00 committed by Gopher Robot
parent ca3b1b13de
commit 9ace960d54
2 changed files with 12 additions and 17 deletions

View 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)
}

View File

@ -30,14 +30,11 @@ const (
type Table [256]uint64
var (
slicing8TablesBuildOnce sync.Once
slicing8TableISO *[8]Table
slicing8TableECMA *[8]Table
)
func buildSlicing8TablesOnce() {
slicing8TablesBuildOnce.Do(buildSlicing8Tables)
}
var buildSlicing8TablesOnce = sync.OnceFunc(buildSlicing8Tables)
func buildSlicing8Tables() {
slicing8TableISO = makeSlicingBy8Table(makeTable(ISO))